972
edits
[unchecked revision] | [unchecked revision] |
m (→Protected Mode: link) |
m (Fix lint errors) |
||
(18 intermediate revisions by 10 users not shown) | |||
Line 3:
In [[Real Mode]] you use a logical address in the form A:B to address memory. This is translated into a physical address using the equation:
The registers in pure real-mode are limited to 16 bits for addressing. 16 bits can represent any integer between 0 and 64k. This means that if we set A to be a fixed value and allow B to change we can address a 64k area of memory. This 64k area is called a segment.
The base address of a segment is the (A * 0x10) portion of the equation I showed. It should be obvious that segments can overlap.
Line 17:
The x86 line of computers have 6 segment registers (CS, DS, ES, FS, GS, SS). They are totally independent of one another.
{| {{wikitable}}
*CS = Code Segment▼
|-
*DS = Data Segment▼
! CS
*SS = Stack Segment▼
|-
*FS/GS = General Purpose Segments▼
! DS
|-
! SS
|-
! ES
|-
! FS
|-
! GS
|}
DS, ES, FS, GS, SS are used to form addresses when you want to read/write to memory. They don't always have to be explicitly encoded, because some processor operations assume that certain segment registers will be used.
E.g.
Line 45 ⟶ 59:
Here the new value for CS is encoded in the jump instruction. Eg JMP 0x10:0x100 says to load CS with segment 0x10 and IP with 0x100. CS:IP is the logical address of the instruction to be executed.
====Far Call====
This is exactly the same as a far jump, but the current values of CS/IP are pushed onto the [[stack]] before executing at the new position.
====INT====
The processor reads the new value of CS/IP from the Interrupt Vector Table and then executes what is effectively a far call after pushing EFLAGS onto the [[stack]].
====Far Return====
Here the processor pops the return segment/offset from the [[stack]] into CS/IP and switches execution to that address.
====IRET====
This is exactly the same as a far return apart from the processor popping EFLAGS off the [[stack]] in addition to CS/IP.
Apart from these cases no instruction alters the value of CS.
==Protected Mode==
:''Segmentation is considered obsolete memory protection technique in protected mode by both CPU manufacturers and most of programmers. It is no longer supported in long mode. The information here is required to get protected mode working; also 64 bit GDT is needed to enter long mode and segments are still used to jump from long mode to compatibility mode and the other way around. If you want to be serious about OS development, we strongly recommend using flat memory model and [[Paging|paging]] as memory management technique. For more information, consult [[x86-64]].''
:''Read more about [[Global Descriptor Table]]
In [[Protected
Because B can be any value between 0 and
Now for the difference.
Line 76 ⟶ 91:
* The segment presence (Is it present or not)
* The descriptor type (0 = system; 1 = code/data)
* The segment type (Code/Data/Read/Write/Accessed/Conforming/Non-Conforming/Expand-Up/[[Expand_Down|Expand-Down]])
For the purposes of this explanation I'm only interested in 3 things. The base address, the limit and the descriptor type.
Line 103 ⟶ 118:
==Notes Regarding C==
*Most C compilers assume a flat-memory model.
*In this model all the segments cover the full address space (
*This leaves you with 2 descriptors per privilege level (usually Ring 0 and Ring 3
*In general if you want to use the segmentation mechanism, by having the different segment registers represent segments with different base addresses, you won't be able to use a modern C compiler, and may very well be restricted to just Assembly.
*So, if you're going to use C, do what the rest of the C world does, which is set up a flat-memory model, use paging, and ignore the fact that segmentation even exists.
==Notes Regarding Pascal[FPC]==
[[Category:Memory Management]]▼
Above may apply in theory to FreePascal, however, in reality is ignored, if the compiler at all pays any attention to same.
The twin segments for CODE and DATA are used, and as specified above,therefore, required.Size limits, however, are respected.(does NOT have to be 4GB in length)
"*In general if you want to use the segmentation mechanism, by having the different segment registers represent segments with different base addresses, you won't be able to use a modern C compiler, and may very well be restricted to just Assembly."
This is simply NOT true for Freepascal.
The 'A in A:B' is what allows 48 and 64 bit pointer references, not only with Pascal's NewFrontier unit, but FreePascal as well(Word:Longint Pointer reference).
*Assumption of CODE and DATA occupying the same space, (at least with PAE NX bits and Paging units not used) allows ROGUE/virus like code in the first place to take advantage of the machine. INTEL Specs even say this. CODE and DATA must be KEPT separate. Microsoft still is plagued with this problem,despite having NX bits enabled even in the latest OSes.
==See Also==
=== Articles ===
[[Segment Limits#Segmentation|Segment Limits]]
=== Threads ===
===External Links===
*[http://mirror.href.com/thestarman/asm/debug/Segments.html Removing the Mystery from SEGMENT : OFFSET Addressing]
*[http://duartes.org/gustavo/blog/post/memory-translation-and-segmentation Aug 2008: Memory Translation and Segmentation] by Gustavo Duarte
[[Category:X86]]
[[Category:Memory Segmentation]]
|