Take the 2-minute tour ×

I'm wondering how to create a minimal virtual machine that'll be modeled after the Intel 16 bit system. This would be my first actual C project, most of my code is 100 lines or less, but I have the core fundamentals down, read K&R, and understand how things ought to work, so this pretty much is a test of wits.

Could anyone guide me in as far as documentation, tools, tutorials, or plain old tips/pointers on how to go about this, thus far I understand that I require somewhere to store data, a CPU of sorts and some sort of mechanism that functions as an interrupt controller.

I'm doing this to learn: Systems internals, ASM internals and C - three facets of computing that I want to learn in a singular project.

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

Thanks for reading, and hopefully writing!

share|improve this question

4 Answers 4

Virtual machines fall into two categories: those that interpret the code instruction at a time and those that compile the code to native instructions (e.g. "JIT").

The interpretation category is usually built around an instruction execution loop, using a switch statement, computed gotos or function pointers to determine how to execute each instruction.

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

Corewars is a programming challenge game where programs written in "Redcode" run on a MARS VM. There are many MARS VMs, typically written in C.

It has also inspired 8086-based versions, where programs written in 8086 assembler battle.

share|improve this answer
What you describe are not Virtual Machines but emulators. –  E.T Jul 26 at 6:17
@E.T well, the very first line from en.wikipedia.org/wiki/Virtual_machine is "a virtual machine (VM) is an emulation of a particular computer system". So what distinction are you trying to make? –  Will Jul 28 at 9:37
what I mean is that an emulators (if you consider JIT as one for instance) interpret the code bytes and execute some compiled code themselves. A VMM, especially those that use hardware features let the original code run as it would on a physical machine. As a matter of fact, the only things that are emulated are the devices. When people talk about Virtual Machines, the first thing that comes to mind is the HyperVisor that goes with it. –  E.T Jul 29 at 21:32
@E.T you mean that the JVM is misnamed? And would you say that Bochs or QEMU are VMs? –  Will Jul 31 at 17:11
QEMU is a virtualizer (with KVM being the Type II Hypervisor). Bochs is an emulator. Virtual Machines run inside of those. –  E.T Jul 31 at 21:28

Well, for starters I would pick up a reference book for assembly language for the processor you intend to virtualize, like 80286 or similar.

share|improve this answer

For a JIT, you might want to dynamically generate and execute x86 code.

share|improve this answer

If you want to write a Virtual Machine using the x86 VMM technology you will need quite a bit of things.

There are a few instructions that are critical such as VM_ENTER/VM_EXIT (name can change depending on the chip, AMD and INTEL use different names but the functionalities are the same). Those instructions are actually privileged and therefore, you will need to write a kernel module to use them.

The first step for your VM to start is to boot it and therefore, you will need a 'BIOS' which will be loaded. Then you need to emulate devices, etc. You could even run an old version of MSDOS in such a VM if you wanted to.

All in all, it clearly isn't trivial and requires a lot of time and effort.

Now, you could do something similar to what VMWare used to do before the Virtualization ready CPUs appeared.

share|improve this answer

Your Answer

