Search code examples
x86operating-systemkernelprotected-modegdt

why real mode applications cannot be run in a protected mode?


I was studying this real to protected mode transition. I had a doubt whether real to protected mode can happen without loading ldt and idt but by loading gdt. With that on one side, a second doubt arised such that why real mode programs cannot be run in protected mode without shifting to v8086 mode?

Thanks


Solution

  • An LDT is optional. An IDT is a protected-mode equivalent for a real-mode IVT and serves the same purpose. It describes entry points into ISRs and exception handlers. You need an IDT to be able to service hardware and software interrupts and exceptions. If you can live without those, you don't need to set up an IDT.

    Switching to the protected mode requires a bit more than just setting up a GDT and performing LGDT. You need to change CR0 bit 0 to 1, perform a jump, load segment registers (preferably all to avoid issues with uninitialized segment registers during the various context switches) with selectors pointing to the appropriate GDT entries.

    Real-mode code generally can't run in the protected mode (except for the virtual 8086 (sub)mode) because real-mode values in segment registers can't work in the protected mode and because segment:offset addresses are translated into physical addresses differently in the protected mode (read up on GDT and page translation). IOW, adding 1 to the value in a segment register no longer has the effect of adding 16 to the resultant physical address. Further, you can't have a segment that is at the same time readable, writable and executable.

    In theory, you could set up GDT and/or LDT descriptors in such a way than a selector N selects a descriptor for a 64KB segment with the base address of N*16. In practice it's a kludge. However, Borland implemented this scheme in their Borland Pascal 7, so you could write protected-mode programs in a way similar to how you'd write them for the real mode.