1
/* CpuArch.c -- CPU specific code
2
2010-10-26: Igor Pavlov : Public domain */
6
#ifdef MY_CPU_X86_OR_AMD64
8
#if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
12
#if defined(USE_ASM) && !defined(MY_CPU_AMD64)
13
static UInt32 CheckFlag(UInt32 flag)
29
__asm__ __volatile__ (
32
"movl %%EAX,%%EDX\n\t"
38
"xorl %%EDX,%%EAX\n\t"
42
"=c" (flag) : "c" (flag));
46
#define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
48
#define CHECK_CPUID_IS_SUPPORTED
51
static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
57
UInt32 a2, b2, c2, d2;
61
__asm mov EAX, function;
75
__asm__ __volatile__ (
88
__cpuid(CPUInfo, function);
97
Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
99
CHECK_CPUID_IS_SUPPORTED
100
MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
101
MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
105
static UInt32 kVendors[][3] =
107
{ 0x756E6547, 0x49656E69, 0x6C65746E},
108
{ 0x68747541, 0x69746E65, 0x444D4163},
109
{ 0x746E6543, 0x48727561, 0x736C7561}
112
int x86cpuid_GetFirm(const Cx86cpuid *p)
115
for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
117
const UInt32 *v = kVendors[i];
118
if (v[0] == p->vendor[0] &&
119
v[1] == p->vendor[1] &&
120
v[2] == p->vendor[2])
126
Bool CPU_Is_InOrder()
130
UInt32 family, model;
131
if (!x86cpuid_CheckAndRead(&p))
133
family = x86cpuid_GetFamily(&p);
134
model = x86cpuid_GetModel(&p);
135
firm = x86cpuid_GetFirm(&p);
138
case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && model == 0x100C));
139
case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
140
case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
145
#if !defined(MY_CPU_AMD64) && defined(_WIN32)
146
static Bool CPU_Sys_Is_SSE_Supported()
149
vi.dwOSVersionInfoSize = sizeof(vi);
150
if (!GetVersionEx(&vi))
152
return (vi.dwMajorVersion >= 5);
154
#define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
156
#define CHECK_SYS_SSE_SUPPORT
159
Bool CPU_Is_Aes_Supported()
162
CHECK_SYS_SSE_SUPPORT
163
if (!x86cpuid_CheckAndRead(&p))
165
return (p.c >> 25) & 1;
1
/* CpuArch.c -- CPU specific code
2
2010-10-26: Igor Pavlov : Public domain */
9
#include <openssl/ssl.h>
10
#include <openssl/err.h>
11
#include "libclamav/crypto.h"
15
#ifdef MY_CPU_X86_OR_AMD64
17
#if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
21
#if defined(USE_ASM) && !defined(MY_CPU_AMD64)
22
static UInt32 CheckFlag(UInt32 flag)
38
__asm__ __volatile__ (
41
"movl %%EAX,%%EDX\n\t"
47
"xorl %%EDX,%%EAX\n\t"
51
"=c" (flag) : "c" (flag));
55
#define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
57
#define CHECK_CPUID_IS_SUPPORTED
60
static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
66
UInt32 a2, b2, c2, d2;
70
__asm mov EAX, function;
84
__asm__ __volatile__ (
97
__cpuid(CPUInfo, function);
106
Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
108
CHECK_CPUID_IS_SUPPORTED
109
MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
110
MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
114
static UInt32 kVendors[][3] =
116
{ 0x756E6547, 0x49656E69, 0x6C65746E},
117
{ 0x68747541, 0x69746E65, 0x444D4163},
118
{ 0x746E6543, 0x48727561, 0x736C7561}
121
int x86cpuid_GetFirm(const Cx86cpuid *p)
124
for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
126
const UInt32 *v = kVendors[i];
127
if (v[0] == p->vendor[0] &&
128
v[1] == p->vendor[1] &&
129
v[2] == p->vendor[2])
135
Bool CPU_Is_InOrder()
139
UInt32 family, model;
140
if (!x86cpuid_CheckAndRead(&p))
142
family = x86cpuid_GetFamily(&p);
143
model = x86cpuid_GetModel(&p);
144
firm = x86cpuid_GetFirm(&p);
147
case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && model == 0x100C));
148
case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
149
case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
154
#if !defined(MY_CPU_AMD64) && defined(_WIN32)
155
static Bool CPU_Sys_Is_SSE_Supported()
158
vi.dwOSVersionInfoSize = sizeof(vi);
159
if (!GetVersionEx(&vi))
161
return (vi.dwMajorVersion >= 5);
163
#define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
165
#define CHECK_SYS_SSE_SUPPORT
168
Bool CPU_Is_Aes_Supported()
171
CHECK_SYS_SSE_SUPPORT
172
if (!x86cpuid_CheckAndRead(&p))
174
return (p.c >> 25) & 1;