assemblyx86x86-64instruction-setgdt

If LDT does not exist in 64-bit architecture how are 32-bit systems that use it emulated on a 64-bit architecture?


I read that LDT (Local Descriptor Table) does not exist in 64-bit architecture and was wondering how a 32-bit system that uses it is emulated.


Solution

  • Your premise is incorrect. Even when running a 64-bit kernel, x86-64 can still use an LDT. lldt is valid in 64-bit mode. More specifically, a comment on Is an LDT needed? indicates that 64-bit Windows forbids using it, but that's just Windows, not the x86-64 ISA. For example, x86-64 Linux still supports the modify_ldt() system call. (IIRC, Linux didn't bother to add functionality to let you create 64-bit code segments with it, though. But a purely 32-bit process wouldn't need to do that.)

    But that would only be relevant for a 32-bit user-space process that needs to create an LDT entry.

    You were asking about a 32-bit system, i.e. booting an old kernel that isn't aware of x86-64 at all.

    An x86-64 CPU in legacy mode (i.e. running a 32-bit kernel) is exactly identical to a CPU that doesn't support 64-bit mode at all. (Except that it will switch into 64-bit mode if you put certain bits in the right places.) So the CPU being 64-bit capable really has nothing to do with running a fully 32-bit system.


    If you truly mean emulated, then the underlying HW is irrelevant. Write your emulator in any Turing-complete language, and include that feature. (Or use an existing one like BOCHS or Qemu.)

    If you mean hardware virtualization (e.g. Intel VT or AMD-V), then a VM guest can do whatever it wants, including run in legacy mode and boot 32-bit Windows or run your own custom bare-metal OS in real, protected, or long mode. It's irrelevant if the host VM is running 64-bit Windows or whatever, the guest's LDT is its own business and doesn't involve an LDT on the host.

    If you meant virtualization without hardware support, like in the bad old days before Intel VT and AMD-V, that's harder but the hypervisor is outside any of the guests, so they're still independent of each other. It's hard because x86 has some "sensitive" (in the virtualization sense) instructions that don't trap. This is why most people only do x86 virtualization at all with HW support. If that's not available, just emulate, like with BOCHS, or Qemu's JIT dynamic recompilation.