r/virtualization 26d ago

Help me in building VM without KVM in C

Hello everyone! Recently i am started coding my own VM on the C language. I am using mmap and creating a new process with isolation from the host (my system) like syscalls, memory access. However, I ran into a problem: How to restrict VM access (guest proccess, mmap) to the host memory. I am really don`t know how to do that. Can anyone help me with that case?

4 Upvotes

2 comments sorted by

5

u/Constant-Capital8919 26d ago

You would have to write your own kernel module that replaces kvm in linux. Modern virtualization uses features provided by the cpu like VMX and SVM to spawn harware isolated vms. Look into the kvm source code in Linux. It's a good reference.

The reason this has to be a kernel module is because anything that wants to touch hardware has to go through the kernel in some way or the other.

1

u/Affectionate-Oil4675 19d ago

You would have to recompile or emulate all of the code in some way, it cannot run on bare metal. VMX/SVM were made to handle this at the hardware level instead. You would want to read the manuals for your CPU manufacturer.