github.com/iDigitalFlame/xmt@v0.5.4/device/cpu_other.go (about)

     1  //go:build !amd64 && !386
     2  // +build !amd64,!386
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package device
    21  
    22  // IsVirtual attempts to determine if the underlying device is inside a container
    23  // or is running in a virtual machine.
    24  //
    25  // If this returns true, it is suspected that a non-physical device is present.
    26  //
    27  // Different versions of this function are used depending on CPU type.
    28  //   - For x86/x64/amd64 this function uses the CPUID instruction.
    29  //     See https://en.wikipedia.org/wiki/CPUID for more info.
    30  func IsVirtual() bool {
    31  	return isVirtual()
    32  }
    33  func isKnownVendor(b []byte) bool {
    34  	switch len(b) {
    35  	case 0:
    36  		return false
    37  	case 3: // KVM, VMware, Xen
    38  		if (b[0] == 'K' || b[0] == 'k') && (b[1] == 'V' || b[1] == 'v') && (b[2] == 'M' || b[2] == 'm') {
    39  			return true
    40  		}
    41  		if (b[0] == 'V' || b[0] == 'v') && (b[1] == 'M' || b[1] == 'm') && (b[2] == 'W' || b[2] == 'w') {
    42  			return true
    43  		}
    44  		if (b[0] == 'X' || b[0] == 'x') && (b[1] == 'E' || b[1] == 'e') && (b[2] == 'N' || b[2] == 'n') {
    45  			return true
    46  		}
    47  	case 4: // Qemu
    48  		if (b[0] == 'Q' || b[0] == 'q') && (b[1] == 'E' || b[1] == 'e') && (b[2] == 'M' || b[2] == 'm') && (b[3] == 'U' || b[3] == 'u') {
    49  			return true
    50  		}
    51  	case 5: // Bochs, Bhyve
    52  		if (b[0] == 'B' || b[0] == 'b') && (b[1] == 'O' || b[1] == 'o') && (b[3] == 'H' || b[3] == 'h') && (b[4] == 'S' || b[4] == 's') {
    53  			return true
    54  		}
    55  		if (b[0] == 'B' || b[0] == 'b') && (b[1] == 'H' || b[1] == 'h') && (b[3] == 'V' || b[3] == 'v') && (b[4] == 'E' || b[4] == 'e') {
    56  			return true
    57  		}
    58  	case 6: // VMware
    59  		if (b[0] == 'V' || b[0] == 'v') && (b[1] == 'M' || b[1] == 'm') && (b[2] == 'W' || b[2] == 'w') && (b[5] == 'E' || b[5] == 'e') {
    60  			return true
    61  		}
    62  	case 7: // Hyper-V
    63  		if (b[0] == 'H' || b[0] == 'h') && (b[1] == 'Y' || b[1] == 'y') && b[5] == '-' && (b[6] == 'V' || b[6] == 'v') {
    64  			return true
    65  		}
    66  	case 8: // KubeVirt
    67  		if (b[0] == 'K' || b[0] == 'k') && (b[1] == 'U' || b[1] == 'u') && (b[4] == 'V' || b[4] == 'v') && (b[5] == 'I' || b[5] == 'i') {
    68  			return true
    69  		}
    70  	case 9: // OpenStack, Parallels
    71  		if (b[0] == 'O' || b[0] == 'o') && (b[1] == 'P' || b[2] == 'p') && (b[4] == 'S' || b[4] == 's') && (b[7] == 'C' || b[7] == 'c') {
    72  			return true
    73  		}
    74  		if (b[0] == 'P' || b[0] == 'p') && (b[2] == 'R' || b[2] == 'r') && (b[5] == 'L' || b[5] == 'l') && (b[7] == 'L' || b[7] == 'l') {
    75  			return true
    76  		}
    77  	case 10: // VirtualBox
    78  		if (b[0] == 'V' || b[0] == 'v') && (b[3] == 'T' || b[3] == 't') && (b[7] == 'B' || b[7] == 'b') && (b[9] == 'X' || b[9] == 'x') {
    79  			return true
    80  		}
    81  	}
    82  	// Non-fixed tests
    83  	// Amazon *
    84  	if len(b) > 7 && (b[0] == 'A' || b[0] == 'a') && (b[1] == 'M' || b[1] == 'm') && (b[2] == 'A' || b[2] == 'a') && (b[3] == 'Z' || b[3] == 'z') {
    85  		return true
    86  	}
    87  	// innotek *
    88  	if len(b) > 8 && (b[0] == 'I' || b[0] == 'i') && (b[2] == 'N' || b[2] == 'n') && (b[3] == 'O' || b[3] == 'o') && (b[6] == 'K' || b[6] == 'k') {
    89  		return true
    90  	}
    91  	// Apple Virt*
    92  	if len(b) > 10 && (b[0] == 'A' || b[0] == 'a') && (b[2] == 'P' || b[2] == 'p') && (b[3] == 'L' || b[3] == 'l') && (b[6] == 'V' || b[6] == 'v') {
    93  		return true
    94  	}
    95  	return false
    96  }