3

i was trying quite a while to learn what the segment registers purpose is? I All ready know that this comes from the past (8086) where there was only 1 MByte of physical memory. But the CPU's had only 16 Bit wide address registers. With these 16 Address bits you could only address 64K Byte. So the 1MByte Address-space had to be separated into segments each of 64K size.

This seems to me to be a requirement from the past where those engineers hat to work around of somehow limited memory space and such. But my question is what is the advantage of the segment registers today? How or where would you use them registers in a useful way? If you need clarification just comment.

Jester
  • 56,577
  • 4
  • 81
  • 125
Dirk
  • 451
  • 7
  • 21
  • 2
    In a protected mode OS (Windows, Linux, MacOS X), you can't use segments at all, because descriptor tables aren't controlled by you. – Seva Alekseyev Nov 11 '14 at 14:07
  • Feel smart because you can understand all the hideous cruft? That or write a bootloader for a flat-address 64-bit OS I guess. – EOF Nov 11 '14 at 14:15
  • @SevaAlekseyev that's wrong. You use segments for TLS and other stuff like exception handling. Also in 32 bit linux there is a system call to manage LDT entries. – Jester Nov 11 '14 at 14:23
  • @Jester I believe that Seva is right. My reading of his comment is that you have to use the segments that the protected mode OS gave you for the purpose they are supposed to be used. For example the Structured Exception Handling (SEH) on Windows expects a `FS` segment convention to be followed by both OS and compilers and language runtimes. Even on 32bit Windows you can in principle modify the LDT through NtSetLdtEntries but only **OS is THE resource manager** and uses HW segment protection features to manage them and to guarantee the 24x365 uptime. So OS uses segments applications "_don't_" – xmojmr Nov 11 '14 at 14:59
  • @xmojmr OS manages the segments, but application code may very well use them. If your code has just one instruction with a segment override, that counts as "using" in my book. – Jester Nov 11 '14 at 15:34
  • @Jester I agree that from a nitpicking what your book says point of view you are right and Seva is wrong. But from the perspective of OP's question paraphrased as "_..how or where would you use segment registers in a useful way today.._" Seva's argument looks perfectly valid (not wrong) to me – xmojmr Nov 11 '14 at 15:49
  • 2
    @xmojmr Also the OP didn't specify user mode at all, so kernel mode uses count as well. Segmentation is used for the purposes we all agree on, and part of that is OS and part is application. – Jester Nov 11 '14 at 15:54
  • 2
    See http://stackoverflow.com/a/10810340/120163 – Ira Baxter Nov 14 '14 at 00:18
  • 2
    See also http://stackoverflow.com/questions/22446104/do-the-x86-segment-registers-have-special-meaning-usage-on-modern-cpus-and-oses?rq=1 – phuclv Nov 14 '14 at 05:28

2 Answers2

3

Read about the Flat Model.

Segment Register today mostly serve as legacy for backward compatibility (except FS-GS as mentioned by @Jester)

michel9501
  • 120
  • 7
2

Arguably, x86-64 long mode is the technology of "today". There classical segmentation for the most part is nonexistent, with only FS and GS being used for practical purposes. FS is typically used for various thread-local and system data in user mode, while GS is used for kernel mode. This latter use is supported by the SWAPGS instruction.

Jester
  • 56,577
  • 4
  • 81
  • 125