| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
ws_cpuid.h
Go to the documentation of this file.
1
11/*
12 * Get CPU info on platforms where the x86 cpuid instruction can be used.
13 *
14 * Skip 32-bit versions for GCC and Clang, as older IA-32 processors don't
15 * have cpuid.
16 *
17 * Intel has documented the CPUID instruction in the "Intel(r) 64 and IA-32
18 * Architectures Developer's Manual" at
19 *
20 * https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2a-manual.html
21 *
22 * The ws_cpuid() routine will return 0 if cpuinfo isn't available, including
23 * on non-x86 platforms and on 32-bit x86 platforms with GCC and Clang, as
24 * well as non-MSVC and non-GCC-or-Clang platforms.
25 *
26 * The "selector" argument to ws_cpuid() is the "initial EAX value" for the
27 * instruction. The initial ECX value is 0.
28 *
29 * The "CPUInfo" argument points to 4 32-bit values into which the
30 * resulting values of EAX, EBX, ECX, and EDX are store, in order.
31 */
32
33#include "ws_attributes.h"
34
35#include <inttypes.h>
36#include <stdbool.h>
37
53#if defined(_MSC_VER) /* MSVC */
54
55/*
56 * XXX - do the same IA-32 (which doesn't have CPUID prior to some versions
57 * of the 80486 and all versions of the 80586^Woriginal Pentium) vs.
58 * x86-64 (which always has CPUID) stuff that we do with GCC/Clang?
59 *
60 * You will probably not be happy running current versions of Wireshark
61 * on an 80386 or 80486 machine, and we're dropping support for IA-32
62 * on Windows anyway, so the answer is probably "no".
63 */
64#if defined(_M_IX86) || defined(_M_X64)
65 static bool
66ws_cpuid(uint32_t *CPUInfo, uint32_t selector)
67{
68 /* https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex */
69
70 CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
71 __cpuid((int *) CPUInfo, selector);
72 /* XXX, how to check if it's supported on MSVC? just in case clear all flags above */
73 return true;
74}
75#else /* not x86 */
76 static bool
77ws_cpuid(uint32_t *CPUInfo _U_, int selector _U_)
78{
79 /* Not x86, so no cpuid instruction */
80 return false;
81}
82#endif
83
84#elif defined(__GNUC__) /* GCC/clang */
85
86#if defined(__x86_64__)
87 static inline bool
88ws_cpuid(uint32_t *CPUInfo, int selector)
89{
90 __asm__ __volatile__("cpuid"
91 : "=a" (CPUInfo[0]),
92 "=b" (CPUInfo[1]),
93 "=c" (CPUInfo[2]),
94 "=d" (CPUInfo[3])
95 : "a" (selector),
96 "c" (0));
97 return true;
98}
99#elif defined(__i386__)
100 static bool
101ws_cpuid(uint32_t *CPUInfo _U_, int selector _U_)
102{
103 /*
104 * TODO: need a test if older processors have the cpuid instruction.
105 *
106 * The correct way to test for this, according to the Intel64/IA-32
107 * documentation from Intel, in section 17.1 "USING THE CPUID
108 * INSTRUCTION", is to try to change the ID bit (bit 21) in
109 * EFLAGS. If it can be changed, the machine supports CPUID,
110 * otherwise it doesn't.
111 *
112 * Some 486's, and all subsequent processors, support CPUID.
113 *
114 * For those who are curious, the way you distinguish between
115 * an 80386 and an 80486 is to try to set the flag in EFLAGS
116 * that causes unaligned accesses to fault - that's bit 18.
117 * However, if the SMAP bit is set in CR4, that bit controls
118 * whether explicit supervisor-mode access to user-mode pages
119 * are allowed, so that should presumably only be done in a
120 * very controlled environment, such as the system boot process.
121 *
122 * So, if you want to find out what type of CPU the system has,
123 * it's probably best to ask the OS, if it supplies the result
124 * of any CPU type testing it's done.
125 */
126 return false;
127}
128#else /* not x86 */
129 static bool
130ws_cpuid(uint32_t *CPUInfo _U_, int selector _U_)
131{
132 /* Not x86, so no cpuid instruction */
133 return false;
134}
135#endif
136
137#else /* Other compilers */
138
139 static bool
140ws_cpuid(uint32_t *CPUInfo _U_, int selector _U_)
141{
142 return false;
143}
144#endif
145
146 static int
147ws_cpuid_sse42(void)
148{
149 uint32_t CPUInfo[4];
150
151 if (!ws_cpuid(CPUInfo, 1))
152 return 0;
153
154 /* in ECX bit 20 toggled on */
155 return (CPUInfo[2] & (1 << 20));
156}