Question

x86 CPUs have had all kinds of tricky modes and memory segmentation over the generations from 16-bit to 32-bit to 64-bit.

These days with modern OSes using modern CPUs in modern operating modes you don't have to worry about memory segments, but you can still run legacy apps on legacy OSes on the modern CPUs, in which case I presume the CPUs run in a special legacy mode (protected mode, real mode, etc).

So obviously the segment registers exist for reasons of backward compatibility, but is that the only reason?

Are there modern uses for the segment registers too? Or have they just become general purpose registers these days that merely have names reflecting their historic functions?

This question was inspired by comments on this old Stack Overflow thread: Why does the mov instruction have to be used this way?

Was it helpful?

Solution

In 2002, Linux kernel hacker Ingo Molnar used segmentation when implementing Exec Shield, a form of data execution prevention, on 32 bit x86 systems. This is one modern use of segmentation that I'm aware of but mostly in a get the most mileage out of hardware mechanisms you can't change way. Segmentation is not used to implement data execution prevention on x86-64 CPUs with NX support.

The FS and GS segment registers are still used on x86-64:

In 64-bit mode, segmentation is generally (but not completely) disabled, creating a flat 64-bit linear-address space. The processor treats the segment base of CS, DS, ES, SS as zero, creating a linear address that is equal to the effective address. The FS and GS segments are exceptions. These segment registers (which hold the segment base) can be used as an additional base registers in linear address calculations. They facilitate addressing local data and certain operating system data structures.

Intel System Programming Guide, Chapter 3.2.4

On x86-64, Linux uses FS for thread local storage and GS for kernel space per cpu data. See How are the fs/gs registers used in Linux AMD64? and Detail about MSR_GS_BASE in linux x86 64

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top