How can I create my own virtual machine?

I am wondering how to create a minimal virtual machine that will be simulated after a 16-bit Intel system. This will be my first actual C project, most of my code is 100 lines or less, but I have the basic basics, read K&R and understand how things are supposed to work, so this is pretty much a test of wits.

Can anyone help me with any documentation, tools, tutorials or simple old tips / pointers on how to do this, so far I understand that I need to store data somewhere, some kind of processor and something- It's kind of a mechanism that functions like an interrupt controller.

I do this to find out: System internals, ASM internals, and C are three aspects of computing that I want to learn in a unique project.

Please be kind enough not to tell me to do something simpler - it will only be annoying. :)

Thanks for reading and hopefully writing!


source to share

4 answers

Virtual machines fall into two categories: those that interpret a code instruction at the same time, and those that compile code for their own instructions (for example, "JIT").

An interpreter class is usually built around a statement execution loop using a switch statement, computed access points, or function pointers to determine how to execute each statement.

There is a fun platform worth exploring for its simplicity and fun: Corewars.

Corewars is a programming game in which programs written in " Redcode " on MARS VM. There are many MARS VMs that are usually written in C.

It is also inspired by 8086-based versions where programs written in the 8086 assembler battle battle.



Okay, first I would take a reference for the assembly language for the processor you intend to virtualize, such as an 80286 or similar.





If you want to write a virtual machine using VMM x86 technology, you will need a lot of things.

There are several instructions that are critical, for example VM_ENTER / VM_EXIT (the name can change depending on the chip, AMD and INTEL use different names, but the functionality is the same). These instructions are indeed privileged and therefore you will need to write a kernel module to use them.

The first step to start your virtual machine is to boot it, and therefore you will need a "BIOS" to be loaded. Then you need to emulate devices, etc. You can even run an older version of MSDOS in such a VM if you like.

Overall, this is clearly not trivial and requires a lot of time and effort.

Now you can do something similar to what VMWare used to do before there were off-the-shelf processors for virtualization.



All Articles