Return to real mode from long mode and back
Posted: Mon Apr 26, 2021 4:40 am
I'm writing a bootloader that supports BIOS and UEFI and I need to use the VESA functions for the getting framebuffer. I understand that only way to access this functionality is through BIOS interrupts. The problem is I'm in long mode and I would need to switch back into real mode somehow. There's an OSDev page example (https://wiki.osdev.org/Real_Mode#x86_Assembly_Example) for switching from 32-bit protected mode to real mode but not long mode -> real mode.
According to the AMD manual, to return to protected mode (as protected mode is a prerequisite for switching to real mode)
To return from long mode to legacy protected mode with paging enabled, software must deactivate and disable long mode using the following sequence:
According to the AMD manual, to return to protected mode (as protected mode is a prerequisite for switching to real mode)
To return from long mode to legacy protected mode with paging enabled, software must deactivate and disable long mode using the following sequence:
- 1. Switch to compatibility mode and place the processor at the highest privilege level (CPL=0).2.
2. Deactivate long mode by clearing CR0.PG to 0. This causes the processor to clear the LMA bit to 0. The MOV CR0 instruction used to disable paging must be located in an identity-mapped page. Once paging is disabled, the processor behaves as a standard 32-bit x86 processor.
3. Load CR3 with the physical base-address of the legacy page tables.
4. Disable long mode by clearing EFER.LME to 0.
5. Enable legacy page-translation by setting CR0.PG to 1. The instruction following the MOV CR0 that enables paging must be a branch, and both the MOV CR0 and the following branch instruction must be located in an identity-mapped page.