Using VirtualBox to execute specific sections of code

This is for discussing general topics about how to use VirtualBox.
Post Reply
Glidos64
Posts: 2
Joined: 10. Jul 2022, 12:30

Using VirtualBox to execute specific sections of code

Post by Glidos64 »

I'm wondering if it is possible to use VirtualBox to run a VM without an operating system installed - just injecting sections of code to exectute, catching any exceptions the code runs into from outside the VM. I'm just started reading the docs and am struggling to find if there is a level in the API that supports that sort of use.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Using VirtualBox to execute specific sections of code

Post by fth0 »

First of all, a virtual machine is just like a physical PC. It starts executing a BIOS or UEFI provided by VirtualBox, loads the first sector of a disk and executes the code. If this sector contains an MBR or something you provide is up to you.

Additionally, you can use the 12.1.4. The Built-In VM Debugger.
Glidos64
Posts: 2
Joined: 10. Jul 2022, 12:30

Re: Using VirtualBox to execute specific sections of code

Post by Glidos64 »

Useful to know about, but not quite what I was looking for. I was hoping to be able to write CPU instructions directly into the VMs memory while it's not running, set the CPU registers for start up and set it running. Then if the VM ran into something like a SWI have it stop and be able to analyse it's state, change it and restart.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Using VirtualBox to execute specific sections of code

Post by mpack »

A VM doesn't have any "memory" when it isn't running.

When an MBR PC starts up it will execute whatever code it finds in sector zero of the hard drive. That's how PCs boot (at least until EFI came along). I suggest you get yourself a BIOS reference, since that will be the only "OS" you have available to implement a user interface.

Whenever you have a question like this, ask what the answer would be with a physical PC, since the answer is nearly always the same. So, how do you get a PC to run code without turning it on? You don't. How do you get a PC to execute your code without an OS? Put the code in the boot sector of a drive, and get the PC to boot from it. This will obviously require some knowledge, including a knowledge of BIOS and disk images.

VirtualBox is not an interactive coding experience. It's a PC hardware simulator.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Using VirtualBox to execute specific sections of code

Post by fth0 »

Glidos64 wrote:Useful to know about, but not quite what I was looking for. [...]
Then you'll probably have to rephrase your question again ;) because I'd answer your second statement again with the second part of my answer, namely using a debugger.

Or are you perhaps asking for a hypervisor monitoring the host?
Post Reply