1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* This file is part of the KDE libraries
* Copyright (C) 2003 Fredrik Höglund <fredrik@kde.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <csignal>
#include <csetjmp>
#include <config.h>
#include "kcpuinfo.h"
#if defined(__GNUC__) || defined(__INTEL_COMPILER)
# define HAVE_GNU_INLINE_ASM
#endif
typedef void (*kde_sighandler_t) (int);
#ifdef __i386__
static jmp_buf env;
// Sighandler for the SSE OS support check
static void sighandler( int )
{
std::longjmp( env, 1 );
}
#endif
#ifdef __PPC__
static sigjmp_buf TDE_NO_EXPORT jmpbuf;
static sig_atomic_t TDE_NO_EXPORT canjump = 0;
static void TDE_NO_EXPORT sigill_handler( int sig )
{
if ( !canjump ) {
signal( sig, SIG_DFL );
raise( sig );
}
canjump = 0;
siglongjmp( jmpbuf, 1 );
}
#endif
static int getCpuFeatures()
{
volatile int features = 0;
#if defined( HAVE_GNU_INLINE_ASM )
#if defined( __i386__ )
bool haveCPUID = false;
bool have3DNOW = false;
int result = 0;
// First check if the CPU supports the CPUID instruction
__asm__ __volatile__(
// Try to toggle the CPUID bit in the EFLAGS register
"pushf \n\t" // Push the EFLAGS register onto the stack
"popl %%ecx \n\t" // Pop the value into ECX
"movl %%ecx, %%edx \n\t" // Copy ECX to EDX
"xorl $0x00200000, %%ecx \n\t" // Toggle bit 21 (CPUID) in ECX
"pushl %%ecx \n\t" // Push the modified value onto the stack
"popf \n\t" // Pop it back into EFLAGS
// Check if the CPUID bit was successfully toggled
"pushf \n\t" // Push EFLAGS back onto the stack
"popl %%ecx \n\t" // Pop the value into ECX
"xorl %%eax, %%eax \n\t" // Zero out the EAX register
"cmpl %%ecx, %%edx \n\t" // Compare ECX with EDX
"je .Lno_cpuid_support%= \n\t" // Jump if they're identical
"movl $1, %%eax \n\t" // Set EAX to true
".Lno_cpuid_support%=: \n\t"
: "=a"(haveCPUID) : : "%ecx", "%edx" );
// If we don't have CPUID we won't have the other extensions either
if ( ! haveCPUID )
return 0L;
// Execute CPUID with the feature request bit set
__asm__ __volatile__(
"pushl %%ebx \n\t" // Save EBX
"movl $1, %%eax \n\t" // Set EAX to 1 (features request)
"cpuid \n\t" // Call CPUID
"popl %%ebx \n\t" // Restore EBX
: "=d"(result) : : "%eax", "%ecx" );
// Test bit 23 (MMX support)
if ( result & 0x00800000 )
features |= KCPUInfo::IntelMMX;
__asm__ __volatile__(
"pushl %%ebx \n\t"
"movl $0x80000000, %%eax \n\t"
"cpuid \n\t"
"cmpl $0x80000000, %%eax \n\t"
"jbe .Lno_extended%= \n\t"
"movl $0x80000001, %%eax \n\t"
"cpuid \n\t"
"test $0x80000000, %%edx \n\t"
"jz .Lno_extended%= \n\t"
"movl $1, %%eax \n\t" // // Set EAX to true
".Lno_extended%=: \n\t"
"popl %%ebx \n\t" // Restore EBX
: "=a"(have3DNOW) : );
if ( have3DNOW )
features |= KCPUInfo::AMD3DNOW;
#ifdef HAVE_X86_SSE
// Test bit 25 (SSE support)
if ( result & 0x00200000 ) {
features |= KCPUInfo::IntelSSE;
// OS support test for SSE.
// Install our own sighandler for SIGILL.
kde_sighandler_t oldhandler = std::signal( SIGILL, sighandler );
// Try executing an SSE insn to see if we get a SIGILL
if ( setjmp( env ) )
features ^= KCPUInfo::IntelSSE; // The OS support test failed
else
__asm__ __volatile__("xorps %xmm0, %xmm0");
// Restore the default sighandler
std::signal( SIGILL, oldhandler );
// Test bit 26 (SSE2 support)
if ( (result & 0x00400000) && (features & KCPUInfo::IntelSSE) )
features |= KCPUInfo::IntelSSE2;
// Note: The OS requirements for SSE2 are the same as for SSE
// so we don't have to do any additional tests for that.
}
#endif // HAVE_X86_SSE
#elif defined __PPC__ && defined HAVE_PPC_ALTIVEC
signal( SIGILL, sigill_handler );
if ( sigsetjmp( jmpbuf, 1 ) ) {
signal( SIGILL, SIG_DFL );
} else {
canjump = 1;
__asm__ __volatile__( "mtspr 256, %0\n\t"
"vand %%v0, %%v0, %%v0"
: /* none */
: "r" (-1) );
signal( SIGILL, SIG_DFL );
features |= KCPUInfo::AltiVec;
}
#endif // __i386__
#endif //HAVE_GNU_INLINE_ASM
return features;
}
unsigned int KCPUInfo::s_features = getCpuFeatures();
|