Debian Codegraph @

include/private/cpuid-x86.h

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2010-2012, 2014 Université Bordeaux
4 * Copyright © 2010 Cisco Systems, Inc. All rights reserved.
5 * Copyright © 2014 Inria. All rights reserved.
6 * See COPYING in top-level directory.
7 */
8
9 /* Internals for x86's cpuid. */
10
11 #ifndef HWLOC_PRIVATE_CPUID_X86_H
12 #define HWLOC_PRIVATE_CPUID_X86_H
13
14 /* A macro for annotating memory as uninitialized when building with MSAN
15 * (and otherwise having no effect). See below for why this is used with
16 * our custom assembly.
17 */
18 #ifdef __has_feature
19 #define HWLOC_HAS_FEATURE(name) __has_feature(name)
20 #else
21 #define HWLOC_HAS_FEATURE(name) 0
22 #endif
23 #if HWLOC_HAS_FEATURE(memory_sanitizer) || defined(MEMORY_SANITIZER)
24 #include <sanitizer/msan_interface.h>
25 #define HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ptr, len) __msan_unpoison(ptr, len)
26 #else
27 #define HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ptr, len)
28 #endif
29
30 #if (defined HWLOC_X86_32_ARCH) && (!defined HWLOC_HAVE_MSVC_CPUIDEX)
31 static __hwloc_inline int hwloc_have_x86_cpuid(void)
32 {
33 int ret;
34 unsigned tmp, tmp2;
35 __asm__(
36 "mov $0,%0\n\t" /* Not supported a priori */
37
38 "pushfl \n\t" /* Save flags */
39
40 "pushfl \n\t" \
41 "pop %1 \n\t" /* Get flags */ \
42
43 #define TRY_TOGGLE \
44 "xor $0x00200000,%1\n\t" /* Try to toggle ID */ \
45 "mov %1,%2\n\t" /* Save expected value */ \
46 "push %1 \n\t" \
47 "popfl \n\t" /* Try to toggle */ \
48 "pushfl \n\t" \
49 "pop %1 \n\t" \
50 "cmp %1,%2\n\t" /* Compare with expected value */ \
51 "jnz 0f\n\t" /* Unexpected, failure */ \
52
53 TRY_TOGGLE /* Try to set/clear */
54 TRY_TOGGLE /* Try to clear/set */
55
56 "mov $1,%0\n\t" /* Passed the test! */
57
58 "0: \n\t"
59 "popfl \n\t" /* Restore flags */
60
61 : "=r" (ret), "=&r" (tmp), "=&r" (tmp2));
62 return ret;
63 }
64 #endif /* !defined HWLOC_X86_32_ARCH && !defined HWLOC_HAVE_MSVC_CPUIDEX*/
65 #if (defined HWLOC_X86_64_ARCH) || (defined HWLOC_HAVE_MSVC_CPUIDEX)
66 static __hwloc_inline int hwloc_have_x86_cpuid(void) { return 1; }
67 #endif /* HWLOC_X86_64_ARCH */
68
69 static __hwloc_inline void hwloc_x86_cpuid(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx)
70 {
71 #ifdef HWLOC_HAVE_MSVC_CPUIDEX
72 int regs[4];
73 __cpuidex(regs, *eax, *ecx);
74 *eax = regs[0];
75 *ebx = regs[1];
76 *ecx = regs[2];
77 *edx = regs[3];
78 #else /* HWLOC_HAVE_MSVC_CPUIDEX */
79 /* Note: gcc might want to use bx or the stack for %1 addressing, so we can't
80 * use them :/ */
81 #ifdef HWLOC_X86_64_ARCH
82 hwloc_uint64_t sav_rbx;
83 __asm__(
84 "mov %%rbx,%2\n\t"
85 "cpuid\n\t"
86 "xchg %2,%%rbx\n\t"
87 "movl %k2,%1\n\t"
88 : "+a" (*eax), "=m" (*ebx), "=&r"(sav_rbx),
89 "+c" (*ecx), "=&d" (*edx));
90 /* MSAN does not recognize the effect of the above assembly on the memory operand
91 * (`"=m"(*ebx)`). This may get improved in MSAN at some point in the future, e.g.
92 * see https://github.com/llvm/llvm-project/pull/77393. */
93 HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ebx, sizeof *ebx);
94 #elif defined(HWLOC_X86_32_ARCH)
95 __asm__(
96 "mov %%ebx,%1\n\t"
97 "cpuid\n\t"
98 "xchg %%ebx,%1\n\t"
99 : "+a" (*eax), "=&SD" (*ebx), "+c" (*ecx), "=&d" (*edx));
100 /* See above. */
101 HWLOC_ANNOTATE_MEMORY_IS_INITIALIZED(ebx, sizeof *ebx);
102 #else
103 #error unknown architecture
104 #endif
105 #endif /* HWLOC_HAVE_MSVC_CPUIDEX */
106 }
107
108 #endif /* HWLOC_PRIVATE_X86_CPUID_H */