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
/*
* jit-cpuid-x86.c - Wrapper for the CPUID instruction.
*
* Copyright (C) 2004 Southern Storm Software, Pty Ltd.
*
* This file is part of the libjit library.
*
* The libjit library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* The libjit library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the libjit library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "jit-cpuid-x86.h"
#if defined(__i386) || defined(__i386__) || defined(_M_IX86)
/*
* Determine if the "cpuid" instruction is present by twiddling
* bit 21 of the EFLAGS register.
*/
static int cpuid_present(void)
{
#if defined(__GNUC__)
int result;
__asm__ __volatile__ (
"\tpushfl\n"
"\tpopl %%eax\n"
"\tmovl %%eax, %%ecx\n"
"\tandl $0x200000, %%ecx\n"
"\txorl $0x200000, %%eax\n"
"\tpushl %%eax\n"
"\tpopfl\n"
"\tpushfl\n"
"\tandl $0x200000, %%eax\n"
"\txorl %%ecx, %%eax\n"
"\tmovl %%eax, %0\n"
: "=m" (result) : : "eax", "ecx"
);
return (result != 0);
#else
return 0;
#endif
}
/*
* Issue a "cpuid" query and get the result.
*/
static void cpuid_query(unsigned int index, jit_cpuid_x86_t *info)
{
#if defined(__GNUC__)
__asm__ __volatile__ (
"\tmovl %0, %%eax\n"
"\tpushl %%ebx\n"
"\txorl %%ebx, %%ebx\n"
"\txorl %%ecx, %%ecx\n"
"\txorl %%edx, %%edx\n"
"\t.byte 0x0F\n" /* cpuid, safe against old assemblers */
"\t.byte 0xA2\n"
"\tmovl %1, %%esi\n"
"\tmovl %%eax, (%%esi)\n"
"\tmovl %%ebx, 4(%%esi)\n"
"\tmovl %%ecx, 8(%%esi)\n"
"\tmovl %%edx, 12(%%esi)\n"
"\tpopl %%ebx\n"
: : "m"(index), "m"(info) : "eax", "ecx", "edx", "esi"
);
#endif
}
int _jit_cpuid_x86_get(unsigned int index, jit_cpuid_x86_t *info)
{
/* Determine if this cpu has the "cpuid" instruction */
if(!cpuid_present())
{
return 0;
}
/* Validate the index */
if((index & 0x80000000) == 0)
{
cpuid_query(0, info);
}
else
{
cpuid_query(0x80000000, info);
}
if(index > info->eax)
{
return 0;
}
/* Execute the actual requested query */
cpuid_query(index, info);
return 1;
}
int _jit_cpuid_x86_has_feature(unsigned int feature)
{
jit_cpuid_x86_t info;
if(!_jit_cpuid_x86_get(JIT_X86CPUID_FEATURES, &info))
{
return 0;
}
return ((info.edx & feature) != 0);
}
unsigned int _jit_cpuid_x86_line_size(void)
{
jit_cpuid_x86_t info;
if(!_jit_cpuid_x86_get(JIT_X86CPUID_FEATURES, &info))
{
return 0;
}
if((info.edx & JIT_X86FEATURE_CLFSH) == 0)
{
return 0;
}
return ((info.ebx & 0x0000FF00) >> 5);
}
#endif /* i386 */