0

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

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
Panther Coder
  • 1,058
  • 1
  • 16
  • 43

1 Answers1

2

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.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Please can you elaborate on the segment:offset translation in protected mode? – Panther Coder Aug 01 '16 at 05:30
  • 3
    @PantherCoder Mandatory reading: Intel® 64 and IA-32 Architectures Software Developer’s Manual, vol. 3 "System Programming Guide", chapters 2 - 5 and around. – Alexey Frunze Aug 01 '16 at 05:55
  • Yes, usually they'd use the processor in 16-bit protected mode. Some earlier OSes and environments would let the memory access fault, the fault handler would generate a set of descriptor entries to allow the instruction to complete properly and then restart at the instruction that caused the fault. Often you'd find use of the LOADALL instruction to load the hidden descriptor entries directly. Of course this had a lot of pitfalls - hard to have an OS and protected mode program run together when some of their descriptors may overlap if you wanted the OS and the program to run at same time. – Michael Petch Aug 01 '16 at 17:32
  • That was also assuming (at that time in history) you weren't using a 286 that had the bug that clobbered the CX register when there was a GPF lol. – Michael Petch Aug 01 '16 at 17:34