Page 1 of 1

LDT uses

Posted: Sat Dec 27, 2008 3:42 pm
by Hangin10
What would some uses of the LDT be?
And don't just say "none" because new stuff should use Long Mode. :P

At first I thought maybe being able to run everything in ring 0, making the segments not
include kernel space. But then I realized that can just as easily be done with the GDT (assuming
paging gives all the segments the same address space and all that's needed is different base/limits
for kernel vs. apps/drivers).

Were they useful for making lists of call gates different for each process?

Re: LDT uses

Posted: Sat Dec 27, 2008 4:18 pm
by Troy Martin
Wikipedia wrote:The LDT is the sibling of the Global Descriptor Table and similarly defines up to 8191 memory segments accessible to programs.

On x86 processors not having paging features, like the Intel 80286, the LDT is essential to implementing separate address spaces for multiple processes. There will be generally one LDT per user process, describing privately held memory, while shared memory and kernel memory will be described by the GDT. The operating system will switch the current LDT when scheduling a new process, using the LLDT machine instruction. On the contrary, the GDT is generally not switched (although this may happen if virtual machine monitors like VMware are running on the computer).

Re: LDT uses

Posted: Sat Dec 27, 2008 10:38 pm
by Hangin10
:oops: doh.

So it's just a holder from when segmentation was important?

Re: LDT uses

Posted: Sat Dec 27, 2008 10:52 pm
by Troy Martin
Looks like it! I guess you could use it as an alternative to TSSes or something.

Re: LDT uses

Posted: Sun Dec 28, 2008 2:16 am
by kasper
Hi all,
Hangin10 wrote:What would some uses of the LDT be?
  • Perhaps you can use them to share some memory between processes. The shared memory can contain pointers, which point to other parts in the shared segment. The shared memory doesn't have to have the same virtual address, since each process can use its own segment selector in the segment register.

    But I'm not sure if this is useful or that allocating a virtual address range free in all sharing processes is never a problem. What do others advise?
  • The [tt]limit[/tt] part of LDT entries allows more fine grained control over what memory you share than 4k pages do.
  • Hangin10 wrote:Were they useful for making lists of call gates different for each process?
    Good idea, you can use them for that. Would it be wise to do so?
Kasper

Re: LDT uses

Posted: Sun Dec 28, 2008 5:50 am
by kasper
Hi again,

I noticed an other use for LDT in the topic L4's hidden secret, started by kiwipresse.
Brendan wrote:[...]
kiwipresse wrote:I'm just wondering why L4 is considered to be so efficient instead of other microkernels? I read the wikipedia article which suggests that they use some fancy IPC and optimize here and there. Does somebody know something more concretly before I dig into sources :?
There's at least 2 techniques that L4 uses that I'm aware of...

The first one is called "small address spaces". The idea here is to use segmentation to split a large address space into a medium address space and several small address spaces; so you can switch from one process to another by changing segment registers if both processes are in the same large address space, which reduces TLB misses because there's no need to flush any TLB entries in this case. For example, the large 4 GiB address space might be split into one 2 GiB medium address space and eight 128 MiB small address spaces (with the remaining 1 GiB used by the kernel). Also, if a process is running in a small address space and grows too big it'll be shifted into it's own medium address space, so that processes aren't limited to the size of a small address space.
[...]
Kasper

Re: LDT uses

Posted: Sun Dec 28, 2008 8:01 am
by Love4Boobies
LDTs are dropped on 64-bit modes (in fact, segmentation is dropped altogether). Also, as I've said on the topic you're quoting, L4 can't be ported to 64 bits since it is tied to segmentation in order to reduce the number of TLB flushes.

Re: LDT uses

Posted: Sun Dec 28, 2008 8:08 am
by Brendan
Hi,
Love4Boobies wrote:LDTs are dropped on 64-bit modes (in fact, segmentation is dropped altogether).
That's actually a fairly common misconception.

In long mode, you can have 64-bit code, 32-bit code and 16-bit code (just like in protected mode you can have 32-bit code and 16-bit code). All segmentation is still used for 32-bit code and 16-bit code - the only real difference is the GDT and LDT entry format used for these segments (the base address is extended to 64-bit). Also, FS and GS are still used for 64-bit code (the base addresses of these segments are honoured, but the segments limits are ignored). The LDT can still be used in long mode too.


Cheers,

Brendan

Re: LDT uses

Posted: Sun Dec 28, 2008 8:11 am
by Love4Boobies
I don't have time to go through the manuals now (since I'm not at home) but is that only true for the compatibility mode?

Re: LDT uses

Posted: Sun Dec 28, 2008 8:20 am
by Brendan
Hi,
Love4Boobies wrote:I don't have time to go through the manuals now (since I'm not at home) but is that only true for the compatibility mode?
In long mode, the code segment is either 64-bit, 32-bit or 16-bit. If the code segment is 64-bit then you're in "64-bit mode" (most segmentation disabled), and if the code segment is 32-bit or 16-bit then you're in "compatibility mode" (all segmentation enabled). All of these modes are described as "sub-modes" in AMD's manual - you'd still be in "long mode" regardless of the what sort of code segment you use (same paging structures, same GDT/LDT entries, same IDT entries, same TSS, same lack of virtual80x86, etc).

This is one thing I'm planning to take serious advantage of. If device drivers run as separate CPL=3 processes, then all 32-bit device drivers can be run under both 32-bit kernels and 64-bit kernels; which means that for a large number of device drivers there's no need for me to provide 64-bit device drivers.


Cheers,

Brendan