2

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?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Abhinav Sharma
  • 147
  • 2
  • 9
  • If booting via legacy BIOS you are put in REAL MODE. v8086 mode is a special protected mode that allows virtualization of 8086 tasks. To get into that mode you need to enter 32-bit protected mode first and then switch into v8086 mode. – Michael Petch Oct 31 '19 at 22:31
  • With multiboot (via GRUB) you start in 32-bit protected mode (with a20 line enabled). If you want to get to real mode then you need to switch out of protected mode into real mode and then when finished you switch back to protected mode. v8086 mode is more easily reached from protected mode (as v8086 is a special form of 32-bit protected mode) however you need to write a v8086 monitor to control v8086 mode and that isn't easy. – Michael Petch Oct 31 '19 at 22:39
  • When the computer eventually loads a bootloader from disk, the firmware has already been running and doing tons of stuff since power-on, including switching to protected and long mode. To boot a legacy BIOS MBR bootloader, yes it switches back to real mode. Otherwise to boot a UEFI application it stays in 64-bit mode. – Peter Cordes Oct 31 '19 at 23:46
  • 1
    In one of my alternate accounts on github I had a *basic* skeleton for a v8086 monitor: https://github.com/marleyd386/OSDev/tree/master/examples/v8086-intn-iret , It was meant to be part of an answer on odev.org and an SO Q&A but I never got around to publishing it in such a manner. It was specifically designed to demonstrate handling 16-bit `iret` and `int n` instructions (and nothing more) – Michael Petch Nov 01 '19 at 01:24

1 Answers1

7

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, ...).

Brendan
  • 35,656
  • 2
  • 39
  • 66