pub fn runs_inside_qemu() -> bool {
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
    compile_error!("This crate only works on the x86/x86_64-platform.");

    let id = CpuId::new();

    // ########## STEP 1 ##########
    let feature_info = id.get_feature_info();
    if feature_info.is_none() {
        // should never happen, except the CPU or virtualization environment does weird things
        log::debug!(
            "feature_info is not present, this is REALLY strange. Can't verify if we are in QEMU."
        );
        return false;
    }
    let feature_info = feature_info.unwrap();
    if !feature_info.has_hypervisor() {
        // QEMU is a hypervisor and no real machine => exit if flag not set
        // If we run in a Hypervisor, this flag is set (also see https://lwn.net/Articles/301888/)
        log::debug!("Hypervisor-flag is not set, we are not in QEMU");
        return false;
    }

    // ########## STEP 2 ##########
    // Now we know that we are in a Hypervisor environment
    let hypervisor_info = id.get_hypervisor_info();
    if hypervisor_info.is_none() {
        // should never happen, except the CPU or virtualization environment does weird things
        log::debug!("hypervisor_info is not present but hypervisor-flag is, this is REALLY strange");
        return false;
    }
    let hypervisor_info = hypervisor_info.unwrap();
    // if this returns false, because the hypervisor ID can be "KVM",
    // we still could be executed by QEMU -> further checks needed
    if hypervisor_has_qemu_id(&hypervisor_info) {
        log::debug!("QEMU is the direct hypervisor");
        return true;
    }

    // ########## STEP 3 ##########
    // now check the extended CPU id, which is provided by QEMU
    let extended_info = id.get_extended_function_info();
    if extended_info.is_none() {
        return false;
    }
    let extended_info = extended_info.unwrap();
    let is_qemu = extended_brand_string_contains_qemu(&extended_info);
    if is_qemu {
        log::debug!("Runs inside QEMU with {:?} as accelerator", hypervisor_info.identify());
    }
    is_qemu
}
/// Returns if the code is running inside a QEMU virtual machine.
/// Only works on x86/x86_64 platform.
///
/// Doesn't panic and in case something strange happens, it returns
/// `false` in favor of a `Result`, because these errors are absolutely
/// unlikely.
///
/// ## Example Usage
///
/// ```rust
/// use runs_inside_qemu::runs_inside_qemu;
///
/// fn main() {
///     // If we are in QEMU, we use the nice "debugcon"-feature which maps
///     // the x86 I/O-port `0xe9` to stdout or a file.
///     if runs_inside_qemu() {
///         unsafe {
///             x86::io::outb(0xe9, b'H');
///             x86::io::outb(0xe9, b'e');
///             x86::io::outb(0xe9, b'l');
///             x86::io::outb(0xe9, b'l');
///             x86::io::outb(0xe9, b'o');
///             x86::io::outb(0xe9, b'\n');
///         }
///     }
/// }
/// ```
Returns if the code is running inside a QEMU virtual machine.
Only works on x86/x86_64 platform.

Doesn't panic and in case something strange happens, it returns
`false` in favor of a `Result`, because these errors are absolutely
unlikely.
Returns if the code is running inside a QEMU virtual machine . Only works on x86 / x86_64 platform . Doesn ' t panic and in case something strange happens it returns ` false ` in favor of a ` Result ` because these errors are absolutely unlikely .
pub fn runs_inside_qemu() -> bool {
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
    compile_error!("This crate only works on the x86/x86_64-platform.");
    let id = CpuId::new();
    // ########## STEP 1 ##########
    let feature_info = id.get_feature_info();
    if feature_info.is_none() {
        // should never happen, except the CPU or virtualization environment does weird things
        log::debug!(
            "feature_info is not present, this is REALLY strange. Can't verify if we are in QEMU."
        );
        return false;
    }
    let feature_info = feature_info.unwrap();
    if !feature_info.has_hypervisor() {
        // QEMU is a hypervisor and no real machine => exit if flag not set
        // If we run in a Hypervisor, this flag is set (also see https://lwn.net/Articles/301888/)
        log::debug!("Hypervisor-flag is not set, we are not in QEMU");
        return false;
    }
    // ########## STEP 2 ##########
    // Now we know that we are in a Hypervisor environment
    let hypervisor_info = id.get_hypervisor_info();
    if hypervisor_info.is_none() {
        // should never happen, except the CPU or virtualization environment does weird things
        log::debug!("hypervisor_info is not present but hypervisor-flag is, this is REALLY strange");
        return false;
    }
    let hypervisor_info = hypervisor_info.unwrap();
    // if this returns false, because the hypervisor ID can be "KVM",
    // we still could be executed by QEMU -> further checks needed
    if hypervisor_has_qemu_id(&hypervisor_info) {
        log::debug!("QEMU is the direct hypervisor");
        return true;
    }
    // ########## STEP 3 ##########
    // now check the extended CPU id, which is provided by QEMU
    let extended_info = id.get_extended_function_info();
    if extended_info.is_none() {
        return false;
    }
    let extended_info = extended_info.unwrap();
    let is_qemu = extended_brand_string_contains_qemu(&extended_info);
    if is_qemu {
        log::debug!("Runs inside QEMU with {:?} as accelerator", hypervisor_info.identify());
    }
    is_qemu
}
pub fn runs_inside_qemu ( ) -> bool { # [ cfg ( not ( any ( target_arch " " target_arch " " ) ) ) ] compile_error ! ( " " ) ; let id = CpuId :: new ( ) ; // ########## STEP 1 ########## let feature_info = id . get_feature_info ( ) ; if feature_info . is_none ( ) { // should never happen, except the CPU or virtualization environment does weird things log :: debug ! ( " " ) ; return false ; } let feature_info = feature_info . unwrap ( ) ; if ! feature_info . has_hypervisor ( ) { // QEMU is a hypervisor and no real machine => exit if flag not set // If we run in a Hypervisor, this flag is set (also see https://lwn.net/Articles/301888/) log :: debug ! ( " " ) ; return false ; } // ########## STEP 2 ########## // Now we know that we are in a Hypervisor environment let hypervisor_info = id . get_hypervisor_info ( ) ; if hypervisor_info . is_none ( ) { // should never happen, except the CPU or virtualization environment does weird things log :: debug ! ( " " ) ; return false ; } let hypervisor_info = hypervisor_info . unwrap ( ) ; // if this returns false, because the hypervisor ID can be "KVM", // we still could be executed by QEMU -> further checks needed if hypervisor_has_qemu_id ( & hypervisor_info ) { log :: debug ! ( " " ) ; return true ; } // ########## STEP 3 ########## // now check the extended CPU id, which is provided by QEMU let extended_info = id . get_extended_function_info ( ) ; if extended_info . is_none ( ) { return false ; } let extended_info = extended_info . unwrap ( ) ; let is_qemu = extended_brand_string_contains_qemu ( & extended_info ) ; if is_qemu { log :: debug ! ( " " hypervisor_info identify ( ) ) ; } is_qemu }
Returns if the code is running inside a QEMU virtual machine.
Returns if the code is running inside a QEMU virtual machine .
example usage
```rust
use runs_inside_qemu::runs_inside_qemu.

fn main() {
If we are in QEMU, we use the nice "debugcon"-feature which maps
the x86 I/O-port `0xe9` to stdout or a file.
` ` ` rust use runs_inside_qemu :: runs_inside_qemu . fn main () { If we are in QEMU we use the nice " debugcon " - feature which maps the x86 I / O - port ` 0xe9 ` to stdout or a file .