From what I've read, on modern OS including windows and linux, segment registers such as CS, DS, etc are not used. My questions are:
If that's the case, where is segment selector stored?
Does each thread has its own segment selector?
From what I've read, on modern OS including windows and linux, segment registers such as CS, DS, etc are not used. My questions are:
If that's the case, where is segment selector stored?
Does each thread has its own segment selector?
What you read was over-simplified.
Segment registers have to be used to make the CPU work in protected / long mode, they just aren't used for anything interesting. (Except for FS or GS being used for thread-local storage). Modern OSes use a flat memory model where all segments have base=0 and limit=4GiB (and in 64-bit mode, the HW ignores base and limit, except for FS/GS).
The code-segment descriptor selected by CS determines whether the process runs in long mode or compat mode, under a 64-bit kernel. (The L bit in the descriptor). See http://wiki.osdev.org/GDT.
All 32-bit processes use the same CS value, while all 64-bit processes use the same CS value (but different from 32-bit processes). Modern OSes don't always save/restore all segment registers, e.g. on returning to user mode after a system call, some of Linux's return paths use a constant __USER32_CS
or __USER_CS
. There's also a __USER_DS
constant that's used to initialize DS and ES so they select a descriptor that works. (See some links and comments on the system-call return path in this answer.)