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