Skip to contentSkip to conversation

target/mips: add missing Octeon user-mode support

PATCH Superseded James Hilliard 2 participants 21 messages Apr 21 – Apr 22 Arm/Amlogic Meson SoC support

v2 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/mips: add missing Octeon user-mode support

This series updates MIPS linux-user unaligned-access behavior and fills in missing Octeon user-mode instruction support used by existing Octeon binaries.

The first patch models the Linux/MIPS MIPS_FIXADE policy so user-mode unaligned scalar accesses default to software fixups and sysmips(MIPS_FIXADE) can toggle SIGBUS/BUS_ADRALN behavior.

The Octeon patches add integer, indexed memory, atomic, multiplier, COP2 crypto, CHORD, and LLM support. The series also adds a small mips64/mips64el TCG guest test covering representative Octeon integer, multiplier, and COP2 selector paths. The final patch corrects the Octeon68XX CP1 feature bits and FCR defaults.

Changes since v1:
- Split BADDU/DMUL destination fixes into a preliminary patch.
- Split the SEQ/SNE decode refactoring into a preliminary patch.
- Moved Octeon multiplier state to uint64_t arrays and updated VMState.
- Switched Octeon helper ABIs to i64/uint64_t where applicable.
- Moved COP2 selector decode/support logic into octeon_translate.c.
- Converted Octeon translator TCG ops to explicit i64 forms.
- Added in-tree TCG tests for mips64 and mips64el linux-user.
- Used switch ranges and g_assert_not_reached() for SHA3/ZUC shared selector handling.
- Dropped Octeon prefixes from generic Camellia helper routines.
- Replaced the reflected GFM 64-bit carryless multiply loop with crypto/clmul.h.
- Moved the Octeon68XX CP1 CPU-model correction to the end of the series.
- Added migration coverage for Octeon COP2 crypto and LLM sparse state.

---
James Hilliard (13):
linux-user/mips, target/mips: honor MIPS_FIXADE for unaligned accesses target/mips: fix Octeon arithmetic destination handling target/mips: split Octeon SEQ/SNE decode
target/mips: use explicit i64 TCG ops for Octeon translation target/mips: add Octeon arithmetic and memory instructions target/mips: add Octeon LA* atomic instructions target/mips: add Octeon COP2 crypto core support target/mips: add Octeon SMS4 crypto support
target/mips: add Octeon SHA3 crypto support
target/mips: add Octeon ZUC crypto support
target/mips: add Octeon Camellia crypto support target/mips: add Octeon CHORD and LLM COP2 support target/mips: expose Octeon68XX floating-point support

 MAINTAINERS                                   |    2 +
 linux-user/mips/cpu_loop.c                    |    5 +
 linux-user/mips/target_syscall.h              |    3 +
 linux-user/mips64/target_syscall.h            |    3 +
 linux-user/syscall.c                          |   60 +
 target/mips/cpu-defs.c.inc                    |   10 +-
 target/mips/cpu.c                             |   75 +-
 target/mips/cpu.h                             |  249 +++
 target/mips/helper.h                          |    6 +
 target/mips/internal.h                        |    3 +
 target/mips/system/machine.c                  |  142 ++
 target/mips/tcg/meson.build                   |    1 +
 target/mips/tcg/octeon.decode                 |   47 +-
 target/mips/tcg/octeon_crypto.c               | 2479 +++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c            |  704 ++++++-
 target/mips/tcg/op_helper.c                   |  105 ++
 target/mips/tcg/translate.c                   |   23 +-
 target/mips/tcg/translate.h                   |    1 +
 tests/tcg/mips/Makefile.target                |   11 +
 tests/tcg/mips/user/isa/octeon/octeon-insns.c |  216 +++
 tests/tcg/mips64/Makefile.target              |   20 +
 tests/tcg/mips64el/Makefile.target            |    8 +
 22 files changed, 4129 insertions(+), 44 deletions(-)

---
base-commit: 98b060da3a4f92b2a994ead5b16a87e783baf77c
change-id: 20260420-mips-octeon-missing-insns-v2-5e693770cf2c

Best regards,

Signature · 1 line
James Hilliard <[email protected]>
#1 lore raw

PATCH v2 1/13 linux-user/mips, target/mips: honor MIPS_FIXADE for unaligned accesses

Linux/MIPS enables software fixups for user-mode unaligned scalar accesses by default through MIPS_FIXADE/TIF_FIXADE. QEMU linux-user did not model that ABI, so MIPS guests took fatal AdEL/AdES exceptions unless translation was forced to use unaligned host accesses.

Replace the target-specific workaround with a generic MIPS linux-user implementation:

- key MIPS translation blocks on the linux-user unaligned policy,
- implement sysmips(MIPS_FIXADE) to toggle that policy,
- raise SIGBUS/BUS_ADRALN when fixups are disabled, and
- handle the existing sysmips(MIPS_ATOMIC_SET) and MIPS_FLUSH_CACHE commands in the new target-side sysmips dispatcher.

This makes the default behaviour match native Linux/MIPS more closely and keeps the signal semantics consistent with the kernel ABI without regressing other user-mode sysmips commands.

---
 linux-user/mips/cpu_loop.c         |  5 ++++
 linux-user/mips/target_syscall.h   |  3 ++
 linux-user/mips64/target_syscall.h |  3 ++
 linux-user/syscall.c               | 60 ++++++++++++++++++++++++++++++++++++++
 target/mips/cpu.c                  |  8 +++--
 target/mips/cpu.h                  |  4 +++
 target/mips/tcg/translate.c        |  6 +++-
 7 files changed, 86 insertions(+), 3 deletions(-)
linux-user/mips/cpu_loop.c +5 -0
index fa264b27ec..ff9d293c29 100644
@@ -161,6 +161,11 @@ done_syscall:
         case EXCP_DSPDIS:
             force_sig(TARGET_SIGILL);
             break;
+        case EXCP_AdEL:
+        case EXCP_AdES:
+            force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
+                            env->CP0_BadVAddr);
+            break;
         case EXCP_INTERRUPT:
             /* just indicate that signals should be handled asap */
             break;
linux-user/mips/target_syscall.h +3 -0
index dfcdf320b7..be6942445a 100644
@@ -10,6 +10,9 @@
 #define TARGET_MCL_ONFAULT 4
 
 #define TARGET_FORCE_SHMLBA
+#define TARGET_SYSMIPS_FLUSH_CACHE     3
+#define TARGET_SYSMIPS_FIXADE          7
+#define TARGET_SYSMIPS_ATOMIC_SET   2001
 
 static inline abi_ulong target_shmlba(CPUMIPSState *env)
 {
linux-user/mips64/target_syscall.h +3 -0
index 9135bf5e8b..c11d0a0888 100644
@@ -10,6 +10,9 @@
 #define TARGET_MCL_ONFAULT 4
 
 #define TARGET_FORCE_SHMLBA
+#define TARGET_SYSMIPS_FLUSH_CACHE     3
+#define TARGET_SYSMIPS_FIXADE          7
+#define TARGET_SYSMIPS_ATOMIC_SET   2001
 
 static inline abi_ulong target_shmlba(CPUMIPSState *env)
 {
linux-user/syscall.c +60 -0

72 lines not on this page · raw message

target/mips/cpu.c +6 -2
index 5f88c077db..ec70c10985 100644
@@ -555,11 +555,15 @@ static int mips_cpu_mmu_index(CPUState *cs, bool ifunc)
 static TCGTBCPUState mips_get_tb_cpu_state(CPUState *cs)
 {
     CPUMIPSState *env = cpu_env(cs);
+    uint32_t flags = env->hflags & MIPS_HFLAG_TB_MASK;
+
+#ifdef CONFIG_USER_ONLY
+    flags |= TB_FLAG_UNALIGN * !cs->prctl_unalign_sigbus;
+#endif
 
     return (TCGTBCPUState){
         .pc = env->active_tc.PC,
-        .flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK |
-                                MIPS_HFLAG_HWRENA_ULR),
+        .flags = flags,
     };
 }
 
target/mips/cpu.h +4 -0
index ed662135cb..ac81470576 100644
@@ -1161,6 +1161,10 @@ typedef struct CPUArchState {
 #define MIPS_HFLAG_ELPA  0x4000000
 #define MIPS_HFLAG_ITC_CACHE  0x8000000 /* CACHE instr. operates on ITC tag */
 #define MIPS_HFLAG_ERL   0x10000000 /* error level flag */
+#define MIPS_HFLAG_TB_MASK (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK | \
+                            MIPS_HFLAG_HWRENA_ULR)
+
+#define TB_FLAG_UNALIGN  0x40000000
     target_ulong btarget;        /* Jump / branch target               */
     target_ulong bcond;          /* Branch condition (if needed)       */
 
target/mips/tcg/translate.c +5 -1
index 54849e9ff1..2e3fe1fa16 100644
@@ -15067,6 +15067,7 @@ static void mips_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
 {
     DisasContext *ctx = container_of(dcbase, DisasContext, base);
     CPUMIPSState *env = cpu_env(cs);
+    uint32_t tb_flags = ctx->base.tb->flags;
 
     ctx->page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
     ctx->saved_pc = -1;
@@ -15089,7 +15090,7 @@ static void mips_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     ctx->CP0_LLAddr_shift = env->CP0_LLAddr_shift;
     ctx->cmgcr = (env->CP0_Config3 >> CP0C3_CMGCR) & 1;
     /* Restore delay slot state from the tb context.  */
-    ctx->hflags = (uint32_t)ctx->base.tb->flags; /* FIXME: maybe use 64 bits? */
+    ctx->hflags = tb_flags & MIPS_HFLAG_TB_MASK;
     ctx->ulri = (env->CP0_Config3 >> CP0C3_ULRI) & 1;
     ctx->ps = ((env->active_fpu.fcr0 >> FCR0_PS) & 1) ||
              (env->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F));
@@ -15109,6 +15110,9 @@ static void mips_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
     ctx->default_tcg_memop_mask = (!(ctx->insn_flags & ISA_NANOMIPS32) &&
                                   (ctx->insn_flags & (ISA_MIPS_R6 |
                                   INSN_LOONGSON3A))) ? MO_UNALN : MO_ALIGN;
+    if (tb_flags & TB_FLAG_UNALIGN) {
+        ctx->default_tcg_memop_mask = MO_UNALN;
+    }
 
     /*
      * Execute a branch and its delay slot as a single instruction.
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 01/13] linux-user/mips, target/mips: honor MIPS_FIXADE for unaligned accesses

Quoted 14 lines · James Hilliard

Linux/MIPS enables software fixups for user-mode unaligned scalar accesses by default through MIPS_FIXADE/TIF_FIXADE. QEMU linux-user did not model that ABI, so MIPS guests took fatal AdEL/AdES exceptions unless translation was forced to use unaligned host accesses.

Replace the target-specific workaround with a generic MIPS linux-user implementation:

- key MIPS translation blocks on the linux-user unaligned policy,
- implement sysmips(MIPS_FIXADE) to toggle that policy,
- raise SIGBUS/BUS_ADRALN when fixups are disabled, and
- handle the existing sysmips(MIPS_ATOMIC_SET) and MIPS_FLUSH_CACHE commands in the new target-side sysmips dispatcher.

Quoted 5 lines

This makes the default behaviour match native Linux/MIPS more closely and keeps the signal semantics consistent with the kernel ABI without regressing other user-mode sysmips commands.

This patch does multiple things.

(1) sysmips flush_cache.
(2) sysmips atomic_set.
(3) As stated, unaligned access controls.

I suggest breaking it into 3 patches, in the order above. That will add a bare-bones do_sysmips function in the first patch.

Quoted 22 lines
linux-user/syscall.c +22 -0
+#ifdef TARGET_NR_sysmips
+static abi_long do_sysmips_atomic_set(CPUArchState *env, abi_ulong addr,
+                                      abi_long value)
+{
+    uint32_t *ptr;
+    abi_long old;
+
+    if (addr & 3) {
+        return -TARGET_EINVAL;
+    }
+
+    ptr = lock_user(VERIFY_WRITE, addr, sizeof(*ptr), true);
+    if (!ptr) {
+        return -TARGET_EINVAL;
+    }
+
+    old = tswap32(qatomic_xchg(ptr, tswap32((uint32_t)value)));
+    unlock_user(ptr, addr, sizeof(*ptr));
+
+    /*
+     * MIPS uses a separate error flag, but the common linux-user syscall
+     * path infers that flag from the return value.  Successful atomic_set

linux-user/syscall.c static abi_long do_prctl_syscall_user_dispatch(CPUArchState *env, · line 6629

Quoted 10 lines
linux-user/syscall.c +10 -0
+     * results can overlap the target errno range, so write the result
+     * registers here and ask the CPU loop to leave them alone.
+     */
+    if ((abi_ulong)old >= (abi_ulong)-TARGET_EDQUOT) {
+        env->active_tc.gpr[2] = old;
+        env->active_tc.gpr[7] = 0;
+        return -QEMU_ESIGRETURN;
+    }
+
+    return old;

The comment is good, but the IF is unnecessary: always use the ESIGRETURN path.

r~

#3 lore raw

PATCH v2 2/13 target/mips: fix Octeon arithmetic destination handling

In reply to James Hilliard

BADDU and DMUL write their results to rd, not rt. Avoid treating rt == $zero as a nop, and route writes through gen_store_gpr() so rd == $zero is handled consistently.

---
Changes v1 -> v2:
- Split the BADDU/DMUL destination handling fix out of the Octeon arithmetic instruction patch. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/tcg/octeon_translate.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
target/mips/tcg/octeon_translate.c +7 -5
index b2eca29e06..41e0d770f5 100644
@@ -45,7 +45,7 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
 {
     TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {
         /* nop */
         return true;
     }
@@ -55,8 +55,9 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
     gen_load_gpr(t0, a->rs);
     gen_load_gpr(t1, a->rt);
 
-    tcg_gen_add_tl(t0, t0, t1);
-    tcg_gen_andi_i64(cpu_gpr[a->rd], t0, 0xff);
+    tcg_gen_add_i64(t0, t0, t1);
+    tcg_gen_andi_i64(t0, t0, 0xff);
+    gen_store_gpr(t0, a->rd);
     return true;
 }
 
@@ -64,7 +65,7 @@ static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
 {
     TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {
         /* nop */
         return true;
     }
@@ -74,7 +75,8 @@ static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
     gen_load_gpr(t0, a->rs);
     gen_load_gpr(t1, a->rt);
 
-    tcg_gen_mul_i64(cpu_gpr[a->rd], t0, t1);
+    tcg_gen_mul_i64(t0, t0, t1);
+    gen_store_gpr(t0, a->rd);
     return true;
 }
 
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v2 02/13] target/mips: fix Octeon arithmetic destination handling

Quoted 3 lines · James Hilliard

BADDU and DMUL write their results to rd, not rt. Avoid treating rt == $zero as a nop, and route writes through gen_store_gpr() so rd == $zero is handled consistently.

If you do the second, you can completely remove the initial IF.

That has been my preferred solution for other targets: minimize the number of checks for these unlikely cases for all instruction implementations. You know for a fact that no compiler is going to generate such instructions. The only place they'll appear are within architectural compliance testsuites. Let code be generated and then the store_gpr helper will discard the results.

The only place a special write-to-zero-register check is useful are for the architected NOP instructions, because we do expect a fair few of those. Even then, when using decodetree, consider specialization within the decode, not within the helpers. See the nop/copy/or example at the end of docs/devel/decodetree.rst.

r~

#5 lore raw

PATCH v2 3/13 target/mips: split Octeon SEQ/SNE decode

In reply to James Hilliard

Decode the equality and inequality forms as explicit SEQ/SNE and SEQI/SNEI instructions rather than using shared generated SEQNE/SEQNEI entries.

The explicit decoder names match the architectural mnemonics, which makes the translator entry points and trace/debug output easier to correlate with the instruction set.

---
Changes v1 -> v2:
- Split the SEQ/SNE decode cleanup out of the Octeon arithmetic instruction patch. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/tcg/octeon.decode      |  8 ++++++--
 target/mips/tcg/octeon_translate.c | 26 +++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 5 deletions(-)
target/mips/tcg/octeon.decode +6 -2
index 102a05860d..8a262feb1d 100644
@@ -30,6 +30,8 @@ BBIT         11 set:1 . 10 rs:5 ..... offset:s16 p=%bbit_p
 # SNEI rt, rs, immediate
 
 @r3          ...... rs:5 rt:5 rd:5 ..... ......
+&cmp3        rs rt rd ne
+&cmpi        rs rt imm ne
 %bitfield_p  0:1 6:5
 @bitfield    ...... rs:5 rt:5 lenm1:5 ..... ..... . p=%bitfield_p
 
@@ -38,8 +40,10 @@ DMUL         011100 ..... ..... ..... 00000 000011 @r3
 EXTS         011100 ..... ..... ..... ..... 11101 . @bitfield
 CINS         011100 ..... ..... ..... ..... 11001 . @bitfield
 POP          011100 rs:5 00000 rd:5 00000 10110 dw:1
-SEQNE        011100 rs:5 rt:5 rd:5 00000 10101 ne:1
-SEQNEI       011100 rs:5 rt:5 imm:s10 10111 ne:1
+SEQ          011100 rs:5 rt:5 rd:5 00000 101010 &cmp3 ne=0
+SNE          011100 rs:5 rt:5 rd:5 00000 101011 &cmp3 ne=1
+SEQI         011100 rs:5 rt:5 imm:s10 101110 &cmpi ne=0
+SNEI         011100 rs:5 rt:5 imm:s10 101111 &cmpi ne=1
 
 &lx          base index rd
 @lx          ...... base:5 index:5 rd:5 ...... ..... &lx
target/mips/tcg/octeon_translate.c +23 -3
index 41e0d770f5..fce01eb196 100644
@@ -131,7 +131,7 @@ static bool trans_POP(DisasContext *ctx, arg_POP *a)
     return true;
 }
 
-static bool trans_SEQNE(DisasContext *ctx, arg_SEQNE *a)
+static bool trans_seqne(DisasContext *ctx, const arg_cmp3 *a)
 {
     TCGv t0, t1;
 
@@ -154,7 +154,17 @@ static bool trans_SEQNE(DisasContext *ctx, arg_SEQNE *a)
     return true;
 }
 
-static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a)
+static bool trans_SEQ(DisasContext *ctx, arg_cmp3 *a)
+{
+    return trans_seqne(ctx, a);
+}
+
+static bool trans_SNE(DisasContext *ctx, arg_cmp3 *a)
+{
+    return trans_seqne(ctx, a);
+}
+
+static bool trans_seqnei(DisasContext *ctx, const arg_cmpi *a)
 {
     TCGv t0;
 
@@ -168,7 +178,7 @@ static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a)
     gen_load_gpr(t0, a->rs);
 
     /* Sign-extend to 64 bit value */
-    target_ulong imm = a->imm;
+    int64_t imm = a->imm;
     if (a->ne) {
         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_gpr[a->rt], t0, imm);
     } else {
@@ -177,6 +187,16 @@ static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a)
     return true;
 }
 
+static bool trans_SEQI(DisasContext *ctx, arg_cmpi *a)
+{
+    return trans_seqnei(ctx, a);
+}
+
+static bool trans_SNEI(DisasContext *ctx, arg_cmpi *a)
+{
+    return trans_seqnei(ctx, a);
+}
+
 static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
 {
     gen_lx(ctx, a->rd, a->base, a->index, mop);
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH v2 03/13] target/mips: split Octeon SEQ/SNE decode

Quoted 32 lines · James Hilliard

Decode the equality and inequality forms as explicit SEQ/SNE and SEQI/SNEI instructions rather than using shared generated SEQNE/SEQNEI entries.

The explicit decoder names match the architectural mnemonics, which makes the translator entry points and trace/debug output easier to correlate with the instruction set.

---
Changes v1 -> v2:
- Split the SEQ/SNE decode cleanup out of the Octeon arithmetic instruction patch. (suggested by Philippe Mathieu-Daudé)

---
  target/mips/tcg/octeon.decode      |  8 ++++++--
  target/mips/tcg/octeon_translate.c | 26 +++++++++++++++++++++++---
  2 files changed, 29 insertions(+), 5 deletions(-)
target/mips/tcg/octeon.decode +2 -0
index 102a05860d..8a262feb1d 100644
@@ -30,6 +30,8 @@ BBIT         11 set:1 . 10 rs:5 ..... offset:s16 p=%bbit_p
  # SNEI rt, rs, immediate
 
  @r3          ...... rs:5 rt:5 rd:5 ..... ......
+&cmp3        rs rt rd ne
+&cmpi        rs rt imm ne
  %bitfield_p  0:1 6:5
  @bitfield    ...... rs:5 rt:5 lenm1:5 ..... ..... . p=%bitfield_p
 

target/mips/tcg/octeon.decode DMUL 011100 ..... ..... ..... 00000 000011 @r3 · line 46

Quoted 9 lines
target/mips/tcg/octeon.decode +4 -2
@@ -38,8 +40,10 @@ DMUL         011100 ..... ..... ..... 00000 000011 @r3
  EXTS         011100 ..... ..... ..... ..... 11101 . @bitfield
  CINS         011100 ..... ..... ..... ..... 11001 . @bitfield
  POP          011100 rs:5 00000 rd:5 00000 10110 dw:1
-SEQNE        011100 rs:5 rt:5 rd:5 00000 10101 ne:1
-SEQNEI       011100 rs:5 rt:5 imm:s10 10111 ne:1
+SEQ          011100 rs:5 rt:5 rd:5 00000 101010 &cmp3 ne=0
+SNE          011100 rs:5 rt:5 rd:5 00000 101011 &cmp3 ne=1
+SEQI         011100 rs:5 rt:5 imm:s10 101110 &cmpi ne=0
+SNEI         011100 rs:5 rt:5 imm:s10 101111 &cmpi ne=1

By splitting the insns, you no longer need the NE field. You can also then use @r3 to avoid repeating the arguments.

target/mips/tcg/octeon.decode DMUL 011100 ..... ..... ..... 00000 000011 @r3 · line 47

Quoted 2 lines
target/mips/tcg/octeon.decode +1 -1
-static bool trans_SEQNE(DisasContext *ctx, arg_SEQNE *a)
+static bool trans_seqne(DisasContext *ctx, const arg_cmp3 *a)

This would become

static bool do_seqne(DisasContext *ctx, arg_r3 *a, TCGCond cond)

{
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();

    gen_load_gpr(t0, a->rs);
    gen_load_gpr(t1, a->rt);
    tcg_gen_setcond_tl(cond, t0, t0, t1);
    gen_store_gpr(t0, a->rd);
}

static bool trans_SEQ(DisasContext *ctx, arg_r3 *a)

{
    return do_seqne(ctx, a, TCG_COND_EQ);
}

etc.

r~

#7 lore raw

PATCH v2 4/13 target/mips: use explicit i64 TCG ops for Octeon translation

In reply to James Hilliard

Octeon instructions are only available on 64-bit CPUs. Use explicit i64 TCG temporaries and operations in the existing Octeon translator paths, and reject 64-bit-only forms when decoding for a non-64-bit target.

This matches the architectural restriction directly and avoids depending on target_ulong width in translator code that is only valid for MIPS64.

---
 target/mips/tcg/octeon_translate.c | 76 +++++++++++++++++++++++---------------
 1 file changed, 47 insertions(+), 29 deletions(-)
target/mips/tcg/octeon_translate.c +47 -29
index fce01eb196..73090ce6d8 100644
@@ -13,9 +13,15 @@
 /* Include the auto-generated decoder.  */
 #include "decode-octeon.c.inc"
 
+static bool octeon_check_64(DisasContext *ctx)
+{
+    check_mips_64(ctx);
+    return ctx->base.is_jmp == DISAS_NEXT;
+}
+
 static bool trans_BBIT(DisasContext *ctx, arg_BBIT *a)
 {
-    TCGv p;
+    TCGv_i64 p;
 
     if (ctx->hflags & MIPS_HFLAG_BMASK) {
         LOG_DISAS("Branch in delay / forbidden slot at PC 0x%" VADDR_PRIx "\n",
@@ -25,14 +31,14 @@ static bool trans_BBIT(DisasContext *ctx, arg_BBIT *a)
     }
 
     /* Load needed operands */
-    TCGv t0 = tcg_temp_new();
+    TCGv_i64 t0 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
 
-    p = tcg_constant_tl(1ULL << a->p);
+    p = tcg_constant_i64(1ULL << a->p);
     if (a->set) {
-        tcg_gen_and_tl(bcond, p, t0);
+        tcg_gen_and_i64(bcond, p, t0);
     } else {
-        tcg_gen_andc_tl(bcond, p, t0);
+        tcg_gen_andc_i64(bcond, p, t0);
     }
 
     ctx->hflags |= MIPS_HFLAG_BC;
@@ -43,15 +49,15 @@ static bool trans_BBIT(DisasContext *ctx, arg_BBIT *a)
 
 static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
 {
-    TCGv t0, t1;
+    TCGv_i64 t0, t1;
 
     if (a->rd == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
-    t1 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
+    t1 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
     gen_load_gpr(t1, a->rt);
 
@@ -63,15 +69,19 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
 
 static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
 {
-    TCGv t0, t1;
+    TCGv_i64 t0, t1;
+
+    if (!octeon_check_64(ctx)) {
+        return true;
+    }
 
     if (a->rd == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
-    t1 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
+    t1 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
     gen_load_gpr(t1, a->rt);
 
@@ -82,74 +92,78 @@ static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
 
 static bool trans_EXTS(DisasContext *ctx, arg_EXTS *a)
 {
-    TCGv t0;
+    TCGv_i64 t0;
 
     if (a->rt == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
-    tcg_gen_sextract_tl(t0, t0, a->p, a->lenm1 + 1);
+    tcg_gen_sextract_i64(t0, t0, a->p, a->lenm1 + 1);
     gen_store_gpr(t0, a->rt);
     return true;
 }
 
 static bool trans_CINS(DisasContext *ctx, arg_CINS *a)
 {
-    TCGv t0;
+    TCGv_i64 t0;
 
     if (a->rt == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
-    tcg_gen_deposit_z_tl(t0, t0, a->p, a->lenm1 + 1);
+    tcg_gen_deposit_z_i64(t0, t0, a->p, a->lenm1 + 1);
     gen_store_gpr(t0, a->rt);
     return true;
 }
 
 static bool trans_POP(DisasContext *ctx, arg_POP *a)
 {
-    TCGv t0;
+    TCGv_i64 t0;
+
+    if (a->dw && !octeon_check_64(ctx)) {
+        return true;
+    }
 
     if (a->rd == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
     gen_load_gpr(t0, a->rs);
     if (!a->dw) {
         tcg_gen_andi_i64(t0, t0, 0xffffffff);
     }
-    tcg_gen_ctpop_tl(t0, t0);
+    tcg_gen_ctpop_i64(t0, t0);
     gen_store_gpr(t0, a->rd);
     return true;
 }
 
 static bool trans_seqne(DisasContext *ctx, const arg_cmp3 *a)
 {
-    TCGv t0, t1;
+    TCGv_i64 t0, t1;
 
     if (a->rd == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
-    t1 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
+    t1 = tcg_temp_new_i64();
 
     gen_load_gpr(t0, a->rs);
     gen_load_gpr(t1, a->rt);
 
     if (a->ne) {
-        tcg_gen_setcond_tl(TCG_COND_NE, cpu_gpr[a->rd], t1, t0);
+        tcg_gen_setcond_i64(TCG_COND_NE, cpu_gpr[a->rd], t1, t0);
     } else {
-        tcg_gen_setcond_tl(TCG_COND_EQ, cpu_gpr[a->rd], t1, t0);
+        tcg_gen_setcond_i64(TCG_COND_EQ, cpu_gpr[a->rd], t1, t0);
     }
     return true;
 }
@@ -166,23 +180,23 @@ static bool trans_SNE(DisasContext *ctx, arg_cmp3 *a)
 
 static bool trans_seqnei(DisasContext *ctx, const arg_cmpi *a)
 {
-    TCGv t0;
+    TCGv_i64 t0;
 
     if (a->rt == 0) {
         /* nop */
         return true;
     }
 
-    t0 = tcg_temp_new();
+    t0 = tcg_temp_new_i64();
 
     gen_load_gpr(t0, a->rs);
 
     /* Sign-extend to 64 bit value */
     int64_t imm = a->imm;
     if (a->ne) {
-        tcg_gen_setcondi_tl(TCG_COND_NE, cpu_gpr[a->rt], t0, imm);
+        tcg_gen_setcondi_i64(TCG_COND_NE, cpu_gpr[a->rt], t0, imm);
     } else {
-        tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_gpr[a->rt], t0, imm);
+        tcg_gen_setcondi_i64(TCG_COND_EQ, cpu_gpr[a->rt], t0, imm);
     }
     return true;
 }
@@ -199,6 +213,10 @@ static bool trans_SNEI(DisasContext *ctx, arg_cmpi *a)
 
 static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
 {
+    if (mop == MO_UQ && !octeon_check_64(ctx)) {
+        return true;
+    }
+
     gen_lx(ctx, a->rd, a->base, a->index, mop);
 
     return true;
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v2 04/13] target/mips: use explicit i64 TCG ops for Octeon translation

Quoted 17 lines · James Hilliard

Octeon instructions are only available on 64-bit CPUs. Use explicit i64 TCG temporaries and operations in the existing Octeon translator paths, and reject 64-bit-only forms when decoding for a non-64-bit target.

This matches the architectural restriction directly and avoids depending on target_ulong width in translator code that is only valid for MIPS64.

---
  target/mips/tcg/octeon_translate.c | 76 +++++++++++++++++++++++---------------
  1 file changed, 47 insertions(+), 29 deletions(-)
target/mips/tcg/octeon_translate.c +0 -0
index fce01eb196..73090ce6d8 100644

target/mips/tcg/octeon_translate.c line 21

Quoted 9 lines
target/mips/tcg/octeon_translate.c +6 -0
@@ -13,9 +13,15 @@
  /* Include the auto-generated decoder.  */
  #include "decode-octeon.c.inc"
 
+static bool octeon_check_64(DisasContext *ctx)
+{
+    check_mips_64(ctx);
+    return ctx->base.is_jmp == DISAS_NEXT;
+}
+

Why are you checking is_jmp?

The check_mips_64 test is redundant with the tests in translate.c that precede decode_ext_octeon. So the *only* thing you should be doing here is s/_tl/_i64/ et at.

r~

#9 lore raw

PATCH v2 5/13 target/mips: add Octeon arithmetic and memory instructions

In reply to James Hilliard

Octeon CPUs define additional integer, memory, and multiply operations beyond the base MIPS64 ISA. Add the missing decode and helper support for the remaining regular Octeon instructions used by existing user-mode workloads.

This covers the indexed load forms, cache block zero operations, the Octeon multiply family, population count handling, and related arithmetic and atomic memory operations in the translator and helper layer.

Add a TCG smoke test that exercises the arithmetic, compare, population count, and multiplier instructions introduced here.

---

Changes v1 -> v2:

- Fold the initial Octeon arithmetic and multiplier smoke test into this patch.

- Keep SAA/SAAD atomics naturally aligned for Octeon L2 transactions.

- Split pre-existing BADDU/DMUL and SEQ/SNE translator changes into preliminary patches. (suggested by Philippe Mathieu-Daudé)

- Store Octeon multiplier state in uint64_t arrays instead of target_ulong scalar fields. (suggested by Philippe Mathieu-Daudé)

- Keep Octeon68XX CPU feature exposure out of this patch. (suggested by Philippe Mathieu-Daudé)

---
 MAINTAINERS                                   |   2 +
 target/mips/cpu.h                             |   4 +
 target/mips/helper.h                          |   3 +
 target/mips/system/machine.c                  |  33 +++++
 target/mips/tcg/octeon.decode                 |  22 ++++
 target/mips/tcg/octeon_translate.c            | 166 ++++++++++++++++++++++++++
 target/mips/tcg/op_helper.c                   |  99 +++++++++++++++
 tests/tcg/mips/Makefile.target                |  11 ++
 tests/tcg/mips/user/isa/octeon/octeon-insns.c | 145 ++++++++++++++++++++++
 tests/tcg/mips64/Makefile.target              |  20 ++++
 tests/tcg/mips64el/Makefile.target            |   8 ++
 11 files changed, 513 insertions(+)
MAINTAINERS +2 -0
index ad215eced8..3cfb10d076 100644
@@ -306,6 +306,8 @@ F: target/mips/
 F: disas/*mips.c
 F: docs/system/cpu-models-mips.rst.inc
 F: tests/tcg/mips/
+F: tests/tcg/mips64/
+F: tests/tcg/mips64el/
 
 OpenRISC TCG CPUs
 M: Stafford Horne <[email protected]>
target/mips/cpu.h +4 -0
index ac81470576..540edf2906 100644
@@ -497,6 +497,10 @@ struct TCState {
     target_ulong CP0_TCScheFBack;
     int32_t CP0_Debug_tcstatus;
     target_ulong CP0_UserLocal;
+    struct {
+        uint64_t MPL[2 * 3];
+        uint64_t P[2 * 3];
+    } octeon;
 
     int32_t msacsr;
 
target/mips/helper.h +3 -0
index b6cd53c853..756ef92bfd 100644
@@ -24,6 +24,9 @@ DEF_HELPER_FLAGS_1(dbitswap, TCG_CALL_NO_RWG_SE, tl, tl)
 DEF_HELPER_3(crc32, tl, tl, tl, i32)
 DEF_HELPER_3(crc32c, tl, tl, tl, i32)
 DEF_HELPER_FLAGS_4(rotx, TCG_CALL_NO_RWG_SE, tl, tl, i32, i32, i32)
+DEF_HELPER_3(octeon_vmulu, i64, env, i64, i64)
+DEF_HELPER_3(octeon_vmm0, i64, env, i64, i64)
+DEF_HELPER_3(octeon_v3mulu, i64, env, i64, i64)
 
 /* microMIPS functions */
 DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/system/machine.c +33 -0
index 473d3ab036..c29b7c56fa 100644
@@ -118,6 +118,17 @@ static const VMStateDescription vmstate_inactive_tc = {
     .fields = vmstate_tc_fields
 };
 
+static const VMStateDescription vmstate_octeon_multiplier_tc = {
+    .name = "cpu/tc/octeon_multiplier",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT64_ARRAY(octeon.MPL, TCState, 2 * 3),
+        VMSTATE_UINT64_ARRAY(octeon.P, TCState, 2 * 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 /* MVP state */
 
 static const VMStateDescription vmstate_mvp = {
@@ -237,6 +248,27 @@ static const VMStateDescription mips_vmstate_timer = {
     }
 };
 
+static bool mips_octeon_needed(void *opaque)
+{
+    MIPSCPU *cpu = opaque;
+
+    return cpu->env.insn_flags & INSN_OCTEON;
+}
+
+static const VMStateDescription mips_vmstate_octeon_multiplier = {
+    .name = "cpu/octeon_multiplier",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = mips_octeon_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_STRUCT(env.active_tc, MIPSCPU, 1,
+                       vmstate_octeon_multiplier_tc, TCState),
+        VMSTATE_STRUCT_ARRAY(env.tcs, MIPSCPU, MIPS_SHADOW_SET_MAX, 1,
+                             vmstate_octeon_multiplier_tc, TCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 const VMStateDescription vmstate_mips_cpu = {
     .name = "cpu",
     .version_id = 21,
@@ -353,6 +385,7 @@ const VMStateDescription vmstate_mips_cpu = {
     },
     .subsections = (const VMStateDescription * const []) {
         &mips_vmstate_timer,
+        &mips_vmstate_octeon_multiplier,
         NULL
     }
 };
target/mips/tcg/octeon.decode +22 -0
index 8a262feb1d..2eb15aa17f 100644
@@ -44,10 +44,32 @@ SEQ          011100 rs:5 rt:5 rd:5 00000 101010 &cmp3 ne=0
 SNE          011100 rs:5 rt:5 rd:5 00000 101011 &cmp3 ne=1
 SEQI         011100 rs:5 rt:5 imm:s10 101110 &cmpi ne=0
 SNEI         011100 rs:5 rt:5 imm:s10 101111 &cmpi ne=1
+&r2          rs rt
+MTM0         011100 rs:5 rt:5 00000 00000 001000 &r2
+MTP0         011100 rs:5 rt:5 00000 00000 001001 &r2
+MTP1         011100 rs:5 rt:5 00000 00000 001010 &r2
+MTP2         011100 rs:5 rt:5 00000 00000 001011 &r2
+MTM1         011100 rs:5 rt:5 00000 00000 001100 &r2
+MTM2         011100 rs:5 rt:5 00000 00000 001101 &r2
+VMULU        011100 ..... ..... ..... 00000 001111 @r3
+VMM0         011100 ..... ..... ..... 00000 010000 @r3
+V3MULU       011100 ..... ..... ..... 00000 010001 @r3
+
+&saa         base rt
+@saa         ...... base:5 rt:5 ................ &saa
+SAA          011100 ..... ..... 00000 00000 011000 @saa
+SAAD         011100 ..... ..... 00000 00000 011001 @saa
+
+&zcb         base
+ZCB          011100 base:5 00000 00000 11100 011111 &zcb
+ZCBT         011100 base:5 00000 00000 11101 011111 &zcb
 
 &lx          base index rd
 @lx          ...... base:5 index:5 rd:5 ...... ..... &lx
 LWX          011111 ..... ..... ..... 00000 001010 @lx
 LHX          011111 ..... ..... ..... 00100 001010 @lx
+LHUX         011111 ..... ..... ..... 10100 001010 @lx
 LBUX         011111 ..... ..... ..... 00110 001010 @lx
+LWUX         011111 ..... ..... ..... 10000 001010 @lx
+LBX          011111 ..... ..... ..... 10110 001010 @lx
 LDX          011111 ..... ..... ..... 01000 001010 @lx
target/mips/tcg/octeon_translate.c +166 -0

179 lines not on this page · raw message

target/mips/tcg/op_helper.c +99 -0

105 lines not on this page · raw message

tests/tcg/mips/Makefile.target +11 -0
index 5d17c1706e..d9dc16f8ec 100644
@@ -8,6 +8,17 @@ MIPS_SRC=$(SRC_PATH)/tests/tcg/mips
 # Set search path for all sources
 VPATH 		+= $(MIPS_SRC)
 
+ifneq ($(findstring 64,$(TARGET_NAME)),)
+VPATH += $(MIPS_SRC)/user/isa/octeon
+
+MIPS64_TESTS=octeon-insns
+
+TESTS += $(MIPS64_TESTS)
+
+octeon-insns: CFLAGS+=-mabi=64
+run-octeon-insns: QEMU_OPTS+=-cpu Octeon68XX
+endif
+
 # hello-mips is 32 bit only
 ifeq ($(findstring 64,$(TARGET_NAME)),)
 MIPS_TESTS=hello-mips
tests/tcg/mips/user/isa/octeon/octeon-insns.c new file +145 -0

145 lines not on this page · raw message

tests/tcg/mips64/Makefile.target new file +20 -0
new file mode 100644
index 0000000000..042855844a
@@ -0,0 +1,20 @@
+# -*- Mode: makefile -*-
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# MIPS64 - included from tests/tcg/Makefile.target
+#
+
+MIPS64_SRC=$(SRC_PATH)/tests/tcg/mips64
+MIPS_OCTEON_SRC=$(SRC_PATH)/tests/tcg/mips/user/isa/octeon
+
+# Set search path for all sources
+VPATH 		+= $(MIPS64_SRC) $(MIPS_OCTEON_SRC)
+
+MIPS64_TESTS=octeon-insns
+
+TESTS += $(MIPS64_TESTS)
+
+$(MIPS64_TESTS): CFLAGS+=-mabi=64
+
+run-octeon-insns: QEMU_OPTS+=-cpu Octeon68XX
tests/tcg/mips64el/Makefile.target new file +8 -0
new file mode 100644
index 0000000000..dbc5f8dc5f
@@ -0,0 +1,8 @@
+# -*- Mode: makefile -*-
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# MIPS64 little-endian - included from tests/tcg/Makefile.target
+#
+
+include $(SRC_PATH)/tests/tcg/mips64/Makefile.target
Signature · 1 line
2.53.0
#10 lore raw

Re: [PATCH v2 05/13] target/mips: add Octeon arithmetic and memory instructions

Quoted 24 lines · James Hilliard

Octeon CPUs define additional integer, memory, and multiply operations beyond the base MIPS64 ISA. Add the missing decode and helper support for the remaining regular Octeon instructions used by existing user-mode workloads.

This covers the indexed load forms, cache block zero operations, the Octeon multiply family, population count handling, and related arithmetic and atomic memory operations in the translator and helper layer.

Add a TCG smoke test that exercises the arithmetic, compare, population count, and multiplier instructions introduced here.

---

Changes v1 -> v2:

- Fold the initial Octeon arithmetic and multiplier smoke test into this patch.

- Keep SAA/SAAD atomics naturally aligned for Octeon L2 transactions.

Quoted 8 lines

- Split pre-existing BADDU/DMUL and SEQ/SNE translator changes into preliminary patches. (suggested by Philippe Mathieu-Daudé)

- Store Octeon multiplier state in uint64_t arrays instead of target_ulong scalar fields. (suggested by Philippe Mathieu-Daudé)

- Keep Octeon68XX CPU feature exposure out of this patch. (suggested by Philippe Mathieu-Daudé)

This really needs to be split per instruction, and one just for adding the new register state.

Quoted 4 lines
target/mips/tcg/op_helper.c +4 -0
+static void octeon_add_limb(uint64_t *sum, int limb_count,
+                            uint64_t value, int limb)
+{
+    while (limb < limb_count) {

target/mips/tcg/op_helper.c target_ulong helper_rotx(target_ulong rs, uint32_t shift, uint32_t shiftx, · line 160

Quoted 10 lines
target/mips/tcg/op_helper.c +10 -0
+        uint64_t old = sum[limb];
+
+        sum[limb] += value;
+        if (sum[limb] >= old) {
+            break;
+        }
+        value = 1;
+        limb++;
+    }
+}

You may get better out of the compiler with uadd64_overflow().

r~

#11 lore raw

PATCH v2 6/13 target/mips: add Octeon LA* atomic instructions

In reply to James Hilliard

Implement the Octeon LA* read-modify-write atomic instruction family:
LAI/LAID, LAD/LADD, LAA/LAAD, LAS/LASD, LAC/LACD, and LAW/LAWD.

These operations are architecturally distinct from SAA/SAAD and are used by existing Octeon user-mode code for atomic counters, bit operations, and exchange-style updates.

---
Changes v1 -> v2:
- Keep LA* atomics naturally aligned per Octeon L2 transaction semantics.
- Use explicit i64 TCG ops in the LA* translator paths. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/tcg/octeon.decode      |  17 ++++
 target/mips/tcg/octeon_translate.c | 170 +++++++++++++++++++++++++++++++++++++
 2 files changed, 187 insertions(+)
target/mips/tcg/octeon.decode +17 -0
index 2eb15aa17f..9dcc5a6ae0 100644
@@ -60,6 +60,23 @@ V3MULU       011100 ..... ..... ..... 00000 010001 @r3
 SAA          011100 ..... ..... 00000 00000 011000 @saa
 SAAD         011100 ..... ..... 00000 00000 011001 @saa
 
+&la          base rd
+&laa         base add rd
+@la          ...... base:5 ..... rd:5 ........... &la
+@laa         ...... base:5 add:5 rd:5 ........... &laa
+LAI          011100 ..... 00000 ..... 00010 011111 @la
+LAID         011100 ..... 00000 ..... 00011 011111 @la
+LAD          011100 ..... 00000 ..... 00110 011111 @la
+LADD         011100 ..... 00000 ..... 00111 011111 @la
+LAA          011100 ..... ..... ..... 10010 011111 @laa
+LAAD         011100 ..... ..... ..... 10011 011111 @laa
+LAS          011100 ..... 00000 ..... 01010 011111 @la
+LASD         011100 ..... 00000 ..... 01011 011111 @la
+LAC          011100 ..... 00000 ..... 01110 011111 @la
+LACD         011100 ..... 00000 ..... 01111 011111 @la
+LAW          011100 ..... ..... ..... 10110 011111 @laa
+LAWD         011100 ..... ..... ..... 10111 011111 @laa
+
 &zcb         base
 ZCB          011100 base:5 00000 00000 11100 011111 &zcb
 ZCBT         011100 base:5 00000 00000 11101 011111 &zcb
target/mips/tcg/octeon_translate.c +170 -0

182 lines not on this page · raw message

Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH v2 06/13] target/mips: add Octeon LA* atomic instructions

Quoted 71 lines · James Hilliard

Implement the Octeon LA* read-modify-write atomic instruction family:
LAI/LAID, LAD/LADD, LAA/LAAD, LAS/LASD, LAC/LACD, and LAW/LAWD.

These operations are architecturally distinct from SAA/SAAD and are used by existing Octeon user-mode code for atomic counters, bit operations, and exchange-style updates.

---
Changes v1 -> v2:
- Keep LA* atomics naturally aligned per Octeon L2 transaction semantics.
- Use explicit i64 TCG ops in the LA* translator paths. (suggested by Philippe Mathieu-Daudé)

---
  target/mips/tcg/octeon.decode      |  17 ++++
  target/mips/tcg/octeon_translate.c | 170 +++++++++++++++++++++++++++++++++++++
  2 files changed, 187 insertions(+)
target/mips/tcg/octeon.decode +17 -0
index 2eb15aa17f..9dcc5a6ae0 100644
@@ -60,6 +60,23 @@ V3MULU       011100 ..... ..... ..... 00000 010001 @r3
  SAA          011100 ..... ..... 00000 00000 011000 @saa
  SAAD         011100 ..... ..... 00000 00000 011001 @saa
 
+&la          base rd
+&laa         base add rd
+@la          ...... base:5 ..... rd:5 ........... &la
+@laa         ...... base:5 add:5 rd:5 ........... &laa
+LAI          011100 ..... 00000 ..... 00010 011111 @la
+LAID         011100 ..... 00000 ..... 00011 011111 @la
+LAD          011100 ..... 00000 ..... 00110 011111 @la
+LADD         011100 ..... 00000 ..... 00111 011111 @la
+LAA          011100 ..... ..... ..... 10010 011111 @laa
+LAAD         011100 ..... ..... ..... 10011 011111 @laa
+LAS          011100 ..... 00000 ..... 01010 011111 @la
+LASD         011100 ..... 00000 ..... 01011 011111 @la
+LAC          011100 ..... 00000 ..... 01110 011111 @la
+LACD         011100 ..... 00000 ..... 01111 011111 @la
+LAW          011100 ..... ..... ..... 10110 011111 @laa
+LAWD         011100 ..... ..... ..... 10111 011111 @laa
+
  &zcb         base
  ZCB          011100 base:5 00000 00000 11100 011111 &zcb
  ZCBT         011100 base:5 00000 00000 11101 011111 &zcb
target/mips/tcg/octeon_translate.c +16 -0
index cdb4d36c8e..6309cd5fff 100644
@@ -254,6 +254,164 @@ static bool trans_saa(DisasContext *ctx, arg_saa *a, MemOp mop)
      return true;
  }
 
+static bool trans_la_common(DisasContext *ctx, int base, int add_reg, int rd,
+                            int64_t imm, bool dw)
+{
+    if (dw && !octeon_check_64(ctx)) {
+        return true;
+    }
+
+    TCGv_i64 addr = tcg_temp_new_i64();
+
+    gen_base_offset_addr(ctx, addr, base, 0);
+
+    if (dw) {
+#if TARGET_LONG_BITS == 64
+        TCGv_i64 value = tcg_temp_new_i64();
+        TCGv_i64 old = tcg_temp_new_i64();
+        MemOp amo = mo_endian(ctx) | MO_UQ | MO_ALIGN;

target/mips/tcg/octeon_translate.c static bool trans_saa(DisasContext *ctx, arg_saa *a, MemOp mop) · line 282

Quoted 10 lines
target/mips/tcg/octeon_translate.c +10 -0
+
+        if (add_reg >= 0) {
+            gen_load_gpr(value, add_reg);
+        } else {
+            tcg_gen_movi_i64(value, imm);
+        }
+
+        tcg_gen_atomic_fetch_add_i64(old, addr, value, ctx->mem_idx, amo);
+        gen_store_gpr(old, rd);
+#endif

Surely patch 2 was all about octeon being only for 64-bit cpus. Why the ifdef?

r~

#13 lore raw

Re: [PATCH v2 06/13] target/mips: add Octeon LA* atomic instructions

In reply to James Hilliard
Quoted 72 lines · James Hilliard
target/mips/tcg/octeon_translate.c +65 -0
+static bool trans_lai(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_la_common(ctx, a->base, -1, a->rd, 1, false);
+}
+
+static bool trans_laid(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_la_common(ctx, a->base, -1, a->rd, 1, true);
+}
+
+static bool trans_lad(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_la_common(ctx, a->base, -1, a->rd, -1, false);
+}
+
+static bool trans_ladd(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_la_common(ctx, a->base, -1, a->rd, -1, true);
+}
+
+static bool trans_laa(DisasContext *ctx, arg_laa *a, int unused)
+{
+    return trans_la_common(ctx, a->base, a->add, a->rd, 0, false);
+}
+
+static bool trans_laad(DisasContext *ctx, arg_laa *a, int unused)
+{
+    return trans_la_common(ctx, a->base, a->add, a->rd, 0, true);
+}
+
+static bool trans_las(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_law_common(ctx, a->base, -1, a->rd, -1, false);
+}
+
+static bool trans_lasd(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_law_common(ctx, a->base, -1, a->rd, -1, true);
+}
+
+static bool trans_lac(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_law_common(ctx, a->base, -1, a->rd, 0, false);
+}
+
+static bool trans_lacd(DisasContext *ctx, arg_la *a, int unused)
+{
+    return trans_law_common(ctx, a->base, -1, a->rd, 0, true);
+}
+
+static bool trans_law(DisasContext *ctx, arg_laa *a, int unused)
+{
+    return trans_law_common(ctx, a->base, a->add, a->rd, 0, false);
+}
+
+static bool trans_lawd(DisasContext *ctx, arg_laa *a, int unused)
+{
+    return trans_law_common(ctx, a->base, a->add, a->rd, 0, true);
+}
+
  static bool trans_ZCB(DisasContext *ctx, arg_zcb *a)
  {
      TCGv_i64 addr = tcg_temp_new_i64();
@@ -376,6 +534,18 @@ static bool trans_vmul(DisasContext *ctx, arg_decode_ext_octeon1 *a,
 
  TRANS(SAA,  trans_saa, MO_UL);
  TRANS(SAAD, trans_saa, MO_UQ);
+TRANS(LAI,  trans_lai, 0);
+TRANS(LAID, trans_laid, 0);
+TRANS(LAD,  trans_lad, 0);
+TRANS(LADD, trans_ladd, 0);
+TRANS(LAA,  trans_laa, 0);

target/mips/tcg/octeon_translate.c static bool trans_vmul(DisasContext *ctx, arg_decode_ext_octeon1 *a, · line 551

Quoted 10 lines
target/mips/tcg/octeon_translate.c +7 -0
+TRANS(LAAD, trans_laad, 0);
+TRANS(LAS,  trans_las, 0);
+TRANS(LASD, trans_lasd, 0);
+TRANS(LAC,  trans_lac, 0);
+TRANS(LACD, trans_lacd, 0);
+TRANS(LAW,  trans_law, 0);
+TRANS(LAWD, trans_lawd, 0);
  TRANS(LBX,  trans_lx, MO_SB);
  TRANS(LBUX, trans_lx, MO_UB);
  TRANS(LHX,  trans_lx, MO_SW);

This is incorrect usage of TRANS(), or a mis-feature of the mips version of TRANS().

If TRANS() is changed such that 'a' is not implied, then you can expand the macro here:

TRANS(LAI, trans_la_common, a->base, -1, a->rd, 1, false)

Otherwise, isolate common usage within each argument set:

TRANS(LAI, do_lai, 1, false)
TRANS(LAID, do_lai, 1, true)
TRANS(LAD, do_lai, -1, false)
TRANS(LADD, do_lai, -1, true)

TRANS(LAA, do_laa, 1, false)
TRANS(LAAD, do_laa, 1, true)

etc.

#14 lore raw

PATCH v2 7/13 target/mips: add Octeon COP2 crypto core support

In reply to James Hilliard

Octeon processors expose their crypto engines through selector-driven DMFC2/DMTC2 accesses rather than architected standalone opcodes. Add the common COP2 state, selector decode, and helper plumbing for the base engine set.

This covers the hash, AES, CRC, GFM, 3DES, KASUMI, and SNOW3G engines and moves the implementation into octeon_crypto.c to keep the MIPS helper layer manageable.

Model the AES result/input selector bank as writable as well as readable. Octeon COP2 engines use these slots as result-input staging registers, so the shared bank belongs with the base selector support.

Extend the TCG smoke test with AES key-register readback checks for the selector window introduced here.

---
Changes v1 -> v2:
- Fold the AES COP2 selector readback smoke coverage into this patch.
- Move Octeon COP2 decode plumbing into octeon_translate.c. (suggested by Philippe Mathieu-Daudé)
- Use uint64_t/i64 helper types for Octeon COP2 state transfers. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.h                             |  165 +++
 target/mips/helper.h                          |    2 +
 target/mips/system/machine.c                  |   37 +
 target/mips/tcg/meson.build                   |    1 +
 target/mips/tcg/octeon_crypto.c               | 1654 +++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c            |  210 ++++
 target/mips/tcg/translate.c                   |    9 +
 target/mips/tcg/translate.h                   |    1 +
 tests/tcg/mips/user/isa/octeon/octeon-insns.c |   71 ++
 9 files changed, 2150 insertions(+)
target/mips/cpu.h +165 -0
index 540edf2906..9e398e1ce5 100644
@@ -529,6 +529,169 @@ struct TCState {
 };
 
 struct MIPSITUState;
+typedef enum MIPSOcteonSharedMode {
+    OCTEON_SHARED_MODE_NONE = 0,
+    OCTEON_SHARED_MODE_SHA512,
+    OCTEON_SHARED_MODE_SNOW3G,
+} MIPSOcteonSharedMode;
+
+typedef enum MIPSOcteonCop2Sel {
+    OCTEON_COP2_SEL_HSH_DAT0 = 0x0040,
+    OCTEON_COP2_SEL_HSH_DAT1,
+    OCTEON_COP2_SEL_HSH_DAT2,
+    OCTEON_COP2_SEL_HSH_DAT3,
+    OCTEON_COP2_SEL_HSH_DAT4,
+    OCTEON_COP2_SEL_HSH_DAT5,
+    OCTEON_COP2_SEL_HSH_DAT6,
+    OCTEON_COP2_SEL_HSH_IV0 = 0x0048,
+    OCTEON_COP2_SEL_HSH_IV1,
+    OCTEON_COP2_SEL_HSH_IV2,
+    OCTEON_COP2_SEL_HSH_IV3,
+    OCTEON_COP2_SEL_SHA3_DAT24 = 0x0050,
+    OCTEON_COP2_SEL_SHA3_DAT15_WRITE = 0x0051,
+    OCTEON_COP2_SEL_HSH_STARTSHA1_COMPAT = 0x0057,
+    OCTEON_COP2_SEL_GFM_MUL_REFLECT0 = 0x0058,
+    OCTEON_COP2_SEL_GFM_MUL_REFLECT1,
+    OCTEON_COP2_SEL_GFM_RESINP_REFLECT0 = 0x005a,
+    OCTEON_COP2_SEL_GFM_RESINP_REFLECT1,
+    OCTEON_COP2_SEL_GFM_XOR0_REFLECT = 0x005c,
+    OCTEON_COP2_SEL_3DES_KEY0 = 0x0080,
+    OCTEON_COP2_SEL_3DES_KEY1,
+    OCTEON_COP2_SEL_3DES_KEY2,
+    OCTEON_COP2_SEL_3DES_IV = 0x0084,
+    OCTEON_COP2_SEL_3DES_RESULT = 0x0088,
+    OCTEON_COP2_SEL_3DES_RESULT_IN = 0x0098,
+    OCTEON_COP2_SEL_3DES_ENC_CBC = 0x4088,
+    /*
+     * Octeon reuses the 3DES key/result bank for KASUMI and only adds
+     * KASUMI-specific operation selectors.
+     */
+    OCTEON_COP2_SEL_KAS_ENC_CBC = 0x4089,
+    OCTEON_COP2_SEL_3DES_ENC = 0x408a,
+    OCTEON_COP2_SEL_KAS_ENC = 0x408b,
+    OCTEON_COP2_SEL_3DES_DEC_CBC = 0x408c,
+    OCTEON_COP2_SEL_3DES_DEC = 0x408e,
+    OCTEON_COP2_SEL_AES_RESULT0 = 0x0100,
+    OCTEON_COP2_SEL_AES_RESULT1,
+    OCTEON_COP2_SEL_AES_IV0,
+    OCTEON_COP2_SEL_AES_IV1,
+    OCTEON_COP2_SEL_AES_KEY0,
+    OCTEON_COP2_SEL_AES_KEY1,
+    OCTEON_COP2_SEL_AES_KEY2,
+    OCTEON_COP2_SEL_AES_KEY3,
+    OCTEON_COP2_SEL_AES_ENC_CBC0 = 0x0108,
+    OCTEON_COP2_SEL_AES_ENC0 = 0x010a,
+    OCTEON_COP2_SEL_AES_DEC_CBC0 = 0x010c,
+    OCTEON_COP2_SEL_AES_DEC0 = 0x010e,
+    OCTEON_COP2_SEL_AES_KEYLENGTH = 0x0110,
+    OCTEON_COP2_SEL_AES_DAT0 = 0x0111,
+    OCTEON_COP2_SEL_CRC_POLYNOMIAL = 0x0200,
+    OCTEON_COP2_SEL_CRC_IV = 0x0201,
+    OCTEON_COP2_SEL_CRC_LEN = 0x0202,
+    OCTEON_COP2_SEL_CRC_IV_REFLECT = 0x0203,
+    OCTEON_COP2_SEL_CRC_WRITE_BYTE = 0x0204,
+    OCTEON_COP2_SEL_CRC_WRITE_HALF = 0x0205,
+    OCTEON_COP2_SEL_CRC_WRITE_WORD = 0x0206,
+    OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL = 0x4200,
+    OCTEON_COP2_SEL_CRC_WRITE_LEN = 0x1202,
+    OCTEON_COP2_SEL_CRC_WRITE_IV_REFLECT = 0x0211,
+    OCTEON_COP2_SEL_CRC_WRITE_BYTE_REFLECT = 0x0214,
+    OCTEON_COP2_SEL_CRC_WRITE_HALF_REFLECT = 0x0215,
+    OCTEON_COP2_SEL_CRC_WRITE_WORD_REFLECT = 0x0216,
+    OCTEON_COP2_SEL_CRC_WRITE_DWORD = 0x1207,
+    OCTEON_COP2_SEL_CRC_WRITE_VAR = 0x1208,
+    OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL_REFLECT = 0x4210,
+    OCTEON_COP2_SEL_CRC_WRITE_DWORD_REFLECT = 0x1217,
+    OCTEON_COP2_SEL_CRC_WRITE_VAR_REFLECT = 0x1218,
+    /*
+     * Octeon shares 0x0240..0x0257 between SHA512 state/data and the SNOW3G
+     * RESULT/FSM/LFSR window.
+     */
+    OCTEON_COP2_SEL_HSH_DATW0 = 0x0240,
+    OCTEON_COP2_SEL_HSH_DATW1,
+    OCTEON_COP2_SEL_HSH_DATW2,
+    OCTEON_COP2_SEL_HSH_DATW3,
+    OCTEON_COP2_SEL_HSH_DATW4,
+    OCTEON_COP2_SEL_HSH_DATW5,
+    OCTEON_COP2_SEL_HSH_DATW6,
+    OCTEON_COP2_SEL_HSH_DATW7,
+    OCTEON_COP2_SEL_HSH_DATW8,
+    OCTEON_COP2_SEL_HSH_DATW9,
+    OCTEON_COP2_SEL_HSH_DATW10,
+    OCTEON_COP2_SEL_HSH_DATW11,
+    OCTEON_COP2_SEL_HSH_DATW12,
+    OCTEON_COP2_SEL_HSH_DATW13,
+    OCTEON_COP2_SEL_HSH_DATW14,
+    OCTEON_COP2_SEL_HSH_DATW15,
+    OCTEON_COP2_SEL_HSH_IVW0 = 0x0250,
+    OCTEON_COP2_SEL_HSH_IVW1,
+    OCTEON_COP2_SEL_HSH_IVW2,
+    OCTEON_COP2_SEL_HSH_IVW3,
+    OCTEON_COP2_SEL_HSH_IVW4,
+    OCTEON_COP2_SEL_HSH_IVW5,
+    OCTEON_COP2_SEL_HSH_IVW6,
+    OCTEON_COP2_SEL_HSH_IVW7,
+    OCTEON_COP2_SEL_SNOW3G_LFSR0 = 0x0240,
+    OCTEON_COP2_SEL_SNOW3G_LFSR1,
+    OCTEON_COP2_SEL_SNOW3G_LFSR2,
+    OCTEON_COP2_SEL_SNOW3G_LFSR3,
+    OCTEON_COP2_SEL_SNOW3G_LFSR4,
+    OCTEON_COP2_SEL_SNOW3G_LFSR5,
+    OCTEON_COP2_SEL_SNOW3G_LFSR6,
+    OCTEON_COP2_SEL_SNOW3G_LFSR7,
+    OCTEON_COP2_SEL_SNOW3G_RESULT = 0x0250,
+    OCTEON_COP2_SEL_SNOW3G_FSM0,
+    OCTEON_COP2_SEL_SNOW3G_FSM1,
+    OCTEON_COP2_SEL_SNOW3G_FSM2,
+    OCTEON_COP2_SEL_GFM_MUL0 = 0x0258,
+    OCTEON_COP2_SEL_GFM_MUL1,
+    OCTEON_COP2_SEL_GFM_RESINP0,
+    OCTEON_COP2_SEL_GFM_RESINP1,
+    OCTEON_COP2_SEL_GFM_XOR0,
+    OCTEON_COP2_SEL_GFM_POLY = 0x025e,
+    OCTEON_COP2_SEL_AES_ENC_CBC1 = 0x3109,
+    OCTEON_COP2_SEL_AES_ENC1 = 0x310b,
+    OCTEON_COP2_SEL_AES_DEC_CBC1 = 0x310d,
+    OCTEON_COP2_SEL_AES_DEC1 = 0x310f,
+    OCTEON_COP2_SEL_HSH_STARTMD5 = 0x4047,
+    OCTEON_COP2_SEL_SNOW3G_START = 0x404d,
+    OCTEON_COP2_SEL_SNOW3G_MORE = 0x404e,
+    OCTEON_COP2_SEL_HSH_STARTSHA256 = 0x404f,
+    OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT = 0x405d,
+    OCTEON_COP2_SEL_HSH_STARTSHA1 = 0x4057,
+    OCTEON_COP2_SEL_HSH_STARTSHA512 = 0x424f,
+    OCTEON_COP2_SEL_GFM_XORMUL1 = 0x425d,
+} MIPSOcteonCop2Sel;
+
+typedef struct MIPSOcteonCryptoState {
+    uint64_t des3_key[3];
+    uint64_t des3_iv;
+    uint64_t des3_result;
+    uint64_t hash_state[4];
+    uint64_t hash_block[8];
+    uint64_t sha512_state[8];
+    uint64_t sha512_block[16];
+    uint64_t aes_iv[2];
+    uint64_t aes_key[4];
+    uint64_t aes_result[2];
+    uint64_t aes_input[2];
+    uint64_t gfm_mul[2];
+    uint64_t gfm_result[2];
+    uint64_t gfm_xor0;
+    uint64_t gfm_reflect_mul[2];
+    uint64_t gfm_reflect_result[2];
+    uint64_t gfm_reflect_xor0;
+    uint16_t gfm_poly;
+    uint8_t aes_keylen;
+    uint32_t shared_mode;
+    uint32_t crc_poly;
+    uint32_t crc_iv;
+    uint32_t crc_len;
+    uint32_t snow3g_fsm[3];
+    uint32_t snow3g_lfsr[16];
+    uint64_t snow3g_result;
+} MIPSOcteonCryptoState;
+
 typedef struct CPUArchState {
     TCState active_tc;
     CPUMIPSFPUContext active_fpu;
@@ -550,6 +713,8 @@ typedef struct CPUArchState {
 #define MSAIR_ProcID    8
 #define MSAIR_Rev       0
 
+    MIPSOcteonCryptoState octeon_crypto;
+
 /*
  * CP0 Register 0
  */
target/mips/helper.h +2 -0
index 756ef92bfd..899120d0ca 100644
@@ -27,6 +27,8 @@ DEF_HELPER_FLAGS_4(rotx, TCG_CALL_NO_RWG_SE, tl, tl, i32, i32, i32)
 DEF_HELPER_3(octeon_vmulu, i64, env, i64, i64)
 DEF_HELPER_3(octeon_vmm0, i64, env, i64, i64)
 DEF_HELPER_3(octeon_v3mulu, i64, env, i64, i64)
+DEF_HELPER_2(octeon_cop2_dmfc2, i64, env, i32)
+DEF_HELPER_3(octeon_cop2_dmtc2, void, env, i64, i32)
 
 /* microMIPS functions */
 DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/system/machine.c +37 -0
index c29b7c56fa..d5452a3157 100644
@@ -269,6 +269,42 @@ static const VMStateDescription mips_vmstate_octeon_multiplier = {
     }
 };
 
+static const VMStateDescription mips_vmstate_octeon_crypto = {
+    .name = "cpu/octeon_crypto",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = mips_octeon_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.des3_key, MIPSCPU, 3),
+        VMSTATE_UINT64(env.octeon_crypto.des3_iv, MIPSCPU),
+        VMSTATE_UINT64(env.octeon_crypto.des3_result, MIPSCPU),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.hash_state, MIPSCPU, 4),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.hash_block, MIPSCPU, 8),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.sha512_state, MIPSCPU, 8),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.sha512_block, MIPSCPU, 16),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_iv, MIPSCPU, 2),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_key, MIPSCPU, 4),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_result, MIPSCPU, 2),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_input, MIPSCPU, 2),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.gfm_mul, MIPSCPU, 2),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.gfm_result, MIPSCPU, 2),
+        VMSTATE_UINT64(env.octeon_crypto.gfm_xor0, MIPSCPU),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.gfm_reflect_mul, MIPSCPU, 2),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.gfm_reflect_result, MIPSCPU, 2),
+        VMSTATE_UINT64(env.octeon_crypto.gfm_reflect_xor0, MIPSCPU),
+        VMSTATE_UINT16(env.octeon_crypto.gfm_poly, MIPSCPU),
+        VMSTATE_UINT8(env.octeon_crypto.aes_keylen, MIPSCPU),
+        VMSTATE_UINT32(env.octeon_crypto.shared_mode, MIPSCPU),
+        VMSTATE_UINT32(env.octeon_crypto.crc_poly, MIPSCPU),
+        VMSTATE_UINT32(env.octeon_crypto.crc_iv, MIPSCPU),
+        VMSTATE_UINT32(env.octeon_crypto.crc_len, MIPSCPU),
+        VMSTATE_UINT32_ARRAY(env.octeon_crypto.snow3g_fsm, MIPSCPU, 3),
+        VMSTATE_UINT32_ARRAY(env.octeon_crypto.snow3g_lfsr, MIPSCPU, 16),
+        VMSTATE_UINT64(env.octeon_crypto.snow3g_result, MIPSCPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 const VMStateDescription vmstate_mips_cpu = {
     .name = "cpu",
     .version_id = 21,
@@ -386,6 +422,7 @@ const VMStateDescription vmstate_mips_cpu = {
     .subsections = (const VMStateDescription * const []) {
         &mips_vmstate_timer,
         &mips_vmstate_octeon_multiplier,
+        &mips_vmstate_octeon_crypto,
         NULL
     }
 };
target/mips/tcg/meson.build +1 -0
index fff9cd6c7f..4ee359874a 100644
@@ -18,6 +18,7 @@ mips_ss.add(files(
   'lmmi_helper.c',
   'msa_helper.c',
   'msa_translate.c',
+  'octeon_crypto.c',
   'op_helper.c',
   'rel6_translate.c',
   'translate.c',
target/mips/tcg/octeon_crypto.c new file +1654 -0

1,654 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +210 -0

216 lines not on this page · raw message

target/mips/tcg/translate.c +9 -0
index 2e3fe1fa16..ca9cee7ed6 100644
@@ -14860,6 +14860,15 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
         }
         break;
     case OPC_CP2:
+#if defined(TARGET_MIPS64)
+        if (ctx->insn_flags & INSN_OCTEON) {
+            if (gen_octeon_cop2(ctx)) {
+                break;
+            }
+            generate_exception_err(ctx, EXCP_CpU, 2);
+            break;
+        }
+#endif
         check_insn(ctx, ASE_LMMI);
         /* Note that these instructions use different fields.  */
         gen_loongson_multimedia(ctx, sa, rd, rt);
target/mips/tcg/translate.h +1 -0
index 89dde1e712..feb3c47c44 100644
@@ -232,6 +232,7 @@ bool decode_ext_loongson(DisasContext *ctx, uint32_t insn);
 bool decode_ase_lcsr(DisasContext *ctx, uint32_t insn);
 bool decode_ext_tx79(DisasContext *ctx, uint32_t insn);
 bool decode_ext_octeon(DisasContext *ctx, uint32_t insn);
+bool gen_octeon_cop2(DisasContext *ctx);
 #endif
 bool decode_ext_vr54xx(DisasContext *ctx, uint32_t insn);
 
tests/tcg/mips/user/isa/octeon/octeon-insns.c +71 -0

83 lines not on this page · raw message

Signature · 1 line
2.53.0
#15 lore raw

PATCH v2 8/13 target/mips: add Octeon SMS4 crypto support

In reply to James Hilliard

On Octeon, the SMS4 engine is exposed through selectors that alias the AES register bank. Add the missing selectors and model the shared RESULT, IV, and key state so the hardware interface matches the processor behaviour.

Use the in-tree SM4 tables to implement the block operation without adding a host crypto dependency.

---
Changes v1 -> v2:
- Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.h                  |  18 ++++++
 target/mips/tcg/octeon_crypto.c    | 109 +++++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c |   4 ++
 3 files changed, 131 insertions(+)
target/mips/cpu.h +18 -0
index 9e398e1ce5..887aeff12d 100644
@@ -585,6 +585,20 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_AES_DEC0 = 0x010e,
     OCTEON_COP2_SEL_AES_KEYLENGTH = 0x0110,
     OCTEON_COP2_SEL_AES_DAT0 = 0x0111,
+    /*
+     * SMS4 reuses the AES result/input, IV, and key banks and only adds
+     * operation selectors for ECB/CBC encrypt/decrypt.
+     */
+    OCTEON_COP2_SEL_SMS4_RESINP0 = OCTEON_COP2_SEL_AES_RESULT0,
+    OCTEON_COP2_SEL_SMS4_RESINP1 = OCTEON_COP2_SEL_AES_RESULT1,
+    OCTEON_COP2_SEL_SMS4_IV0 = OCTEON_COP2_SEL_AES_IV0,
+    OCTEON_COP2_SEL_SMS4_IV1 = OCTEON_COP2_SEL_AES_IV1,
+    OCTEON_COP2_SEL_SMS4_KEY0 = OCTEON_COP2_SEL_AES_KEY0,
+    OCTEON_COP2_SEL_SMS4_KEY1 = OCTEON_COP2_SEL_AES_KEY1,
+    OCTEON_COP2_SEL_SMS4_ENC_CBC0 = OCTEON_COP2_SEL_AES_ENC_CBC0,
+    OCTEON_COP2_SEL_SMS4_ENC0 = OCTEON_COP2_SEL_AES_ENC0,
+    OCTEON_COP2_SEL_SMS4_DEC_CBC0 = OCTEON_COP2_SEL_AES_DEC_CBC0,
+    OCTEON_COP2_SEL_SMS4_DEC0 = OCTEON_COP2_SEL_AES_DEC0,
     OCTEON_COP2_SEL_CRC_POLYNOMIAL = 0x0200,
     OCTEON_COP2_SEL_CRC_IV = 0x0201,
     OCTEON_COP2_SEL_CRC_LEN = 0x0202,
@@ -653,6 +667,10 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_AES_ENC1 = 0x310b,
     OCTEON_COP2_SEL_AES_DEC_CBC1 = 0x310d,
     OCTEON_COP2_SEL_AES_DEC1 = 0x310f,
+    OCTEON_COP2_SEL_SMS4_ENC_CBC1 = 0x3119,
+    OCTEON_COP2_SEL_SMS4_ENC1 = 0x311b,
+    OCTEON_COP2_SEL_SMS4_DEC_CBC1 = 0x311d,
+    OCTEON_COP2_SEL_SMS4_DEC1 = 0x311f,
     OCTEON_COP2_SEL_HSH_STARTMD5 = 0x4047,
     OCTEON_COP2_SEL_SNOW3G_START = 0x404d,
     OCTEON_COP2_SEL_SNOW3G_MORE = 0x404e,
target/mips/tcg/octeon_crypto.c +109 -0

133 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +4 -0
index ac5065398d..84fe3b38d0 100644
@@ -186,6 +186,10 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_AES_ENC1:
     case OCTEON_COP2_SEL_AES_DEC_CBC1:
     case OCTEON_COP2_SEL_AES_DEC1:
+    case OCTEON_COP2_SEL_SMS4_ENC_CBC1:
+    case OCTEON_COP2_SEL_SMS4_ENC1:
+    case OCTEON_COP2_SEL_SMS4_DEC_CBC1:
+    case OCTEON_COP2_SEL_SMS4_DEC1:
         return true;
     default:
         return false;
Signature · 1 line
2.53.0
#16 lore raw

PATCH v2 9/13 target/mips: add Octeon SHA3 crypto support

In reply to James Hilliard

Add the Octeon SHA3 register window and STARTOP selector.

Keep the shared HSH/SHA3/SHA512 write path coherent, then model the dedicated 25-lane Keccak state and the Keccak-f[1600] permutation so the COP2 SHA3 interface follows the hardware behaviour.

---
Changes v1 -> v2:
- Use switch ranges and g_assert_not_reached() for SHA3 selector position decoding. (suggested by Philippe Mathieu-Daudé)
- Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.h                  |  22 +++++
 target/mips/system/machine.c       |   1 +
 target/mips/tcg/octeon_crypto.c    | 171 +++++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c |  22 +++++
 4 files changed, 216 insertions(+)
target/mips/cpu.h +22 -0
index 887aeff12d..34fe7e1285 100644
@@ -533,6 +533,7 @@ typedef enum MIPSOcteonSharedMode {
     OCTEON_SHARED_MODE_NONE = 0,
     OCTEON_SHARED_MODE_SHA512,
     OCTEON_SHARED_MODE_SNOW3G,
+    OCTEON_SHARED_MODE_SHA3,
 } MIPSOcteonSharedMode;
 
 typedef enum MIPSOcteonCop2Sel {
@@ -637,6 +638,7 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_HSH_DATW13,
     OCTEON_COP2_SEL_HSH_DATW14,
     OCTEON_COP2_SEL_HSH_DATW15,
+    OCTEON_COP2_SEL_SHA3_DAT15_READ = 0x024f,
     OCTEON_COP2_SEL_HSH_IVW0 = 0x0250,
     OCTEON_COP2_SEL_HSH_IVW1,
     OCTEON_COP2_SEL_HSH_IVW2,
@@ -663,6 +665,24 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_GFM_RESINP1,
     OCTEON_COP2_SEL_GFM_XOR0,
     OCTEON_COP2_SEL_GFM_POLY = 0x025e,
+    OCTEON_COP2_SEL_SHA3_XORDAT0 = 0x02c0,
+    OCTEON_COP2_SEL_SHA3_XORDAT1,
+    OCTEON_COP2_SEL_SHA3_XORDAT2,
+    OCTEON_COP2_SEL_SHA3_XORDAT3,
+    OCTEON_COP2_SEL_SHA3_XORDAT4,
+    OCTEON_COP2_SEL_SHA3_XORDAT5,
+    OCTEON_COP2_SEL_SHA3_XORDAT6,
+    OCTEON_COP2_SEL_SHA3_XORDAT7,
+    OCTEON_COP2_SEL_SHA3_XORDAT8,
+    OCTEON_COP2_SEL_SHA3_XORDAT9,
+    OCTEON_COP2_SEL_SHA3_XORDAT10,
+    OCTEON_COP2_SEL_SHA3_XORDAT11,
+    OCTEON_COP2_SEL_SHA3_XORDAT12,
+    OCTEON_COP2_SEL_SHA3_XORDAT13,
+    OCTEON_COP2_SEL_SHA3_XORDAT14,
+    OCTEON_COP2_SEL_SHA3_XORDAT15,
+    OCTEON_COP2_SEL_SHA3_XORDAT16,
+    OCTEON_COP2_SEL_SHA3_XORDAT17,
     OCTEON_COP2_SEL_AES_ENC_CBC1 = 0x3109,
     OCTEON_COP2_SEL_AES_ENC1 = 0x310b,
     OCTEON_COP2_SEL_AES_DEC_CBC1 = 0x310d,
@@ -675,6 +695,7 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_SNOW3G_START = 0x404d,
     OCTEON_COP2_SEL_SNOW3G_MORE = 0x404e,
     OCTEON_COP2_SEL_HSH_STARTSHA256 = 0x404f,
+    OCTEON_COP2_SEL_SHA3_STARTOP = 0x4052,
     OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT = 0x405d,
     OCTEON_COP2_SEL_HSH_STARTSHA1 = 0x4057,
     OCTEON_COP2_SEL_HSH_STARTSHA512 = 0x424f,
@@ -689,6 +710,7 @@ typedef struct MIPSOcteonCryptoState {
     uint64_t hash_block[8];
     uint64_t sha512_state[8];
     uint64_t sha512_block[16];
+    uint64_t sha3_state[25];
     uint64_t aes_iv[2];
     uint64_t aes_key[4];
     uint64_t aes_result[2];
target/mips/system/machine.c +1 -0
index d5452a3157..c646e8b836 100644
@@ -282,6 +282,7 @@ static const VMStateDescription mips_vmstate_octeon_crypto = {
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.hash_block, MIPSCPU, 8),
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.sha512_state, MIPSCPU, 8),
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.sha512_block, MIPSCPU, 16),
+        VMSTATE_UINT64_ARRAY(env.octeon_crypto.sha3_state, MIPSCPU, 25),
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_iv, MIPSCPU, 2),
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_key, MIPSCPU, 4),
         VMSTATE_UINT64_ARRAY(env.octeon_crypto.aes_result, MIPSCPU, 2),
target/mips/tcg/octeon_crypto.c +171 -0

222 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +22 -0
index 84fe3b38d0..3f8c17678a 100644
@@ -72,6 +72,7 @@ static bool octeon_cop2_is_supported_dmfc2(uint16_t sel)
     case OCTEON_COP2_SEL_HSH_IVW6:
     case OCTEON_COP2_SEL_HSH_IVW7:
     case OCTEON_COP2_SEL_AES_DAT0:
+    case OCTEON_COP2_SEL_SHA3_DAT24:
     case OCTEON_COP2_SEL_GFM_MUL_REFLECT0:
     case OCTEON_COP2_SEL_GFM_MUL_REFLECT1:
     case OCTEON_COP2_SEL_GFM_RESINP_REFLECT0:
@@ -156,6 +157,8 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_HSH_DATW13:
     case OCTEON_COP2_SEL_HSH_DATW14:
     case OCTEON_COP2_SEL_HSH_DATW15:
+    case OCTEON_COP2_SEL_SHA3_DAT24:
+    case OCTEON_COP2_SEL_SHA3_DAT15_WRITE:
     case OCTEON_COP2_SEL_HSH_IVW0:
     case OCTEON_COP2_SEL_HSH_IVW1:
     case OCTEON_COP2_SEL_HSH_IVW2:
@@ -173,11 +176,30 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_GFM_RESINP1:
     case OCTEON_COP2_SEL_GFM_XOR0:
     case OCTEON_COP2_SEL_GFM_POLY:
+    case OCTEON_COP2_SEL_SHA3_XORDAT0:
+    case OCTEON_COP2_SEL_SHA3_XORDAT1:
+    case OCTEON_COP2_SEL_SHA3_XORDAT2:
+    case OCTEON_COP2_SEL_SHA3_XORDAT3:
+    case OCTEON_COP2_SEL_SHA3_XORDAT4:
+    case OCTEON_COP2_SEL_SHA3_XORDAT5:
+    case OCTEON_COP2_SEL_SHA3_XORDAT6:
+    case OCTEON_COP2_SEL_SHA3_XORDAT7:
+    case OCTEON_COP2_SEL_SHA3_XORDAT8:
+    case OCTEON_COP2_SEL_SHA3_XORDAT9:
+    case OCTEON_COP2_SEL_SHA3_XORDAT10:
+    case OCTEON_COP2_SEL_SHA3_XORDAT11:
+    case OCTEON_COP2_SEL_SHA3_XORDAT12:
+    case OCTEON_COP2_SEL_SHA3_XORDAT13:
+    case OCTEON_COP2_SEL_SHA3_XORDAT14:
+    case OCTEON_COP2_SEL_SHA3_XORDAT15:
+    case OCTEON_COP2_SEL_SHA3_XORDAT16:
+    case OCTEON_COP2_SEL_SHA3_XORDAT17:
     case OCTEON_COP2_SEL_HSH_STARTSHA1_COMPAT:
     case OCTEON_COP2_SEL_HSH_STARTMD5:
     case OCTEON_COP2_SEL_SNOW3G_START:
     case OCTEON_COP2_SEL_SNOW3G_MORE:
     case OCTEON_COP2_SEL_HSH_STARTSHA256:
+    case OCTEON_COP2_SEL_SHA3_STARTOP:
     case OCTEON_COP2_SEL_HSH_STARTSHA1:
     case OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT:
     case OCTEON_COP2_SEL_HSH_STARTSHA512:
Signature · 1 line
2.53.0
#17 lore raw

PATCH v2 10/13 target/mips: add Octeon ZUC crypto support

In reply to James Hilliard

Add the Octeon ZUC START and MORE selectors and model the shared state window used by the hardware interface.

This covers the keystream and MAC engine state, including the save-and-restore view that overlaps the HSH/SHA3 bank. Shared-window writes also update the SHA512/SHA3 backing state so guests can switch between engines without stale register contents.

---
Changes v1 -> v2:
- Add shared-window selector predicates and assert on unreachable ZUC selector switches. (suggested by Philippe Mathieu-Daudé)
- Preserve aliased HSH/SHA3/SHA512 backing state during ZUC shared-window writes.
- Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.h                  |   7 +
 target/mips/system/machine.c       |   4 +
 target/mips/tcg/octeon_crypto.c    | 353 +++++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c |   2 +
 4 files changed, 366 insertions(+)
target/mips/cpu.h +7 -0
index 34fe7e1285..437d43636b 100644
@@ -533,6 +533,7 @@ typedef enum MIPSOcteonSharedMode {
     OCTEON_SHARED_MODE_NONE = 0,
     OCTEON_SHARED_MODE_SHA512,
     OCTEON_SHARED_MODE_SNOW3G,
+    OCTEON_SHARED_MODE_ZUC,
     OCTEON_SHARED_MODE_SHA3,
 } MIPSOcteonSharedMode;
 
@@ -696,6 +697,8 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_SNOW3G_MORE = 0x404e,
     OCTEON_COP2_SEL_HSH_STARTSHA256 = 0x404f,
     OCTEON_COP2_SEL_SHA3_STARTOP = 0x4052,
+    OCTEON_COP2_SEL_ZUC_START = 0x4055,
+    OCTEON_COP2_SEL_ZUC_MORE = 0x4056,
     OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT = 0x405d,
     OCTEON_COP2_SEL_HSH_STARTSHA1 = 0x4057,
     OCTEON_COP2_SEL_HSH_STARTSHA512 = 0x424f,
@@ -730,6 +733,10 @@ typedef struct MIPSOcteonCryptoState {
     uint32_t snow3g_fsm[3];
     uint32_t snow3g_lfsr[16];
     uint64_t snow3g_result;
+    uint32_t zuc_fsm[2];
+    uint32_t zuc_lfsr[16];
+    uint32_t zuc_window[3];
+    uint32_t zuc_tresult;
 } MIPSOcteonCryptoState;
 
 typedef struct CPUArchState {
target/mips/system/machine.c +4 -0
index c646e8b836..1176d879eb 100644
@@ -302,6 +302,10 @@ static const VMStateDescription mips_vmstate_octeon_crypto = {
         VMSTATE_UINT32_ARRAY(env.octeon_crypto.snow3g_fsm, MIPSCPU, 3),
         VMSTATE_UINT32_ARRAY(env.octeon_crypto.snow3g_lfsr, MIPSCPU, 16),
         VMSTATE_UINT64(env.octeon_crypto.snow3g_result, MIPSCPU),
+        VMSTATE_UINT32_ARRAY(env.octeon_crypto.zuc_fsm, MIPSCPU, 2),
+        VMSTATE_UINT32_ARRAY(env.octeon_crypto.zuc_lfsr, MIPSCPU, 16),
+        VMSTATE_UINT32_ARRAY(env.octeon_crypto.zuc_window, MIPSCPU, 3),
+        VMSTATE_UINT32(env.octeon_crypto.zuc_tresult, MIPSCPU),
         VMSTATE_END_OF_LIST()
     }
 };
target/mips/tcg/octeon_crypto.c +353 -0

377 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +2 -0
index 3f8c17678a..965dab1b34 100644
@@ -200,6 +200,8 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_SNOW3G_MORE:
     case OCTEON_COP2_SEL_HSH_STARTSHA256:
     case OCTEON_COP2_SEL_SHA3_STARTOP:
+    case OCTEON_COP2_SEL_ZUC_START:
+    case OCTEON_COP2_SEL_ZUC_MORE:
     case OCTEON_COP2_SEL_HSH_STARTSHA1:
     case OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT:
     case OCTEON_COP2_SEL_HSH_STARTSHA512:
Signature · 1 line
2.53.0
#18 lore raw

PATCH v2 11/13 target/mips: add Octeon Camellia crypto support

In reply to James Hilliard

Add the Octeon Camellia ROUND, FL, and FLINV selectors and model the round engine that reuses the AES RESULT/RESINP bank.

Implement the Camellia F-function and FL layers directly from RFC 3713 so guest-managed key schedules can drive the engine through the hardware interface.

---
Changes v1 -> v2:
- Drop the Octeon prefix from generic Camellia helper routines. (suggested by Philippe Mathieu-Daudé)
- Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.h                  |   9 +++
 target/mips/tcg/octeon_crypto.c    | 120 +++++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c |   3 +
 3 files changed, 132 insertions(+)
target/mips/cpu.h +9 -0
index 437d43636b..8411ef7de4 100644
@@ -587,6 +587,14 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_AES_DEC0 = 0x010e,
     OCTEON_COP2_SEL_AES_KEYLENGTH = 0x0110,
     OCTEON_COP2_SEL_AES_DAT0 = 0x0111,
+    /*
+     * Camellia reuses the AES RESULT/RESINP bank and adds per-round and
+     * diffusion-layer selectors for the guest-managed key schedule.
+     */
+    OCTEON_COP2_SEL_CAMELLIA_RESINP0 = OCTEON_COP2_SEL_AES_RESULT0,
+    OCTEON_COP2_SEL_CAMELLIA_RESINP1 = OCTEON_COP2_SEL_AES_RESULT1,
+    OCTEON_COP2_SEL_CAMELLIA_FL = 0x0115,
+    OCTEON_COP2_SEL_CAMELLIA_FLINV = 0x0116,
     /*
      * SMS4 reuses the AES result/input, IV, and key banks and only adds
      * operation selectors for ECB/CBC encrypt/decrypt.
@@ -688,6 +696,7 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_AES_ENC1 = 0x310b,
     OCTEON_COP2_SEL_AES_DEC_CBC1 = 0x310d,
     OCTEON_COP2_SEL_AES_DEC1 = 0x310f,
+    OCTEON_COP2_SEL_CAMELLIA_ROUND = 0x3114,
     OCTEON_COP2_SEL_SMS4_ENC_CBC1 = 0x3119,
     OCTEON_COP2_SEL_SMS4_ENC1 = 0x311b,
     OCTEON_COP2_SEL_SMS4_DEC_CBC1 = 0x311d,
target/mips/tcg/octeon_crypto.c +120 -0

138 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +3 -0
index 965dab1b34..004b937412 100644
@@ -115,6 +115,8 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_AES_DEC_CBC0:
     case OCTEON_COP2_SEL_AES_DEC0:
     case OCTEON_COP2_SEL_AES_KEYLENGTH:
+    case OCTEON_COP2_SEL_CAMELLIA_FL:
+    case OCTEON_COP2_SEL_CAMELLIA_FLINV:
     case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL:
     case OCTEON_COP2_SEL_CRC_IV:
     case OCTEON_COP2_SEL_CRC_WRITE_LEN:
@@ -210,6 +212,7 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_AES_ENC1:
     case OCTEON_COP2_SEL_AES_DEC_CBC1:
     case OCTEON_COP2_SEL_AES_DEC1:
+    case OCTEON_COP2_SEL_CAMELLIA_ROUND:
     case OCTEON_COP2_SEL_SMS4_ENC_CBC1:
     case OCTEON_COP2_SEL_SMS4_ENC1:
     case OCTEON_COP2_SEL_SMS4_DEC_CBC1:
Signature · 1 line
2.53.0
#19 lore raw

PATCH v2 12/13 target/mips: add Octeon CHORD and LLM COP2 support

In reply to James Hilliard

Add the Octeon CHORD hardware register access path and the LLM narrow/wide read and write windows.

Model both CHORD access forms, including the rdhwr $30 path and the legacy dmfc2 alias, and implement sparse backing storage for the two LLM sets so user-mode code can save, restore, and probe the architectural state.

---
Changes v1 -> v2:
- Use neutral selector-slot wording for the LLM/CHORD alias comment.
- Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu.c                  | 67 +++++++++++++++++++++++++++++++++++
 target/mips/cpu.h                  | 20 +++++++++++
 target/mips/helper.h               |  1 +
 target/mips/internal.h             |  3 ++
 target/mips/system/machine.c       | 67 +++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_crypto.c    | 72 ++++++++++++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c | 13 +++++++
 target/mips/tcg/op_helper.c        |  6 ++++
 target/mips/tcg/translate.c        |  8 +++++
 9 files changed, 257 insertions(+)
target/mips/cpu.c +67 -0
index ec70c10985..ebcb814360 100644
@@ -27,6 +27,7 @@
 #include "internal.h"
 #include "kvm_mips.h"
 #include "qemu/module.h"
+#include "qemu/qtree.h"
 #include "system/kvm.h"
 #include "system/qtest.h"
 #include "hw/core/qdev-properties.h"
@@ -183,6 +184,57 @@ static bool mips_cpu_has_work(CPUState *cs)
 
 #include "cpu-defs.c.inc"
 
+static gint mips_octeon_u64_tree_compare(gconstpointer a, gconstpointer b,
+                                         gpointer user_data)
+{
+    uint64_t av = *(const uint64_t *)a;
+    uint64_t bv = *(const uint64_t *)b;
+
+    return (av > bv) - (av < bv);
+}
+
+QTree *mips_octeon_llm_tree_new(void)
+{
+    return q_tree_new_full(mips_octeon_u64_tree_compare,
+                           NULL, g_free, g_free);
+}
+
+uint64_t mips_octeon_llm_load(QTree *tree, uint64_t addr)
+{
+    uint64_t key = addr;
+    uint64_t *value = tree ? q_tree_lookup(tree, &key) : NULL;
+
+    return value ? *value : 0;
+}
+
+void mips_octeon_llm_store(QTree **treep, uint64_t addr, uint64_t value)
+{
+    uint64_t *key;
+    uint64_t *stored;
+
+    if (!*treep) {
+        *treep = mips_octeon_llm_tree_new();
+    }
+
+    key = g_new(uint64_t, 1);
+    stored = g_new(uint64_t, 1);
+    *key = addr;
+    *stored = value;
+    q_tree_replace(*treep, key, stored);
+}
+
+static void mips_octeon_destroy_llm_state(MIPSOcteonCryptoState *crypto)
+{
+    if (crypto->llm_narrow) {
+        q_tree_destroy(crypto->llm_narrow);
+        crypto->llm_narrow = NULL;
+    }
+    if (crypto->llm_wide) {
+        q_tree_destroy(crypto->llm_wide);
+        crypto->llm_wide = NULL;
+    }
+}
+
 static void mips_cpu_reset_hold(Object *obj, ResetType type)
 {
     CPUState *cs = CPU(obj);
@@ -194,6 +246,7 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
         mcc->parent_phases.hold(obj, type);
     }
 
+    mips_octeon_destroy_llm_state(&env->octeon_crypto);
     memset(env, 0, offsetof(CPUMIPSState, end_reset_fields));
 
     /* Reset registers to their default values */
@@ -248,6 +301,9 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
     env->active_fpu.fcr31 = env->cpu_model->CP1_fcr31;
     env->msair = env->cpu_model->MSAIR;
     env->insn_flags = env->cpu_model->insn_flags;
+    if (env->insn_flags & INSN_OCTEON) {
+        env->octeon_crypto.chord = 1;
+    }
 
 #if defined(CONFIG_USER_ONLY)
     env->CP0_Status = (MIPS_HFLAG_UM << CP0St_KSU);
@@ -264,6 +320,9 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
      * hardware registers.
      */
     env->CP0_HWREna |= 0x0000000F;
+    if (env->insn_flags & INSN_OCTEON) {
+        env->CP0_HWREna |= 0x40000000u;
+    }
     if (env->CP0_Config1 & (1 << CP0C1_FP)) {
         env->CP0_Status |= (1 << CP0St_CU1);
     }
@@ -422,6 +481,13 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
 #endif
 }
 
+static void mips_cpu_finalize(Object *obj)
+{
+    MIPSCPU *cpu = MIPS_CPU(obj);
+
+    mips_octeon_destroy_llm_state(&cpu->env.octeon_crypto);
+}
+
 static void mips_cpu_disas_set_info(const CPUState *cs, disassemble_info *info)
 {
     const MIPSCPU *cpu = MIPS_CPU(cs);
@@ -636,6 +702,7 @@ static const TypeInfo mips_cpu_type_info = {
     .instance_size = sizeof(MIPSCPU),
     .instance_align = __alignof(MIPSCPU),
     .instance_init = mips_cpu_initfn,
+    .instance_finalize = mips_cpu_finalize,
     .abstract = true,
     .class_size = sizeof(MIPSCPUClass),
     .class_init = mips_cpu_class_init,
target/mips/cpu.h +20 -0
index 8411ef7de4..e4ccb5e1c7 100644
@@ -11,6 +11,7 @@
 #include "fpu/softfloat-types.h"
 #include "hw/core/clock.h"
 #include "mips-defs.h"
+#include "qemu/qtree.h"
 
 typedef struct CPUMIPSTLBContext CPUMIPSTLBContext;
 
@@ -609,6 +610,21 @@ typedef enum MIPSOcteonCop2Sel {
     OCTEON_COP2_SEL_SMS4_ENC0 = OCTEON_COP2_SEL_AES_ENC0,
     OCTEON_COP2_SEL_SMS4_DEC_CBC0 = OCTEON_COP2_SEL_AES_DEC_CBC0,
     OCTEON_COP2_SEL_SMS4_DEC0 = OCTEON_COP2_SEL_AES_DEC0,
+    /*
+     * Selector 0x0400 is the narrow LLM read selector and is also used as a
+     * DMFC2 alias for the CHORD POW tag-switch completion bit.
+     */
+    OCTEON_COP2_SEL_LLM_READ_ADDR0 = 0x0400,
+    OCTEON_COP2_SEL_CHORD = OCTEON_COP2_SEL_LLM_READ_ADDR0,
+    OCTEON_COP2_SEL_LLM_WRITE_ADDR_INTERNAL0 = 0x0401,
+    OCTEON_COP2_SEL_LLM_DATA0 = 0x0402,
+    OCTEON_COP2_SEL_LLM_READ64_ADDR0 = 0x0404,
+    OCTEON_COP2_SEL_LLM_WRITE64_ADDR_INTERNAL0 = 0x0405,
+    OCTEON_COP2_SEL_LLM_READ_ADDR1 = 0x0408,
+    OCTEON_COP2_SEL_LLM_WRITE_ADDR_INTERNAL1 = 0x0409,
+    OCTEON_COP2_SEL_LLM_DATA1 = 0x040a,
+    OCTEON_COP2_SEL_LLM_READ64_ADDR1 = 0x040c,
+    OCTEON_COP2_SEL_LLM_WRITE64_ADDR_INTERNAL1 = 0x040d,
     OCTEON_COP2_SEL_CRC_POLYNOMIAL = 0x0200,
     OCTEON_COP2_SEL_CRC_IV = 0x0201,
     OCTEON_COP2_SEL_CRC_LEN = 0x0202,
@@ -746,6 +762,10 @@ typedef struct MIPSOcteonCryptoState {
     uint32_t zuc_lfsr[16];
     uint32_t zuc_window[3];
     uint32_t zuc_tresult;
+    uint64_t llm_data[2];
+    uint64_t chord;
+    QTree *llm_narrow;
+    QTree *llm_wide;
 } MIPSOcteonCryptoState;
 
 typedef struct CPUArchState {
target/mips/helper.h +1 -0
index 899120d0ca..dad91d7451 100644
@@ -200,6 +200,7 @@ DEF_HELPER_1(rdhwr_cc, tl, env)
 DEF_HELPER_1(rdhwr_ccres, tl, env)
 DEF_HELPER_1(rdhwr_performance, tl, env)
 DEF_HELPER_1(rdhwr_xnp, tl, env)
+DEF_HELPER_1(rdhwr_chord, tl, env)
 DEF_HELPER_2(pmon, void, env, int)
 DEF_HELPER_1(wait, void, env)
 
target/mips/internal.h +3 -0
index 28eb28936b..b40e65864a 100644
@@ -93,6 +93,9 @@ extern const int mips_defs_number;
 
 int mips_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
 int mips_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
+QTree *mips_octeon_llm_tree_new(void);
+uint64_t mips_octeon_llm_load(QTree *tree, uint64_t addr);
+void mips_octeon_llm_store(QTree **treep, uint64_t addr, uint64_t value);
 
 #define USEG_LIMIT      ((target_ulong)(int32_t)0x7FFFFFFFUL)
 #define KSEG0_BASE      ((target_ulong)(int32_t)0x80000000UL)
target/mips/system/machine.c +67 -0

79 lines not on this page · raw message

target/mips/tcg/octeon_crypto.c +72 -0

90 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +13 -0
index 004b937412..d9c5d7ab83 100644
@@ -82,6 +82,9 @@ static bool octeon_cop2_is_supported_dmfc2(uint16_t sel)
     case OCTEON_COP2_SEL_GFM_RESINP0:
     case OCTEON_COP2_SEL_GFM_RESINP1:
     case OCTEON_COP2_SEL_GFM_POLY:
+    case OCTEON_COP2_SEL_CHORD:
+    case OCTEON_COP2_SEL_LLM_DATA0:
+    case OCTEON_COP2_SEL_LLM_DATA1:
         return true;
     default:
         return false;
@@ -117,6 +120,16 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_AES_KEYLENGTH:
     case OCTEON_COP2_SEL_CAMELLIA_FL:
     case OCTEON_COP2_SEL_CAMELLIA_FLINV:
+    case OCTEON_COP2_SEL_LLM_READ_ADDR0:
+    case OCTEON_COP2_SEL_LLM_WRITE_ADDR_INTERNAL0:
+    case OCTEON_COP2_SEL_LLM_DATA0:
+    case OCTEON_COP2_SEL_LLM_READ64_ADDR0:
+    case OCTEON_COP2_SEL_LLM_WRITE64_ADDR_INTERNAL0:
+    case OCTEON_COP2_SEL_LLM_READ_ADDR1:
+    case OCTEON_COP2_SEL_LLM_WRITE_ADDR_INTERNAL1:
+    case OCTEON_COP2_SEL_LLM_DATA1:
+    case OCTEON_COP2_SEL_LLM_READ64_ADDR1:
+    case OCTEON_COP2_SEL_LLM_WRITE64_ADDR_INTERNAL1:
     case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL:
     case OCTEON_COP2_SEL_CRC_IV:
     case OCTEON_COP2_SEL_CRC_WRITE_LEN:
target/mips/tcg/op_helper.c +6 -0
index 47b13a583c..d73ec80af0 100644
@@ -354,6 +354,12 @@ target_ulong helper_rdhwr_xnp(CPUMIPSState *env)
     return (env->CP0_Config5 >> CP0C5_XNP) & 1;
 }
 
+target_ulong helper_rdhwr_chord(CPUMIPSState *env)
+{
+    check_hwrena(env, 30, GETPC());
+    return env->octeon_crypto.chord;
+}
+
 void helper_pmon(CPUMIPSState *env, int function)
 {
     function /= 2;
target/mips/tcg/translate.c +8 -0
index ca9cee7ed6..183ae940ff 100644
@@ -10920,6 +10920,14 @@ void gen_rdhwr(DisasContext *ctx, int rt, int rd, int sel)
         }
         break;
 #endif
+    case 30:
+        if (!(ctx->insn_flags & INSN_OCTEON)) {
+            gen_reserved_instruction(ctx);
+            break;
+        }
+        gen_helper_rdhwr_chord(t0, tcg_env);
+        gen_store_gpr(t0, rt);
+        break;
     default:            /* Invalid */
         MIPS_INVAL("rdhwr");
         gen_reserved_instruction(ctx);
Signature · 1 line
2.53.0
#20 lore raw

PATCH v2 13/13 target/mips: expose Octeon68XX floating-point support

In reply to James Hilliard

Octeon68XX cores implement CP1. Advertise that in the CPU definition by setting Config1.FP, enabling the writable Status bits, and providing the FCR0/FCR31 defaults used by this CPU model.

This lets guests observe the expected floating-point feature bits and use CP1 with -cpu Octeon68XX.

---
Changes v1 -> v2:
- Move this CPU-model correction into a separate final patch. (suggested by Philippe Mathieu-Daudé)

---
 target/mips/cpu-defs.c.inc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
target/mips/cpu-defs.c.inc +8 -2
index d93b9d341a..6c084fcaea 100644
@@ -997,7 +997,8 @@ const mips_def_t mips_defs[] =
         .CP0_PRid = 0x000D9100,
         .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR) | (0x2 << CP0C0_AT) |
                        (MMU_TYPE_R4000 << CP0C0_MT),
-        .CP0_Config1 = MIPS_CONFIG1 | (0x3F << CP0C1_MMU) |
+        .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) |
+                       (0x3F << CP0C1_MMU) |
                        (1 << CP0C1_IS) | (4 << CP0C1_IL) | (1 << CP0C1_IA) |
                        (1 << CP0C1_DS) | (4 << CP0C1_DL) | (1 << CP0C1_DA) |
                        (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
@@ -1011,7 +1012,12 @@ const mips_def_t mips_defs[] =
         .CP0_PageGrain = (1 << CP0PG_ELPA),
         .SYNCI_Step = 32,
         .CCRes = 2,
-        .CP0_Status_rw_bitmask = 0x12F8FFFF,
+        .CP0_Status_rw_bitmask = 0x36F8FFFF,
+        .CP1_fcr0 = (1 << FCR0_F64) | (1 << FCR0_3D) | (1 << FCR0_PS) |
+                    (1 << FCR0_L) | (1 << FCR0_W) | (1 << FCR0_D) |
+                    (1 << FCR0_S) | (0x00 << FCR0_PRID) | (0x0 << FCR0_REV),
+        .CP1_fcr31 = 0,
+        .CP1_fcr31_rw_bitmask = 0xFF83FFFF,
         .SEGBITS = 42,
         .PABITS = 49,
         .insn_flags = CPU_MIPS64R2 | INSN_OCTEON,
Signature · 1 line
2.53.0