Checking whether an OS is in a virtualized environment
The hypervisor provides a mechanism for an OS to determine if it is running in a virtualized environment and in particular, a QNX hypervisor environment.
ARM platforms
On ARM platforms, check the model
property in the Flattened Device
Tree (FDT) that describes the underlying system. If this property is set with
QVM-v8A
, then the OS is hosted in a QNX hypervisor VM
(i.e., it is a guest OS).
You can obtain the VMID through the vdev API; for details, refer to the qvm_guest_vmid() entry in the Virtual Device Developer's API Reference.
x86 platforms
For x86 platforms, see your hardware documentation to learn which CPUID register bit your
OS needs to check to know if it is running in a hypervisor, and where in the register it
needs to look for the VM ID string. The ID string for QNX hypervisor VMs is QNXQVMBS
.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
int cpuid(uint32_t id, uint32_t *regs) {
asm volatile ("cpuid"
: "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (id), "c" (0));
return 0;
}
static const char * const qnxstr = "QNXQVMBS";
int main(int argc, char *argv[])
{
uint32_t regs[4];
cpuid(1, regs);
if ((regs[2] & (1 << 31)) == 0) {
puts("We are not running in a hypervisor");
return 1; /* not a hypervisor */
}
/* It is a hypervisor, but is it OUR hypervisor? */
cpuid(0x40000000, regs);
regs[1] = htonl(regs[1]);
regs[2] = htonl(regs[2]);
if (memcmp(®s[1], qnxstr, 4) != 0 ||
memcmp(®s[2], qnxstr + 4, 4) != 0) {
puts("This is a hypervisor but not a QNX hypervisor system");
return 1; /* not ours :( */
}
/* It is a QNX hypervisor. */
puts("This is a QNX QVM hypervisor system");
return 0;
}
Linux and Android
You can adapt the C code above for Linux and Android on x86, or run the cpuid utility.