9
votes

I've been reading Intel manual about Virtual Memory (Segmentation + Paging). As I understand there is special register that holds Global Descriptor Table (GDT). GDT contains NULL Descriptor, Local Descriptor Table, TSS, and Segment Descriptor. Also almost all Operating Systems (OS) has one GDT and that gets loaded during boot time and cannot be changed (Maybe!!!). My questions are:

  1. Where all of those information stored (NULL, LDT, TSS, and Segment Descriptor)? Are they in Physical Memory, RAM or Local Hard Drive?
  2. Are Segment Descriptors the same as Code Segment (ASM: CS), Data Segment (ASM: DS), ... and if so how many of those are stored in GDT (Is there only one CS, one DS, ...)?
  3. Does Protable Executable (PE) Loader manipulate any of these information?
3
Voting to close as too broad: too many questions in one.Ciro Santilli 新疆再教育营六四事件法轮功郝海东

3 Answers

14
votes

Where all of those information stored (NULL, LDT, TSS, and Segment Descriptor)? Are they in Physical Memory, RAM or Local Hard Drive?

The CPU, if it does not cache internally the contents of the GDT, would need to access it every now and then, maybe all the time. The CPU cannot just go and read it from the disk, because it does not know anything about the disk or the file system on it, nor does it know how not to interfere with the OS accessing the same disk. The CPU cannot rely on the OS's help either, because the OS too, in the process of getting something from the disk, can make the CPU fetch something from the GDT. Catch 22.

You really don't want the GDT to be swapped out to the disk. If not for the reason above, then because of performance implications. And so the GDT is always in memory, physical memory (=RAM).

Are Segment Descriptors the same as Code Segment (ASM: CS), Data Segment (ASM: DS), ... and if so how many of those are stored in GDT (Is there only one CS, one DS, ...)?

Segment registers (CS,DS,etc) contain segment selectors, which are basically indices into the GDT (or LDT) in protected mode, these indices point/select the segment descriptors in the descriptor table(s). In the real and virtual 8086 modes it's the same, except the tables aren't used because segment locations and sizes (which we get from the tables in protected mode) aren't arbitrary and need no look up, they are computed immediately.

Note, the following terms mean different things and are not to be confused or used interchangeably:

  • segment register
  • segment selector
  • segment descriptor
  • segment descriptor table
  • segment

Does Protable Executable (PE) Loader manipulate any of these information?

It shouldn't. Not directly, at least. Most Windows programs use the same segment selector values in CS, DS, ES, SS. The GDT entries, to which these selectors point, don't change, they're global and shared among all programs.

The only thing that usually varies between programs and threads is the segment (and possibly its selector) that's used to access thread-local storage (TLS). The FS or GS segment register holds the selector pointing to this TLS segment. And all accesses to the TLS are done with instructions that use the appropriate segment override prefix (FS: or GS:). TLS is unique to each thread.

7
votes

OSDev.org gives a nice run down of the GDT, however, the tutorial detailing its implementation is of way more interest. (you may also be interested in its smaller, less privileged cousin, the LDT)

  1. the GDT resides in main memory, and is loaded using a linear address (via LGDT), thus it may be in paged memory.

  2. Yes (see the glossary), and you can store whatever segment bases you like, generally, CS and DS are the bare minimum (and user+kernel versions are required if using SYSENTER/SYSEXIT).

  3. No, it would rather alter the LDT (as x86 provides facilities for swapping them, but not the GDT, which requires a real mode instruction to be set, and jumping between real & protected mode isn't feasible).

1
votes

I agree with the answers above, but a more complete answer for question number 2 would be:
Segment Descriptor specifies the size of the segment, the access rights and privilege level for the segment, the segment type, and the location of the first byte of the segment in the linear address space (called the base address of the segment). So Segment Descriptor is not just the CS, DS,... register.