Page 1 of 1

Memory and segments?

Posted: Wed Jun 23, 2004 4:18 pm
by Googlan
I just wanna know if I got this right...
in PM, you can access a memoryadress by using
Segment:Offset
like
08h:0Ah for the 410-hundered byte (if that segment has a base adress thats 400 in decimal)... but I can also use the physical adress 410. SO why define my segments, I can reach the memoryadresses anyway?
E.g. in my bootloader, I put the code segment at 08h, and uses
08h:1000 to reach my kernel..
but I can also use just 1000 to reach my kernel(since the code segments base adress is 0).. so do I have to set up my segments and GDT? Cause it seems like I can reach the memory anyways..

Re:Memory and segments?

Posted: Wed Jun 23, 2004 6:00 pm
by Curufir
Fun confusion time.

***

Real-Mode:

Addressing is done as SEGMENT:OFFSET

PHYSICAL address = (SEGMENT * 0x10) + OFFSET

Both values are 16-bit, so unless you alter SEGMENT you are restricted to a 64k region called a...*drumroll*...segment. Different SEGMENT:OFFSET addresses may represent the same PHYSICAL address in this scheme.

***

Unreal-Mode:

Same as real-mode, but OFFSET is 32-bit.

***

Protected-Mode:

Addressing is done as DESCRIPTOR:OFFSET

LINEAR address = DESCRIPTOR BASE + OFFSET

Without paging LINEAR = PHYSICAL

With paging PHYSICAL address is determined via translating the LINEAR address according to your page tables (It could be anywhere).

Because OFFSET is 32-bits you can address any point in memory without needing to change descriptors (If you use 4Gb, 0 base descriptors).

Re:Memory and segments?

Posted: Wed Jun 23, 2004 6:15 pm
by GOggla
Thanks, that was what I ment :) Sorry if I sounded a bif confused and dizzy, been a long day and lots and lots of documentation :)
But if I only use one segment and set the base adress to 0, then there?s no readon for me writing Descriptor:Offset, right?

Re:Memory and segments?

Posted: Wed Jun 23, 2004 6:31 pm
by Curufir
GOggla wrote: But if I only use one segment and set the base adress to 0, then there?s no readon for me writing Descriptor:Offset, right?
Once you've set up the various descriptors then you can forget about it for a while, but you need to be aware that this is the scheme that's being used in the background.

Eg. push eax actually uses the descriptor in SS to form the address SS:ESP of your stack.

In fact any instruction that touches memory will use the descriptors.

Reason you need to be aware of this is that when changing privilege levels (Rings) you're going to need to change descriptors as well. Just be aware that although you can't see them, and don't have to use them explicitly, they are actually present and being used.