[1] | 1 | /** @file
|
---|
[93515] | 2 | * IPRT - X86 and AMD64 Helpers.
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[98103] | 6 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 7 | *
|
---|
[96407] | 8 | * This file is part of VirtualBox base platform packages, as
|
---|
| 9 | * available from https://www.virtualbox.org.
|
---|
[5999] | 10 | *
|
---|
[96407] | 11 | * This program is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU General Public License
|
---|
| 13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 14 | * License.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful, but
|
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 23 | *
|
---|
[5999] | 24 | * The contents of this file may alternatively be used under the terms
|
---|
| 25 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 28 | * CDDL are applicable instead of those of the GPL.
|
---|
| 29 | *
|
---|
| 30 | * You may elect to license modified versions of this file under the
|
---|
| 31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 32 | *
|
---|
| 33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 34 | */
|
---|
| 35 |
|
---|
[93515] | 36 | #ifndef IPRT_INCLUDED_x86_helpers_h
|
---|
| 37 | #define IPRT_INCLUDED_x86_helpers_h
|
---|
[76507] | 38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 39 | # pragma once
|
---|
| 40 | #endif
|
---|
[1] | 41 |
|
---|
[29250] | 42 | #include <iprt/types.h>
|
---|
[1] | 43 |
|
---|
| 44 |
|
---|
[93515] | 45 | /** @defgroup grp_rt_x86_helpers x86 Helper Functions
|
---|
| 46 | * @ingroup grp_rt_x86
|
---|
[1] | 47 | * @{
|
---|
| 48 | */
|
---|
| 49 |
|
---|
[54254] | 50 |
|
---|
[1] | 51 | /**
|
---|
[21943] | 52 | * Tests if it a genuine Intel CPU based on the ASMCpuId(0) output.
|
---|
[8889] | 53 | *
|
---|
| 54 | * @returns true/false.
|
---|
| 55 | * @param uEBX EBX return from ASMCpuId(0)
|
---|
| 56 | * @param uECX ECX return from ASMCpuId(0)
|
---|
| 57 | * @param uEDX EDX return from ASMCpuId(0)
|
---|
| 58 | */
|
---|
[93515] | 59 | DECLINLINE(bool) RTX86IsIntelCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
|
---|
[8889] | 60 | {
|
---|
[81613] | 61 | /* 'GenuineIntel' */
|
---|
| 62 | return uEBX == UINT32_C(0x756e6547) /* 'Genu' */
|
---|
| 63 | && uEDX == UINT32_C(0x49656e69) /* 'ineI' */
|
---|
| 64 | && uECX == UINT32_C(0x6c65746e); /* 'ntel' */
|
---|
[8889] | 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
[44169] | 69 | * Tests if it an authentic AMD CPU based on the ASMCpuId(0) output.
|
---|
[24452] | 70 | *
|
---|
| 71 | * @returns true/false.
|
---|
| 72 | * @param uEBX EBX return from ASMCpuId(0)
|
---|
| 73 | * @param uECX ECX return from ASMCpuId(0)
|
---|
| 74 | * @param uEDX EDX return from ASMCpuId(0)
|
---|
| 75 | */
|
---|
[93515] | 76 | DECLINLINE(bool) RTX86IsAmdCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
|
---|
[24452] | 77 | {
|
---|
[81613] | 78 | /* 'AuthenticAMD' */
|
---|
| 79 | return uEBX == UINT32_C(0x68747541) /* 'Auth' */
|
---|
| 80 | && uEDX == UINT32_C(0x69746e65) /* 'enti' */
|
---|
| 81 | && uECX == UINT32_C(0x444d4163); /* 'dAMD' */
|
---|
[24452] | 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
[44169] | 86 | * Tests if it a centaur hauling VIA CPU based on the ASMCpuId(0) output.
|
---|
| 87 | *
|
---|
| 88 | * @returns true/false.
|
---|
| 89 | * @param uEBX EBX return from ASMCpuId(0).
|
---|
| 90 | * @param uECX ECX return from ASMCpuId(0).
|
---|
| 91 | * @param uEDX EDX return from ASMCpuId(0).
|
---|
| 92 | */
|
---|
[93515] | 93 | DECLINLINE(bool) RTX86IsViaCentaurCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
|
---|
[44169] | 94 | {
|
---|
[81613] | 95 | /* 'CentaurHauls' */
|
---|
| 96 | return uEBX == UINT32_C(0x746e6543) /* 'Cent' */
|
---|
| 97 | && uEDX == UINT32_C(0x48727561) /* 'aurH' */
|
---|
| 98 | && uECX == UINT32_C(0x736c7561); /* 'auls' */
|
---|
[44169] | 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
[76886] | 103 | * Tests if it a Shanghai CPU based on the ASMCpuId(0) output.
|
---|
| 104 | *
|
---|
| 105 | * @returns true/false.
|
---|
| 106 | * @param uEBX EBX return from ASMCpuId(0).
|
---|
| 107 | * @param uECX ECX return from ASMCpuId(0).
|
---|
| 108 | * @param uEDX EDX return from ASMCpuId(0).
|
---|
| 109 | */
|
---|
[93515] | 110 | DECLINLINE(bool) RTX86IsShanghaiCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
|
---|
[76886] | 111 | {
|
---|
[81613] | 112 | /* ' Shanghai ' */
|
---|
| 113 | return uEBX == UINT32_C(0x68532020) /* ' Sh' */
|
---|
| 114 | && uEDX == UINT32_C(0x68676e61) /* 'angh' */
|
---|
| 115 | && uECX == UINT32_C(0x20206961); /* 'ai ' */
|
---|
[76886] | 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
[81605] | 120 | * Tests if it a genuine Hygon CPU based on the ASMCpuId(0) output.
|
---|
| 121 | *
|
---|
| 122 | * @returns true/false.
|
---|
| 123 | * @param uEBX EBX return from ASMCpuId(0)
|
---|
| 124 | * @param uECX ECX return from ASMCpuId(0)
|
---|
| 125 | * @param uEDX EDX return from ASMCpuId(0)
|
---|
| 126 | */
|
---|
[93515] | 127 | DECLINLINE(bool) RTX86IsHygonCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
|
---|
[81605] | 128 | {
|
---|
[81613] | 129 | /* 'HygonGenuine' */
|
---|
| 130 | return uEBX == UINT32_C(0x6f677948) /* Hygo */
|
---|
| 131 | && uECX == UINT32_C(0x656e6975) /* uine */
|
---|
| 132 | && uEDX == UINT32_C(0x6e65476e); /* nGen */
|
---|
[81605] | 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
[44169] | 137 | * Checks whether ASMCpuId_EAX(0x00000000) indicates a valid range.
|
---|
| 138 | *
|
---|
| 139 | *
|
---|
| 140 | * @returns true/false.
|
---|
| 141 | * @param uEAX The EAX value of CPUID leaf 0x00000000.
|
---|
| 142 | *
|
---|
| 143 | * @note This only succeeds if there are at least two leaves in the range.
|
---|
| 144 | * @remarks The upper range limit is just some half reasonable value we've
|
---|
| 145 | * picked out of thin air.
|
---|
| 146 | */
|
---|
[93515] | 147 | DECLINLINE(bool) RTX86IsValidStdRange(uint32_t uEAX)
|
---|
[44169] | 148 | {
|
---|
| 149 | return uEAX >= UINT32_C(0x00000001) && uEAX <= UINT32_C(0x000fffff);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Checks whether ASMCpuId_EAX(0x80000000) indicates a valid range.
|
---|
| 155 | *
|
---|
| 156 | * This only succeeds if there are at least two leaves in the range.
|
---|
| 157 | *
|
---|
| 158 | * @returns true/false.
|
---|
| 159 | * @param uEAX The EAX value of CPUID leaf 0x80000000.
|
---|
| 160 | *
|
---|
| 161 | * @note This only succeeds if there are at least two leaves in the range.
|
---|
| 162 | * @remarks The upper range limit is just some half reasonable value we've
|
---|
| 163 | * picked out of thin air.
|
---|
| 164 | */
|
---|
[93515] | 165 | DECLINLINE(bool) RTX86IsValidExtRange(uint32_t uEAX)
|
---|
[44169] | 166 | {
|
---|
| 167 | return uEAX >= UINT32_C(0x80000001) && uEAX <= UINT32_C(0x800fffff);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
[70942] | 172 | * Checks whether ASMCpuId_EAX(0x40000000) indicates a valid range.
|
---|
| 173 | *
|
---|
| 174 | * This only succeeds if there are at least two leaves in the range.
|
---|
| 175 | *
|
---|
| 176 | * @returns true/false.
|
---|
| 177 | * @param uEAX The EAX value of CPUID leaf 0x40000000.
|
---|
| 178 | *
|
---|
[93515] | 179 | * @note Unlike RTX86IsValidStdRange() and RTX86IsValidExtRange(), a single
|
---|
| 180 | * leaf is okay here. So, you always need to check the range.
|
---|
[70942] | 181 | * @remarks The upper range limit is take from the intel docs.
|
---|
| 182 | */
|
---|
[93515] | 183 | DECLINLINE(bool) RTX86IsValidHypervisorRange(uint32_t uEAX)
|
---|
[70942] | 184 | {
|
---|
| 185 | return uEAX >= UINT32_C(0x40000000) && uEAX <= UINT32_C(0x4fffffff);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
[8882] | 190 | * Extracts the CPU family from ASMCpuId(1) or ASMCpuId(0x80000001)
|
---|
| 191 | *
|
---|
| 192 | * @returns Family.
|
---|
| 193 | * @param uEAX EAX return from ASMCpuId(1) or ASMCpuId(0x80000001).
|
---|
| 194 | */
|
---|
[93515] | 195 | DECLINLINE(uint32_t) RTX86GetCpuFamily(uint32_t uEAX)
|
---|
[8882] | 196 | {
|
---|
| 197 | return ((uEAX >> 8) & 0xf) == 0xf
|
---|
| 198 | ? ((uEAX >> 20) & 0x7f) + 0xf
|
---|
| 199 | : ((uEAX >> 8) & 0xf);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /**
|
---|
[8889] | 204 | * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001), Intel variant.
|
---|
| 205 | *
|
---|
| 206 | * @returns Model.
|
---|
| 207 | * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
|
---|
| 208 | */
|
---|
[93515] | 209 | DECLINLINE(uint32_t) RTX86GetCpuModelIntel(uint32_t uEAX)
|
---|
[8889] | 210 | {
|
---|
| 211 | return ((uEAX >> 8) & 0xf) == 0xf || (((uEAX >> 8) & 0xf) == 0x6) /* family! */
|
---|
| 212 | ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
|
---|
| 213 | : ((uEAX >> 4) & 0xf);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001), AMD variant.
|
---|
| 219 | *
|
---|
| 220 | * @returns Model.
|
---|
| 221 | * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
|
---|
| 222 | */
|
---|
[93515] | 223 | DECLINLINE(uint32_t) RTX86GetCpuModelAMD(uint32_t uEAX)
|
---|
[8889] | 224 | {
|
---|
| 225 | return ((uEAX >> 8) & 0xf) == 0xf
|
---|
| 226 | ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
|
---|
| 227 | : ((uEAX >> 4) & 0xf);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
[8882] | 232 | * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001)
|
---|
| 233 | *
|
---|
| 234 | * @returns Model.
|
---|
| 235 | * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
|
---|
[93515] | 236 | * @param fIntel Whether it's an intel CPU. Use RTX86IsIntelCpu() or
|
---|
| 237 | * RTX86IsIntelCpu().
|
---|
[8882] | 238 | */
|
---|
[93515] | 239 | DECLINLINE(uint32_t) RTX86GetCpuModel(uint32_t uEAX, bool fIntel)
|
---|
[8882] | 240 | {
|
---|
[8889] | 241 | return ((uEAX >> 8) & 0xf) == 0xf || (((uEAX >> 8) & 0xf) == 0x6 && fIntel) /* family! */
|
---|
[8882] | 242 | ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
|
---|
| 243 | : ((uEAX >> 4) & 0xf);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | /**
|
---|
| 248 | * Extracts the CPU stepping from ASMCpuId(1) or ASMCpuId(0x80000001)
|
---|
| 249 | *
|
---|
| 250 | * @returns Model.
|
---|
| 251 | * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
|
---|
| 252 | */
|
---|
[93515] | 253 | DECLINLINE(uint32_t) RTX86GetCpuStepping(uint32_t uEAX)
|
---|
[8882] | 254 | {
|
---|
| 255 | return uEAX & 0xf;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[1] | 259 | /** @} */
|
---|
[93515] | 260 | #endif /* !IPRT_INCLUDED_x86_helpers_h */
|
---|
[1] | 261 |
|
---|