Skip to contentSkip to conversation

target/sparc: Block-init stores for the crypto feature

PATCH In review Stian Halseth 3 participants 7 messages Oct 28 – Oct 31

lore
Share & tools
lore
#0 lore raw

v2 cover target/sparc: Block-init stores for the crypto feature

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 start 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.

v2:
- Test the alignment with TCG_COND_TSTNE (Richard)
- Skip offset 0 for 8-byte stores, which write it themselves (Richard). stba/stha/stwa at offset 0 still zero it: a T4 zeroes the rest of the first doubleword for those too.
- Rebased on master, now that the crypto instructions are in

v1: https://lore.kernel.org/qemu-devel/[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   | 43 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 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 | 43 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
target/sparc/translate.c +41 -2
index 1fc8439a47..416625c6dc 100644
@@ -1853,6 +1853,27 @@ 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,
+                             MemOp size)
+{
+    TCGLabel *skip = gen_new_label();
+    TCGv t = tcg_temp_new();
+    TCGv_i64 zero = tcg_constant_i64(0);
+
+    tcg_gen_brcondi_tl(TCG_COND_TSTNE, addr, 31, skip);
+    /* A narrower store also zeroes the rest of the first doubleword. */
+    for (int i = size == MO_64 ? 8 : 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 +1884,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, da->memop & MO_SIZE);
+        }
         /* fall through */
 
     case GET_ASI_DIRECT:
@@ -2169,6 +2197,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, MO_64);
+            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
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] target/sparc: Model block-initializing stores

Quoted 10 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.

Quoted 4 lines
---
  target/sparc/translate.c | 43 ++++++++++++++++++++++++++++++++++++++--
  1 file changed, 41 insertions(+), 2 deletions(-)

r~

Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] target/sparc: Model block-initializing stores

In reply to Stian Halseth
Quoted 77 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.

---
  target/sparc/translate.c | 43 ++++++++++++++++++++++++++++++++++++++--
  1 file changed, 41 insertions(+), 2 deletions(-)
target/sparc/translate.c +34 -2
index 1fc8439a47..416625c6dc 100644
@@ -1853,6 +1853,27 @@ 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,
+                             MemOp size)
+{
+    TCGLabel *skip = gen_new_label();
+    TCGv t = tcg_temp_new();
+    TCGv_i64 zero = tcg_constant_i64(0);
+
+    tcg_gen_brcondi_tl(TCG_COND_TSTNE, addr, 31, skip);
+    /* A narrower store also zeroes the rest of the first doubleword. */
+    for (int i = size == MO_64 ? 8 : 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 +1884,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, da->memop & MO_SIZE);
+        }
          /* fall through */
 
      case GET_ASI_DIRECT:
@@ -2169,6 +2197,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, MO_64);

target/sparc/translate.c static void gen_stf_asi(DisasContext *dc, DisasASI *da, MemOp orig_size, · line 2213

Quoted 10 lines
target/sparc/translate.c +7 -0
+            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) {

ATB,

Mark.

#4 lore raw

PATCH v2 2/2 linux-user/sparc: Report HWCAP_SPARC_CRYPTO for crypto CPU feature

In reply to Stian Halseth

Set 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
index 181f1e00b5..0412741285 100644
@@ -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
#5 lore raw
In reply to Stian Halseth

Hi Mark,

Would it be possible to get these two patches into your next sparc pull request, before the soft freeze on Monday?

They complete the crypto series for linux-user, so it would be good to have them in 11.2 together with patches 1-6.

Thanks!

/Stian

Quoted 24 lines · Stian Halseth

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 start 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.

v2:
- Test the alignment with TCG_COND_TSTNE (Richard)
- Skip offset 0 for 8-byte stores, which write it themselves (Richard). stba/stha/stwa at offset 0 still zero it: a T4 zeroes the rest of the first doubleword for those too.
- Rebased on master, now that the crypto instructions are in

v1: https://lore.kernel.org/qemu-devel/[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   | 43 ++++++++++++++++++++++++++++++++++++--

 2 files changed, 42 insertions(+), 2 deletions(-)
#6 lore raw
Quoted 11 lines · Stian Halseth

Hi Mark,

Would it be possible to get these two patches into your next sparc pull request, before the soft freeze on Monday?

They complete the crypto series for linux-user, so it would be good to have them in 11.2 together with patches 1-6.

Thanks!

/Stian

Hi Stian,

I'll do my best to look at these patches over the weekend.

ATB,

Mark.