Virtual 8086 Mode

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Virtual 8086 Mode

Post by bloodline »

Hi,

I am thinking about adding a software 8086 emulator to my OS to handle real mode BIOS calls (specifically to the VIDEO Card BIOS) rather than forcing the CPU into actually enter some kind of "real mode", and I notice that the Virtual 8086 Mode page on the wiki is really quite limited in the information it provides (not really offering suggestions for a software solution), as well as all the links at the bottom of the page are now all broken.

https://wiki.osdev.org/Virtual_8086_Mode
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
nullplan
Member
Member
Posts: 1780
Joined: Wed Aug 30, 2017 8:24 am

Re: Virtual 8086 Mode

Post by nullplan »

bloodline wrote:I am thinking about adding a software 8086 emulator to my OS to handle real mode BIOS calls (specifically to the VIDEO Card BIOS) rather than forcing the CPU into actually enter some kind of "real mode", and I notice that the Virtual 8086 Mode page on the wiki is really quite limited in the information it provides (not really offering suggestions for a software solution),
Well, the two aren't related. V8086 mode needs a monitor and is only available in Protected Mode on x86, while x86 emulation is available anywhere (Long Mode on x86, or on any other architecture), but requires an actual emulator. While you can solve the same problem with both of them (running BIOS/expansion ROM code while not in a state to do so natively), for one, usually better solutions exist (for storage: Write a native driver for your OS; for graphics. Write a native driver, or use firmware/bootloader to set a good video mode and only touch the framebuffer afterwards), and for two, they are completely different approaches.

The article you mentioned does have a link to the Virtual Monitor page, which sports two of the same links. Maybe Virtual Monitor and V8086 Mode should be merged (they are basically the same topic, anyway), and a page on x86 emulation be added and then linked from the V8086 Mode page.

My advice on getting your own emulator started is getting stuck in the appendices of the AMD APM vol. 3. That's the one with the general purpose instructions. The appendices detail the format of instructions (including prefixes and operand encodings in the different operating modes). If I were to write such an emulator, I would do it as I would write every CPU emulator: Define a structure containing all the registers you wish to implement. Design functions to handle memory read/write in all four sizes, as well as I/O read/write in all four sizes (well, OK, legacy BIOS code is unlikely to use Long Mode, so maybe you only need to support three sizes). For memory, emulate a system that has the BIOS IVT (as read at system start), and the BDA at 0x400, and the EBDA at wherever that one was, and the BIOS ROM at its various locations, and a tiny program at 0x600 (which should at least not be allocated by BIOS). I'm thinking of just putting an "int XX" opcode there. And then:

Code: Select all

regs.cs = 0;
regs.ip = 0x600;
while (regs.cs != 0 || regs.ip != 0x602)
  x86_step(&regs);
That, or you make a special opcode that means "exit emulator" and put it in an unused spot in the opcode map. And then x86_step() checks for external interrupts and does what x86 does on external interrupt (save interrupt frame to stack, redirect control flow according to IVT). And otherwise it decodes and executes an instruction. Remember to keep track of operating mode, default operand size, adjusted operand size after 66h prefix, all of that good stuff. But not in theory a whole lot different from all the things you have to know to write assembler.

Then you can put all of this stuff into its own kernel task, so it does not clog up the machine, and Bob should be your uncle.
bloodline wrote: as well as all the links at the bottom of the page are now all broken.
Well, one of them isn't. But yes, the dead ones should be removed or updated. However, the links to x86emu don't belong on that page, IMHO. Again, emulation and V8086 mode are quite different.
Carpe diem!
Post Reply