#include <stdio.h>
#include <openssl/crypto.h>
#include "crypto_arch.h"
#include "x86_arch.h"
uint64_t OPENSSL_ia32cap_P;
uint64_t crypto_cpu_caps_i386;
extern uint64_t crypto_cpu_caps;
static inline void
cpuid(uint32_t eax, uint32_t *out_eax, uint32_t *out_ebx, uint32_t *out_ecx,
uint32_t *out_edx)
{
uint32_t ebx = 0, ecx = 0, edx = 0;
#ifndef OPENSSL_NO_ASM
__asm__ ("cpuid": "+a"(eax), "+b"(ebx), "+c"(ecx), "+d"(edx));
#else
eax = 0;
#endif
if (out_eax != NULL)
*out_eax = eax;
if (out_ebx != NULL)
*out_ebx = ebx;
if (out_ecx != NULL)
*out_ecx = ecx;
if (out_edx != NULL)
*out_edx = edx;
}
static inline void
xgetbv(uint32_t ecx, uint32_t *out_eax, uint32_t *out_edx)
{
uint32_t eax = 0, edx = 0;
#ifndef OPENSSL_NO_ASM
__asm__ ("xgetbv": "+a"(eax), "+c"(ecx), "+d"(edx));
#endif
if (out_eax != NULL)
*out_eax = eax;
if (out_edx != NULL)
*out_edx = edx;
}
void
crypto_cpu_caps_init(void)
{
uint32_t eax, ebx, ecx, edx;
uint64_t caps = 0;
cpuid(0, &eax, &ebx, &ecx, &edx);
if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
caps |= CPUCAP_MASK_INTEL;
if (eax < 1)
return;
cpuid(1, &eax, &ebx, &ecx, &edx);
if ((edx & IA32CAP_MASK0_FXSR) != 0)
caps |= CPUCAP_MASK_FXSR;
if ((edx & IA32CAP_MASK0_HT) != 0)
caps |= CPUCAP_MASK_HT;
if ((edx & IA32CAP_MASK0_MMX) != 0) {
caps |= CPUCAP_MASK_MMX;
crypto_cpu_caps_i386 |= CRYPTO_CPU_CAPS_I386_MMX;
}
if ((edx & IA32CAP_MASK0_SSE) != 0) {
caps |= CPUCAP_MASK_SSE;
crypto_cpu_caps_i386 |= CRYPTO_CPU_CAPS_I386_SSE;
}
if ((edx & IA32CAP_MASK0_SSE2) != 0)
caps |= CPUCAP_MASK_SSE2;
if ((ecx & IA32CAP_MASK1_AESNI) != 0) {
caps |= CPUCAP_MASK_AESNI;
crypto_cpu_caps_i386 |= CRYPTO_CPU_CAPS_I386_AES;
}
if ((ecx & IA32CAP_MASK1_PCLMUL) != 0) {
caps |= CPUCAP_MASK_PCLMUL;
crypto_cpu_caps_i386 |= CRYPTO_CPU_CAPS_I386_CLMUL;
}
if ((ecx & IA32CAP_MASK1_SSSE3) != 0)
caps |= CPUCAP_MASK_SSSE3;
if ((ecx & IA32CAP_MASK1_OSXSAVE) != 0) {
xgetbv(0, &eax, NULL);
if (((eax >> 1) & 3) == 3 && (ecx & IA32CAP_MASK1_AVX) != 0)
caps |= CPUCAP_MASK_AVX;
}
if ((caps & CPUCAP_MASK_AESNI) != 0)
crypto_cpu_caps |= CRYPTO_CPU_CAPS_ACCELERATED_AES;
OPENSSL_ia32cap_P = caps;
}