views:

326

answers:

2

From wikipedia:

"Segmentation cannot be turned off on x86 processors, so many operating systems use a flat memory model to make segmentation unnoticeable to programs. For instance, the Linux kernel sets up only 4 segments"

I mean since protection is already taken care of by the virtual memory subsystem (PTEs have a protection bit) why would you need 4 segments (instead of 2: i.e. data/code with DPL 3 since you can execute code residing in a lower privileged segment)?

Thanks.

+2  A: 

You didn't quote enough of that wikipedia page where it describes the four segments and why all are needed...

Usually, however, implied segments are used. All instruction fetches come from the code segment in the CS register. Most memory references come from data segment in the DS register. Processor stack references, either implicitly (e.g. push and pop instructions) or explicitly (memory accesses using the ESP or (E)BP registers) use the stack segment in the SS register. Finally, string instructions (e.g. stos, movs) also use the extra segment ES.

So if you want to set up a flat model where programmers don't need to think about segmentation, you need to set up all four of these segment registers (CS, DS, SS, ES) to have the same base. Then addresses computed with respect to all four are equivalent.

That page shows an example with all four set to base=0, limit=4Gb

John M
That does not answer my question. The way segmentation is implemented in protected mode is though an indirection table (i.e. the GDT/LDT) so you still could setup 2 segments and have 2 registers point to the same entry in the table...
anonymous
You'd have to do extra work to avoid the implicit segment references if you didn't have valid setups for all 4. If you only had CS and DS set up, you'd have to use a segment override prefix for the instructions where SS and ES are implicitly used. So it's much easier to just set up all 4 identically. Then you can forget segmentation exists. I'm rusty enough on my x86 that I'm probably missing some case where even the use of override prefixes would cause more trouble. Why open this can of worms at all? What trouble is it to set up all 4?
John M
A: 

You have a separate set of segments for kernel and user mode so that user mode code cannot write to kernel mode data. That would be a bad thing.

MSN