anonymous
anonymous

Reputation:

Memory Segmentation on modern OSes: why do you need 4 segments?

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.

Upvotes: 2

Views: 1136

Answers (2)

MSN
MSN

Reputation: 54604

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.

Upvotes: 2

John M
John M

Reputation: 13229

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

Upvotes: 3

Related Questions