Flat vs Segmented Memory Model

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
desjardins

Flat vs Segmented Memory Model

Post by desjardins »

Im trying to ensure I understand the difference and implications of one model over the other.

Flat is where you set up 2 Segments in the GDT, a Code and a Data segment, both running at CPL=0, and set to use from 0 -> 4GB of memory.

Segmented is where you set up (for example) 4 Segments in the GDT, a Code and Data segment running at CPL=0 using say the first 1MB of memory; and a Code and Data segment running at CPL=3, which uses from 1MB -> 4GB of memory.

If you choose the segmented model, when you perform a System Call you would need to modify CS and DS to point to the kernel segments, and then restore them when you exit? The OS would still be responsible for ensuring 2 user processes do not access each others memory, but the cpu can now ensure user processes do not touch kernel memory.

Under the flat model, the os is responsible for all memory security.

Ive also seen some articles (http://www.osdev.org/osfaq2/index.php/What%20Segments%20are%20About) that say most compilers only support the Flat model, so is this really the best design choice?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Flat vs Segmented Memory Model

Post by Candy »

Ive also seen some articles (http://www.osdev.org/osfaq2/index.php/What%20Segments%20are%20About) that say most compilers only support the Flat model, so is this really the best design choice?
No.
desjardins wrote: Im trying to ensure I understand the difference and implications of one model over the other.

Flat is where you set up 2 Segments in the GDT, a Code and a Data segment, both running at CPL=0, and set to use from 0 -> 4GB of memory.

Segmented is where you set up (for example) 4 Segments in the GDT, a Code and Data segment running at CPL=0 using say the first 1MB of memory; and a Code and Data segment running at CPL=3, which uses from 1MB -> 4GB of memory.

If you choose the segmented model, when you perform a System Call you would need to modify CS and DS to point to the kernel segments, and then restore them when you exit? The OS would still be responsible for ensuring 2 user processes do not access each others memory, but the cpu can now ensure user processes do not touch kernel memory.

Under the flat model, the os is responsible for all memory security.
That's true, also, for any security in non-segmented you NEED paging. Theoretically, segmentation plus paging would be the best system. However, it's also the least practical nowadays and it's even being phased out (ia32e, amd64, ia64) so it's probably one of the worst current choices. Everybody is forcing you to use the flat model so it's the best choice for a current time OS developer. However, it's not the best.
mystran

Re:Flat vs Segmented Memory Model

Post by mystran »

Most of the time when you use the "flat" model, you still setup 4 segments:
- 4GB CPL0 CS
- 4GB CPL0 DS/SS
- 4GB CPL3 CS
- 4GB CPL3 DS/SS

The "segmented" model is more or less an alternative to paging: instead of giving each process an address space, and then filling those with pages when the program allocates more memory, in segmented model you simply let programs allocate segments, and have then figure out what segment to use manually. You can then swap in/out the segments, just like you would do with the pages.

Finally, the segmentation support of a processor can be useful even for a "flat" model; small segments are often used for "thread-local" storage, even when the memory model is essentially "flat".
Post Reply