0

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:

  1. If that's the case, where is segment selector stored?

  2. Does each thread has its own segment selector?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

2

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.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Modern OSes use flat memory model. So how does protection work between user and kernel mode? Could you elaborate? thanks – c.critter Jan 16 '18 at 03:53
  • @c.critter [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory), where the OS manages the [page tables](https://en.wikipedia.org/wiki/Page_table). User-space can only read/write the pages the OS maps into its virtual address space. – Peter Cordes Jan 16 '18 at 04:22