Stack setup: should use low memory?

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
User avatar
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Stack setup: should use low memory?

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

Re: Stack setup: should use low memory?

Post 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.
"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
CocaCola
Member
Member
Posts: 36
Joined: Fri Sep 07, 2012 9:11 am

Re: Stack setup: should use low memory?

Post 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:
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Stack setup: should use low memory?

Post by AJ »

Hi,

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

Cheers,
Adam
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Stack setup: should use low memory?

Post by egos »

CocaCola, reset segment registers using own GDT.
If you have seen bad English in my words, tell me what's wrong, please.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Stack setup: should use low memory?

Post 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.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Stack setup: should use low memory?

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