target-i386: add PCLMULQDQ and AES-NI instructions
cover target-i386: add PCLMULQDQ and AES-NI instructions
This patch series adds the PCLMULQDQ and AES-NI instructions to the x86 emulation. Along with the SSE4.1 and SSE4.2 series, this brings the instructions emulation to the level of a Westmere CPU.
It has been tested with the valgrind testsuite and with the kernel autotest.
Changes v1 -> v2:
- Patch 3: Declare all constant tables as static
Changes v2 -> v3:
- Use constant tables from aes.c.
- Fix AES instructions when source and destination registers are the same.
Aurelien Jarno (5):
target-i386: add pclmulqdq instruction
target-i386: enable PCLMULQDQ on Westmere CPU
aes: move aes.h from include/block to include/qemu
aes: make Td[0-5] and Te[0-5] tables non static
target-i386: add AES-NI instructions
block/qcow.c | 2 +- block/qcow2.c | 2 +- block/qcow2.h | 2 +- include/block/aes.h | 26 --- include/qemu/aes.h | 45 ++++ target-i386/cpu.c | 19 +- target-i386/fpu_helper.c | 1 + target-i386/ops_sse.h | 111 +++++++++ target-i386/ops_sse_header.h | 11 + target-i386/translate.c | 10 + util/aes.c | 506 +++++++++++++++++++++--------------------- 11 files changed, 443 insertions(+), 292 deletions(-) delete mode 100644 include/block/aes.h create mode 100644 include/qemu/aes.h
Signature · 1 line
1.7.10.4
PATCH 1/5 target-i386: add pclmulqdq instruction
--- target-i386/cpu.c | 19 +++++++++---------- target-i386/ops_sse.h | 24 ++++++++++++++++++++++++ target-i386/ops_sse_header.h | 5 +++++ target-i386/translate.c | 3 +++ 4 files changed, 41 insertions(+), 10 deletions(-)
target-i386/cpu.c +9 -10
@@ -387,17 +387,16 @@ typedef struct x86_def_t {CPUID_PSE36 (needed for Solaris) */ /* missing: CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */ -#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \ - CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | CPUID_EXT_SSE41 | \ - CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | CPUID_EXT_MOVBE | \ - CPUID_EXT_HYPERVISOR) +#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | \ + CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \ + CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \ + CPUID_EXT_MOVBE | CPUID_EXT_HYPERVISOR)/* missing: -CPUID_EXT_PCLMULQDQ, CPUID_EXT_DTES64, CPUID_EXT_DSCPL, -CPUID_EXT_VMX, CPUID_EXT_SMX, CPUID_EXT_EST, CPUID_EXT_TM2, -CPUID_EXT_CID, CPUID_EXT_FMA, CPUID_EXT_XTPR, CPUID_EXT_PDCM, -CPUID_EXT_PCID, CPUID_EXT_DCA, CPUID_EXT_X2APIC, -CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_AES, CPUID_EXT_XSAVE, -CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C, +CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX, +CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA, +CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA, +CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_AES, +CPUID_EXT_XSAVE, CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C, CPUID_EXT_RDRAND */ #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \ CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
target-i386/ops_sse.h +24 -0
@@ -2179,6 +2179,30 @@ target_ulong helper_popcnt(CPUX86State *env, target_ulong n, uint32_t type)return POPCOUNT (n,5 );#endif } + +void glue (helper_pclmulqdq ,SUFFIX )(CPUX86State *env,Reg *d,Reg *s, +uint32_t ctrl) +{ +uint64_t ah, al, b, resh, resl; + + ah =0 ; + al = d->Q ((ctrl &1 ) !=0 ); + b = s->Q ((ctrl &16 ) !=0 ); + resh = resl =0 ; + +while (b) { +if (b &1 ) { + resl ^= al; + resh ^= ah; + } + ah = (ah <<1 ) | (al >>63 ); + al <<=1 ; + b >>=1 ; + } + + d->Q (0 ) = resl; + d->Q (1 ) = resh; +}#endif #undef SHIFT
target-i386/ops_sse_header.h +5 -0
@@ -336,6 +336,11 @@ DEF_HELPER_3(crc32, tl, i32, tl, i32) DEF_HELPER_3(popcnt, tl, env, tl, i32)#endif +/* AES-NI op helpers */ +#if SHIFT ==1 +DEF_HELPER_4(glue(pclmulqdq, SUFFIX), void, env, Reg, Reg, i32) +#endif +#undef SHIFT#undef Reg#undef SUFFIX
target-i386/translate.c +3 -0
@@ -3147,6 +3147,8 @@ struct SSEOpHelper_eppi {#define SSE41_OP (x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }#define SSE42_OP (x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 } +#define PCLMULQDQ_OP (x) { { NULL, gen_helper_ ## x ## _xmm }, \ + CPUID_EXT_PCLMULQDQ }static const struct SSEOpHelper_epp sse_op_table6[256 ] = { [0x00 ] =SSSE3_OP (pshufb),
@@ -3216,6 +3218,7 @@ static const struct SSEOpHelper_eppi sse_op_table7[256] = { [0x40] = SSE41_OP(dpps), [0x41] = SSE41_OP(dppd), [0x42] = SSE41_OP(mpsadbw), + [0x44] = PCLMULQDQ_OP(pclmulqdq), [0x60] = SSE42_OP(pcmpestrm), [0x61] = SSE42_OP(pcmpestri), [0x62] = SSE42_OP(pcmpistrm),
Signature · 1 line
1.7.10.4
PATCH 2/5 target-i386: enable PCLMULQDQ on Westmere CPU
In reply to Aurelien JarnoThe PCLMULQDQ instruction has been introduced on the Westmere CPU.
--- target-i386/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target-i386/cpu.c +1 -1
@@ -687,7 +687,7 @@ static x86_def_t builtin_x86_defs[] = { CPUID_DE | CPUID_FP87, .ext_features =CPUID_EXT_AES |CPUID_EXT_POPCNT |CPUID_EXT_SSE42 |CPUID_EXT_SSE41 |CPUID_EXT_CX16 |CPUID_EXT_SSSE3 | - CPUID_EXT_SSE3, + CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSE3, .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX, .ext3_features = CPUID_EXT3_LAHF_LM, .xlevel = 0x8000000A,
Signature · 1 line
1.7.10.4
PATCH 3/5 aes: move aes.h from include/block to include/qemu
In reply to Aurelien JarnoMove aes.h from include/block to include/qemu to show it can be reused by other subsystems.
--- block/qcow.c | 2 +- block/qcow2.c | 2 +- block/qcow2.h | 2 +- include/block/aes.h | 26 -------------------------- include/qemu/aes.h | 26 ++++++++++++++++++++++++++ util/aes.c | 2 +- 6 files changed, 30 insertions(+), 30 deletions(-) delete mode 100644 include/block/aes.h create mode 100644 include/qemu/aes.h
block/qcow.c +1 -1
@@ -25,7 +25,7 @@#include "block/block_int.h" #include "qemu/module.h" #include <zlib.h> -#include "block/aes.h" +#include "qemu/aes.h" #include "migration/migration.h" /**************************************************************/
block/qcow2.c +1 -1
@@ -25,7 +25,7 @@#include "block/block_int.h" #include "qemu/module.h" #include <zlib.h> -#include "block/aes.h" +#include "qemu/aes.h" #include "block/qcow2.h" #include "qemu/error-report.h" #include "qapi/qmp/qerror.h"
block/qcow2.h +1 -1
@@ -25,7 +25,7 @@#ifndef BLOCK_QCOW2_H #define BLOCK_QCOW2_H -#include "block/aes.h" +#include "qemu/aes.h" #include "block/coroutine.h" //#define DEBUG_ALLOC
include/block/aes.h deleted +0 -26
@@ -1,26 +0,0 @@ -#ifndef QEMU_AES_H -#define QEMU_AES_H - -#define AES_MAXNR 14 -#define AES_BLOCK_SIZE 16 - -struct aes_key_st { -uint32_t rd_key[4 *(AES_MAXNR +1 )]; -int rounds; -}; -typedef struct aes_key_st AES_KEY ; - -int AES_set_encrypt_key (const unsigned char *userKey,const int bits, -AES_KEY *key); -int AES_set_decrypt_key (const unsigned char *userKey,const int bits, -AES_KEY *key); - -void AES_encrypt (const unsigned char *in,unsigned char *out, -const AES_KEY *key); -void AES_decrypt (const unsigned char *in,unsigned char *out, -const AES_KEY *key); -void AES_cbc_encrypt (const unsigned char *in,unsigned char *out, -const unsigned long length,const AES_KEY *key, -unsigned char *ivec,const int enc); - -#endif
include/qemu/aes.h new file +26 -0
@@ -0,0 +1,26 @@ +#ifndef QEMU_AES_H +#define QEMU_AES_H + +#define AES_MAXNR 14 +#define AES_BLOCK_SIZE 16 + +struct aes_key_st { +uint32_t rd_key[4 *(AES_MAXNR +1 )]; +int rounds; +}; +typedef struct aes_key_st AES_KEY ; + +int AES_set_encrypt_key (const unsigned char *userKey,const int bits, +AES_KEY *key); +int AES_set_decrypt_key (const unsigned char *userKey,const int bits, +AES_KEY *key); + +void AES_encrypt (const unsigned char *in,unsigned char *out, +const AES_KEY *key); +void AES_decrypt (const unsigned char *in,unsigned char *out, +const AES_KEY *key); +void AES_cbc_encrypt (const unsigned char *in,unsigned char *out, +const unsigned long length,const AES_KEY *key, +unsigned char *ivec,const int enc); + +#endif
util/aes.c +1 -1
@@ -28,7 +28,7 @@* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "qemu-common.h" -#include "block/aes.h" +#include "qemu/aes.h" #ifndef NDEBUG #define NDEBUG
Signature · 1 line
1.7.10.4
PATCH 4/5 aes: make Td[0-5] and Te[0-5] tables non static
In reply to Aurelien JarnoRemove static attribute to Td[0-5] and Te[0-5] tables so that they can be used outside of aes.c. Change their type from u32 to uint32_t, to keep the u32 udef local to aes.c. Prefix them with AES_ so that they do not conflict with other symbols.
--- include/qemu/aes.h | 19 ++ util/aes.c | 504 ++++++++++++++++++++++++++-------------------------- 2 files changed, 271 insertions(+), 252 deletions(-)
Note: while renaming Td/Te tables, I replaced tabs with spaces, but kept the remaining code style to keep the unrolled loops readable.
include/qemu/aes.h +19 -0
@@ -23,4 +23,23 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, const unsigned long length, const AES_KEY *key, unsigned char *ivec, const int enc); +/* +AES_Te0[x] = S [x].[02, 01, 01, 03]; +AES_Te1[x] = S [x].[03, 02, 01, 01]; +AES_Te2[x] = S [x].[01, 03, 02, 01]; +AES_Te3[x] = S [x].[01, 01, 03, 02]; +AES_Te4[x] = S [x].[01, 01, 01, 01]; + +AES_Td0[x] = Si[x].[0e, 09, 0d, 0b]; +AES_Td1[x] = Si[x].[0b, 0e, 09, 0d]; +AES_Td2[x] = Si[x].[0d, 0b, 0e, 09]; +AES_Td3[x] = Si[x].[09, 0d, 0b, 0e]; +AES_Td4[x] = Si[x].[01, 01, 01, 01]; +*/ + +extern const uint32_t AES_Te0[256 ], AES_Te1[256 ], AES_Te2[256 ], + AES_Te3[256 ], AES_Te4[256 ]; +extern const uint32_t AES_Td0[256 ], AES_Td1[256 ], AES_Td2[256 ], + AES_Td3[256 ], AES_Td4[256 ]; +#endif
util/aes.c +252 -252
718 lines not on this page · raw message
Signature · 1 line
1.7.10.4
PATCH 5/5 target-i386: add AES-NI instructions
In reply to Aurelien Jarno--- target-i386/cpu.c | 6 +-- target-i386/fpu_helper.c | 1 + target-i386/ops_sse.h | 87 ++++++++++++++++++++++++++++++++++++++++++ target-i386/ops_sse_header.h | 6 +++ target-i386/translate.c | 7 ++++ 5 files changed, 104 insertions(+), 3 deletions(-)
target-i386/cpu.c +3 -3
@@ -390,13 +390,13 @@ typedef struct x86_def_t {#define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | \ CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \ CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \ - CPUID_EXT_MOVBE | CPUID_EXT_HYPERVISOR) + CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR)/* missing: CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA, -CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_AES, -CPUID_EXT_XSAVE, CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C, +CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_XSAVE, +CPUID_EXT_OSXSAVE, CPUID_EXT_AVX, CPUID_EXT_F16C, CPUID_EXT_RDRAND */ #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \ CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
target-i386/fpu_helper.c +1 -0
@@ -20,6 +20,7 @@#include <math.h> #include "cpu.h" #include "helper.h" +#include "qemu/aes.h" #include "qemu/host-utils.h" #if !defined(CONFIG_USER_ONLY )
target-i386/ops_sse.h +87 -0
93 lines not on this page · raw message
target-i386/ops_sse_header.h +6 -0
@@ -338,6 +338,12 @@ DEF_HELPER_3(popcnt, tl, env, tl, i32)/* AES-NI op helpers */ #if SHIFT ==1 +DEF_HELPER_3(glue(aesdec, SUFFIX), void, env, Reg, Reg) +DEF_HELPER_3(glue(aesdeclast, SUFFIX), void, env, Reg, Reg) +DEF_HELPER_3(glue(aesenc, SUFFIX), void, env, Reg, Reg) +DEF_HELPER_3(glue(aesenclast, SUFFIX), void, env, Reg, Reg) +DEF_HELPER_3(glue(aesimc, SUFFIX), void, env, Reg, Reg) +DEF_HELPER_4(glue(aeskeygenassist, SUFFIX), void, env, Reg, Reg, i32) DEF_HELPER_4(glue(pclmulqdq, SUFFIX), void, env, Reg, Reg, i32)#endif
target-i386/translate.c +7 -0
@@ -3149,6 +3149,7 @@ struct SSEOpHelper_eppi {#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }#define PCLMULQDQ_OP (x) { { NULL, gen_helper_ ## x ## _xmm }, \ CPUID_EXT_PCLMULQDQ } +#define AESNI_OP (x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }static const struct SSEOpHelper_epp sse_op_table6[256 ] = { [0x00 ] =SSSE3_OP (pshufb),
@@ -3197,6 +3198,11 @@ static const struct SSEOpHelper_epp sse_op_table6[256] = { [0x3f] = SSE41_OP(pmaxud), [0x40] = SSE41_OP(pmulld), [0x41] = SSE41_OP(phminposuw), + [0xdb] = AESNI_OP(aesimc), + [0xdc] = AESNI_OP(aesenc), + [0xdd] = AESNI_OP(aesenclast), + [0xde] = AESNI_OP(aesdec), + [0xdf] = AESNI_OP(aesdeclast), };static const struct SSEOpHelper_eppi sse_op_table7[256 ] = {
@@ -3223,6 +3229,7 @@ static const struct SSEOpHelper_eppi sse_op_table7[256] = { [0x61] = SSE42_OP(pcmpestri), [0x62] = SSE42_OP(pcmpistrm), [0x63] = SSE42_OP(pcmpistri), + [0xdf] = AESNI_OP(aeskeygenassist), }; static void gen_sse(CPUX86State *env, DisasContext *s, int b,
Signature · 1 line
1.7.10.4
Quoted 7 lines · Aurelien Jarno
This patch series adds the PCLMULQDQ and AES-NI instructions to the x86 emulation. Along with the SSE4.1 and SSE4.2 series, this brings the instructions emulation to the level of a Westmere CPU.
It has been tested with the valgrind testsuite and with the kernel autotest.
Quoted 7 lines
Changes v1 -> v2:
- Patch 3: Declare all constant tables as staticChanges v2 -> v3:
- Use constant tables fromaes.c.
- Fix AES instructions when source and destination registers are the same.
All patches look good to me, thanks.
Quoted 27 lines
Aurelien Jarno (5):
target-i386: add pclmulqdq instruction
target-i386: enable PCLMULQDQ on Westmere CPU
aes: moveaes.hfrominclude/blocktoinclude/qemu
aes: make Td[0-5] and Te[0-5] tables non static
target-i386: add AES-NI instructionsblock/qcow.c | 2 +- block/qcow2.c | 2 +- block/qcow2.h | 2 +- include/block/aes.h | 26 --- include/qemu/aes.h | 45 ++++ target-i386/cpu.c | 19 +- target-i386/fpu_helper.c | 1 + target-i386/ops_sse.h | 111 +++++++++ target-i386/ops_sse_header.h | 11 + target-i386/translate.c | 10 + util/aes.c | 506 +++++++++++++++++++++--------------------- 11 files changed, 443 insertions(+), 292 deletions(-) delete mode 100644 include/block/aes.h create mode 100644 include/qemu/aes.hSignature · 1 line
1.7.10.4
Quoted 15 lines · Aurelien Jarno
This patch series adds the PCLMULQDQ and AES-NI instructions to the x86 emulation. Along with the SSE4.1 and SSE4.2 series, this brings the instructions emulation to the level of a Westmere CPU.
It has been tested with the valgrind testsuite and with the kernel autotest.
Changes v1 -> v2:
- Patch 3: Declare all constant tables as staticChanges v2 -> v3:
- Use constant tables fromaes.c.
- Fix AES instructions when source and destination registers are the same.
Quoted 6 lines
Aurelien Jarno (5):
target-i386: add pclmulqdq instruction
target-i386: enable PCLMULQDQ on Westmere CPU
aes: moveaes.hfrominclude/blocktoinclude/qemu
aes: make Td[0-5] and Te[0-5] tables non static
target-i386: add AES-NI instructions
All now,
r~