Page 1 of 1

Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 9:53 am
by CocaCola
The Multiboot documentation advises that the OS creates its own stack:
http://www.gnu.org/software/grub/manual ... hine-state
Multiboot specification wrote:All other processor registers and flag bits are undefined. This includes, in particular:

‘ESP’
The OS image must create its own stack as soon as it needs one.
The OSDev Wiki has an article on stacks, however I'm still left with a couple of questions:
http://wiki.osdev.org/Stack#Setup_the_stack
OSDev Wiki wrote:Take care when implementing your kernel. If you use segmentation, the DS segment should be configured to have it's base at the same address as SS does. Otherwise you may run into problems when passing pointers to local variables into functions, because normal GPRs can't access the stack the way you might think.
What exactly is this warning about? And I admit that I don't know how to set up the DS segment.
Is this the DS register or the data segment as described in the GDT?

Now back to the intended topic: is it a good idea to use the low memory (640 KB) for the stack seen as the kernel is moved at above 1 MB?

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 10:04 am
by Combuster
You can use a limited form of segmentation with GCC. The compiler expects that SS, DS and ES point to the same memory, and thus have the same base address. Typically, this is abbreviated to the shorthand where DS=ES=SS - which is exactly how it is implemented in 99.9% of the cases.

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 12:05 pm
by CocaCola
Combuster wrote:You can use a limited form of segmentation with GCC. The compiler expects that SS, DS and ES point to the same memory, and thus have the same base address. Typically, this is abbreviated to the shorthand where DS=ES=SS - which is exactly how it is implemented in 99.9% of the cases.
I do not understand what you mean by the "DS=ES=SS" shorthand, because it does not look like valid code.
Even if I wanted to do it in Assembly I would need to use an auxiliary register, such as EAX, no?

Please, could you clarify how to set these up?
Also, I note that the example in the Wiki doesn't do this, and neither do the kernel tutorials I've read so far.

Code: Select all

SECTION .text

setup_stack:
    
    MOV  ESP, stack_end  ; Set the stack pointer

SECTION .bss

stack_begin:
    RESB 4096  ; Reserve 4 KiB stack space
stack_end:

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 12:15 pm
by AJ
Hi,

Combuster was not suggesting that DS=ES=SS was valid code, he was explaining the principle.

Cheers,
Adam

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 1:51 pm
by egos
CocaCola, reset segment registers using own GDT.

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 2:52 pm
by linguofreak
CocaCola wrote:
OSDev Wiki wrote:Take care when implementing your kernel. If you use segmentation, the DS segment should be configured to have it's base at the same address as SS does. Otherwise you may run into problems when passing pointers to local variables into functions, because normal GPRs can't access the stack the way you might think.
What exactly is this warning about?
Each segment has a base address, which is a value that is added to every pointer into that segment. If DS and SS have the same base, then DS:100000 and SS:100000 will both point to the same memory. If the base of DS is 80000000 and the base of SS is 0, then DS:100000 will point to 80100000 and SS:100000 will point to 00100000.

C compilers tend to assume that DS, ES, and SS are all the same, and thus that DS:XYZ, ES:XYZ and SS:XYZ point to the same place.
And I admit that I don't know how to set up the DS segment.
Is this the DS register or the data segment as described in the GDT?

Both. You'll set up a descriptor for a data segment in the GDT. Then you'll load that segment into DS, ES, and SS.

Re: Stack setup: should use low memory?

Posted: Sun Jun 23, 2013 2:54 pm
by linguofreak
CocaCola wrote:I do not understand what you mean by the "DS=ES=SS" shorthand, because it does not look like valid code.
It simply means that DS, ES, and SS are all the same.