[PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations

Harald Freudenberger posted 14 patches 2 weeks, 2 days ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Ilya Leoshkevich <iii@linux.ibm.com>, David Hildenbrand <david@kernel.org>, Thomas Huth <thuth@redhat.com>
[PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations
Posted by Harald Freudenberger 2 weeks, 2 days ago
Fix missing parts for MSA 9 kdsa and rework the cpacf
handling code so that further extensions can be made in
a clean and structured way.

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
---
 target/s390x/tcg/crypto_helper.c | 85 ++++++++++++++++++++++++++------
 target/s390x/tcg/insn-data.h.inc |  1 +
 target/s390x/tcg/translate.c     |  7 +++
 3 files changed, 79 insertions(+), 14 deletions(-)

diff --git a/target/s390x/tcg/crypto_helper.c b/target/s390x/tcg/crypto_helper.c
index 4447bb66ee..0b00d8ba5b 100644
--- a/target/s390x/tcg/crypto_helper.c
+++ b/target/s390x/tcg/crypto_helper.c
@@ -268,6 +268,57 @@ static void fill_buf_random(CPUS390XState *env, uintptr_t ra,
     }
 }
 
+static int cpacf_kimd(CPUS390XState *env, const uintptr_t ra,
+                      uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x03: /* CPACF_KIMD_SHA_512 */
+        rc = cpacf_sha512(env, ra, env->regs[1], &env->regs[r2],
+                          &env->regs[r2 + 1], S390_FEAT_TYPE_KIMD);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
+static int cpacf_klmd(CPUS390XState *env, const uintptr_t ra,
+                      uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x03: /* CPACF_KLMD_SHA_512 */
+        rc = cpacf_sha512(env, ra, env->regs[1], &env->regs[r2],
+                          &env->regs[r2 + 1], S390_FEAT_TYPE_KLMD);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
+static int cpacf_ppno(CPUS390XState *env, uintptr_t ra,
+                      uint32_t r1, uint32_t r2, uint32_t r3, uint8_t fc)
+{
+    int rc = 0;
+
+    switch (fc) {
+    case 0x72: /* CPACF_PRNO_TRNG */
+        fill_buf_random(env, ra, &env->regs[r1], &env->regs[r1 + 1]);
+        fill_buf_random(env, ra, &env->regs[r2], &env->regs[r2 + 1]);
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    return rc;
+}
+
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
                      uint32_t type)
 {
@@ -276,14 +327,15 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     const uint8_t fc = env->regs[0] & 0x7fULL;
     uint8_t subfunc[16] = { 0 };
     uint64_t param_addr;
-    int i;
+    int i, rc = 0;
 
     switch (type) {
-    case S390_FEAT_TYPE_KMAC:
+    case S390_FEAT_TYPE_KDSA:
     case S390_FEAT_TYPE_KIMD:
     case S390_FEAT_TYPE_KLMD:
-    case S390_FEAT_TYPE_PCKMO:
+    case S390_FEAT_TYPE_KMAC:
     case S390_FEAT_TYPE_PCC:
+    case S390_FEAT_TYPE_PCKMO:
         if (mod) {
             tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
         }
@@ -295,24 +347,29 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra);
     }
 
-    switch (fc) {
-    case 0: /* query subfunction */
-        for (i = 0; i < 16; i++) {
+    /* handle query subfunction */
+    if (fc == 0) {
+        for (i = 0; i < sizeof(subfunc); i++) {
             param_addr = wrap_address(env, env->regs[1] + i);
             cpu_stb_data_ra(env, param_addr, subfunc[i], ra);
         }
+        goto out;
+    }
+
+    switch (type) {
+    case S390_FEAT_TYPE_KIMD:
+        rc = cpacf_kimd(env, ra, r1, r2, r3, fc);
         break;
-    case 3: /* CPACF_*_SHA_512 */
-        return cpacf_sha512(env, ra, env->regs[1], &env->regs[r2],
-                            &env->regs[r2 + 1], type);
-    case 114: /* CPACF_PRNO_TRNG */
-        fill_buf_random(env, ra, &env->regs[r1], &env->regs[r1 + 1]);
-        fill_buf_random(env, ra, &env->regs[r2], &env->regs[r2 + 1]);
+    case S390_FEAT_TYPE_KLMD:
+        rc = cpacf_klmd(env, ra, r1, r2, r3, fc);
+        break;
+    case S390_FEAT_TYPE_PPNO:
+        rc = cpacf_ppno(env, ra, r1, r2, r3, fc);
         break;
     default:
-        /* we don't implement any other subfunction yet */
         g_assert_not_reached();
     }
 
-    return 0;
+out:
+    return rc;
 }
diff --git a/target/s390x/tcg/insn-data.h.inc b/target/s390x/tcg/insn-data.h.inc
index ec730ee091..b6095711f2 100644
--- a/target/s390x/tcg/insn-data.h.inc
+++ b/target/s390x/tcg/insn-data.h.inc
@@ -1012,6 +1012,7 @@
     D(0xb92e, KM,      RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KM)
     D(0xb92f, KMC,     RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMC)
     D(0xb929, KMA,     RRF_b, MSA8, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMA)
+    D(0xb93a, KDSA,    RRE,   MSA9, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KDSA)
     E(0xb93c, PPNO,    RRE,   MSA5, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_PPNO, IF_IO)
     D(0xb93e, KIMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KIMD)
     D(0xb93f, KLMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KLMD)
diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c
index 4d2b8c5e2b..a5fdc05d37 100644
--- a/target/s390x/tcg/translate.c
+++ b/target/s390x/tcg/translate.c
@@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
     case S390_FEAT_TYPE_PCKMO:
     case S390_FEAT_TYPE_PCC:
         break;
+    case S390_FEAT_TYPE_KDSA:
+        if (r1) {
+            gen_program_exception(s, PGM_SPECIFICATION);
+            return DISAS_NORETURN;
+        }
+        break;
     default:
         g_assert_not_reached();
     };
@@ -6018,6 +6024,7 @@ enum DisasInsnEnum {
 #define FAC_MSA4        S390_FEAT_MSA_EXT_4 /* msa-extension-4 facility */
 #define FAC_MSA5        S390_FEAT_MSA_EXT_5 /* msa-extension-5 facility */
 #define FAC_MSA8        S390_FEAT_MSA_EXT_8 /* msa-extension-8 facility */
+#define FAC_MSA9        S390_FEAT_MSA_EXT_9 /* msa-extension-9 facility */
 #define FAC_ECT         S390_FEAT_EXTRACT_CPU_TIME
 #define FAC_PCI         S390_FEAT_ZPCI /* z/PCI facility */
 #define FAC_AIS         S390_FEAT_ADAPTER_INT_SUPPRESSION
-- 
2.43.0
Re: [PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations
Posted by Janosch Frank 1 week, 6 days ago
On 1/23/26 12:42, Harald Freudenberger wrote:
> Fix missing parts for MSA 9 kdsa and rework the cpacf
> handling code so that further extensions can be made in
> a clean and structured way.
> 
> Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
[...]
> diff --git a/target/s390x/tcg/insn-data.h.inc b/target/s390x/tcg/insn-data.h.inc
> index ec730ee091..b6095711f2 100644
> --- a/target/s390x/tcg/insn-data.h.inc
> +++ b/target/s390x/tcg/insn-data.h.inc
> @@ -1012,6 +1012,7 @@
>       D(0xb92e, KM,      RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KM)
>       D(0xb92f, KMC,     RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMC)
>       D(0xb929, KMA,     RRF_b, MSA8, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KMA)
> +    D(0xb93a, KDSA,    RRE,   MSA9, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KDSA)
>       E(0xb93c, PPNO,    RRE,   MSA5, 0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_PPNO, IF_IO)

@Christian:
Is it time to do the rename to PRNO?
According to the architecture we deprecated PPNO in favor of PRNO with 
introduction of MSA7.

>       D(0xb93e, KIMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KIMD)
>       D(0xb93f, KLMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, S390_FEAT_TYPE_KLMD)
> diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c
> index 4d2b8c5e2b..a5fdc05d37 100644
> --- a/target/s390x/tcg/translate.c
> +++ b/target/s390x/tcg/translate.c
> @@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, DisasOps *o)
>       case S390_FEAT_TYPE_PCKMO:
>       case S390_FEAT_TYPE_PCC:
>           break;
> +    case S390_FEAT_TYPE_KDSA:
> +        if (r1) {
> +            gen_program_exception(s, PGM_SPECIFICATION);

Are you sure that's a spec?

r1 and the 8 free bits in the instruction should be set to zero but the 
architecture doesn't say you'll get a spec if they are not 0.

The architecture reserves the right to use r1 and the free bits for new 
function codes. If you don't set them to 0 you might invoke new 
functionality that you don't know about.

> +            return DISAS_NORETURN;
> +        }
> +        break;
>       default:
>           g_assert_not_reached();
>       };
> @@ -6018,6 +6024,7 @@ enum DisasInsnEnum {
>   #define FAC_MSA4        S390_FEAT_MSA_EXT_4 /* msa-extension-4 facility */
>   #define FAC_MSA5        S390_FEAT_MSA_EXT_5 /* msa-extension-5 facility */
>   #define FAC_MSA8        S390_FEAT_MSA_EXT_8 /* msa-extension-8 facility */
> +#define FAC_MSA9        S390_FEAT_MSA_EXT_9 /* msa-extension-9 facility */
>   #define FAC_ECT         S390_FEAT_EXTRACT_CPU_TIME
>   #define FAC_PCI         S390_FEAT_ZPCI /* z/PCI facility */
>   #define FAC_AIS         S390_FEAT_ADAPTER_INT_SUPPRESSION
Re: [PATCH v3 01/14] target/s390x: Rework s390 cpacf implementations
Posted by Harald Freudenberger 1 week, 6 days ago
On 2026-01-26 13:53, Janosch Frank wrote:
> On 1/23/26 12:42, Harald Freudenberger wrote:
>> Fix missing parts for MSA 9 kdsa and rework the cpacf
>> handling code so that further extensions can be made in
>> a clean and structured way.
>> 
>> Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
> [...]
>> diff --git a/target/s390x/tcg/insn-data.h.inc 
>> b/target/s390x/tcg/insn-data.h.inc
>> index ec730ee091..b6095711f2 100644
>> --- a/target/s390x/tcg/insn-data.h.inc
>> +++ b/target/s390x/tcg/insn-data.h.inc
>> @@ -1012,6 +1012,7 @@
>>       D(0xb92e, KM,      RRE,   MSA,  0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KM)
>>       D(0xb92f, KMC,     RRE,   MSA,  0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KMC)
>>       D(0xb929, KMA,     RRF_b, MSA8, 0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KMA)
>> +    D(0xb93a, KDSA,    RRE,   MSA9, 0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KDSA)
>>       E(0xb93c, PPNO,    RRE,   MSA5, 0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_PPNO, IF_IO)
> 
> @Christian:
> Is it time to do the rename to PRNO?
> According to the architecture we deprecated PPNO in favor of PRNO with
> introduction of MSA7.
> 
>>       D(0xb93e, KIMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KIMD)
>>       D(0xb93f, KLMD,    RRE,   MSA,  0, 0, 0, 0, msa, 0, 
>> S390_FEAT_TYPE_KLMD)
>> diff --git a/target/s390x/tcg/translate.c 
>> b/target/s390x/tcg/translate.c
>> index 4d2b8c5e2b..a5fdc05d37 100644
>> --- a/target/s390x/tcg/translate.c
>> +++ b/target/s390x/tcg/translate.c
>> @@ -2565,6 +2565,12 @@ static DisasJumpType op_msa(DisasContext *s, 
>> DisasOps *o)
>>       case S390_FEAT_TYPE_PCKMO:
>>       case S390_FEAT_TYPE_PCC:
>>           break;
>> +    case S390_FEAT_TYPE_KDSA:
>> +        if (r1) {
>> +            gen_program_exception(s, PGM_SPECIFICATION);
> 
> Are you sure that's a spec?
> 
> r1 and the 8 free bits in the instruction should be set to zero but
> the architecture doesn't say you'll get a spec if they are not 0.
> 
> The architecture reserves the right to use r1 and the free bits for
> new function codes. If you don't set them to 0 you might invoke new
> functionality that you don't know about.
> 

Yep, you are right. This check is not needed at all.
The code should look like:

        ...
        case S390_FEAT_TYPE_PCKMO:
        case S390_FEAT_TYPE_PCC:
        case S390_FEAT_TYPE_KDSA:
            break;
        ...

>> +            return DISAS_NORETURN;
>> +        }
>> +        break;
>>       default:
>>           g_assert_not_reached();
>>       };
>> @@ -6018,6 +6024,7 @@ enum DisasInsnEnum {
>>   #define FAC_MSA4        S390_FEAT_MSA_EXT_4 /* msa-extension-4 
>> facility */
>>   #define FAC_MSA5        S390_FEAT_MSA_EXT_5 /* msa-extension-5 
>> facility */
>>   #define FAC_MSA8        S390_FEAT_MSA_EXT_8 /* msa-extension-8 
>> facility */
>> +#define FAC_MSA9        S390_FEAT_MSA_EXT_9 /* msa-extension-9 
>> facility */
>>   #define FAC_ECT         S390_FEAT_EXTRACT_CPU_TIME
>>   #define FAC_PCI         S390_FEAT_ZPCI /* z/PCI facility */
>>   #define FAC_AIS         S390_FEAT_ADAPTER_INT_SUPPRESSION