VirtualBox

source: vbox/trunk/include/iprt/x86-helpers.h@ 106967

Last change on this file since 106967 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/** @file
2 * IPRT - X86 and AMD64 Helpers.
3 */
4
5/*
6 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
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 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
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
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.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_x86_helpers_h
37#define IPRT_INCLUDED_x86_helpers_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43
44
45/** @defgroup grp_rt_x86_helpers x86 Helper Functions
46 * @ingroup grp_rt_x86
47 * @{
48 */
49
50
51/**
52 * Tests if it a genuine Intel CPU based on the ASMCpuId(0) output.
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 */
59DECLINLINE(bool) RTX86IsIntelCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
60{
61 /* 'GenuineIntel' */
62 return uEBX == UINT32_C(0x756e6547) /* 'Genu' */
63 && uEDX == UINT32_C(0x49656e69) /* 'ineI' */
64 && uECX == UINT32_C(0x6c65746e); /* 'ntel' */
65}
66
67
68/**
69 * Tests if it an authentic AMD CPU based on the ASMCpuId(0) output.
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 */
76DECLINLINE(bool) RTX86IsAmdCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
77{
78 /* 'AuthenticAMD' */
79 return uEBX == UINT32_C(0x68747541) /* 'Auth' */
80 && uEDX == UINT32_C(0x69746e65) /* 'enti' */
81 && uECX == UINT32_C(0x444d4163); /* 'dAMD' */
82}
83
84
85/**
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 */
93DECLINLINE(bool) RTX86IsViaCentaurCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
94{
95 /* 'CentaurHauls' */
96 return uEBX == UINT32_C(0x746e6543) /* 'Cent' */
97 && uEDX == UINT32_C(0x48727561) /* 'aurH' */
98 && uECX == UINT32_C(0x736c7561); /* 'auls' */
99}
100
101
102/**
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 */
110DECLINLINE(bool) RTX86IsShanghaiCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
111{
112 /* ' Shanghai ' */
113 return uEBX == UINT32_C(0x68532020) /* ' Sh' */
114 && uEDX == UINT32_C(0x68676e61) /* 'angh' */
115 && uECX == UINT32_C(0x20206961); /* 'ai ' */
116}
117
118
119/**
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 */
127DECLINLINE(bool) RTX86IsHygonCpu(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
128{
129 /* 'HygonGenuine' */
130 return uEBX == UINT32_C(0x6f677948) /* Hygo */
131 && uECX == UINT32_C(0x656e6975) /* uine */
132 && uEDX == UINT32_C(0x6e65476e); /* nGen */
133}
134
135
136/**
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 */
147DECLINLINE(bool) RTX86IsValidStdRange(uint32_t uEAX)
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 */
165DECLINLINE(bool) RTX86IsValidExtRange(uint32_t uEAX)
166{
167 return uEAX >= UINT32_C(0x80000001) && uEAX <= UINT32_C(0x800fffff);
168}
169
170
171/**
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 *
179 * @note Unlike RTX86IsValidStdRange() and RTX86IsValidExtRange(), a single
180 * leaf is okay here. So, you always need to check the range.
181 * @remarks The upper range limit is take from the intel docs.
182 */
183DECLINLINE(bool) RTX86IsValidHypervisorRange(uint32_t uEAX)
184{
185 return uEAX >= UINT32_C(0x40000000) && uEAX <= UINT32_C(0x4fffffff);
186}
187
188
189/**
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 */
195DECLINLINE(uint32_t) RTX86GetCpuFamily(uint32_t uEAX)
196{
197 return ((uEAX >> 8) & 0xf) == 0xf
198 ? ((uEAX >> 20) & 0x7f) + 0xf
199 : ((uEAX >> 8) & 0xf);
200}
201
202
203/**
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 */
209DECLINLINE(uint32_t) RTX86GetCpuModelIntel(uint32_t uEAX)
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 */
223DECLINLINE(uint32_t) RTX86GetCpuModelAMD(uint32_t uEAX)
224{
225 return ((uEAX >> 8) & 0xf) == 0xf
226 ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
227 : ((uEAX >> 4) & 0xf);
228}
229
230
231/**
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).
236 * @param fIntel Whether it's an intel CPU. Use RTX86IsIntelCpu() or
237 * RTX86IsIntelCpu().
238 */
239DECLINLINE(uint32_t) RTX86GetCpuModel(uint32_t uEAX, bool fIntel)
240{
241 return ((uEAX >> 8) & 0xf) == 0xf || (((uEAX >> 8) & 0xf) == 0x6 && fIntel) /* family! */
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 */
253DECLINLINE(uint32_t) RTX86GetCpuStepping(uint32_t uEAX)
254{
255 return uEAX & 0xf;
256}
257
258
259/** @} */
260#endif /* !IPRT_INCLUDED_x86_helpers_h */
261
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette