There is what they were intended for, and what they are used for by Windows and Linux.

The original intention behind the segment registers was to allow a program to access many different (large) segments of memory that were intended to be independent and part of a persistent virtual store. The idea was taken from the 1966 Multics operating system, that treated files as simply addressable memory segments. No BS "Open file, write record, close file", just "Store this value into that virtual data segment" with dirty page flushing.

<aside> 💡 1966 年 Multics 为了弥补指令长度不足,为了实现对更多地址的寻址,发明了分段式(Segment)内存寻址,段基址 + offset。

但是后来随着指令长度变长,分段已经没必要,只靠 offset 就能对足够大的区域寻址,所以现代 Linux 采用 Flat Segment 设计,即所有的段基址都是 0,实际上只用了 offset。

</aside>

Our current 2010 operating systems are a giant step backwards, which is why they are called "Eunuchs". You can only address your process space's single segment, giving a so-called "flat (IMHO dull) address space". The segment registers on the x86-32 machine can still be used for real segment registers, but nobody has bothered (Andy Grove, former Intel president, had a rather famous public fit last century when he figured out after all those Intel engineers spent energy and his money to implement this feature, that nobody was going to use it. Go, Andy!)

<aside> 💡 AMD 在设计 x86-64 时决定彻底抛弃对 Multics 段寄存器的兼容,将这些寄存器改为它用。 Linux 上 GS 用来保存一些 CPU 专用数据的地址,FS 用来保存 TLS 地址。Windows 上用处略有不同。

过去 GS 和 FS 保存的数据都存在 GDT 里,如今通过专用寄存器保存这些地址,就不用再去主存的 GDT 里查询了。

为了加速内核切换,AMD x86-64 又增加了三个 MSR:FSBase、GSBase、KernelGSBase。

</aside>

AMD in going to 64 bits decided they didn't care if they eliminated Multics as a choice (that's the charitable interpretation; the uncharitable one is they were clueless about Multics) and so disabled the general capability of segment registers in 64 bit mode. There was still a need for threads to access thread local store, and each thread needed a a pointer ... somewhere in the immediately accessible thread state (e.g, in the registers) ... to thread local store. Since Windows and Linux both used FS and GS (thanks Nick for the clarification) for this purpose in the 32 bit version, AMD decided to let the 64 bit segment registers (GS and FS) be used essentially only for this purpose (I think you can make them point anywhere in your process space; I don't know if the application code can load them or not). Intel in their panic to not lose market share to AMD on 64 bits, and Andy being retired, decided to just copy AMD's scheme.

<aside> 💡 这里作者认为为这些数据在虚拟内存地址里设置一个固定地址就可以免除这些寄存器了。

这是出于安全考虑,内存里的所有东西都不应该有固定地址,而都应该是动态分配的,所以所有的东西都需要通过“某些方法”来寻址。

</aside>

It would have been architecturally prettier IMHO to make each thread's memory map have an absolute virtual address (e.g, 0-FFF say) that was its thread local storage (no [segment] register pointer needed!); I did this in an 8 bit OS back in the 1970s and it was extremely handy, like having another big stack of registers to work in.

So, the segment registers are now kind of like your appendix. They serve a vestigial purpose. To our collective loss.

Those that don't know history aren't doomed to repeat it; they're doomed to doing something dumber.