[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
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How to detect Virtual Box (OS Level)

Post by SpyderTL »

octacone wrote:Also: why does paging mess up my LBF, when I enable paging I can't plot pixels.
You need to make sure that you map enough pages to cover your entire LFB, either exactly the same as your physical address (identity map), or if not, make sure your code is using your new virtual address instead of your physical address. You also need to make sure these pages are marked as present, writable, and that it will never be swapped to disk.
octacone wrote: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?
Yes. Details on PCI configuration space (your offset registers) can be found on the PCI wiki page. Just simply loop over bus 1-255, then device 1-32, then function 1-15, and pass 0x0000 as your offset, and you'll get both the Vendor ID and Device ID for every PCI device in your system back. If it comes back as 0xFFFFFFFF, then that particular function (slot) is empty.
Last edited by SpyderTL on Fri Aug 12, 2016 1:41 pm, edited 2 times in total.
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:
octacone wrote:Also: why does paging mess up my LBF, when I enable paging I can't plot pixels.
You need to make sure that you map enough pages to cover your entire LFB, either exactly the same as your physical address (identity map), or if not, make sure your code is using your new virtual address instead of your physical address. You also need to make sure these pages are locked so that they don't get swapped to disk.
My paging is very simple and not final, so I better keep it turned off until I make a better version.
Right now I just want to detect Qemu and hard code what I can.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
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 »

Never, ever, hard-code anything. The linear frame buffer can be anywhere. It's only ISA BGA that has it at 0xE0000000. PCI BGA can be anywhere; there are no rules. Even Bochs supports PCI BGA if you enable it in your configuration and the framebuffer can be relocated.
PCI is very easy, and your question says you didn't read the PCI Wiki entry.
Just for completion:

Code: Select all

unsigned int pci_read(pci_dev_t* device, char reg)
{
	unsigned char bus, slot, function;
	unsigned int config;
	bus = device->bus;
	slot = device->slot;
	function = device->function;
	config = (unsigned int)(bus<<16) | (slot<<11) | (function<<8) | (reg&0xFC) | 0x80000000;
	outd(PCI_CONFIG_INDEX, config);
	iowait();
	return (unsigned int)ind(PCI_CONFIG_DATA);
}

void pci_write(pci_dev_t* device, char reg, unsigned int val)
{
	char bus, slot, function;
	unsigned int config;
	bus = device->bus;
	slot = device->slot;
	function = device->function;
	config = (unsigned int)(bus<<16) | (slot<<11) | (function<<8) | (reg&0xFC) | 0x80000000;
	outd(PCI_CONFIG_INDEX, config);
	iowait();
	outd(PCI_CONFIG_DATA, val);
	iowait();
}
P.S: I really suggest reading the Wiki before posting here, and after reading try and learn from trial and error. If that doesn't help, only then post here.
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:Never, ever, hard-code anything. The linear frame buffer can be anywhere. It's only ISA BGA that has it at 0xE0000000. PCI BGA can be anywhere; there are no rules. Even Bochs supports PCI BGA if you enable it in your configuration and the framebuffer can be relocated.
PCI is very easy, and your question says you didn't read the PCI Wiki entry.
Just for completion:

Code: Select all

unsigned int pci_read(pci_dev_t* device, char reg)
{
	unsigned char bus, slot, function;
	unsigned int config;
	bus = device->bus;
	slot = device->slot;
	function = device->function;
	config = (unsigned int)(bus<<16) | (slot<<11) | (function<<8) | (reg&0xFC) | 0x80000000;
	outd(PCI_CONFIG_INDEX, config);
	iowait();
	return (unsigned int)ind(PCI_CONFIG_DATA);
}

void pci_write(pci_dev_t* device, char reg, unsigned int val)
{
	char bus, slot, function;
	unsigned int config;
	bus = device->bus;
	slot = device->slot;
	function = device->function;
	config = (unsigned int)(bus<<16) | (slot<<11) | (function<<8) | (reg&0xFC) | 0x80000000;
	outd(PCI_CONFIG_INDEX, config);
	iowait();
	outd(PCI_CONFIG_DATA, val);
	iowait();
}
Well, when you are left alone without any ideas you just hard code stuff. :P
I didn't read the entire PCI wiki because I was hoping to hard code my LFB because PCI seemed too complex. I don't know what I am supposed to be looking for but yeah.
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 »

If reading and understanding the wiki page on PCI is too complex for you, this may not be your field.
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 »

Kazinsal wrote:If reading and understanding the wiki page on PCI is too complex for you, this may not be your field.
Better say it is time consuming.
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 »

I tried to prototype something...ugh...

typedef struct pci_dev_t
{
uint32_t bus;
uint32_t slot;
uint32_t function;
};

pci_dev_t framebf;

unsigned int pci_read(pci_dev_t* device, char reg)
{
unsigned char bus, slot, function;
unsigned int config;
bus = device->bus;
slot = device->slot;
function = device->function;
config = (unsigned int)(bus<<16) | (slot<<11) | (function<<8) | (reg&0xFC) | 0x80000000;
outportlong(0xCF8, config);
IO_Wait();
return (unsigned int)inportlong(0xCFC);
}

void FindLinearFrameBuffer()
{
framebf.bus = 0;
framebf.slot = 0;
framebf.function = "0x1234:0x1111"; //this is what I don't get
linearFrameBufferBGA = pci_read(struct framebf, regs); //what to put inside regs (some number I guess)
}
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
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 »

octacone wrote:
Kazinsal wrote:If reading and understanding the wiki page on PCI is too complex for you, this may not be your field.
Better say it is time consuming.
No, it is not. It took me less than a day to write my first PCI enumerator, which could search for devices by class codes or device/vendor ID combinations.
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:
octacone wrote:
Kazinsal wrote:If reading and understanding the wiki page on PCI is too complex for you, this may not be your field.
Better say it is time consuming.
No, it is not. It took me less than a day to write my first PCI enumerator, which could search for devices by class codes or device/vendor ID combinations.
#typedef timeConsuming >=2hours
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 »

I stand even more firmly by my previous statement now.

If you give up on something after two hours you're going to drop this hobby like it's on fire the second you encounter a heisenbug.
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 »

Kazinsal wrote:I stand even more firmly by my previous statement now.

If you give up on something after two hours you're going to drop this hobby like it's on fire the second you encounter a heisenbug.
I am not going to drop my hobby because I am annoyed. Already working on my PCI controller, I hope to finish it in less than two days and after that I want to be able to read my Linear Frame Buffer.

Just another question: once I get all the vendor IDs, device IDs, slots, functions, etc... How am I going to tell which one of them is Linear Frame Buffer?
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 »

octacone wrote:Just another question: once I get all the vendor IDs, device IDs, slots, functions, etc... How am I going to tell which one of them is Linear Frame Buffer?
By reading the wiki.
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 »

sleephacker wrote:
octacone wrote:Just another question: once I get all the vendor IDs, device IDs, slots, functions, etc... How am I going to tell which one of them is Linear Frame Buffer?
By reading the wiki.
And by reading source code.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
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 ~ »

If you want to detect whether you are running inside any emulator like Bochs, QEMU, VirtualBox, etc., you must know it very well and just inspect if it contains the hardware devices, the BIOS strings/signatures/splash screens/model names, network devices for sharing files between host and guest, and any other particular detail to identify functional system features that only one of those emulators would have. You would need to run several snippets, say, from FreeDOS running under an emulator to see what it takes to correctly detect whether it's an emulator and name it, or whether it's a real machine. If you do it under emulated Windows NT/XP/7 or Linux, you would need to somehow dump the BIOS and/or the CMOS from them. (The question is, how do you know if you booted from BIOS or UEFI and if its contents have got lost by a kernel overwrite?)
octacone wrote:I am not going to drop my hobby because I am annoyed. Already working on my PCI controller, I hope to finish it in less than two days and after that I want to be able to read my Linear Frame Buffer.

Just another question: once I get all the vendor IDs, device IDs, slots, functions, etc... How am I going to tell which one of them is Linear Frame Buffer?
I have a minimum time span of 3 days to start seeing results when it comes to difficult and new things.

First, you have to detect if the PCI controller is present or not (unless it's an old 386 or even older PC, PCI should be present, or the motherboard wouldn't even turn on if the PCI controller became damaged).

Then you have to use the BIOS to determine if PCI supports configuration mechanism 1, mechanism 2 or both in your system.
Read the following Wiki entry to see how to use the BIOS for it:
http://wiki.osdev.org/PCI


You can also use the following assembly code to try to detect PCI using configuration mechanism 1 (to have a really stable system, you should enter Protected Mode, start a separate V86 task to call the PCI BIOS services and if it crashes, you know that either the PCI BIOS is bogus and the PCI controller only supports Configuration Mechanism 1, or it is just not present in the motherboard):

00000000__Detect_PCI_Bus_Presence_Config_Mechanism_1.asm

Code: Select all

;Function to detect the presence of the PCI controller/bus
;using Configuration Mechanism 1.
;
;If it was found, it will return 0x80000000.
;;
_00000000__Detect_PCI_Bus_Presence_Config_Mechanism_1:
 pushf
 push ebx
 push edx



 .PCI_Addr_Reg_Write_Bus0_Dev0_Fn0_Reg0:
 ;;
  mov dx,0xCFC
  mov ebx,0x80000000
  mov eax,ebx
  out dx,eax


 .PCI_Addr_Reg_Read_Bus0_Dev0_Fn0_Reg0:
 ;;
  in eax,dx
  cmp eax,ebx
  je .FoundAndEnd


     .NotFound:
     ;;
      xor eax,eax



 .FoundAndEnd:
 ;;


 pop edx
 pop ebx
 popf
ret



;EOF
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 »

Why am I getting yellow instead of cyan?

Code: Select all

void PutPixel(int x, int y, Color color)
{
	unsigned location = (y * screenWidthBGA * 4) + (x * 4);
  	linearFrameBufferBGA[location + 0] = color.R;  
  	linearFrameBufferBGA[location + 1] = color.G;  
  	linearFrameBufferBGA[location + 2] = color.B; 
  	linearFrameBufferBGA[location + 3] = color.A;     
}
"Color" is just a structure that contains R/G/B/A values.
Cyan is Color ColorCyan = {0, 255, 255, 100}; but I keep getting yellow. :|
When I make ColorCyan = {255, 255, 0, 100}; then I get cyan but 255, 255, 0 is yellow. :O

Wiki says that I should be accessing pixels by 00AARRGGBB but when I do that nothing changes. :|
Now I made this sort of a hack that works. (Is it okay?)

Code: Select all


void PutPixel(int x, int y, Color color)
{
	unsigned location = (y * screenWidthBGA * 4) + (x * 4);
  	linearFrameBufferBGA[location + 0] = 0;  
  	linearFrameBufferBGA[location + 2] = color.R;  
  	linearFrameBufferBGA[location + 1] = color.G; 
  	linearFrameBufferBGA[location + 0] = color.B;     
}
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Post Reply