target/sparc: Add support for SPARC crypto instructions
v3 cover target/sparc: Add support for SPARC crypto instructions
This series implements SPARC crypto instructions for QEMU. Most other arches already support crypto instructions, but SPARC is lagging behind.
The following instructions are added:
- AES rounds and key expansions
- CRC32C
- SHA1/256/512
- RDCFR (Compatibility Feature Register)
Camellia is intentionally left out as it's possibly being removed from the kernel code, and does not seem to be needed. DES/MD5/MPMUL also left out, as Linux doesn't use them.
Motivated by the need for QEMU-testable SPARC crypto code without access to real hardware. An issue was raised by kernel maintainer Eric Biggers
in: https://github.com/sparclinux/issues/issues/77
A new +crypto CPU feature enables the instructions and the CFR. This is off by default, and does not change default behaviour.
The most difficult part to get right, and the biggest patch, was the SHA instructions. On SPARC, a single instruction hashes one whole block. sha1, sha256 and sha512 each do a complete FIPS 180-4 compression.
Testing:
- Every instruction tested against SPARC T4-1 and SPARC T7-1
- Tested QEMU on big-endian host (sparc64 T7-1)
- Linux 7.3-rc5 crypto and CRC KUnit suites (208 passed, 0 failed)
The Linux test used an unsent kernel patch plus -prom-env nvramrc.
AI tool disclosure:
- An early incomplete prototype was made using LLM tools
- Used to research the OSA 2011 and FIPS 180-4 document. Implementation strategies and review of the _existing_ QEMU code.
- Local test tooling. Scripts for comparing the output vs real SPARC hardware.
- Review of patches, fixes made by me
- No code, comments or commit messages directly from AI tools
References:
OSA (Oracle SPARC Architecture) 2011:
https://www.oracle.com/technetwork/server-storage/sun-sparc-enterprise/documentation/140521-ua2011-d096-p-ext-2306580.pdf
qemu-devel RFC:
https://lists.gnu.org/archive/html/qemu-devel/2026-09/msg09494.html
Changes in v3:
- 2/8: Return CFR; R-b Philippe
- 3/8: AES rounds; message line on the next commit setting CFR.aes (Philippe); R-b Philippe (T-b kept).
- 4/8: AES key expansion; deposit64() in aes_kexpand() (Philippe); R-b Philippe
- 7/8: (NEW) target/sparc: Allow block-init stores when the CPU has the crypto feature. Multiarch glibc got SIGILL in memcpy/memset with +crypto. Placed before the linux-user patch. Reported on v2 7/7.
- 8/8: v2 patch 7, unchanged
v2: https://lore.kernel.org/qemu-devel/[email protected]/
Changes in v2:
- 1/7: use suggested commit message (Philippe); R-b Philippe
- 2/7: start with an empty CFR. Bits set in each instruction patch
- 3/7: byte-order comment, rk half zeroed explicitly; T-b Philippe
- 4/7: sets CFR.aes; "3-operand"/"4-operand" wording.
- 5/7: sets CFR.crc32c; "3-operand" wording; explicit 64-bit return; R-b + T-b Philippe
- 6/7: sets the SHA bits; comment on the narrowing; R-b + T-b Philippe
- 7/7: -cpu TI-UltraSparc-II,+crypto mentioned; R-b Philippe
- Tests: Follow-up, as agreed (Philippe)
v1: https://lore.kernel.org/qemu-devel/[email protected]/
Stian Halseth (8):
target/sparc: Add crypto feature
target/sparc: Return CFR when crypto feature is enabled target/sparc: Add the eight AES round instructions target/sparc: Add the AES key expansion instructions target/sparc: Add the CRC32C instruction
target/sparc: Add the SHA instructions
target/sparc: Allow block-init stores when the CPU has the crypto
feature
linux-user/sparc: Report HWCAP_SPARC_CRYPTO for crypto CPU feature
linux-user/sparc/elfload.c | 1 + target/sparc/cpu-feature.h.inc | 1 + target/sparc/cpu.c | 3 + target/sparc/crypto_helper.c | 442 +++++++++++++++++++++++++++++++++ target/sparc/helper.h | 16 ++ target/sparc/insns.decode | 26 +- target/sparc/meson.build | 2 +- target/sparc/translate.c | 89 ++++++- 8 files changed, 576 insertions(+), 4 deletions(-) create mode 100644 target/sparc/crypto_helper.c
base-commit: 11661d857254b27fca5568c6deacf4f6b6f3460f
Signature · 1 line
2.43.0
PATCH v3 1/8 target/sparc: Add crypto feature
Add a "crypto" feature bit gating the OSA 2011 cryptographic instructions (AES, SHA-1/256/512, CRC32C, CFR in ASR 26). Off by default; no CPU model enables it yet; can be set explicitly with "-cpu ...,+crypto". SPARC64 only.
--- target/sparc/cpu-feature.h.inc | 1 + target/sparc/cpu.c | 3 +++ target/sparc/translate.c | 2 ++ 3 files changed, 6 insertions(+)
target/sparc/cpu-feature.h.inc +1 -0
@@ -16,3 +16,4 @@ FEATURE(FMAF) FEATURE(VIS3) FEATURE(IMA) FEATURE(VIS4) +FEATURE(CRYPTO)
target/sparc/cpu.c +3 -0
@@ -560,6 +560,7 @@ static const char * const feature_name[] = { [CPU_FEATURE_BIT_VIS3] = "vis3", [CPU_FEATURE_BIT_IMA] = "ima", [CPU_FEATURE_BIT_VIS4] = "vis4", + [CPU_FEATURE_BIT_CRYPTO] = "crypto", #else [CPU_FEATURE_BIT_MUL] = "mul", [CPU_FEATURE_BIT_DIV] = "div",
@@ -977,6 +978,8 @@ static const Property sparc_cpu_properties[] = { CPU_FEATURE_BIT_IMA, false),DEFINE_PROP_BIT ("vis4" ,SPARCCPU , env.def.features,CPU_FEATURE_BIT_VIS4 , false), +DEFINE_PROP_BIT ("crypto" ,SPARCCPU , env.def.features, +CPU_FEATURE_BIT_CRYPTO , false), #elseDEFINE_PROP_BIT ("mul" ,SPARCCPU , env.def.features, CPU_FEATURE_BIT_MUL, false),
target/sparc/translate.c +2 -0
@@ -2507,6 +2507,7 @@ static int extract_qfpreg(DisasContext *dc, int x)# define avail_VIS3 (C ) ((C)->def->features & CPU_FEATURE_VIS3)# define avail_VIS3B (C ) avail_VIS3(C)# define avail_VIS4 (C ) ((C)->def->features & CPU_FEATURE_VIS4) +# define avail_CRYPTO (C ) ((C)->def->features & CPU_FEATURE_CRYPTO)#else # define avail_32 (C ) true# define avail_ASR17 (C ) ((C)->def->features & CPU_FEATURE_ASR17)
@@ -2524,6 +2525,7 @@ static int extract_qfpreg(DisasContext *dc, int x)# define avail_VIS3 (C ) false# define avail_VIS3B (C ) false# define avail_VIS4 (C ) false +# define avail_CRYPTO (C ) false#endif /*
Signature · 1 line
2.43.0
Re: [PATCH v3 1/8] target/sparc: Add crypto feature
Quoted 56 lines · Stian Halseth
Add a "crypto" feature bit gating the OSA 2011 cryptographic instructions (AES, SHA-1/256/512, CRC32C, CFR in ASR 26). Off by default; no CPU model enables it yet; can be set explicitly with "-cpu ...,+crypto". SPARC64 only.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/cpu-feature.h.inc | 1 + target/sparc/cpu.c | 3 +++ target/sparc/translate.c | 2 ++ 3 files changed, 6 insertions(+)target/sparc/cpu-feature.h.inc +1
-0@@ -16,3 +16,4 @@ FEATURE(FMAF) FEATURE(VIS3) FEATURE(IMA) FEATURE(VIS4) +FEATURE(CRYPTO)target/sparc/cpu.c +3
-0@@ -560,6 +560,7 @@ static const char * const feature_name[] = { [CPU_FEATURE_BIT_VIS3] = "vis3", [CPU_FEATURE_BIT_IMA] = "ima", [CPU_FEATURE_BIT_VIS4] = "vis4", + [CPU_FEATURE_BIT_CRYPTO] = "crypto", #else [CPU_FEATURE_BIT_MUL] = "mul", [CPU_FEATURE_BIT_DIV] = "div",@@ -977,6 +978,8 @@ static const Property sparc_cpu_properties[] = { CPU_FEATURE_BIT_IMA, false),DEFINE_PROP_BIT ("vis4" ,SPARCCPU , env.def.features,CPU_FEATURE_BIT_VIS4 , false), +DEFINE_PROP_BIT ("crypto" ,SPARCCPU , env.def.features, +CPU_FEATURE_BIT_CRYPTO , false), #elseDEFINE_PROP_BIT ("mul" ,SPARCCPU , env.def.features, CPU_FEATURE_BIT_MUL, false),target/sparc/translate.c +1
-0@@ -2507,6 +2507,7 @@ static int extract_qfpreg(DisasContext *dc, int x)# define avail_VIS3 (C ) ((C)->def->features & CPU_FEATURE_VIS3)# define avail_VIS3B (C ) avail_VIS3(C)# define avail_VIS4 (C ) ((C)->def->features & CPU_FEATURE_VIS4) +# define avail_CRYPTO (C ) ((C)->def->features & CPU_FEATURE_CRYPTO)#else # define avail_32 (C ) true# define avail_ASR17 (C ) ((C)->def->features & CPU_FEATURE_ASR17)
target/sparc/translate.c static int extract_qfpreg(DisasContext *dc, int x) · line 2531
Quoted 7 lines
target/sparc/translate.c +1
-0@@ -2524,6 +2525,7 @@ static int extract_qfpreg(DisasContext *dc, int x)# define avail_VIS3 (C ) false# define avail_VIS3B (C ) false# define avail_VIS4 (C ) false +# define avail_CRYPTO (C ) false#endif /*
ATB,
Mark.
PATCH v3 2/8 target/sparc: Return CFR when crypto feature is enabled
In reply to Stian HalsethASR 26 returns a Compatibility Feature Register (CFR) when the crypto feature is on. Without the crypto feature, a read keeps strand status as before.
Return an empty CFR. Each instruction patch sets its bit. Guests can read the CFR before using each crypto instruction.
Based on OSA (Oracle SPARC Architecture) 2011:
- 5.5 (Ancillary State Registers), table 5-8
- 5.5.11 Compatibility Feature Register (CFR), table 5-15
--- target/sparc/insns.decode | 5 ++++- target/sparc/translate.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-)
target/sparc/insns.decode +4 -1
@@ -106,7 +106,10 @@ CALL 01 i:s30 RDTICK_CMPR 10 rd:5 101000 10111 0 0000000000000 RDSTICK 10 rd:5 101000 11000 0 0000000000000 RDSTICK_CMPR 10 rd:5 101000 11001 0 0000000000000 - RDSTRAND_STATUS 10 rd:5 101000 11010 0 0000000000000 + { + RDCFR 10 rd:5 101000 11010 0 0000000000000 + RDSTRAND_STATUS 10 rd:5 101000 11010 0 0000000000000 + } ] # The v8 manual, section B.30 STBAR instruction, says
target/sparc/translate.c +14 -0
@@ -3022,6 +3022,20 @@ static TCGv do_rdstrand_status(DisasContext *dc, TCGv dst)TRANS (RDSTRAND_STATUS ,HYPV , do_rd_special, true, a->rd, do_rdstrand_status) +/* CFR bits (OSA 2011, 5.5.11) */ +#define CFR_AES (1 << 0) +#define CFR_SHA1 (1 << 5) +#define CFR_SHA256 (1 << 6) +#define CFR_SHA512 (1 << 7) +#define CFR_CRC32C (1 << 11) + +static TCGv do_rdcfr (DisasContext *dc,TCGv dst) +{ +return tcg_constant_tl (0 ); +} + +TRANS(RDCFR, CRYPTO, do_rd_special, true, a->rd, do_rdcfr) +static TCGv do_rdpsr (DisasContext *dc,TCGv dst) {gen_helper_rdpsr (dst, tcg_env);
Signature · 1 line
2.43.0
Re: [PATCH v3 2/8] target/sparc: Return CFR when crypto feature is enabled
Quoted 49 lines · Stian Halseth
ASR 26 returns a Compatibility Feature Register (CFR) when the crypto feature is on. Without the crypto feature, a read keeps strand status as before.
Return an empty CFR. Each instruction patch sets its bit. Guests can read the CFR before using each crypto instruction.
Based on OSA (Oracle SPARC Architecture) 2011:
- 5.5 (Ancillary State Registers), table 5-8
- 5.5.11 Compatibility Feature Register (CFR), table 5-15Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/insns.decode | 5 ++++- target/sparc/translate.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-)target/sparc/insns.decode +4
-1@@ -106,7 +106,10 @@ CALL 01 i:s30 RDTICK_CMPR 10 rd:5 101000 10111 0 0000000000000 RDSTICK 10 rd:5 101000 11000 0 0000000000000 RDSTICK_CMPR 10 rd:5 101000 11001 0 0000000000000 - RDSTRAND_STATUS 10 rd:5 101000 11010 0 0000000000000 + { + RDCFR 10 rd:5 101000 11010 0 0000000000000 + RDSTRAND_STATUS 10 rd:5 101000 11010 0 0000000000000 + } ] # The v8 manual, section B.30 STBAR instruction, saystarget/sparc/translate.c +7
-0@@ -3022,6 +3022,20 @@ static TCGv do_rdstrand_status(DisasContext *dc, TCGv dst)TRANS (RDSTRAND_STATUS ,HYPV , do_rd_special, true, a->rd, do_rdstrand_status) +/* CFR bits (OSA 2011, 5.5.11) */ +#define CFR_AES (1 << 0) +#define CFR_SHA1 (1 << 5) +#define CFR_SHA256 (1 << 6) +#define CFR_SHA512 (1 << 7) +#define CFR_CRC32C (1 << 11) +
target/sparc/translate.c static TCGv do_rdstrand_status(DisasContext *dc, TCGv dst) · line 3041
Quoted 10 lines
target/sparc/translate.c +7
-0+static TCGv do_rdcfr (DisasContext *dc,TCGv dst) +{ +return tcg_constant_tl (0 ); +} + +TRANS(RDCFR, CRYPTO, do_rd_special, true, a->rd, do_rdcfr) +static TCGv do_rdpsr (DisasContext *dc,TCGv dst) {gen_helper_rdpsr (dst, tcg_env);
ATB,
Mark.
PATCH v3 3/8 target/sparc: Add the eight AES round instructions
In reply to Stian HalsethImplement the eight AES round instructions by using QEMU's shared crypto/aes-round.h, gated behind the crypto CPU feature:
- AES_EROUND01/23 and last variants
- AES_DROUND01/23 and last variants
Based on OSA (Oracle SPARC Architecture) 2011, section 7.3:
AES Cryptographic Operations (4 operand)
Next commit will complete the feature and expose its availability to the guest by setting the AES bit in CFR.
--- target/sparc/crypto_helper.c | 89 ++++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 8 ++++ target/sparc/insns.decode | 9 ++++ target/sparc/meson.build | 2 +- target/sparc/translate.c | 16 +++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 target/sparc/crypto_helper.c
target/sparc/crypto_helper.c new file +89 -0
@@ -0,0 +1,89 @@ +/* +* SPARC crypto instructions for QEMU +* +* Based on Oracle SPARC Architecture 2011 +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "exec/helper-proto.h" +#include "crypto/aes-round.h" + +/* +* SPARC four-operand AES round (Section 7.3). +* s2:s3 holds the state; half selects output columns 0-1 or 2-3, and +* key supplies their round key. dec selects decryption; last omits +* MixColumns/InvMixColumns. Return the selected 64-bit half. +* +* Note all 64-bit values (state and key) hold AES bytes in big-endian +* order, i.e. the most significant byte is the first state byte. +*/ +static uint64_t aes_round (uint64_t key,uint64_t s2,uint64_t s3, +int half,bool dec,bool last) +{ +AESState st, rk, r; +const unsigned first =HOST_BIG_ENDIAN ?0 :1 ; +const unsigned out = first ^ half; + + st.d[first] = s2;/* columns 0-1 */ + st.d[first ^1 ] = s3;/* columns 2-3 */ + rk.d[out] = key; + rk.d[out ^1 ] =0 ; + +if (dec) { +if (last) { +aesdec_ISB_ISR_AK (&r, &st, &rk, true); + }else { +aesdec_ISB_ISR_AK_IMC (&r, &st, &rk, true); + } + }else { +if (last) { +aesenc_SB_SR_AK (&r, &st, &rk, true); + }else { +aesenc_SB_SR_MC_AK (&r, &st, &rk, true); + } + } + +return r.d[out]; +} + +uint64_t helper_aes_eround01 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , false, false); +} + +uint64_t helper_aes_eround23 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , false, false); +} + +uint64_t helper_aes_dround01 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , true, false); +} + +uint64_t helper_aes_dround23 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , true, false); +} + +uint64_t helper_aes_eround01_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , false, true); +} + +uint64_t helper_aes_eround23_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , false, true); +} + +uint64_t helper_aes_dround01_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , true, true); +} + +uint64_t helper_aes_dround23_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , true, true); +}
target/sparc/helper.h +8 -0
@@ -122,6 +122,14 @@ DEF_HELPER_FLAGS_2(fchksm16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fmean16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fslas16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fslas32, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround01, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround23, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround01, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround23, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \
target/sparc/insns.decode +9 -0
@@ -596,6 +596,15 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ NCP 10 ----- 110111 ----- --------- ----- # v8 CPop2 } +AES_EROUND01 10 ..... 011001 ..... ..... 0000 ..... @d_d_d_d +AES_EROUND23 10 ..... 011001 ..... ..... 0001 ..... @d_d_d_d +AES_DROUND01 10 ..... 011001 ..... ..... 0010 ..... @d_d_d_d +AES_DROUND23 10 ..... 011001 ..... ..... 0011 ..... @d_d_d_d +AES_EROUND01_L 10 ..... 011001 ..... ..... 0100 ..... @d_d_d_d +AES_EROUND23_L 10 ..... 011001 ..... ..... 0101 ..... @d_d_d_d +AES_DROUND01_L 10 ..... 011001 ..... ..... 0110 ..... @d_d_d_d +AES_DROUND23_L 10 ..... 011001 ..... ..... 0111 ..... @d_d_d_d + ## ## Major Opcode 11 -- load and store instructions ##
target/sparc/meson.build +1 -1
@@ -12,7 +12,7 @@ sparc_ss.add(files( 'win_helper.c', )) sparc_ss.add(when: 'TARGET_SPARC', if_true: files('int32_helper.c')) -sparc_ss.add(when: 'TARGET_SPARC64', if_true: files('int64_helper.c', 'vis_helper.c')) +sparc_ss.add(when: 'TARGET_SPARC64', if_true: files('int64_helper.c', 'vis_helper.c', 'crypto_helper.c')) sparc_system_ss = ss.source_set() sparc_system_ss.add(files(
target/sparc/translate.c +16 -0
@@ -101,6 +101,14 @@# define gen_helper_xmulx ({ qemu_build_not_reached(); NULL; })# define gen_helper_xmulxhi ({ qemu_build_not_reached(); NULL; })# define MAXTL_MASK 0 +# define gen_helper_aes_eround01 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround23 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround01 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround23 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround01_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround23_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround01_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0
@@ -5418,6 +5426,14 @@ TRANS(FNMSUBd, FMAF, do_dddd, a, gen_op_fnmsubd) TRANS(FNMADDd, FMAF, do_dddd, a, gen_op_fnmaddd) TRANS(FPMADDX, IMA, do_dddd, a, gen_op_fpmaddx) TRANS(FPMADDXHI, IMA, do_dddd, a, gen_op_fpmaddxhi) +TRANS(AES_EROUND01, CRYPTO, do_dddd, a, gen_helper_aes_eround01) +TRANS(AES_EROUND23, CRYPTO, do_dddd, a, gen_helper_aes_eround23) +TRANS(AES_DROUND01, CRYPTO, do_dddd, a, gen_helper_aes_dround01) +TRANS(AES_DROUND23, CRYPTO, do_dddd, a, gen_helper_aes_dround23) +TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) +TRANS(AES_EROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_eround23_l) +TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) +TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l)static bool trans_FALIGNDATAi (DisasContext *dc,arg_r_r_r *a) {
Signature · 1 line
2.43.0
Re: [PATCH v3 3/8] target/sparc: Add the eight AES round instructions
Quoted 194 lines · Stian Halseth
Implement the eight AES round instructions by using QEMU's shared
crypto/aes-round.h, gated behind the crypto CPU feature:
- AES_EROUND01/23 and last variants
- AES_DROUND01/23 and last variantsBased on OSA (Oracle SPARC Architecture) 2011, section 7.3:
AES Cryptographic Operations (4 operand)Next commit will complete the feature and expose its availability to the guest by setting the AES bit in CFR.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/crypto_helper.c | 89 ++++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 8 ++++ target/sparc/insns.decode | 9 ++++ target/sparc/meson.build | 2 +- target/sparc/translate.c | 16 +++++++ 5 files changed, 123 insertions(+), 1 deletion(-)create mode 100644
target/sparc/crypto_helper.ctarget/sparc/crypto_helper.c new file +89
-0@@ -0,0 +1,89 @@ +/* +* SPARC crypto instructions for QEMU +* +* Based on Oracle SPARC Architecture 2011 +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "exec/helper-proto.h" +#include "crypto/aes-round.h" + +/* +* SPARC four-operand AES round (Section 7.3). +* s2:s3 holds the state; half selects output columns 0-1 or 2-3, and +* key supplies their round key. dec selects decryption; last omits +* MixColumns/InvMixColumns. Return the selected 64-bit half. +* +* Note all 64-bit values (state and key) hold AES bytes in big-endian +* order, i.e. the most significant byte is the first state byte. +*/ +static uint64_t aes_round (uint64_t key,uint64_t s2,uint64_t s3, +int half,bool dec,bool last) +{ +AESState st, rk, r; +const unsigned first =HOST_BIG_ENDIAN ?0 :1 ; +const unsigned out = first ^ half; + + st.d[first] = s2;/* columns 0-1 */ + st.d[first ^1 ] = s3;/* columns 2-3 */ + rk.d[out] = key; + rk.d[out ^1 ] =0 ; + +if (dec) { +if (last) { +aesdec_ISB_ISR_AK (&r, &st, &rk, true); + }else { +aesdec_ISB_ISR_AK_IMC (&r, &st, &rk, true); + } + }else { +if (last) { +aesenc_SB_SR_AK (&r, &st, &rk, true); + }else { +aesenc_SB_SR_MC_AK (&r, &st, &rk, true); + } + } + +return r.d[out]; +} + +uint64_t helper_aes_eround01 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , false, false); +} + +uint64_t helper_aes_eround23 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , false, false); +} + +uint64_t helper_aes_dround01 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , true, false); +} + +uint64_t helper_aes_dround23 (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , true, false); +} + +uint64_t helper_aes_eround01_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , false, true); +} + +uint64_t helper_aes_eround23_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , false, true); +} + +uint64_t helper_aes_dround01_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,0 , true, true); +} + +uint64_t helper_aes_dround23_l (uint64_t key,uint64_t s2,uint64_t s3) +{ +return aes_round (key, s2, s3,1 , true, true); +}target/sparc/helper.h +8
-0@@ -122,6 +122,14 @@ DEF_HELPER_FLAGS_2(fchksm16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fmean16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fslas16, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(fslas32, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround01, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround23, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround01, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround23, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_eround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \target/sparc/insns.decode +9
-0@@ -596,6 +596,15 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ NCP 10 ----- 110111 ----- --------- ----- # v8 CPop2 } +AES_EROUND01 10 ..... 011001 ..... ..... 0000 ..... @d_d_d_d +AES_EROUND23 10 ..... 011001 ..... ..... 0001 ..... @d_d_d_d +AES_DROUND01 10 ..... 011001 ..... ..... 0010 ..... @d_d_d_d +AES_DROUND23 10 ..... 011001 ..... ..... 0011 ..... @d_d_d_d +AES_EROUND01_L 10 ..... 011001 ..... ..... 0100 ..... @d_d_d_d +AES_EROUND23_L 10 ..... 011001 ..... ..... 0101 ..... @d_d_d_d +AES_DROUND01_L 10 ..... 011001 ..... ..... 0110 ..... @d_d_d_d +AES_DROUND23_L 10 ..... 011001 ..... ..... 0111 ..... @d_d_d_d + ## ## Major Opcode 11 -- load and store instructions ##target/sparc/meson.build +1
-1@@ -12,7 +12,7 @@ sparc_ss.add(files( 'win_helper.c', )) sparc_ss.add(when: 'TARGET_SPARC', if_true: files('int32_helper.c')) -sparc_ss.add(when: 'TARGET_SPARC64', if_true: files('int64_helper.c', 'vis_helper.c')) +sparc_ss.add(when: 'TARGET_SPARC64', if_true: files('int64_helper.c', 'vis_helper.c', 'crypto_helper.c')) sparc_system_ss = ss.source_set() sparc_system_ss.add(files(target/sparc/translate.c +9
-0@@ -101,6 +101,14 @@# define gen_helper_xmulx ({ qemu_build_not_reached(); NULL; })# define gen_helper_xmulxhi ({ qemu_build_not_reached(); NULL; })# define MAXTL_MASK 0 +# define gen_helper_aes_eround01 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround23 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround01 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround23 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround01_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_eround23_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround01_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0@@ -5418,6 +5426,14 @@ TRANS(FNMSUBd, FMAF, do_dddd, a, gen_op_fnmsubd) TRANS(FNMADDd, FMAF, do_dddd, a, gen_op_fnmaddd) TRANS(FPMADDX, IMA, do_dddd, a, gen_op_fpmaddx) TRANS(FPMADDXHI, IMA, do_dddd, a, gen_op_fpmaddxhi) +TRANS (AES_EROUND01 ,CRYPTO , do_dddd, a, gen_helper_aes_eround01)
target/sparc/translate.c TRANS(FNMSUBd, FMAF, do_dddd, a, gen_op_fnmsubd) · line 5439
Quoted 10 lines
target/sparc/translate.c +7
-0+TRANS(AES_EROUND23, CRYPTO, do_dddd, a, gen_helper_aes_eround23) +TRANS(AES_DROUND01, CRYPTO, do_dddd, a, gen_helper_aes_dround01) +TRANS(AES_DROUND23, CRYPTO, do_dddd, a, gen_helper_aes_dround23) +TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) +TRANS(AES_EROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_eround23_l) +TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) +TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l)static bool trans_FALIGNDATAi (DisasContext *dc,arg_r_r_r *a) {
ATB,
Mark.
PATCH v3 4/8 target/sparc: Add the AES key expansion instructions
In reply to Stian HalsethImplement the AES key expansion instructions according to OSA (Oracle SPARC Architecture) 2011 sections 7.3 and 7.4 and set the AES bit in the CFR.
4-operand form (7.3):
- AES_KEXPAND1
3-operand form (7.4):
- AES_KEXPAND0
- AES_KEXPAND2
Like the AES round instructions, they are gated behind the crypto CPU feature and disabled for sparc32.
--- target/sparc/crypto_helper.c | 58 ++++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 3 ++ target/sparc/insns.decode | 7 +++++ target/sparc/translate.c | 30 ++++++++++++++++++- 4 files changed, 97 insertions(+), 1 deletion(-)
target/sparc/crypto_helper.c +58 -0
@@ -9,6 +9,7 @@#include "qemu/osdep.h" #include "exec/helper-proto.h" #include "crypto/aes-round.h" +#include "crypto/aes.h" /* * SPARC four-operand AES round (Section 7.3).
@@ -87,3 +88,60 @@ uint64_t helper_aes_dround23_l(uint64_t key, uint64_t s2, uint64_t s3) {return aes_round (key, s2, s3,1 , true, true); } + +/* Combine the transformed word with the two words in rs1. */ +static uint64_t aes_kexpand (uint64_t rs1,uint32_t t) +{ +uint32_t hi = (rs1 >>32 ) ^ t; +uint32_t lo = (uint32_t )rs1 ^ hi; + +return deposit64 (lo,32 ,32 , hi); +} + +/* Apply the AES S-box to each byte, preserving byte positions. */ +static uint32_t aes_subword (uint32_t x) +{ +return ((uint32_t )AES_sbox[x >>24 ] <<24 ) | + ((uint32_t )AES_sbox[(x >>16 ) &0xff ] <<16 ) | + ((uint32_t )AES_sbox[(x >>8 ) &0xff ] <<8 ) | + (uint32_t )AES_sbox[x &0xff ]; +} + +/* +* AES Cryptographic Operations 4-operand (Section 7.3) +* AES key expansion with RotWord, SubWord, and RCON. +*/ +uint64_t helper_aes_kexpand1 (uint64_t rs1,uint64_t rs2,uint32_t imm) +{ +static const uint8_t rcon[10 ] = { +0x01 ,0x02 ,0x04 ,0x08 ,0x10 , +0x20 ,0x40 ,0x80 ,0x1b ,0x36 , + }; +uint32_t t =rol32 ((uint32_t )rs2,8 ); + + t =aes_subword (t); + +if (imm <ARRAY_SIZE (rcon)) { + t ^= (uint32_t )rcon[imm] <<24 ; + } + +return aes_kexpand (rs1, t); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion with SubWord, without RotWord or RCON. +*/ +uint64_t helper_aes_kexpand0 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1,aes_subword (rs2)); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion without SubWord, RotWord, or RCON. +*/ +uint64_t helper_aes_kexpand2 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1, (uint32_t )rs2); +}
target/sparc/helper.h +3 -0
@@ -130,6 +130,9 @@ DEF_HELPER_FLAGS_3(aes_eround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_eround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) +DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \
target/sparc/insns.decode +7 -0
@@ -86,6 +86,10 @@ CALL 01 i:s30 @d_d_d_d .. ..... ...... ..... ..... .... ..... \ &r_r_r_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 rs3=%dfp_rs3 +&r_r_i_r rd rs1 imm rs2 +@d_d_i_d .. ..... ...... ..... imm:5 .... ..... \ + &r_r_i_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 + { [ STBAR_v9 10 00000 101000 01111 0 0000000000000
@@ -571,6 +575,8 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU8 10 ..... 110110 ..... 1 0101 1101 ..... @d_d_d FPMAXU16 10 ..... 110110 ..... 1 0101 1110 ..... @d_d_d FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d + AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d + AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \
@@ -604,6 +610,7 @@ AES_EROUND01_L 10 ..... 011001 ..... ..... 0100 ..... @d_d_d_d AES_EROUND23_L 10 ..... 011001 ..... ..... 0101 ..... @d_d_d_d AES_DROUND01_L 10 ..... 011001 ..... ..... 0110 ..... @d_d_d_d AES_DROUND23_L 10 ..... 011001 ..... ..... 0111 ..... @d_d_d_d +AES_KEXPAND1 10 ..... 011001 ..... ..... 1000 ..... @d_d_i_d ## ## Major Opcode 11 -- load and store instructions
target/sparc/translate.c +29 -1
@@ -109,6 +109,8 @@# define gen_helper_aes_eround23_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround01_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0
@@ -3039,7 +3041,7 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (0 ); +return tcg_constant_tl (CFR_AES ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
@@ -5434,6 +5436,32 @@ TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) TRANS(AES_EROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_eround23_l) TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) +TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) +TRANS (AES_KEXPAND2 ,CRYPTO , do_ddd, a, gen_helper_aes_kexpand2) + +/* AES key expansion with RCON selected by the 5-bit immediate in imm */ +static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a) +{ +#ifdef TARGET_SPARC64 +TCGv_i64 dst, src1, src2; + +if (!avail_CRYPTO (dc)) { +return false; + } +if (gen_trap_ifnofpu (dc)) { +return true; + } + + dst =tcg_temp_new_i64 (); + src1 =gen_load_fpr_D (dc, a->rs1); + src2 =gen_load_fpr_D (dc, a->rs2); +gen_helper_aes_kexpand1 (dst, src1, src2,tcg_constant_i32 (a->imm)); +gen_store_fpr_D (dc, a->rd, dst); +return advance_pc (dc); +#else +return false; +#endif +}static bool trans_FALIGNDATAi (DisasContext *dc,arg_r_r_r *a) {
Signature · 1 line
2.43.0
Re: [PATCH v3 4/8] target/sparc: Add the AES key expansion instructions
Quoted 165 lines · Stian Halseth
Implement the AES key expansion instructions according to OSA (Oracle SPARC Architecture) 2011 sections 7.3 and 7.4 and set the AES bit in the CFR.
4-operand form (7.3):
-AES_KEXPAND13-operand form (7.4):
-AES_KEXPAND0
-AES_KEXPAND2Like the AES round instructions, they are gated behind the crypto CPU feature and disabled for sparc32.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/crypto_helper.c | 58 ++++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 3 ++ target/sparc/insns.decode | 7 +++++ target/sparc/translate.c | 30 ++++++++++++++++++- 4 files changed, 97 insertions(+), 1 deletion(-)target/sparc/crypto_helper.c +58
-0@@ -9,6 +9,7 @@#include "qemu/osdep.h" #include "exec/helper-proto.h" #include "crypto/aes-round.h" +#include "crypto/aes.h" /* * SPARC four-operand AES round (Section 7.3). @@ -87,3 +88,60 @@ uint64_t helper_aes_dround23_l(uint64_t key, uint64_t s2, uint64_t s3) {return aes_round (key, s2, s3,1 , true, true); } + +/* Combine the transformed word with the two words in rs1. */ +static uint64_t aes_kexpand (uint64_t rs1,uint32_t t) +{ +uint32_t hi = (rs1 >>32 ) ^ t; +uint32_t lo = (uint32_t )rs1 ^ hi; + +return deposit64 (lo,32 ,32 , hi); +} + +/* Apply the AES S-box to each byte, preserving byte positions. */ +static uint32_t aes_subword (uint32_t x) +{ +return ((uint32_t )AES_sbox[x >>24 ] <<24 ) | + ((uint32_t )AES_sbox[(x >>16 ) &0xff ] <<16 ) | + ((uint32_t )AES_sbox[(x >>8 ) &0xff ] <<8 ) | + (uint32_t )AES_sbox[x &0xff ]; +} + +/* +* AES Cryptographic Operations 4-operand (Section 7.3) +* AES key expansion with RotWord, SubWord, and RCON. +*/ +uint64_t helper_aes_kexpand1 (uint64_t rs1,uint64_t rs2,uint32_t imm) +{ +static const uint8_t rcon[10 ] = { +0x01 ,0x02 ,0x04 ,0x08 ,0x10 , +0x20 ,0x40 ,0x80 ,0x1b ,0x36 , + }; +uint32_t t =rol32 ((uint32_t )rs2,8 ); + + t =aes_subword (t); + +if (imm <ARRAY_SIZE (rcon)) { + t ^= (uint32_t )rcon[imm] <<24 ; + } + +return aes_kexpand (rs1, t); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion with SubWord, without RotWord or RCON. +*/ +uint64_t helper_aes_kexpand0 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1,aes_subword (rs2)); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion without SubWord, RotWord, or RCON. +*/ +uint64_t helper_aes_kexpand2 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1, (uint32_t )rs2); +}target/sparc/helper.h +3
-0@@ -130,6 +130,9 @@ DEF_HELPER_FLAGS_3(aes_eround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_eround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) +DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \target/sparc/insns.decode +7
-0@@ -86,6 +86,10 @@ CALL 01 i:s30 @d_d_d_d .. ..... ...... ..... ..... .... ..... \ &r_r_r_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 rs3=%dfp_rs3 +&r_r_i_r rd rs1 imm rs2 +@d_d_i_d .. ..... ...... ..... imm:5 .... ..... \ + &r_r_i_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 + { [ STBAR_v9 10 00000 101000 01111 0 0000000000000@@ -571,6 +575,8 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU8 10 ..... 110110 ..... 1 0101 1101 ..... @d_d_d FPMAXU16 10 ..... 110110 ..... 1 0101 1110 ..... @d_d_d FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d + AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d + AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \@@ -604,6 +610,7 @@ AES_EROUND01_L 10 ..... 011001 ..... ..... 0100 ..... @d_d_d_d AES_EROUND23_L 10 ..... 011001 ..... ..... 0101 ..... @d_d_d_d AES_DROUND01_L 10 ..... 011001 ..... ..... 0110 ..... @d_d_d_d AES_DROUND23_L 10 ..... 011001 ..... ..... 0111 ..... @d_d_d_d +AES_KEXPAND1 10 ..... 011001 ..... ..... 1000 ..... @d_d_i_d ## ## Major Opcode 11 -- load and store instructionstarget/sparc/translate.c +3
-1@@ -109,6 +109,8 @@# define gen_helper_aes_eround23_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround01_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0@@ -3039,7 +3041,7 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (0 ); +return tcg_constant_tl (CFR_AES ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
target/sparc/translate.c TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) · line 5445
Quoted 10 lines
target/sparc/translate.c +7
-0@@ -5434,6 +5436,32 @@ TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) TRANS(AES_EROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_eround23_l) TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) +TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) +TRANS (AES_KEXPAND2 ,CRYPTO , do_ddd, a, gen_helper_aes_kexpand2) + +/* AES key expansion with RCON selected by the 5-bit immediate in imm */ +static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a) +{ +#ifdef TARGET_SPARC64
Please can we match the existing style in translate.c and remove the indent here?
target/sparc/translate.c TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) · line 5453
Quoted 8 lines
target/sparc/translate.c +8
-0+TCGv_i64 dst, src1, src2; + +if (!avail_CRYPTO (dc)) { +return false; + } +if (gen_trap_ifnofpu (dc)) { +return true; + }
Question: is it possible for a 64-bit CPU not to have a FPU? Even more so, one with the crypto extensions present? I wonder if gen_trap_ifnofpu() isn't needed here.
target/sparc/translate.c TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) · line 5467
Quoted 13 lines
target/sparc/translate.c +10
-0+ dst =tcg_temp_new_i64 (); + src1 =gen_load_fpr_D (dc, a->rs1); + src2 =gen_load_fpr_D (dc, a->rs2); +gen_helper_aes_kexpand1 (dst, src1, src2,tcg_constant_i32 (a->imm)); +gen_store_fpr_D (dc, a->rd, dst); +return advance_pc (dc); +#else +return false; +#endif +}static bool trans_FALIGNDATAi (DisasContext *dc,arg_r_r_r *a) {
Otherwise LGTM.
ATB,
Mark.
Re: [PATCH v3 4/8] target/sparc: Add the AES key expansion instructions
Hi Mark,
Quoted 3 lines · Mark Cave-Ayland
Please can we match the existing style in
translate.cand remove the indent here?
Noted for v4.
Quoted 10 lines
Quoted 9 lines
+
TCGv_i64dst, src1, src2;
+
+ if (!avail_CRYPTO(dc)) {
+ return false;
+ }
+ if (gen_trap_ifnofpu(dc)) {
+ return true;
+ }
Quoted 4 lines
Question: is it possible for a 64-bit CPU not to have a FPU? Even more so, one with
the crypto extensions present? I wonder ifgen_trap_ifnofpu()isn't needed here.
Not to my knowledge, but the gen_trap_ifnofpu() is checking if FPU is enabled, not if it's present. Every sparc64 CPU has an FPU, but it can be switched off. It's mentioned in the OSA 2011 spec.
/Stian
Re: [PATCH v3 4/8] target/sparc: Add the AES key expansion instructions
Quoted 19 lines · Stian Halseth
Hi Mark,
Quoted 3 lines · Mark Cave-Ayland
Please can we match the existing style in
translate.cand remove the indent here?Noted for v4.
Quoted 10 lines
Quoted 9 lines
+
TCGv_i64dst, src1, src2;
+
+ if (!avail_CRYPTO(dc)) {
+ return false;
+ }
+ if (gen_trap_ifnofpu(dc)) {
+ return true;
+ }
Quoted 8 lines
Quoted 4 lines
Question: is it possible for a 64-bit CPU not to have a FPU? Even more so, one with
the crypto extensions present? I wonder ifgen_trap_ifnofpu()isn't needed here.Not to my knowledge, but the
gen_trap_ifnofpu()is checking if FPU is enabled, not if it's present. Every sparc64 CPU has an FPU, but it can be switched off. It's mentioned in the OSA 2011 spec.
(goes and looks)
Yes, you're right. I did wonder if the FPU registers were simply being used for storage (as opposed to carrying out the instructions), however OSA 2011 indicates that these instructions can throw an fp_disabled trap which suggests otherwise.
So it looks like this is correct.
ATB,
Mark.
Re: [PATCH v3 4/8] target/sparc: Add the AES key expansion instructions
In reply to Stian HalsethQuoted 187 lines · Stian Halseth
Implement the AES key expansion instructions according to OSA (Oracle SPARC Architecture) 2011 sections 7.3 and 7.4 and set the AES bit in the CFR.
4-operand form (7.3):
-AES_KEXPAND13-operand form (7.4):
-AES_KEXPAND0
-AES_KEXPAND2Like the AES round instructions, they are gated behind the crypto CPU feature and disabled for sparc32.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/crypto_helper.c | 58 ++++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 3 ++ target/sparc/insns.decode | 7 +++++ target/sparc/translate.c | 30 ++++++++++++++++++- 4 files changed, 97 insertions(+), 1 deletion(-)target/sparc/crypto_helper.c +58
-0@@ -9,6 +9,7 @@#include "qemu/osdep.h" #include "exec/helper-proto.h" #include "crypto/aes-round.h" +#include "crypto/aes.h" /* * SPARC four-operand AES round (Section 7.3). @@ -87,3 +88,60 @@ uint64_t helper_aes_dround23_l(uint64_t key, uint64_t s2, uint64_t s3) {return aes_round (key, s2, s3,1 , true, true); } + +/* Combine the transformed word with the two words in rs1. */ +static uint64_t aes_kexpand (uint64_t rs1,uint32_t t) +{ +uint32_t hi = (rs1 >>32 ) ^ t; +uint32_t lo = (uint32_t )rs1 ^ hi; + +return deposit64 (lo,32 ,32 , hi); +} + +/* Apply the AES S-box to each byte, preserving byte positions. */ +static uint32_t aes_subword (uint32_t x) +{ +return ((uint32_t )AES_sbox[x >>24 ] <<24 ) | + ((uint32_t )AES_sbox[(x >>16 ) &0xff ] <<16 ) | + ((uint32_t )AES_sbox[(x >>8 ) &0xff ] <<8 ) | + (uint32_t )AES_sbox[x &0xff ]; +} + +/* +* AES Cryptographic Operations 4-operand (Section 7.3) +* AES key expansion with RotWord, SubWord, and RCON. +*/ +uint64_t helper_aes_kexpand1 (uint64_t rs1,uint64_t rs2,uint32_t imm) +{ +static const uint8_t rcon[10 ] = { +0x01 ,0x02 ,0x04 ,0x08 ,0x10 , +0x20 ,0x40 ,0x80 ,0x1b ,0x36 , + }; +uint32_t t =rol32 ((uint32_t )rs2,8 ); + + t =aes_subword (t); + +if (imm <ARRAY_SIZE (rcon)) { + t ^= (uint32_t )rcon[imm] <<24 ; + } + +return aes_kexpand (rs1, t); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion with SubWord, without RotWord or RCON. +*/ +uint64_t helper_aes_kexpand0 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1,aes_subword (rs2)); +} + +/* +* AES Cryptographic Operations 3-operand (Section 7.4) +* AES key expansion without SubWord, RotWord, or RCON. +*/ +uint64_t helper_aes_kexpand2 (uint64_t rs1,uint64_t rs2) +{ +return aes_kexpand (rs1, (uint32_t )rs2); +}target/sparc/helper.h +3
-0@@ -130,6 +130,9 @@ DEF_HELPER_FLAGS_3(aes_eround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_eround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround01_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) +DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) +DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \target/sparc/insns.decode +7
-0@@ -86,6 +86,10 @@ CALL 01 i:s30 @d_d_d_d .. ..... ...... ..... ..... .... ..... \ &r_r_r_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 rs3=%dfp_rs3 +&r_r_i_r rd rs1 imm rs2 +@d_d_i_d .. ..... ...... ..... imm:5 .... ..... \ + &r_r_i_r rd=%dfp_rd rs1=%dfp_rs1 rs2=%dfp_rs2 + { [ STBAR_v9 10 00000 101000 01111 0 0000000000000@@ -571,6 +575,8 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU8 10 ..... 110110 ..... 1 0101 1101 ..... @d_d_d FPMAXU16 10 ..... 110110 ..... 1 0101 1110 ..... @d_d_d FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d + AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d + AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \@@ -604,6 +610,7 @@ AES_EROUND01_L 10 ..... 011001 ..... ..... 0100 ..... @d_d_d_d AES_EROUND23_L 10 ..... 011001 ..... ..... 0101 ..... @d_d_d_d AES_DROUND01_L 10 ..... 011001 ..... ..... 0110 ..... @d_d_d_d AES_DROUND23_L 10 ..... 011001 ..... ..... 0111 ..... @d_d_d_d +AES_KEXPAND1 10 ..... 011001 ..... ..... 1000 ..... @d_d_i_d ## ## Major Opcode 11 -- load and store instructionstarget/sparc/translate.c +22
-1@@ -109,6 +109,8 @@# define gen_helper_aes_eround23_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround01_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0@@ -3039,7 +3041,7 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (0 ); +return tcg_constant_tl (CFR_AES ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)@@ -5434,6 +5436,32 @@ TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) TRANS(AES_EROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_eround23_l) TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) +TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) +TRANS (AES_KEXPAND2 ,CRYPTO , do_ddd, a, gen_helper_aes_kexpand2) + +/* AES key expansion with RCON selected by the 5-bit immediate in imm */ +static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a) +{ +#ifdef TARGET_SPARC64 +TCGv_i64 dst, src1, src2; + +if (!avail_CRYPTO (dc)) { +return false; + } +if (gen_trap_ifnofpu (dc)) { +return true; + } + + dst =tcg_temp_new_i64 (); + src1 =gen_load_fpr_D (dc, a->rs1); + src2 =gen_load_fpr_D (dc, a->rs2);
target/sparc/translate.c TRANS(AES_EROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_eround01_l) · line 5467
Quoted 10 lines
target/sparc/translate.c +7
-0+gen_helper_aes_kexpand1 (dst, src1, src2,tcg_constant_i32 (a->imm)); +gen_store_fpr_D (dc, a->rd, dst); +return advance_pc (dc); +#else +return false; +#endif +}static bool trans_FALIGNDATAi (DisasContext *dc,arg_r_r_r *a) {
Since the outstanding queries have now been answered satisfactorily:
I'll fix up the indentation when I apply it to my local branch.
ATB,
Mark.
PATCH v3 5/8 target/sparc: Add the CRC32C instruction
In reply to Stian HalsethImplement the CRC32C instruction according to OSA (Oracle SPARC Architecture) 2011 section 7.19 and set the CRC32C bit in the CFR.
Using QEMU's crc32c.h and bswap.h.
Gated behind the crypto CPU feature and disabled for sparc32.
--- target/sparc/crypto_helper.c | 21 +++++++++++++++++++++ target/sparc/helper.h | 1 + target/sparc/insns.decode | 1 + target/sparc/translate.c | 4 +++- 4 files changed, 26 insertions(+), 1 deletion(-)
target/sparc/crypto_helper.c +21 -0
@@ -10,6 +10,8 @@#include "exec/helper-proto.h" #include "crypto/aes-round.h" #include "crypto/aes.h" +#include "qemu/bswap.h" +#include "qemu/crc32c.h" /* * SPARC four-operand AES round (Section 7.3).
@@ -145,3 +147,22 @@ uint64_t helper_aes_kexpand2(uint64_t rs1, uint64_t rs2) {return aes_kexpand (rs1, (uint32_t )rs2); } + +/* +* CRC32C Operation 3-operand (Section 7.19) +* Accumulate CRC32C over the eight bytes of rs2, most significant first. +*/ +uint64_t helper_crc32c (uint64_t rs1,uint64_t rs2) +{ +uint8_t buf[8 ]; +/* IV and result are byte-swapped in the register. */ +uint32_t crc =bswap32 ((uint32_t )rs1); + +stq_be_p (buf, rs2); + +/* Undo crc32c()'s final inversion for the instruction's raw result. */ + crc =crc32c (crc, buf,sizeof (buf)) ^UINT32_MAX ; + +/* Result lives in the low 32 bits; upper half reads as zero. */ +return (uint64_t )bswap32 (crc); +}
target/sparc/helper.h +1 -0
@@ -133,6 +133,7 @@ DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_2(crc32c, TCG_CALL_NO_RWG_SE, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \
target/sparc/insns.decode +1 -0
@@ -577,6 +577,7 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d + CRC32C 10 ..... 110110 ..... 1 0100 0111 ..... @d_d_d FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \
target/sparc/translate.c +3 -1
@@ -111,6 +111,7 @@# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_crc32c ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0
@@ -3041,7 +3042,7 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (CFR_AES ); +return tcg_constant_tl (CFR_AES |CFR_CRC32C ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
@@ -5438,6 +5439,7 @@ TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) TRANS(AES_KEXPAND2, CRYPTO, do_ddd, a, gen_helper_aes_kexpand2) +TRANS (CRC32C ,CRYPTO , do_ddd, a, gen_helper_crc32c) /* AES key expansion with RCON selected by the 5-bit immediate in imm */static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a)
Signature · 1 line
2.43.0
Re: [PATCH v3 5/8] target/sparc: Add the CRC32C instruction
Quoted 99 lines · Stian Halseth
Implement the CRC32C instruction according to OSA (Oracle SPARC Architecture) 2011 section 7.19 and set the CRC32C bit in the CFR.
Using QEMU's
crc32c.handbswap.h.
Gated behind the crypto CPU feature and disabled for sparc32.Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/crypto_helper.c | 21 +++++++++++++++++++++ target/sparc/helper.h | 1 + target/sparc/insns.decode | 1 + target/sparc/translate.c | 4 +++- 4 files changed, 26 insertions(+), 1 deletion(-)target/sparc/crypto_helper.c +21
-0@@ -10,6 +10,8 @@#include "exec/helper-proto.h" #include "crypto/aes-round.h" #include "crypto/aes.h" +#include "qemu/bswap.h" +#include "qemu/crc32c.h" /* * SPARC four-operand AES round (Section 7.3). @@ -145,3 +147,22 @@ uint64_t helper_aes_kexpand2(uint64_t rs1, uint64_t rs2) {return aes_kexpand (rs1, (uint32_t )rs2); } + +/* +* CRC32C Operation 3-operand (Section 7.19) +* Accumulate CRC32C over the eight bytes of rs2, most significant first. +*/ +uint64_t helper_crc32c (uint64_t rs1,uint64_t rs2) +{ +uint8_t buf[8 ]; +/* IV and result are byte-swapped in the register. */ +uint32_t crc =bswap32 ((uint32_t )rs1); + +stq_be_p (buf, rs2); + +/* Undo crc32c()'s final inversion for the instruction's raw result. */ + crc =crc32c (crc, buf,sizeof (buf)) ^UINT32_MAX ; + +/* Result lives in the low 32 bits; upper half reads as zero. */ +return (uint64_t )bswap32 (crc); +}target/sparc/helper.h +1
-0@@ -133,6 +133,7 @@ DEF_HELPER_FLAGS_3(aes_dround23_l, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_2(crc32c, TCG_CALL_NO_RWG_SE, i64, i64, i64)#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \target/sparc/insns.decode +1
-0@@ -577,6 +577,7 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d + CRC32C 10 ..... 110110 ..... 1 0100 0111 ..... @d_d_d FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \target/sparc/translate.c +2
-1@@ -111,6 +111,7 @@# define gen_helper_aes_dround23_l ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_crc32c ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0@@ -3041,7 +3042,7 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (CFR_AES ); +return tcg_constant_tl (CFR_AES |CFR_CRC32C ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
target/sparc/translate.c TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) · line 5445
Quoted 7 lines
target/sparc/translate.c +1
-0@@ -5438,6 +5439,7 @@ TRANS(AES_DROUND01_L, CRYPTO, do_dddd, a, gen_helper_aes_dround01_l) TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) TRANS(AES_KEXPAND2, CRYPTO, do_ddd, a, gen_helper_aes_kexpand2) +TRANS (CRC32C ,CRYPTO , do_ddd, a, gen_helper_crc32c) /* AES key expansion with RCON selected by the 5-bit immediate in imm */static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a)
ATB,
Mark.
PATCH v3 6/8 target/sparc: Add the SHA instructions
In reply to Stian HalsethImplement the SHA1, SHA256 and SHA512 instructions according to OSA (Oracle SPARC Architecture) 2011 section 7.115, using the algorithms defined in FIPS 180-4, and set the SHA1/SHA256/SHA512 bits in the CFR.
Read and update the fixed floating-point registers and mark the lower FPR bank dirty. Share the SHA-2 Boolean functions between the SHA256 and SHA512 helpers.
Gated behind the crypto CPU feature and disabled for sparc32.
--- target/sparc/crypto_helper.c | 274 +++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 4 + target/sparc/insns.decode | 4 + target/sparc/translate.c | 21 ++- 4 files changed, 302 insertions(+), 1 deletion(-)
target/sparc/crypto_helper.c +274 -0
@@ -12,6 +12,7 @@#include "crypto/aes.h" #include "qemu/bswap.h" #include "qemu/crc32c.h" +#include "cpu.h" /* * SPARC four-operand AES round (Section 7.3).
@@ -166,3 +167,276 @@ uint64_t helper_crc32c(uint64_t rs1, uint64_t rs2)/* Result lives in the low 32 bits; upper half reads as zero. */ return (uint64_t )bswap32 (crc); } + +/* +* Compress one SHA-1 block from f8-f23 into f0-f4. +* Clear f5 to match SPARC M7 behavior. +*/ +void helper_sha1 (CPUSPARCState *env) +{ +uint32_t a = env->fpr[0 ].l.upper; +uint32_t b = env->fpr[0 ].l.lower; +uint32_t c = env->fpr[1 ].l.upper; +uint32_t d = env->fpr[1 ].l.lower; +uint32_t e = env->fpr[2 ].l.upper; +uint32_t w[80 ]; + +/* Load 16 message words; each fpr[] entry holds two words. */ +for (int i =0 ; i <8 ; i++) { + w[2 * i] = env->fpr[4 + i].l.upper; + w[2 * i +1 ] = env->fpr[4 + i].l.lower; + } + +/* Expand the message into the 80-word round schedule. */ +for (int i =16 ; i <80 ; i++) { + w[i] =rol32 (w[i -3 ] ^ w[i -8 ] ^ + w[i -14 ] ^ w[i -16 ],1 ); + } + +/* Each group of 20 rounds uses its own function and constant. */ +for (int i =0 ; i <80 ; i++) { +uint32_t f, k, t; + +if (i <20 ) { + f = (b & c) | (~b & d);/* Choose */ + k =0x5a827999 ; + }else if (i <40 ) { + f = b ^ c ^ d;/* Parity */ + k =0x6ed9eba1 ; + }else if (i <60 ) { + f = (b & c) | (b & d) | (c & d);/* Majority */ + k =0x8f1bbcdc ; + }else { + f = b ^ c ^ d;/* Parity */ + k =0xca62c1d6 ; + } + +/* Advance the five working words, with arithmetic modulo 2^32. */ + t =rol32 (a,5 ) + f + e + k + w[i]; + e = d; + d = c; + c =rol32 (b,30 ); + b = a; + a = t; + } + +/* Accumulate the hash in f0-f4 and clear the unused f5 word. */ + env->fpr[0 ].l.upper += a; + env->fpr[0 ].l.lower += b; + env->fpr[1 ].l.upper += c; + env->fpr[1 ].l.lower += d; + env->fpr[2 ].l.upper += e; + env->fpr[2 ].l.lower =0 ; +} + +/* SHA-2 Boolean functions. */ +static inline uint64_t sha_ch (uint64_t x,uint64_t y,uint64_t z) +{ +return (x & y) ^ (~x & z); +} + +static inline uint64_t sha_maj (uint64_t x,uint64_t y,uint64_t z) +{ +return (x & y) ^ (x & z) ^ (y & z); +} + +static const uint32_t sha256_k[64 ] = { +0x428a2f98 ,0x71374491 ,0xb5c0fbcf ,0xe9b5dba5 , +0x3956c25b ,0x59f111f1 ,0x923f82a4 ,0xab1c5ed5 , +0xd807aa98 ,0x12835b01 ,0x243185be ,0x550c7dc3 , +0x72be5d74 ,0x80deb1fe ,0x9bdc06a7 ,0xc19bf174 , +0xe49b69c1 ,0xefbe4786 ,0x0fc19dc6 ,0x240ca1cc , +0x2de92c6f ,0x4a7484aa ,0x5cb0a9dc ,0x76f988da , +0x983e5152 ,0xa831c66d ,0xb00327c8 ,0xbf597fc7 , +0xc6e00bf3 ,0xd5a79147 ,0x06ca6351 ,0x14292967 , +0x27b70a85 ,0x2e1b2138 ,0x4d2c6dfc ,0x53380d13 , +0x650a7354 ,0x766a0abb ,0x81c2c92e ,0x92722c85 , +0xa2bfe8a1 ,0xa81a664b ,0xc24b8b70 ,0xc76c51a3 , +0xd192e819 ,0xd6990624 ,0xf40e3585 ,0x106aa070 , +0x19a4c116 ,0x1e376c08 ,0x2748774c ,0x34b0bcb5 , +0x391c0cb3 ,0x4ed8aa4a ,0x5b9cca4f ,0x682e6ff3 , +0x748f82ee ,0x78a5636f ,0x84c87814 ,0x8cc70208 , +0x90befffa ,0xa4506ceb ,0xbef9a3f7 ,0xc67178f2 , +}; + +static const uint64_t sha512_k[80 ] = { +UINT64_C (0x428a2f98d728ae22 ),UINT64_C (0x7137449123ef65cd ), +UINT64_C (0xb5c0fbcfec4d3b2f ),UINT64_C (0xe9b5dba58189dbbc ), +UINT64_C (0x3956c25bf348b538 ),UINT64_C (0x59f111f1b605d019 ), +UINT64_C (0x923f82a4af194f9b ),UINT64_C (0xab1c5ed5da6d8118 ), +UINT64_C (0xd807aa98a3030242 ),UINT64_C (0x12835b0145706fbe ), +UINT64_C (0x243185be4ee4b28c ),UINT64_C (0x550c7dc3d5ffb4e2 ), +UINT64_C (0x72be5d74f27b896f ),UINT64_C (0x80deb1fe3b1696b1 ), +UINT64_C (0x9bdc06a725c71235 ),UINT64_C (0xc19bf174cf692694 ), +UINT64_C (0xe49b69c19ef14ad2 ),UINT64_C (0xefbe4786384f25e3 ), +UINT64_C (0x0fc19dc68b8cd5b5 ),UINT64_C (0x240ca1cc77ac9c65 ), +UINT64_C (0x2de92c6f592b0275 ),UINT64_C (0x4a7484aa6ea6e483 ), +UINT64_C (0x5cb0a9dcbd41fbd4 ),UINT64_C (0x76f988da831153b5 ), +UINT64_C (0x983e5152ee66dfab ),UINT64_C (0xa831c66d2db43210 ), +UINT64_C (0xb00327c898fb213f ),UINT64_C (0xbf597fc7beef0ee4 ), +UINT64_C (0xc6e00bf33da88fc2 ),UINT64_C (0xd5a79147930aa725 ), +UINT64_C (0x06ca6351e003826f ),UINT64_C (0x142929670a0e6e70 ), +UINT64_C (0x27b70a8546d22ffc ),UINT64_C (0x2e1b21385c26c926 ), +UINT64_C (0x4d2c6dfc5ac42aed ),UINT64_C (0x53380d139d95b3df ), +UINT64_C (0x650a73548baf63de ),UINT64_C (0x766a0abb3c77b2a8 ), +UINT64_C (0x81c2c92e47edaee6 ),UINT64_C (0x92722c851482353b ), +UINT64_C (0xa2bfe8a14cf10364 ),UINT64_C (0xa81a664bbc423001 ), +UINT64_C (0xc24b8b70d0f89791 ),UINT64_C (0xc76c51a30654be30 ), +UINT64_C (0xd192e819d6ef5218 ),UINT64_C (0xd69906245565a910 ), +UINT64_C (0xf40e35855771202a ),UINT64_C (0x106aa07032bbd1b8 ), +UINT64_C (0x19a4c116b8d2d0c8 ),UINT64_C (0x1e376c085141ab53 ), +UINT64_C (0x2748774cdf8eeb99 ),UINT64_C (0x34b0bcb5e19b48a8 ), +UINT64_C (0x391c0cb3c5c95a63 ),UINT64_C (0x4ed8aa4ae3418acb ), +UINT64_C (0x5b9cca4f7763e373 ),UINT64_C (0x682e6ff3d6b2b8a3 ), +UINT64_C (0x748f82ee5defb2fc ),UINT64_C (0x78a5636f43172f60 ), +UINT64_C (0x84c87814a1f0ab72 ),UINT64_C (0x8cc702081a6439ec ), +UINT64_C (0x90befffa23631e28 ),UINT64_C (0xa4506cebde82bde9 ), +UINT64_C (0xbef9a3f7b2c67915 ),UINT64_C (0xc67178f2e372532b ), +UINT64_C (0xca273eceea26619c ),UINT64_C (0xd186b8c721c0c207 ), +UINT64_C (0xeada7dd6cde0eb1e ),UINT64_C (0xf57d4f7fee6ed178 ), +UINT64_C (0x06f067aa72176fba ),UINT64_C (0x0a637dc5a2c898a6 ), +UINT64_C (0x113f9804bef90dae ),UINT64_C (0x1b710b35131c471b ), +UINT64_C (0x28db77f523047d84 ),UINT64_C (0x32caab7b40c72493 ), +UINT64_C (0x3c9ebe0a15c9bebc ),UINT64_C (0x431d67c49c100d4c ), +UINT64_C (0x4cc5d4becb3e42b6 ),UINT64_C (0x597f299cfc657e2a ), +UINT64_C (0x5fcb6fab3ad6faec ),UINT64_C (0x6c44198c4a475817 ), +}; + +/* Compress 16 numeric message words into the supplied SHA-256 state. */ +static void sha256_compress (uint32_t state[8 ],const uint32_t block[16 ]) +{ +uint32_t a = state[0 ], b = state[1 ], c = state[2 ], d = state[3 ]; +uint32_t e = state[4 ], f = state[5 ], g = state[6 ], h = state[7 ]; +uint32_t w[64 ]; + +/* Expand the message into the 64-word round schedule. */ +memcpy (w, block,16 *sizeof (*w)); +for (int i =16 ; i <64 ; i++) { +uint32_t x = w[i -15 ], y = w[i -2 ]; +uint32_t s0 =ror32 (x,7 ) ^ror32 (x,18 ) ^ (x >>3 ); +uint32_t s1 =ror32 (y,17 ) ^ror32 (y,19 ) ^ (y >>10 ); + + w[i] = w[i -16 ] + s0 + w[i -7 ] + s1; + } + +/* Perform 64 rounds with arithmetic modulo 2^32. */ +for (int i =0 ; i <64 ; i++) { +uint32_t s0 =ror32 (a,2 ) ^ror32 (a,13 ) ^ror32 (a,22 ); +uint32_t s1 =ror32 (e,6 ) ^ror32 (e,11 ) ^ror32 (e,25 ); + +/* sha_ch/sha_maj are shared with SHA-512; narrow to 32 bits. */ +uint32_t t1 = h + s1 + (uint32_t )sha_ch (e, f, g) + + sha256_k[i] + w[i]; +uint32_t t2 = s0 + (uint32_t )sha_maj (a, b, c); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + +/* Add the working words to the original state. */ + state[0 ] += a; + state[1 ] += b; + state[2 ] += c; + state[3 ] += d; + state[4 ] += e; + state[5 ] += f; + state[6 ] += g; + state[7 ] += h; +} + +/* Compress 16 numeric message words into the supplied SHA-512 state. */ +static void sha512_compress (uint64_t state[8 ],const uint64_t block[16 ]) +{ +uint64_t a = state[0 ], b = state[1 ], c = state[2 ], d = state[3 ]; +uint64_t e = state[4 ], f = state[5 ], g = state[6 ], h = state[7 ]; +uint64_t w[80 ]; + +/* Expand the message into the 80-word round schedule. */ +memcpy (w, block,16 *sizeof (*w)); +for (int i =16 ; i <80 ; i++) { +uint64_t x = w[i -15 ], y = w[i -2 ]; +uint64_t s0 =ror64 (x,1 ) ^ror64 (x,8 ) ^ (x >>7 ); +uint64_t s1 =ror64 (y,19 ) ^ror64 (y,61 ) ^ (y >>6 ); + + w[i] = w[i -16 ] + s0 + w[i -7 ] + s1; + } + +/* Perform 80 rounds with arithmetic modulo 2^64. */ +for (int i =0 ; i <80 ; i++) { +uint64_t s0 =ror64 (a,28 ) ^ror64 (a,34 ) ^ror64 (a,39 ); +uint64_t s1 =ror64 (e,14 ) ^ror64 (e,18 ) ^ror64 (e,41 ); +uint64_t t1 = h + s1 +sha_ch (e, f, g) + sha512_k[i] + w[i]; +uint64_t t2 = s0 +sha_maj (a, b, c); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + +/* Add the working words to the original state. */ + state[0 ] += a; + state[1 ] += b; + state[2 ] += c; + state[3 ] += d; + state[4 ] += e; + state[5 ] += f; + state[6 ] += g; + state[7 ] += h; +} + +/* Compress one SHA-256 block from f8-f23 into f0-f7. */ +void helper_sha256 (CPUSPARCState *env) +{ +uint32_t state[8 ], block[16 ]; + +/* Each fpr[] entry holds two architectural 32-bit registers. */ +for (int i =0 ; i <4 ; i++) { + state[2 * i] = env->fpr[i].l.upper; + state[2 * i +1 ] = env->fpr[i].l.lower; + } +for (int i =0 ; i <8 ; i++) { + block[2 * i] = env->fpr[4 + i].l.upper; + block[2 * i +1 ] = env->fpr[4 + i].l.lower; + } + +sha256_compress (state, block); + +for (int i =0 ; i <4 ; i++) { + env->fpr[i].l.upper = state[2 * i]; + env->fpr[i].l.lower = state[2 * i +1 ]; + } +} + +/* +* Compress one SHA-512 block from FD16, FD18, ..., FD46 +* into FD0, FD2, ..., FD14. +*/ +void helper_sha512 (CPUSPARCState *env) +{ +uint64_t state[8 ], block[16 ]; + +/* Each fpr[] entry holds one architectural 64-bit register. */ +for (int i =0 ; i <8 ; i++) { + state[i] = env->fpr[i].ll; + } +for (int i =0 ; i <16 ; i++) { + block[i] = env->fpr[8 + i].ll; + } + +sha512_compress (state, block); + +for (int i =0 ; i <8 ; i++) { + env->fpr[i].ll = state[i]; + } +}
target/sparc/helper.h +4 -0
@@ -134,6 +134,10 @@ DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(crc32c, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_1(sha1, TCG_CALL_NO_RWG, void, env) +DEF_HELPER_FLAGS_1(sha256, TCG_CALL_NO_RWG, void, env) +DEF_HELPER_FLAGS_1(sha512, TCG_CALL_NO_RWG, void, env) +#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \
target/sparc/insns.decode +4 -0
@@ -575,9 +575,13 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU8 10 ..... 110110 ..... 1 0101 1101 ..... @d_d_d FPMAXU16 10 ..... 110110 ..... 1 0101 1110 ..... @d_d_d FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d + AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d CRC32C 10 ..... 110110 ..... 1 0100 0111 ..... @d_d_d + SHA1 10 00000 110110 00000 1 0100 0001 00000 + SHA256 10 00000 110110 00000 1 0100 0010 00000 + SHA512 10 00000 110110 00000 1 0100 0011 00000 FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \
target/sparc/translate.c +20 -1
@@ -112,6 +112,9 @@# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; })# define gen_helper_crc32c ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha1 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha256 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha512 ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0
@@ -3042,7 +3045,8 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (CFR_AES |CFR_CRC32C ); +return tcg_constant_tl (CFR_AES |CFR_CRC32C |CFR_SHA1 | +CFR_SHA256 |CFR_SHA512 ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
@@ -5422,6 +5426,18 @@ static bool do_dddd(DisasContext *dc, arg_r_r_r_r *a,return advance_pc (dc); } +/* SHA helpers update fixed registers in the lower FPR bank. */ +static bool do_sha (DisasContext *dc,void (*func)(TCGv_env )) +{ +if (gen_trap_ifnofpu (dc)) { +return true; + } + +func (tcg_env); +gen_update_fprs_dirty (dc,0 ); +return advance_pc (dc); +} + TRANS(PDIST, VIS1, do_dddd, a, gen_helper_pdist) TRANS(FMADDd, FMAF, do_dddd, a, gen_op_fmaddd)TRANS (FMSUBd,FMAF , do_dddd, a, gen_op_fmsubd)
@@ -5440,6 +5456,9 @@ TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) TRANS(AES_KEXPAND2, CRYPTO, do_ddd, a, gen_helper_aes_kexpand2) TRANS(CRC32C, CRYPTO, do_ddd, a, gen_helper_crc32c) +TRANS(SHA1, CRYPTO, do_sha, gen_helper_sha1) +TRANS(SHA256, CRYPTO, do_sha, gen_helper_sha256) +TRANS (SHA512 ,CRYPTO , do_sha, gen_helper_sha512) /* AES key expansion with RCON selected by the 5-bit immediate in imm */static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a)
Signature · 1 line
2.43.0
Re: [PATCH v3 6/8] target/sparc: Add the SHA instructions
Quoted 368 lines · Stian Halseth
Implement the SHA1, SHA256 and SHA512 instructions according to OSA (Oracle SPARC Architecture) 2011 section 7.115, using the algorithms defined in FIPS 180-4, and set the SHA1/SHA256/SHA512 bits in the CFR.
Read and update the fixed floating-point registers and mark the lower FPR bank dirty. Share the SHA-2 Boolean functions between the SHA256 and SHA512 helpers.
Gated behind the crypto CPU feature and disabled for sparc32.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- target/sparc/crypto_helper.c | 274 +++++++++++++++++++++++++++++++++++ target/sparc/helper.h | 4 + target/sparc/insns.decode | 4 + target/sparc/translate.c | 21 ++- 4 files changed, 302 insertions(+), 1 deletion(-)target/sparc/crypto_helper.c +274
-0@@ -12,6 +12,7 @@#include "crypto/aes.h" #include "qemu/bswap.h" #include "qemu/crc32c.h" +#include "cpu.h" /* * SPARC four-operand AES round (Section 7.3). @@ -166,3 +167,276 @@ uint64_t helper_crc32c(uint64_t rs1, uint64_t rs2)/* Result lives in the low 32 bits; upper half reads as zero. */ return (uint64_t )bswap32 (crc); } + +/* +* Compress one SHA-1 block from f8-f23 into f0-f4. +* Clear f5 to match SPARC M7 behavior. +*/ +void helper_sha1 (CPUSPARCState *env) +{ +uint32_t a = env->fpr[0 ].l.upper; +uint32_t b = env->fpr[0 ].l.lower; +uint32_t c = env->fpr[1 ].l.upper; +uint32_t d = env->fpr[1 ].l.lower; +uint32_t e = env->fpr[2 ].l.upper; +uint32_t w[80 ]; + +/* Load 16 message words; each fpr[] entry holds two words. */ +for (int i =0 ; i <8 ; i++) { + w[2 * i] = env->fpr[4 + i].l.upper; + w[2 * i +1 ] = env->fpr[4 + i].l.lower; + } + +/* Expand the message into the 80-word round schedule. */ +for (int i =16 ; i <80 ; i++) { + w[i] =rol32 (w[i -3 ] ^ w[i -8 ] ^ + w[i -14 ] ^ w[i -16 ],1 ); + } + +/* Each group of 20 rounds uses its own function and constant. */ +for (int i =0 ; i <80 ; i++) { +uint32_t f, k, t; + +if (i <20 ) { + f = (b & c) | (~b & d);/* Choose */ + k =0x5a827999 ; + }else if (i <40 ) { + f = b ^ c ^ d;/* Parity */ + k =0x6ed9eba1 ; + }else if (i <60 ) { + f = (b & c) | (b & d) | (c & d);/* Majority */ + k =0x8f1bbcdc ; + }else { + f = b ^ c ^ d;/* Parity */ + k =0xca62c1d6 ; + } + +/* Advance the five working words, with arithmetic modulo 2^32. */ + t =rol32 (a,5 ) + f + e + k + w[i]; + e = d; + d = c; + c =rol32 (b,30 ); + b = a; + a = t; + } + +/* Accumulate the hash in f0-f4 and clear the unused f5 word. */ + env->fpr[0 ].l.upper += a; + env->fpr[0 ].l.lower += b; + env->fpr[1 ].l.upper += c; + env->fpr[1 ].l.lower += d; + env->fpr[2 ].l.upper += e; + env->fpr[2 ].l.lower =0 ; +} + +/* SHA-2 Boolean functions. */ +static inline uint64_t sha_ch (uint64_t x,uint64_t y,uint64_t z) +{ +return (x & y) ^ (~x & z); +} + +static inline uint64_t sha_maj (uint64_t x,uint64_t y,uint64_t z) +{ +return (x & y) ^ (x & z) ^ (y & z); +} + +static const uint32_t sha256_k[64 ] = { +0x428a2f98 ,0x71374491 ,0xb5c0fbcf ,0xe9b5dba5 , +0x3956c25b ,0x59f111f1 ,0x923f82a4 ,0xab1c5ed5 , +0xd807aa98 ,0x12835b01 ,0x243185be ,0x550c7dc3 , +0x72be5d74 ,0x80deb1fe ,0x9bdc06a7 ,0xc19bf174 , +0xe49b69c1 ,0xefbe4786 ,0x0fc19dc6 ,0x240ca1cc , +0x2de92c6f ,0x4a7484aa ,0x5cb0a9dc ,0x76f988da , +0x983e5152 ,0xa831c66d ,0xb00327c8 ,0xbf597fc7 , +0xc6e00bf3 ,0xd5a79147 ,0x06ca6351 ,0x14292967 , +0x27b70a85 ,0x2e1b2138 ,0x4d2c6dfc ,0x53380d13 , +0x650a7354 ,0x766a0abb ,0x81c2c92e ,0x92722c85 , +0xa2bfe8a1 ,0xa81a664b ,0xc24b8b70 ,0xc76c51a3 , +0xd192e819 ,0xd6990624 ,0xf40e3585 ,0x106aa070 , +0x19a4c116 ,0x1e376c08 ,0x2748774c ,0x34b0bcb5 , +0x391c0cb3 ,0x4ed8aa4a ,0x5b9cca4f ,0x682e6ff3 , +0x748f82ee ,0x78a5636f ,0x84c87814 ,0x8cc70208 , +0x90befffa ,0xa4506ceb ,0xbef9a3f7 ,0xc67178f2 , +}; + +static const uint64_t sha512_k[80 ] = { +UINT64_C (0x428a2f98d728ae22 ),UINT64_C (0x7137449123ef65cd ), +UINT64_C (0xb5c0fbcfec4d3b2f ),UINT64_C (0xe9b5dba58189dbbc ), +UINT64_C (0x3956c25bf348b538 ),UINT64_C (0x59f111f1b605d019 ), +UINT64_C (0x923f82a4af194f9b ),UINT64_C (0xab1c5ed5da6d8118 ), +UINT64_C (0xd807aa98a3030242 ),UINT64_C (0x12835b0145706fbe ), +UINT64_C (0x243185be4ee4b28c ),UINT64_C (0x550c7dc3d5ffb4e2 ), +UINT64_C (0x72be5d74f27b896f ),UINT64_C (0x80deb1fe3b1696b1 ), +UINT64_C (0x9bdc06a725c71235 ),UINT64_C (0xc19bf174cf692694 ), +UINT64_C (0xe49b69c19ef14ad2 ),UINT64_C (0xefbe4786384f25e3 ), +UINT64_C (0x0fc19dc68b8cd5b5 ),UINT64_C (0x240ca1cc77ac9c65 ), +UINT64_C (0x2de92c6f592b0275 ),UINT64_C (0x4a7484aa6ea6e483 ), +UINT64_C (0x5cb0a9dcbd41fbd4 ),UINT64_C (0x76f988da831153b5 ), +UINT64_C (0x983e5152ee66dfab ),UINT64_C (0xa831c66d2db43210 ), +UINT64_C (0xb00327c898fb213f ),UINT64_C (0xbf597fc7beef0ee4 ), +UINT64_C (0xc6e00bf33da88fc2 ),UINT64_C (0xd5a79147930aa725 ), +UINT64_C (0x06ca6351e003826f ),UINT64_C (0x142929670a0e6e70 ), +UINT64_C (0x27b70a8546d22ffc ),UINT64_C (0x2e1b21385c26c926 ), +UINT64_C (0x4d2c6dfc5ac42aed ),UINT64_C (0x53380d139d95b3df ), +UINT64_C (0x650a73548baf63de ),UINT64_C (0x766a0abb3c77b2a8 ), +UINT64_C (0x81c2c92e47edaee6 ),UINT64_C (0x92722c851482353b ), +UINT64_C (0xa2bfe8a14cf10364 ),UINT64_C (0xa81a664bbc423001 ), +UINT64_C (0xc24b8b70d0f89791 ),UINT64_C (0xc76c51a30654be30 ), +UINT64_C (0xd192e819d6ef5218 ),UINT64_C (0xd69906245565a910 ), +UINT64_C (0xf40e35855771202a ),UINT64_C (0x106aa07032bbd1b8 ), +UINT64_C (0x19a4c116b8d2d0c8 ),UINT64_C (0x1e376c085141ab53 ), +UINT64_C (0x2748774cdf8eeb99 ),UINT64_C (0x34b0bcb5e19b48a8 ), +UINT64_C (0x391c0cb3c5c95a63 ),UINT64_C (0x4ed8aa4ae3418acb ), +UINT64_C (0x5b9cca4f7763e373 ),UINT64_C (0x682e6ff3d6b2b8a3 ), +UINT64_C (0x748f82ee5defb2fc ),UINT64_C (0x78a5636f43172f60 ), +UINT64_C (0x84c87814a1f0ab72 ),UINT64_C (0x8cc702081a6439ec ), +UINT64_C (0x90befffa23631e28 ),UINT64_C (0xa4506cebde82bde9 ), +UINT64_C (0xbef9a3f7b2c67915 ),UINT64_C (0xc67178f2e372532b ), +UINT64_C (0xca273eceea26619c ),UINT64_C (0xd186b8c721c0c207 ), +UINT64_C (0xeada7dd6cde0eb1e ),UINT64_C (0xf57d4f7fee6ed178 ), +UINT64_C (0x06f067aa72176fba ),UINT64_C (0x0a637dc5a2c898a6 ), +UINT64_C (0x113f9804bef90dae ),UINT64_C (0x1b710b35131c471b ), +UINT64_C (0x28db77f523047d84 ),UINT64_C (0x32caab7b40c72493 ), +UINT64_C (0x3c9ebe0a15c9bebc ),UINT64_C (0x431d67c49c100d4c ), +UINT64_C (0x4cc5d4becb3e42b6 ),UINT64_C (0x597f299cfc657e2a ), +UINT64_C (0x5fcb6fab3ad6faec ),UINT64_C (0x6c44198c4a475817 ), +}; + +/* Compress 16 numeric message words into the supplied SHA-256 state. */ +static void sha256_compress (uint32_t state[8 ],const uint32_t block[16 ]) +{ +uint32_t a = state[0 ], b = state[1 ], c = state[2 ], d = state[3 ]; +uint32_t e = state[4 ], f = state[5 ], g = state[6 ], h = state[7 ]; +uint32_t w[64 ]; + +/* Expand the message into the 64-word round schedule. */ +memcpy (w, block,16 *sizeof (*w)); +for (int i =16 ; i <64 ; i++) { +uint32_t x = w[i -15 ], y = w[i -2 ]; +uint32_t s0 =ror32 (x,7 ) ^ror32 (x,18 ) ^ (x >>3 ); +uint32_t s1 =ror32 (y,17 ) ^ror32 (y,19 ) ^ (y >>10 ); + + w[i] = w[i -16 ] + s0 + w[i -7 ] + s1; + } + +/* Perform 64 rounds with arithmetic modulo 2^32. */ +for (int i =0 ; i <64 ; i++) { +uint32_t s0 =ror32 (a,2 ) ^ror32 (a,13 ) ^ror32 (a,22 ); +uint32_t s1 =ror32 (e,6 ) ^ror32 (e,11 ) ^ror32 (e,25 ); + +/* sha_ch/sha_maj are shared with SHA-512; narrow to 32 bits. */ +uint32_t t1 = h + s1 + (uint32_t )sha_ch (e, f, g) + + sha256_k[i] + w[i]; +uint32_t t2 = s0 + (uint32_t )sha_maj (a, b, c); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + +/* Add the working words to the original state. */ + state[0 ] += a; + state[1 ] += b; + state[2 ] += c; + state[3 ] += d; + state[4 ] += e; + state[5 ] += f; + state[6 ] += g; + state[7 ] += h; +} + +/* Compress 16 numeric message words into the supplied SHA-512 state. */ +static void sha512_compress (uint64_t state[8 ],const uint64_t block[16 ]) +{ +uint64_t a = state[0 ], b = state[1 ], c = state[2 ], d = state[3 ]; +uint64_t e = state[4 ], f = state[5 ], g = state[6 ], h = state[7 ]; +uint64_t w[80 ]; + +/* Expand the message into the 80-word round schedule. */ +memcpy (w, block,16 *sizeof (*w)); +for (int i =16 ; i <80 ; i++) { +uint64_t x = w[i -15 ], y = w[i -2 ]; +uint64_t s0 =ror64 (x,1 ) ^ror64 (x,8 ) ^ (x >>7 ); +uint64_t s1 =ror64 (y,19 ) ^ror64 (y,61 ) ^ (y >>6 ); + + w[i] = w[i -16 ] + s0 + w[i -7 ] + s1; + } + +/* Perform 80 rounds with arithmetic modulo 2^64. */ +for (int i =0 ; i <80 ; i++) { +uint64_t s0 =ror64 (a,28 ) ^ror64 (a,34 ) ^ror64 (a,39 ); +uint64_t s1 =ror64 (e,14 ) ^ror64 (e,18 ) ^ror64 (e,41 ); +uint64_t t1 = h + s1 +sha_ch (e, f, g) + sha512_k[i] + w[i]; +uint64_t t2 = s0 +sha_maj (a, b, c); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + +/* Add the working words to the original state. */ + state[0 ] += a; + state[1 ] += b; + state[2 ] += c; + state[3 ] += d; + state[4 ] += e; + state[5 ] += f; + state[6 ] += g; + state[7 ] += h; +} + +/* Compress one SHA-256 block from f8-f23 into f0-f7. */ +void helper_sha256 (CPUSPARCState *env) +{ +uint32_t state[8 ], block[16 ]; + +/* Each fpr[] entry holds two architectural 32-bit registers. */ +for (int i =0 ; i <4 ; i++) { + state[2 * i] = env->fpr[i].l.upper; + state[2 * i +1 ] = env->fpr[i].l.lower; + } +for (int i =0 ; i <8 ; i++) { + block[2 * i] = env->fpr[4 + i].l.upper; + block[2 * i +1 ] = env->fpr[4 + i].l.lower; + } + +sha256_compress (state, block); + +for (int i =0 ; i <4 ; i++) { + env->fpr[i].l.upper = state[2 * i]; + env->fpr[i].l.lower = state[2 * i +1 ]; + } +} + +/* +* Compress one SHA-512 block from FD16, FD18, ..., FD46 +* into FD0, FD2, ..., FD14. +*/ +void helper_sha512 (CPUSPARCState *env) +{ +uint64_t state[8 ], block[16 ]; + +/* Each fpr[] entry holds one architectural 64-bit register. */ +for (int i =0 ; i <8 ; i++) { + state[i] = env->fpr[i].ll; + } +for (int i =0 ; i <16 ; i++) { + block[i] = env->fpr[8 + i].ll; + } + +sha512_compress (state, block); + +for (int i =0 ; i <8 ; i++) { + env->fpr[i].ll = state[i]; + } +}target/sparc/helper.h +4
-0@@ -134,6 +134,10 @@ DEF_HELPER_FLAGS_2(aes_kexpand0, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_3(aes_kexpand1, TCG_CALL_NO_RWG_SE, i64, i64, i64, i32) DEF_HELPER_FLAGS_2(aes_kexpand2, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(crc32c, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_1(sha1, TCG_CALL_NO_RWG, void, env) +DEF_HELPER_FLAGS_1(sha256, TCG_CALL_NO_RWG, void, env) +DEF_HELPER_FLAGS_1(sha512, TCG_CALL_NO_RWG, void, env) +#define VIS_CMPHELPER (name) \ DEF_HELPER_FLAGS_2(f##name##8, TCG_CALL_NO_RWG_SE, \ i64, i64, i64) \target/sparc/insns.decode +4
-0@@ -575,9 +575,13 @@ FCMPEq 10 000 cc:2 110101 ..... 0 0101 0111 ..... \ FPMAXU8 10 ..... 110110 ..... 1 0101 1101 ..... @d_d_d FPMAXU16 10 ..... 110110 ..... 1 0101 1110 ..... @d_d_d FPMAXU32 10 ..... 110110 ..... 1 0101 1111 ..... @d_d_d + AES_KEXPAND0 10 ..... 110110 ..... 1 0011 0000 ..... @d_d_d AES_KEXPAND2 10 ..... 110110 ..... 1 0011 0001 ..... @d_d_d CRC32C 10 ..... 110110 ..... 1 0100 0111 ..... @d_d_d + SHA1 10 00000 110110 00000 1 0100 0001 00000 + SHA256 10 00000 110110 00000 1 0100 0010 00000 + SHA512 10 00000 110110 00000 1 0100 0011 00000 FLCMPs 10 000 cc:2 110110 rs1:5 1 0101 0001 rs2:5 FLCMPd 10 000 cc:2 110110 ..... 1 0101 0010 ..... \target/sparc/translate.c +5
-1@@ -112,6 +112,9 @@# define gen_helper_aes_kexpand0 ({ qemu_build_not_reached(); NULL; })# define gen_helper_aes_kexpand2 ({ qemu_build_not_reached(); NULL; })# define gen_helper_crc32c ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha1 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha256 ({ qemu_build_not_reached(); NULL; }) +# define gen_helper_sha512 ({ qemu_build_not_reached(); NULL; })#endif #define DISAS_EXIT DISAS_TARGET_0@@ -3042,7 +3045,8 @@ TRANS(RDSTRAND_STATUS, HYPV, do_rd_special, true, a->rd, do_rdstrand_status)static TCGv do_rdcfr (DisasContext *dc,TCGv dst) { -return tcg_constant_tl (CFR_AES |CFR_CRC32C ); +return tcg_constant_tl (CFR_AES |CFR_CRC32C |CFR_SHA1 | +CFR_SHA256 |CFR_SHA512 ); }TRANS (RDCFR ,CRYPTO , do_rd_special, true, a->rd, do_rdcfr)
target/sparc/translate.c static bool do_dddd(DisasContext *dc, arg_r_r_r_r *a, · line 5434
Quoted 9 lines
target/sparc/translate.c +6
-0@@ -5422,6 +5426,18 @@ static bool do_dddd(DisasContext *dc, arg_r_r_r_r *a,return advance_pc (dc); } +/* SHA helpers update fixed registers in the lower FPR bank. */ +static bool do_sha (DisasContext *dc,void (*func)(TCGv_env )) +{ +if (gen_trap_ifnofpu (dc)) { +return true; + }
Same question here about presence of FPU.
Quoted 9 lines
target/sparc/translate.c +5
-0+func (tcg_env); +gen_update_fprs_dirty (dc,0 ); +return advance_pc (dc); +} + TRANS(PDIST, VIS1, do_dddd, a, gen_helper_pdist) TRANS(FMADDd, FMAF, do_dddd, a, gen_op_fmaddd)TRANS (FMSUBd,FMAF , do_dddd, a, gen_op_fmsubd)
target/sparc/translate.c TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) · line 5464
Quoted 9 lines
target/sparc/translate.c +3
-0@@ -5440,6 +5456,9 @@ TRANS(AES_DROUND23_L, CRYPTO, do_dddd, a, gen_helper_aes_dround23_l) TRANS(AES_KEXPAND0, CRYPTO, do_ddd, a, gen_helper_aes_kexpand0) TRANS(AES_KEXPAND2, CRYPTO, do_ddd, a, gen_helper_aes_kexpand2) TRANS(CRC32C, CRYPTO, do_ddd, a, gen_helper_crc32c) +TRANS(SHA1, CRYPTO, do_sha, gen_helper_sha1) +TRANS(SHA256, CRYPTO, do_sha, gen_helper_sha256) +TRANS (SHA512 ,CRYPTO , do_sha, gen_helper_sha512) /* AES key expansion with RCON selected by the 5-bit immediate in imm */static bool trans_AES_KEXPAND1 (DisasContext *dc,arg_r_r_i_r *a)
Otherwise:
ATB,
Mark.
Re: [PATCH v3 6/8] target/sparc: Add the SHA instructions
Hi Mark,
Quoted 10 lines · Mark Cave-Ayland
target/sparc/translate.c static bool do_dddd(DisasContext *dc, arg_r_r_r_r *a, · line 5434
Quoted 7 lines
target/sparc/translate.c +6
-0+/* SHA helpers update fixed registers in the lower FPR bank. */ +static bool do_sha (DisasContext *dc,void (*func)(TCGv_env )) +{ + if (gen_trap_ifnofpu(dc)) { + return true; + }Same question here about presence of FPU.
Awaiting your reply to the same question in patch 5.
Thanks for taking the time to review my patches!
/Stian
PATCH v3 7/8 target/sparc: Allow block-init stores when the CPU has the crypto feature
In reply to Stian HalsethThe next patch reports HWCAP_SPARC_CRYPTO to linux-user guests. A multiarch glibc then picks its niagara4 memcpy/memset, which store with ASI 0xe2. QEMU only allows that store on CPUs with HYPV, so every dynamically linked program dies with SIGILL.
The crypto opcodes exist only on T4 and later, and all of those have the block-init ASIs.
Reproduced with Gentoo sparc64 stage3 sysroot:
qemu-sparc64 -L <stage3 root> -cpu \
TI-UltraSparc-IIi,+crypto <stage3 root>/usr/bin/dd --version
--- target/sparc/translate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
target/sparc/translate.c +6 -2
@@ -1863,8 +1863,12 @@ static void gen_st_asi(DisasContext *dc, DisasASI *da, TCGv src, TCGv addr)if (TARGET_LONG_BITS ==32 ) {gen_exception (dc,TT_ILL_INSN );break ; - }else if (!(dc->def->features &CPU_FEATURE_HYPV )) { -/* Pre OpenSPARC CPUs don't have these */ + }else if (!(dc->def->features & + (CPU_FEATURE_HYPV |CPU_FEATURE_CRYPTO ))) { +/* +* Pre-OpenSPARC CPUs lack block-init stores. Crypto instructions +* imply T4 or later, which supports them. +*/ gen_exception (dc,TT_ILL_INSN );break ; }
Signature · 1 line
2.43.0
Re: [PATCH v3 7/8] target/sparc: Allow block-init stores when the CPU has the crypto feature
Quoted 26 lines · Stian Halseth
The next patch reports
HWCAP_SPARC_CRYPTOto linux-user guests. A multiarch glibc then picks its niagara4 memcpy/memset, which store with ASI0xe2. QEMU only allows that store on CPUs with HYPV, so every dynamically linked program dies with SIGILL.The crypto opcodes exist only on T4 and later, and all of those have the block-init ASIs.
Reproduced with Gentoo sparc64 stage3 sysroot:
qemu-sparc64 -L <stage3 root> -cpu \
TI-UltraSparc-IIi,+crypto <stage3 root>/usr/bin/dd --versionSigned-off-by Stian Halseth <[email protected]>--- target/sparc/translate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)target/sparc/translate.c +0
-1@@ -1863,8 +1863,12 @@ static void gen_st_asi(DisasContext *dc, DisasASI *da, TCGv src, TCGv addr)if (TARGET_LONG_BITS ==32 ) {gen_exception (dc,TT_ILL_INSN );break ; - }else if (!(dc->def->features &CPU_FEATURE_HYPV )) {
target/sparc/translate.c static void gen_st_asi(DisasContext *dc, DisasASI *da, TCGv src, TCGv addr) · line 1874
Quoted 10 lines
target/sparc/translate.c +6
-1-/* Pre OpenSPARC CPUs don't have these */ + }else if (!(dc->def->features & + (CPU_FEATURE_HYPV |CPU_FEATURE_CRYPTO ))) { +/* +* Pre-OpenSPARC CPUs lack block-init stores. Crypto instructions +* imply T4 or later, which supports them. +*/ gen_exception (dc,TT_ILL_INSN );break ; }
Hmmm. Feels like we're getting a bit blurred here between the crypto feature and other sun4v features, however I think we're just going to have to go with this for now. Presumably no other interesting ASIs have showed up in your tests so far?
ATB,
Mark.
Re: [PATCH v3 7/8] target/sparc: Allow block-init stores when the CPU has the crypto feature
Hi Mark,
Quoted 5 lines · Mark Cave-Ayland
Hmmm. Feels like we're getting a bit blurred here between the crypto feature and other sun4v features, however I think we're just going to have to go with this for now.
Yes. The approach was discussed on the list. As Richard mentioned, ideally the sun4v platform could be used, but since it's incomplete, it would require a lot of work.
Quoted 3 lines
Presumably no other interesting ASIs have showed up in your tests so far?
Thanks for the review, but your question made me think about openssl, and I found a real problem that I didn't see before. Please don't apply 7/8 and 8/8.
On real hardware, a store to ASI 0xe2 at an aligned address also zeroes the rest of the block. I measured it: the T4 zeroes the 32-byte block, the M7 the whole 64-byte line. Other offsets are plain stores.
glibc's niagara4 memset depends on this. For zeroing it only stores two of every eight words and lets the hardware clear the rest. QEMU does a plain 8-byte store, so with 7/8 and 8/8 a memset(p, 0, n) of more than about 120 bytes leaves stale data behind. OpenSSL from a Gentoo stage3 crashes at startup because of it. So 7/8 only turns the SIGILL into silent corruption.
And then I found a second gap..!
QEMU rejects the FP form (stda %f, 0xe2), which OpenSSL's T4 AES code uses for large buffers. OpenSSL probes the CFR itself rather than AT_HWCAP, so with only 1-6 and -cpu ...,+vis3,+crypto, AES-CBC/CTR on large inputs gets SIGILL. SHA and the other instructions work fine with OpenSSL.
My suggestion: 1-6 can go in as Philippe proposed, since the feature is opt-in. I'll withdraw 7/8 and 8/8 and send a separate series that models block-init stores properly (integer and FP, zero-fill as on the T4), with the hwcap patch on top. I have a memset/memcpy test that passes natively on the T4 and catches this, so that series will come with better testing.
Sorry for the churn, and thanks for asking the right question.
/Stian
Re: [PATCH v3 7/8] target/sparc: Allow block-init stores when the CPU has the crypto feature
Quoted 46 lines · Stian Halseth
Hi Mark,
Quoted 5 lines · Mark Cave-Ayland
Hmmm. Feels like we're getting a bit blurred here between the crypto feature and other sun4v features, however I think we're just going to have to go with this for now.
Yes. The approach was discussed on the list. As Richard mentioned, ideally the sun4v platform could be used, but since it's incomplete, it would require a lot of work.
Quoted 3 lines
Presumably no other interesting ASIs have showed up in your tests so far?
Thanks for the review, but your question made me think about openssl, and I found a real problem that I didn't see before. Please don't apply 7/8 and 8/8.
On real hardware, a store to ASI
0xe2at an aligned address also zeroes the rest of the block. I measured it: the T4 zeroes the 32-byte block, the M7 the whole 64-byte line. Other offsets are plain stores.glibc's niagara4 memset depends on this. For zeroing it only stores two of every eight words and lets the hardware clear the rest. QEMU does a plain 8-byte store, so with 7/8 and 8/8 a
memset(p, 0, n)of more than about 120 bytes leaves stale data behind. OpenSSL from a Gentoo stage3 crashes at startup because of it. So 7/8 only turns the SIGILL into silent corruption.And then I found a second gap..!
QEMU rejects the FP form (stda %f,0xe2), which OpenSSL's T4 AES code uses for large buffers. OpenSSL probes the CFR itself rather thanAT_HWCAP, so with only 1-6 and -cpu ...,+vis3,+crypto, AES-CBC/CTR on large inputs gets SIGILL. SHA and the other instructions work fine with OpenSSL.My suggestion: 1-6 can go in as Philippe proposed, since the feature is opt-in. I'll withdraw 7/8 and 8/8 and send a separate series that models block-init stores properly (integer and FP, zero-fill as on the T4), with the hwcap patch on top. I have a memset/memcpy test that passes natively on the T4 and catches this, so that series will come with better testing.
Quoted 3 lines
Sorry for the churn, and thanks for asking the right question.
/Stian
Ack. I'll let this settle overnight as it is getting late here, and aim to send a PR for patches 1-6 tomorrow evening.
ATB,
Mark.
Re: [PATCH v3 7/8] target/sparc: Allow block-init stores when the CPU has the crypto feature
Hi Mark,
Quoted 42 lines · Mark Cave-Ayland
Quoted 42 lines
Thanks for the review, but your question made me think about openssl,
and I found a real problem that I didn't see before. Please don't apply
7/8 and 8/8.On real hardware, a store to ASI
0xe2at an aligned address also zeroes
the rest of the block. I measured it: the T4 zeroes the 32-byte block,
the M7 the whole 64-byte line. Other offsets are plain stores.glibc's niagara4 memset depends on this. For zeroing it only stores two of every eight words and lets the hardware clear the rest. QEMU does a plain 8-byte store, so with 7/8 and 8/8 a
memset(p, 0, n)of more than about 120 bytes leaves stale data behind. OpenSSL from a Gentoo stage3 crashes at startup because of it. So 7/8 only turns the SIGILL into silent corruption.And then I found a second gap..!
QEMU rejects the FP form (stda %f,0xe2), which OpenSSL's T4 AES code
uses for large buffers. OpenSSL probes the CFR itself rather thanAT_HWCAP, so with only 1-6 and -cpu ...,+vis3,+crypto, AES-CBC/CTR on
large inputs gets SIGILL. SHA and the other instructions work fine with
OpenSSL.My suggestion: 1-6 can go in as Philippe proposed, since the feature
is opt-in. I'll withdraw 7/8 and 8/8 and send a separate series that
models block-init stores properly (integer and FP, zero-fill as on the
T4), with the hwcap patch on top. I have a memset/memcpy test that passes natively on the T4 and catches this, so that series will come
with better testing.
Quoted 7 lines
Quoted 3 lines
Sorry for the churn, and thanks for asking the right question.
/Stian
Ack. I'll let this settle overnight as it is getting late here, and aim to send a PR for patches 1-6 tomorrow evening.
First of all, thanks for the quick turnaround and friendly attitude towards new contributors. It's really motivating.
Was getting late here as well, but fell into a rabbit hole while researching this. AKA long night.
Patches 1-6 should be good to send, thanks. As there aren't any other changes, feel free to fix the indentation in patch 4 before sending.
For this problem introduced with patch 7 and 8, I tried the most obvious fix, to make the block-init stores behave like a real T4's (essentially zero the blocks in the same way). It worked, but sporadically crashed under my testing. More often with +crypto than without. Checked vs an upstream baseline and determined this was pre- existing.
The root cause for the instability is a bug that surfaced more often with +crypto, but isn't caused by it.
Traced it to a TCG bug most likely introduced by commit ebf7a5d294a3 (Sept 2025). I can reproduce it deterministically without the series, and I will follow that in a separate thread.
So two things need to happen for this series to fully work as intended:
1. A translate.c patch modelling block-init stores. Can send as a follow up if 1-6 goes in. I'm currently testing and validating this thoroughly, and then I would like to send it, so we have a fully working implementation in flight. Not just code that cannot be used.
2. Patch for upstream bug (most likely from ebf7a5d294a3) Separate discussion, outside of this series.
I'm still in the validation phase, not trusting my current fixes yet, but hoping to have something concrete to show shortly.
/Stian
PATCH v3 8/8 linux-user/sparc: Report HWCAP_SPARC_CRYPTO for crypto CPU feature
In reply to Stian HalsethSet HWCAP_SPARC_CRYPTO in AT_HWCAP like Linux does on real hardware that supports it. qemu-sparc64 defaults to "TI UltraSparc II", which has no crypto feature, so it must be enabled explicitly, e.g. -cpu TI-UltraSparc-II,+crypto
--- linux-user/sparc/elfload.c | 1 + 1 file changed, 1 insertion(+)
linux-user/sparc/elfload.c +1 -0
@@ -90,6 +90,7 @@ abi_ulong get_elf_hwcap(CPUState *cs) r |= features &CPU_FEATURE_FMAF ?HWCAP_SPARC_FMAF :0 ; r |= features &CPU_FEATURE_VIS3 ?HWCAP_SPARC_VIS3 :0 ; r |= features &CPU_FEATURE_IMA ?HWCAP_SPARC_IMA :0 ; + r |= features &CPU_FEATURE_CRYPTO ?HWCAP_SPARC_CRYPTO :0 ;#endif return r;
Signature · 1 line
2.43.0
Re: [PATCH v3 8/8] linux-user/sparc: Report HWCAP_SPARC_CRYPTO for crypto CPU feature
Quoted 16 lines · Stian Halseth
Set
HWCAP_SPARC_CRYPTOinAT_HWCAPlike Linux does on real hardware that supports it. qemu-sparc64 defaults to "TI UltraSparc II", which has no crypto feature, so it must be enabled explicitly, e.g. -cpu TI-UltraSparc-II,+cryptoReviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Stian Halseth <[email protected]>--- linux-user/sparc/elfload.c | 1 + 1 file changed, 1 insertion(+)linux-user/sparc/elfload.c +0
-0
linux-user/sparc/elfload.c abi_ulong get_elf_hwcap(CPUState *cs) · line 96
Quoted 7 lines
linux-user/sparc/elfload.c +1
-0@@ -90,6 +90,7 @@ abi_ulong get_elf_hwcap(CPUState *cs) r |= features &CPU_FEATURE_FMAF ?HWCAP_SPARC_FMAF :0 ; r |= features &CPU_FEATURE_VIS3 ?HWCAP_SPARC_VIS3 :0 ; r |= features &CPU_FEATURE_IMA ?HWCAP_SPARC_IMA :0 ; + r |= features &CPU_FEATURE_CRYPTO ?HWCAP_SPARC_CRYPTO :0 ;#endif return r;
ATB,
Mark.
Quoted 17 lines · Stian Halseth
This series implements SPARC crypto instructions for QEMU. Most other arches already support crypto instructions, but SPARC is lagging behind.
Changes in v2:
- 1/7: use suggested commit message (Philippe); R-b Philippe
- 2/7: start with an empty CFR. Bits set in each instruction patch
- 3/7: byte-order comment, rk half zeroed explicitly; T-b Philippe
- 4/7: sets CFR.aes; "3-operand"/"4-operand" wording.
- 5/7: sets CFR.crc32c; "3-operand" wording; explicit 64-bit return; R-b + T-b Philippe
- 6/7: sets the SHA bits; comment on the narrowing; R-b + T-b Philippe
- 7/7: -cpu TI-UltraSparc-II,+crypto mentioned; R-b Philippe
- Tests: Follow-up, as agreed (Philippe)
Quoted 7 lines
Stian Halseth (8):
target/sparc: Add crypto feature
target/sparc: Return CFR when crypto feature is enabled target/sparc: Add the eight AES round instructions target/sparc: Add the AES key expansion instructions target/sparc: Add the CRC32C instruction target/sparc: Add the SHA instructions
Mark, Artyom, if you Ack I can queue the first reviewed 6 patches via my mist-target tree. I'll let the rest which is unreviewed go via your or Helge's tree.
Quoted 3 lines
target/sparc: Allow block-init stores when the CPU has the crypto
feature
linux-user/sparc: ReportHWCAP_SPARC_CRYPTOfor crypto CPU feature
Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Stian Halseth
This series implements SPARC crypto instructions for QEMU. Most other arches already support crypto instructions, but SPARC is lagging behind.
Changes in v2:
- 1/7: use suggested commit message (Philippe); R-b Philippe
- 2/7: start with an empty CFR. Bits set in each instruction patch
- 3/7: byte-order comment, rk half zeroed explicitly; T-b Philippe
- 4/7: sets CFR.aes; "3-operand"/"4-operand" wording.
- 5/7: sets CFR.crc32c; "3-operand" wording; explicit 64-bit return; R-b + T-b Philippe
- 6/7: sets the SHA bits; comment on the narrowing; R-b + T-b Philippe
- 7/7: -cpu TI-UltraSparc-II,+crypto mentioned; R-b Philippe
- Tests: Follow-up, as agreed (Philippe)Quoted 7 lines
Stian Halseth (8):
target/sparc: Add crypto feature
target/sparc: Return CFR when crypto feature is enabled target/sparc: Add the eight AES round instructions target/sparc: Add the AES key expansion instructions target/sparc: Add the CRC32C instruction target/sparc: Add the SHA instructions
Quoted 3 lines
Mark, Artyom, if you Ack I can queue the first reviewed 6 patches via my mist-target tree. I'll let the rest which is unreviewed go via your or Helge's tree.
I think the series is basically good: I can test it tomorrow evening and send a PR if it passes a few local tests and CI.
Stian: if the only outstanding issue is the indentation, I can fix that when I merge it into my qemu-sparc branch.
ATB,
Mark.