Search code examples
x86kernelbootloaderosdevmultiboot

Do modern computers boot in real mode or virtual real mode?


So I was reading about the processor modes and came to know that virtual real mode allows a real mode application e.g. DOS application such as BIOS program to run within a protected mode operating system.

So my question is do the current systems load in real mode first and then protected more or directly into virtual real mode because otherwise, we'll have to create a multiboot bootloader starting with real mode then jumping to virtual. Doesn't virtual real mode make it easy?


Solution

  • do the current systems load in real mode first and then protected more or directly into virtual real mode because otherwise, we'll have to create a multiboot bootloader starting with real mode then jumping to virtual. Doesn't virtual real mode make it easy?

    For obsolete systems (that still use BIOS and not UEFI); the firmware has to assume that the boot loader may:

    • switch to protected mode and use virtual 8086 mode (to access BIOS functions), and/or
    • switch between protected mode and real mode as it loads stuff, and/or
    • use "unreal mode"

    Therefore the BIOS can not/must not use protected mode (or virtual 8086 mode) itself, because that may prevent a boot loader from working properly.

    Doesn't virtual real mode make it easy?

    Virtual 8086 mode ("virtual real mode") is a bit painful to support. For it to work properly; you have to have exception handlers (e.g. "general protection fault" handler) that emulate various privileged instructions. Essentially; you get the "CS:IP" from the exception handler's stack, then do some sanity checks (was problem a segment limit violation or ...?), then decode the raw bytes at "CS:IP" to figure out what the code was trying to do, then emulate every possible case while ensuring "100% correct" behavior for each different case.

    The only sane reason to use virtual 8086 mode is when you want to run applications designed for an ancient real mode OS (e.g. MS-DOS) under an ancient multi-tasking 32-bit OS (e.g. Windows 95); and the hassle of emulating all of the privileged instructions is relatively minor compared to the huge hassle of emulating all of the other hardware (virtual PIT chip, virtual keyboard controller, virtual video card, ...).