target/sparc: Block-init stores for the crypto feature
cover target/sparc: Block-init stores for the crypto feature
This completes the SPARC crypto series. It goes on top of patches 1-6 of v3 and replaces the withdrawn 7/8 and 8/8.
With the crypto feature, guests treat the CPU as a T4 or later. On those CPUs a block-initializing store (ASI 0xe2 and the other twin ASIs) to the first doubleword of a 32-byte block also zeroes the rest of the block. glibc's niagara4 memset and Linux's NG4memset depend on it, and OpenSSL's T4 AES code uses the FP form (stdfa). Patch 1 models this for both forms; patch 2 then reports HWCAP_SPARC_CRYPTO in linux-user, unchanged from v3 8/8.
Testing:
- Zero-fill behaviour measured on a SPARC T4 (stxa and stdfa, cached and uncached); QEMU now produces the same result.
- glibc memcpy/mempcpy/memset checked against byte loops over many sizes and alignments, with a Gentoo stage3 sysroot (multiarch glibc, niagara4 routines): passes, and fails without patch 1.
- Real userspace from that stage3 under qemu-sparc64 with and without +crypto, output identical: OpenSSL (AES CBC/CTR/ECB/CFB/OFB, digests, HMAC, PBKDF2, RSA/EC, speed), Python, perl, coreutils, gzip/xz/bzip2, tar, sed/awk.
- Instruction vectors still match M7 silicon, also on a big-endian host; Linux crypto KUnit 208/0.
While testing I hit a separate upstream TCG crash in qemu-user, not caused by this series. Testing these patches with real programs under qemu-user is more reliable with that fix applied:
https://lore.kernel.org/qemu-devel/[email protected]/
Based-on: <[email protected]>
Stian Halseth (2):
target/sparc: Model block-initializing stores linux-user/sparc: Report HWCAP_SPARC_CRYPTO for crypto CPU feature
linux-user/sparc/elfload.c | 1 + target/sparc/translate.c | 42 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 target/sparc: Model block-initializing stores
On T4 and later, a block-initializing store to the first doubleword of a 32-byte block zeroes the rest of the block (measured on a T4). glibc's niagara4 memset and Linux's NG4memset rely on this, and OpenSSL's T4 AES code uses the FP form, stdfa.
QEMU only accepted the integer form, on HYPV CPUs, as a plain store, so with the crypto hwcap a multiarch glibc memset leaves stale data. Accept both forms when the CPU has the crypto feature, which implies T4 or later, and zero the 32-byte block as T4 does.
--- target/sparc/translate.c | 42 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
target/sparc/translate.c +40 -2
@@ -1853,6 +1853,26 @@ static void gen_ld_asi(DisasContext *dc, DisasASI *da, TCGv dst, TCGv addr) } } +/* +* On T4 and later, a block-initializing store to the first doubleword of +* a 32-byte block zeroes the rest of the block. glibc and Linux memset +* rely on this and store only one doubleword per block when zeroing. +*/ +static void gen_blkinit_zero (DisasContext *dc,DisasASI *da,TCGv addr) +{ +TCGLabel *skip =gen_new_label (); +TCGv t =tcg_temp_new (); +TCGv_i64 zero =tcg_constant_i64 (0 ); + +tcg_gen_andi_tl (t, addr,31 ); +tcg_gen_brcondi_tl (TCG_COND_NE , t,0 , skip); +for (int i =0 ; i <32 ; i +=8 ) { +tcg_gen_addi_tl (t, addr, i); +tcg_gen_qemu_st_i64 (zero, t, da->mem_idx,MO_UQ ); + } +gen_set_label (skip); +} +static void gen_st_asi (DisasContext *dc,DisasASI *da,TCGv src,TCGv addr) {switch (da->type) {
@@ -1863,12 +1883,19 @@ 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 has them. +*/ gen_exception (dc,TT_ILL_INSN );break ; }/* In OpenSPARC T1+ CPUs TWINX ASIs in store are ST_BLKINIT_ ASIs */ +if (dc->def->features &CPU_FEATURE_CRYPTO ) { +gen_blkinit_zero (dc, da, addr); + }/* fall through */ case GET_ASI_DIRECT :
@@ -2169,6 +2196,17 @@ static void gen_stf_asi(DisasContext *dc, DisasASI *da, MemOp orig_size, }break ; +case GET_ASI_DTWINX : +/* stdfa with a block-initializing ASI, T4 and later */ +if (orig_size ==MO_64 && (dc->def->features &CPU_FEATURE_CRYPTO )) { +gen_blkinit_zero (dc, da, addr); + d64 =gen_load_fpr_D (dc, rd); +tcg_gen_qemu_st_i64 (d64, addr, da->mem_idx, memop |MO_ALIGN ); + }else { +gen_exception (dc,TT_ILL_INSN ); + } +break ; +case GET_ASI_BLOCK :/* Valid for stdfa on aligned registers only. */ if (orig_size ==MO_64 && (rd &7 ) ==0 ) {
Signature · 1 line
2.43.0
Re: [PATCH 1/2] target/sparc: Model block-initializing stores
Quoted 26 lines · Stian Halseth
On T4 and later, a block-initializing store to the first doubleword of a 32-byte block zeroes the rest of the block (measured on a T4). glibc's niagara4 memset and Linux's NG4memset rely on this, and OpenSSL's T4 AES code uses the FP form, stdfa.
QEMU only accepted the integer form, on HYPV CPUs, as a plain store, so with the crypto hwcap a multiarch glibc memset leaves stale data. Accept both forms when the CPU has the crypto feature, which implies T4 or later, and zero the 32-byte block as T4 does.
Signed-off-by Stian Halseth <[email protected]>--- target/sparc/translate.c | 42 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)target/sparc/translate.c +3
-0@@ -1853,6 +1853,26 @@ static void gen_ld_asi(DisasContext *dc, DisasASI *da, TCGv dst, TCGv addr) } } +/* +* On T4 and later, a block-initializing store to the first doubleword of +* a 32-byte block zeroes the rest of the block. glibc and Linux memset
target/sparc/translate.c static void gen_ld_asi(DisasContext *dc, DisasASI *da, TCGv dst, TCGv addr) · line 1868
Quoted 10 lines
target/sparc/translate.c +10
-0+* rely on this and store only one doubleword per block when zeroing. +*/ +static void gen_blkinit_zero (DisasContext *dc,DisasASI *da,TCGv addr) +{ +TCGLabel *skip =gen_new_label (); +TCGv t =tcg_temp_new (); +TCGv_i64 zero =tcg_constant_i64 (0 ); + +tcg_gen_andi_tl (t, addr,31 ); +tcg_gen_brcondi_tl (TCG_COND_NE , t,0 , skip);
tcg_gen_brcondi_tl(TCG_COND_TSTNE, addr, 31, skip);
target/sparc/translate.c static void gen_ld_asi(DisasContext *dc, DisasASI *da, TCGv dst, TCGv addr) · line 1872
Quoted 4 lines
target/sparc/translate.c +4
-0+for (int i =0 ; i <32 ; i +=8 ) { +tcg_gen_addi_tl (t, addr, i); +tcg_gen_qemu_st_i64 (zero, t, da->mem_idx,MO_UQ ); + }
You don't need to store to offset 0, because...
target/sparc/translate.c static void gen_stf_asi(DisasContext *dc, DisasASI *da, MemOp orig_size, · line 2204
Quoted 6 lines
target/sparc/translate.c +6
-0+case GET_ASI_DTWINX : +/* stdfa with a block-initializing ASI, T4 and later */ +if (orig_size ==MO_64 && (dc->def->features &CPU_FEATURE_CRYPTO )) { +gen_blkinit_zero (dc, da, addr); + d64 =gen_load_fpr_D (dc, rd); +tcg_gen_qemu_st_i64 (d64, addr, da->mem_idx, memop |MO_ALIGN );
... that's done here.
r~
Re: [PATCH 1/2] target/sparc: Model block-initializing stores
Hi Richard,
Quoted 2 lines · Richard Henderson
tcg_gen_brcondi_tl(TCG_COND_TSTNE, addr, 31, skip);
Thanks, adopting in v2.
Quoted 2 lines
You don't need to store to offset 0, because...
... that's done here.
Yes, for stxa and stdfa, but stba/stha/stwa with the same ASIs also reach gen_blkinit_zero() through gen_st_asi().
I tested on the T4, and those zero the rest of the first doubleword as well (v = stored byte, 0 = zeroed, one 32-byte block):
stba off=0: v0000000000000000000000000000000 stha off=0: vv000000000000000000000000000000 stwa off=0: vvvv0000000000000000000000000000
So v2 skips offset 0 only for 8-byte stores.
Thanks for the review!
/Stian
PATCH 2/2 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