[Answered]How to detect Virtual Box (OS Level)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

[Answered]How to detect Virtual Box (OS Level)

Post by Octacone »

Okay, so today I was working on BGA a bit.
Once I completed my BGA driver I noticed something interesting. VirtualBox and Bochs use 0xE0000000 LFB address and Qemu uses 0xFD000008 LFB address. Now, I found out how to detect Bochs, if Bochs isn't detected then I set LFB to Qemu's LFB. Now how do I detect Virtual Box?
Last edited by Octacone on Sat Aug 13, 2016 2:12 pm, edited 1 time in total.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: How to detect Virtual Box (OS Level)

Post by Ch4ozz »

octacone wrote:Okay, so today I was working on BGA a bit.
Once I completed my BGA driver I noticed something interesting. VirtualBox and Bochs use 0xE0000000 LFB address and Qemu uses 0xFD000008 LFB address. Now, I found out how to detect Bochs, if Bochs isn't detected then I set LFB to Qemu's LFB. Now how do I detect Virtual Box?
Check the ACPI tables or the AHCI device names.
Im pretty sure there is an easier way, but thats the two I noticed during developing my os

Anyways, why do you hardcode this?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

Ch4ozz wrote:
octacone wrote:Okay, so today I was working on BGA a bit.
Once I completed my BGA driver I noticed something interesting. VirtualBox and Bochs use 0xE0000000 LFB address and Qemu uses 0xFD000008 LFB address. Now, I found out how to detect Bochs, if Bochs isn't detected then I set LFB to Qemu's LFB. Now how do I detect Virtual Box?
Check the ACPI tables or the AHCI device names.
Im pretty sure there is an easier way, but thats the two I noticed during developing my os
That is not going to work atm (I don't have ACPI or ACHI).
Ch4ozz wrote:Anyways, why do you hardcode this?
?? Detection or LFB?
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How to detect Virtual Box (OS Level)

Post by SpyderTL »

VirtualBox has a "VirtualBox Guest Service" device on the PCI bus, with Vendor ID 0x80EE and Device ID 0xCAFE.

If this exists, you can assume you're running under VirtualBox. Not sure what difference it makes, though. :)
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

SpyderTL wrote:VirtualBox has a "VirtualBox Guest Service" device on the PCI bus, with Vendor ID 0x80EE and Device ID 0xCAFE.

If this exists, you can assume you're running under VirtualBox. Not sure what difference it makes, though. :)
Very interesting. Can I use ports to detect this or I need to write PCI scanner? Thanks for the hint!
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
sleephacker
Member
Member
Posts: 97
Joined: Thu Aug 06, 2015 6:41 am
Location: Netherlands

Re: How to detect Virtual Box (OS Level)

Post by sleephacker »

You don't need to detect if you're running on bochs/QEMU/virtualbox, I've never used BGA but according to the wiki you can obtain the LFB address through PCI, which is probably more reliable than using hardcoded values.
EDIT: you would need to write a PCI scanner for both options.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

sleephacker wrote:You don't need to detect if you're running on bochs/QEMU/virtualbox, I've never used BGA but according to the wiki you can obtain the LFB address through PCI, which is probably more reliable than using hardcoded values.
EDIT: you would need to write a PCI scanner for both options.
Yeah, I should probably detect LFB rather than defining it. Thanks!!! Going to do it that way. :)
Also: why does paging mess up my LBF, when I enable paging I can't plot pixels.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: How to detect Virtual Box (OS Level)

Post by ~ »

You could see if the whole hardware device environment is the same that VirtualBox has (the same device IDs....).

You could search for VirtualBox BIOS signatures, strings that mention it, BIOS splash bitmaps that correspond to VirtualBox.

You could see if there are VirtualBox-style folder shares.

You could inspect for anything that has to do with VirtualBox in memory.
Last edited by ~ on Fri Aug 12, 2016 10:31 am, edited 1 time in total.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

~ wrote:You could see if the whole hardware device environment is the same that VirtualBox has (the same device IDs....).

You could search for VirtualBox BIOS signatures, strings that mention it, BIOS splash bitmaps that correspond to VirtualBox.

You could see if there are VirtualBox-style folder shares.

You should inspect for anything that has to do with VirtualBox in memory.
Wow, didn't know that there were so many ways. I think that the best way of detecting Virtual Box is by searching for device IDs. +1
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

SpyderTL wrote:VirtualBox has a "VirtualBox Guest Service" device on the PCI bus, with Vendor ID 0x80EE and Device ID 0xCAFE.

If this exists, you can assume you're running under VirtualBox. Not sure what difference it makes, though. :)
How to do this? I want to check Vendor ID/Device ID. So I need a function that sees if a device exists? So what buses/slots I should be looking at? I don't want to write an entire PCI system just to check if a device exists. There must be a way to check if a device with give ID exists. Right?
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: How to detect Virtual Box (OS Level)

Post by Kazinsal »

You have to write a whole PCI bus enumerator in order to reliably find devices.

Considering you'd have to do that anyways in order to write drivers for just about anything, you might as well do it now.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: How to detect Virtual Box (OS Level)

Post by BrightLight »

Detecting VirtualBox just to use BGA isn't worth it.
There is no real proof that a new VirtualBox version won't have the framebuffer at 0xE0000000. Instead, you should read the framebuffer from BAR0 of the PCI BGA device (device 0x1234:0x1111), and if it doesn't exist, only then should you use 0xE0000000. QEMU has a PCI BGA, and, as you have pointed out, uses the framebuffer at 0xFD000000.
EDIT: A PCI enumerator is very simple, actually. Once you have a routine to read and write registers to the PCI configuration space, most of it is done with common sense.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

omarrx024 wrote:Detecting VirtualBox just to use BGA isn't worth it.
There is no real proof that a new VirtualBox version won't have the framebuffer at 0xE0000000. Instead, you should read the framebuffer from BAR0 of the PCI BGA device (device 0x1234:0x1111), and if it doesn't exist, only then should you use 0xE0000000. QEMU has a PCI BGA, and, as you have pointed out, uses the framebuffer at 0xFD000000.
EDIT: A PCI enumerator is very simple, actually. Once you have a routine to read and write registers to the PCI configuration space, most of it is done with common sense.
I have a function that detects both Bochs and VirtualBox and if they don't get detected system makes an assumption that the emulator equals to Qemu. Atm I can read from 32Bit PCI, is that useful? It has (bus, device, function, offset).

I just need if(CheckVendor("0xCAFE") == true)
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

What do I need to input in:
uint32_t Read32BitPCI(uint8_t bus, uint8_t device, uint8_t function, uint32_t offset)

Can I see if my vendor id device exists using that?

Another IDEA :idea: :idea: :idea: :idea:
I can skip Virtual Box detection if I find a way to detect Qemu <------
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to detect Virtual Box (OS Level)

Post by Octacone »

This turned into my worst nightmare.

Is there a simple way to detect Qemu? I bet that somebody know the answer.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Post Reply