Skip to contentSkip to conversation

linux-user/mips, target/mips: honor MIPS_FIXADE for unaligned accesses

PATCH Superseded James Hilliard 3 participants 18 messages Apr 11 – Apr 20 Arm/Amlogic Meson SoC support

v1 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/9 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, and
- raise SIGBUS/BUS_ADRALN when fixups are disabled.

This makes the default behaviour match native Linux/MIPS more closely and keeps the signal semantics consistent with the kernel ABI.

---
 linux-user/mips/cpu_loop.c         |  5 +++++
 linux-user/mips/target_syscall.h   |  4 ++++
 linux-user/mips64/target_syscall.h |  4 ++++
 linux-user/syscall.c               | 24 ++++++++++++++++++++++++
 target/mips/cpu.c                  |  8 ++++++--
 target/mips/cpu.h                  |  4 ++++
 target/mips/tcg/translate.c        |  6 +++++-
 7 files changed, 52 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 +4 -0
index dfcdf320b7..f8dc5c4e06 100644
@@ -10,6 +10,10 @@
 #define TARGET_MCL_ONFAULT 4
 
 #define TARGET_FORCE_SHMLBA
+#define TARGET_SYSMIPS_SETNAME         1
+#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 +4 -0
index 9135bf5e8b..d786fba5a9 100644
@@ -10,6 +10,10 @@
 #define TARGET_MCL_ONFAULT 4
 
 #define TARGET_FORCE_SHMLBA
+#define TARGET_SYSMIPS_SETNAME         1
+#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 +24 -0
index f4b74ad350..4f58eb4afe 100644
@@ -6595,6 +6595,26 @@ static abi_long do_prctl_syscall_user_dispatch(CPUArchState *env,
     }
 }
 
+#ifdef TARGET_NR_sysmips
+static abi_long do_sysmips(CPUArchState *env, abi_long cmd, abi_long arg1)
+{
+    CPUState *cs = env_cpu(env);
+
+    switch (cmd) {
+    case TARGET_SYSMIPS_FIXADE:
+        if (arg1 & ~3) {
+            return -TARGET_EINVAL;
+        }
+        cs->prctl_unalign_sigbus = !(arg1 & 1);
+        return 0;
+    case TARGET_SYSMIPS_FLUSH_CACHE:
+        return 0;
+    default:
+        return -TARGET_EINVAL;
+    }
+}
+#endif
+
 static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
                          abi_long arg3, abi_long arg4, abi_long arg5)
 {
@@ -12067,6 +12087,10 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
     case TARGET_NR_prctl:
         return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
         break;
+#ifdef TARGET_NR_sysmips
+    case TARGET_NR_sysmips:
+        return do_sysmips(cpu_env, arg1, arg2);
+#endif
 #ifdef TARGET_NR_arch_prctl
     case TARGET_NR_arch_prctl:
         return do_arch_prctl(cpu_env, arg1, arg2);
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
#1 lore raw

PATCH 2/9 target/mips: add Octeon arithmetic and memory instructions

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 unaligned load/store forms, cache block zero operations, the Octeon multiply family, population count handling, and related arithmetic and memory operations in the translator and helper layer.

---
 target/mips/cpu-defs.c.inc         |  10 +-
 target/mips/cpu.h                  |  12 +++
 target/mips/helper.h               |   2 +
 target/mips/system/machine.c       |  43 ++++++++
 target/mips/tcg/octeon.decode      |  28 ++++-
 target/mips/tcg/octeon_translate.c | 163 +++++++++++++++++++++++++++--
 target/mips/tcg/op_helper.c        |  84 +++++++++++++++
 7 files changed, 331 insertions(+), 11 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,
target/mips/cpu.h +12 -0
index ac81470576..98cd7ae83e 100644
@@ -497,6 +497,18 @@ struct TCState {
     target_ulong CP0_TCScheFBack;
     int32_t CP0_Debug_tcstatus;
     target_ulong CP0_UserLocal;
+    target_ulong MPL0;
+    target_ulong MPL1;
+    target_ulong MPL2;
+    target_ulong MPL3;
+    target_ulong MPL4;
+    target_ulong MPL5;
+    target_ulong P0;
+    target_ulong P1;
+    target_ulong P2;
+    target_ulong P3;
+    target_ulong P4;
+    target_ulong P5;
 
     int32_t msacsr;
 
target/mips/helper.h +2 -0
index b6cd53c853..3c9f67ec40 100644
@@ -24,6 +24,8 @@ 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, tl, env, tl, tl)
+DEF_HELPER_3(octeon_v3mulu, tl, env, tl, tl)
 
 /* microMIPS functions */
 DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/system/machine.c +43 -0

61 lines not on this page · raw message

target/mips/tcg/octeon.decode +26 -2
index 102a05860d..d90a194b54 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,12 +40,34 @@ 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
+&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
+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
 
 &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 +156 -7

224 lines not on this page · raw message

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

90 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 2/9] target/mips: add Octeon arithmetic and memory instructions

Hi James,

Quoted 9 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 unaligned load/store forms, cache block zero operations, the Octeon multiply family, population count handling, and related arithmetic and memory operations in the translator and helper layer.

Thanks for this series! (Please include a cover letter for v2).

For historical background, see my previous question regarding testing:
https://lore.kernel.org/qemu-devel/[email protected]/

We need few pieces of guest code to test this feature, because we don't want to maintain bitrotten code.

Quoted 29 lines
---
  target/mips/cpu-defs.c.inc         |  10 +-
  target/mips/cpu.h                  |  12 +++
  target/mips/helper.h               |   2 +
  target/mips/system/machine.c       |  43 ++++++++
  target/mips/tcg/octeon.decode      |  28 ++++-
  target/mips/tcg/octeon_translate.c | 163 +++++++++++++++++++++++++++--
  target/mips/tcg/op_helper.c        |  84 +++++++++++++++
  7 files changed, 331 insertions(+), 11 deletions(-)
target/mips/cpu-defs.c.inc +2 -1
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,

target/mips/cpu-defs.c.inc const mips_def_t mips_defs[] = · line 1023

Quoted 10 lines
target/mips/cpu-defs.c.inc +6 -1
-        .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,

Exposing these CPU features to the guest should be the last patch.

Quoted 10 lines
target/mips/cpu.h +2 -0
index ac81470576..98cd7ae83e 100644
@@ -497,6 +497,18 @@ struct TCState {
      target_ulong CP0_TCScheFBack;
      int32_t CP0_Debug_tcstatus;
      target_ulong CP0_UserLocal;
+    target_ulong MPL0;
+    target_ulong MPL1;

target/mips/cpu.h struct TCState { · line 511

Quoted 10 lines
target/mips/cpu.h +10 -0
+    target_ulong MPL2;
+    target_ulong MPL3;
+    target_ulong MPL4;
+    target_ulong MPL5;
+    target_ulong P0;
+    target_ulong P1;
+    target_ulong P2;
+    target_ulong P3;
+    target_ulong P4;
+    target_ulong P5;

This CPU only exists in 64-bit variant, so please directly start with uint64_t (we try to remove target_ulong uses).

We usually model banked registers as array:

  uint64_t MPL[2 * 3];

Although maybe having an octeon field could make the code easier to follow (like the other octeon_crypto structure you introduce):

  struct {
      uint64_t MPL[2 * 3];
      uint64_t P[2 * 3];
  } octeon;
Quoted 148 lines
target/mips/cpu.h +0 -0
      int32_t msacsr;
 
target/mips/helper.h +2 -0
index b6cd53c853..3c9f67ec40 100644
@@ -24,6 +24,8 @@ 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, tl, env, tl, tl)
+DEF_HELPER_3(octeon_v3mulu, tl, env, tl, tl)
 
  /* microMIPS functions */
  DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/system/machine.c +43 -0
index 473d3ab036..1ea5907e55 100644
@@ -118,6 +118,27 @@ 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_UINTTL(MPL0, TCState),
+        VMSTATE_UINTTL(MPL1, TCState),
+        VMSTATE_UINTTL(MPL2, TCState),
+        VMSTATE_UINTTL(MPL3, TCState),
+        VMSTATE_UINTTL(MPL4, TCState),
+        VMSTATE_UINTTL(MPL5, TCState),
+        VMSTATE_UINTTL(P0, TCState),
+        VMSTATE_UINTTL(P1, TCState),
+        VMSTATE_UINTTL(P2, TCState),
+        VMSTATE_UINTTL(P3, TCState),
+        VMSTATE_UINTTL(P4, TCState),
+        VMSTATE_UINTTL(P5, TCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
  /* MVP state */
 
  static const VMStateDescription vmstate_mvp = {
@@ -237,6 +258,27 @@ static const VMStateDescription mips_vmstate_timer = {
      }
  };
 
+static bool mips_octeon_multiplier_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_multiplier_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 +395,7 @@ const VMStateDescription vmstate_mips_cpu = {
      },
      .subsections = (const VMStateDescription * const []) {
          &mips_vmstate_timer,
+        &mips_vmstate_octeon_multiplier,
          NULL
      }
  };
target/mips/tcg/octeon.decode +26 -2
index 102a05860d..d90a194b54 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,12 +40,34 @@ 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
+&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
+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
 
  &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 +2 -0
index b2eca29e06..67b8634dff 100644
@@ -13,6 +13,8 @@
  /* Include the auto-generated decoder.  */
  #include "decode-octeon.c.inc"
 
+typedef void gen_helper_lmi(TCGv, TCGv_ptr, TCGv, TCGv);
+
  static bool trans_BBIT(DisasContext *ctx, arg_BBIT *a)
  {
      TCGv p;

target/mips/tcg/octeon_translate.c static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a) · line 50

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -45,7 +47,7 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
  {
      TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {

Bug fix? Distinct patch please.

Quoted 14 lines
target/mips/tcg/octeon_translate.c +2 -1
          /* nop */
          return true;
      }
@@ -56,7 +58,8 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
      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_andi_tl(t0, t0, 0xff);
+    gen_store_gpr(t0, a->rd);
      return true;
  }
 

target/mips/tcg/octeon_translate.c static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a) · line 70

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -64,7 +67,7 @@ static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
  {
      TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {

Ditto.

Quoted 14 lines
target/mips/tcg/octeon_translate.c +2 -1
          /* nop */
          return true;
      }
@@ -74,7 +77,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_tl(t0, t0, t1);
+    gen_store_gpr(t0, a->rd);
      return true;
  }
 

target/mips/tcg/octeon_translate.c static bool trans_POP(DisasContext *ctx, arg_POP *a) · line 129

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -122,14 +126,14 @@ static bool trans_POP(DisasContext *ctx, arg_POP *a)
      t0 = tcg_temp_new();
      gen_load_gpr(t0, a->rs);
      if (!a->dw) {
-        tcg_gen_andi_i64(t0, t0, 0xffffffff);
+        tcg_gen_andi_tl(t0, t0, 0xffffffff);

No, please avoid *_tl() helpers in this 64-bit only translator.

Quoted 33 lines
target/mips/tcg/octeon_translate.c +12 -2
      }
      tcg_gen_ctpop_tl(t0, t0);
      gen_store_gpr(t0, a->rd);
      return true;
  }
 
-static bool trans_SEQNE(DisasContext *ctx, arg_SEQNE *a)
+static bool trans_seqne(DisasContext *ctx, const arg_cmp3 *a)
  {
      TCGv t0, t1;
 
@@ -152,7 +156,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;
 
@@ -175,6 +189,16 @@ static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a)
      return true;
  }

target/mips/tcg/octeon_translate.c static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a) · line 200

Quoted 10 lines
target/mips/tcg/octeon_translate.c +9 -0
 
+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);
+}

Please move these changes in a preliminary patch to help reviewers.

Quoted 124 lines
target/mips/tcg/octeon_translate.c +113 -0
  static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
  {
      gen_lx(ctx, a->rd, a->base, a->index, mop);
@@ -182,7 +206,132 @@ static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
      return true;
  }
 
+static bool trans_saa(DisasContext *ctx, arg_saa *a, MemOp mop)
+{
+    TCGv addr = tcg_temp_new();
+    MemOp amo = mo_endian(ctx) | mop | ctx->default_tcg_memop_mask;
+
+    gen_base_offset_addr(ctx, addr, a->base, 0);
+
+    if (mop == MO_UQ) {
+        TCGv value = tcg_temp_new();
+        TCGv old = tcg_temp_new();
+
+        gen_load_gpr(value, a->rt);
+        tcg_gen_atomic_fetch_add_tl(old, addr, value, ctx->mem_idx, amo);
+    } else {
+        TCGv value = tcg_temp_new();
+        TCGv_i32 value32 = tcg_temp_new_i32();
+        TCGv_i32 old = tcg_temp_new_i32();
+
+        gen_load_gpr(value, a->rt);
+        tcg_gen_trunc_tl_i32(value32, value);
+        tcg_gen_atomic_fetch_add_i32(old, addr, value32, ctx->mem_idx, amo);
+    }
+
+    return true;
+}
+
+static bool trans_ZCB(DisasContext *ctx, arg_zcb *a)
+{
+    TCGv addr = tcg_temp_new();
+    TCGv line = tcg_temp_new();
+    TCGv zero = tcg_constant_tl(0);
+
+    gen_base_offset_addr(ctx, addr, a->base, 0);
+
+    /*
+     * Octeon zcb operates on a cache block. Model it as zeroing the
+     * containing 128-byte line in memory.
+     */
+    tcg_gen_andi_tl(line, addr, ~((target_ulong)127));
+
+    for (int i = 0; i < 16; i++) {
+        TCGv slot = tcg_temp_new();
+
+        tcg_gen_addi_tl(slot, line, i * 8);
+        tcg_gen_qemu_st_tl(zero, slot, ctx->mem_idx, mo_endian(ctx) | MO_UQ);
+    }
+
+    return true;
+}
+
+static void octeon_store_tc_field(ptrdiff_t offset, TCGv value)
+{
+    tcg_gen_st_tl(value, tcg_env, offset);
+}
+
+static void octeon_zero_partial_product_state(void)
+{
+    TCGv zero = tcg_constant_tl(0);
+
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P0), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P1), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P2), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P3), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P4), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P5), zero);
+}
+
+static bool trans_mtm(DisasContext *ctx, arg_r2 *a, ptrdiff_t offset,
+                      ptrdiff_t high_offset)
+{
+    TCGv value = tcg_temp_new();
+
+    gen_load_gpr(value, a->rs);
+    octeon_store_tc_field(offset, value);
+    gen_load_gpr(value, a->rt);
+    octeon_store_tc_field(high_offset, value);
+    octeon_zero_partial_product_state();
+    return true;
+}
+
+static bool trans_mtp(DisasContext *ctx, arg_r2 *a, ptrdiff_t offset,
+                      ptrdiff_t high_offset)
+{
+    TCGv value = tcg_temp_new();
+
+    gen_load_gpr(value, a->rs);
+    octeon_store_tc_field(offset, value);
+    gen_load_gpr(value, a->rt);
+    octeon_store_tc_field(high_offset, value);
+    return true;
+}
+
+static bool trans_vmul(DisasContext *ctx, arg_decode_ext_octeon1 *a,
+                       gen_helper_lmi *helper)
+{
+    TCGv lhs = tcg_temp_new();
+    TCGv rhs = tcg_temp_new();
+    TCGv result = tcg_temp_new();
+
+    gen_load_gpr(lhs, a->rs);
+    gen_load_gpr(rhs, a->rt);
+    helper(result, tcg_env, lhs, rhs);
+    gen_store_gpr(result, a->rd);
+    return true;
+}
+
+TRANS(SAA,  trans_saa, MO_UL);
+TRANS(SAAD, trans_saa, MO_UQ);
+TRANS(LBX,  trans_lx, MO_SB);
  TRANS(LBUX, trans_lx, MO_UB);
  TRANS(LHX,  trans_lx, MO_SW);
+TRANS(LHUX, trans_lx, MO_UW);
  TRANS(LWX,  trans_lx, MO_SL);
+TRANS(LWUX, trans_lx, MO_UL);
  TRANS(LDX,  trans_lx, MO_UQ);
+TRANS(MTM0, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL0),
+      offsetof(CPUMIPSState, active_tc.MPL3));

target/mips/tcg/octeon_translate.c static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop) · line 335

Quoted 10 lines
target/mips/tcg/octeon_translate.c +10 -0
+TRANS(MTM1, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL1),
+      offsetof(CPUMIPSState, active_tc.MPL4));
+TRANS(MTM2, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL2),
+      offsetof(CPUMIPSState, active_tc.MPL5));
+TRANS(MTP0, trans_mtp, offsetof(CPUMIPSState, active_tc.P0),
+      offsetof(CPUMIPSState, active_tc.P3));
+TRANS(MTP1, trans_mtp, offsetof(CPUMIPSState, active_tc.P1),
+      offsetof(CPUMIPSState, active_tc.P4));
+TRANS(MTP2, trans_mtp, offsetof(CPUMIPSState, active_tc.P2),
+      offsetof(CPUMIPSState, active_tc.P5));

TRANS() takes a VARARG list, so using the array suggestion we can easily rework the helpers and have simply:

TRANS(MTM0, trans_mtm, 0)
TRANS(MTM1, trans_mtm, 1)
TRANS(MTM2, trans_mtm, 2)
TRANS(MTP0, trans_mtp, 0)
TRANS(MTP1, trans_mtp, 1)
TRANS(MTP2, trans_mtp, 2)

Quoted 16 lines
target/mips/tcg/octeon_translate.c +2 -0
+TRANS(VMULU, trans_vmul, gen_helper_octeon_vmulu);
+TRANS(V3MULU, trans_vmul, gen_helper_octeon_v3mulu);
target/mips/tcg/op_helper.c +6 -0
index 4502ae2b5b..d2398b6426 100644
@@ -144,6 +144,90 @@ target_ulong helper_rotx(target_ulong rs, uint32_t shift, uint32_t shiftx,
      return (int64_t)(int32_t)(uint32_t)tmp5;
  }
 
+static void octeon_addc(uint64_t *res, int count, uint64_t value, int index)
+{
+    while (index < count) {
+        uint64_t old = res[index];
+
+        res[index] += value;

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

Quoted 10 lines
target/mips/tcg/op_helper.c +10 -0
+        if (res[index] >= old) {
+            break;
+        }
+        value = 1;
+        index++;
+    }
+}
+
+target_ulong helper_octeon_vmulu(CPUMIPSState *env, target_ulong arg1,
+                                 target_ulong arg2)

s/target_ulong/uint64_t/

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

Quoted 5 lines
target/mips/tcg/op_helper.c +5 -0
+{
+    uint64_t lo, hi;
+    uint64_t res[3] = {};
+
+    mulu64(&lo, &hi, env->active_tc.MPL0, arg1);

(See, you implicitly use target_ulong as uint64_t)

Quoted 8 lines
target/mips/tcg/op_helper.c +8 -0
+    res[0] = lo;
+    res[1] = hi;
+
+    mulu64(&lo, &hi, env->active_tc.MPL1, arg1);
+    octeon_addc(res, 3, lo, 1);
+    octeon_addc(res, 3, hi, 2);
+
+    octeon_addc(res, 3, arg2, 0);

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

Quoted 10 lines
target/mips/tcg/op_helper.c +10 -0
+    octeon_addc(res, 3, env->active_tc.P0, 0);
+    octeon_addc(res, 3, env->active_tc.P1, 1);
+
+    env->active_tc.P0 = res[1];
+    env->active_tc.P1 = res[2];
+    return res[0];
+}
+
+target_ulong helper_octeon_v3mulu(CPUMIPSState *env, target_ulong arg1,
+                                  target_ulong arg2)

Ditto.

Quoted 48 lines
target/mips/tcg/op_helper.c +45 -0
+{
+    uint64_t lo, hi;
+    uint64_t res[7] = {};
+
+    mulu64(&lo, &hi, env->active_tc.MPL0, arg1);
+    res[0] = lo;
+    res[1] = hi;
+
+    mulu64(&lo, &hi, env->active_tc.MPL1, arg1);
+    octeon_addc(res, 7, lo, 1);
+    octeon_addc(res, 7, hi, 2);
+
+    mulu64(&lo, &hi, env->active_tc.MPL2, arg1);
+    octeon_addc(res, 7, lo, 2);
+    octeon_addc(res, 7, hi, 3);
+
+    mulu64(&lo, &hi, env->active_tc.MPL3, arg1);
+    octeon_addc(res, 7, lo, 3);
+    octeon_addc(res, 7, hi, 4);
+
+    mulu64(&lo, &hi, env->active_tc.MPL4, arg1);
+    octeon_addc(res, 7, lo, 4);
+    octeon_addc(res, 7, hi, 5);
+
+    mulu64(&lo, &hi, env->active_tc.MPL5, arg1);
+    octeon_addc(res, 7, lo, 5);
+    octeon_addc(res, 7, hi, 6);
+
+    octeon_addc(res, 7, arg2, 0);
+    octeon_addc(res, 7, env->active_tc.P0, 0);
+    octeon_addc(res, 7, env->active_tc.P1, 1);
+    octeon_addc(res, 7, env->active_tc.P2, 2);
+    octeon_addc(res, 7, env->active_tc.P3, 3);
+    octeon_addc(res, 7, env->active_tc.P4, 4);
+    octeon_addc(res, 7, env->active_tc.P5, 5);
+
+    env->active_tc.P0 = res[1];
+    env->active_tc.P1 = res[2];
+    env->active_tc.P2 = res[3];
+    env->active_tc.P3 = res[4];
+    env->active_tc.P4 = res[5];
+    env->active_tc.P5 = res[6];
+    return res[0];
+}
+
  /* these crc32 functions are based on target/loongarch/tcg/op_helper.c */
  target_ulong helper_crc32(target_ulong val, target_ulong m, uint32_t sz)
  {
#3 lore raw

Re: [PATCH 2/9] target/mips: add Octeon arithmetic and memory instructions

Quoted 14 lines · Philippe Mathieu-Daudé

Hi James,

Quoted 9 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 unaligned load/store forms, cache block zero operations, the Octeon multiply family, population count handling, and related arithmetic and memory operations in the translator and helper layer.

Quoted 4 lines

Thanks for this series! (Please include a cover letter for v2).

For historical background, see my previous question regarding testing:
https://lore.kernel.org/qemu-devel/[email protected]/

I have not yet attempted full system/kernel emulation, only qemu-user userspace binary emulation so far, I've mostly tested this patch series against a mixture of proprietary cavium octeon binaries along with a modified version of the cavium cpri sdk cross compiled with a cavium octeon vendor toolchain(https://github.com/jameshilliard/cavium-cpri) for validating that various crypto test vectors produce their expected outputs. I also validated some of the instructions against
https://github.com/wolfSSL/wolfssl compiled with cavium octeon crypto instructions enabled.

Quoted 2 lines

We need few pieces of guest code to test this feature, because we don't want to maintain bitrotten code.

What sort of tests would we want for that?

Quoted 577 lines
Quoted 30 lines
---
  target/mips/cpu-defs.c.inc         |  10 +-
  target/mips/cpu.h                  |  12 +++
  target/mips/helper.h               |   2 +
  target/mips/system/machine.c       |  43 ++++++++
  target/mips/tcg/octeon.decode      |  28 ++++-
  target/mips/tcg/octeon_translate.c | 163 +++++++++++++++++++++++++++--
  target/mips/tcg/op_helper.c        |  84 +++++++++++++++
  7 files changed, 331 insertions(+), 11 deletions(-)
target/mips/cpu-defs.c.inc +2 -1
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,

target/mips/cpu-defs.c.inc const mips_def_t mips_defs[] = · line 1023

Quoted 10 lines
target/mips/cpu-defs.c.inc +6 -1
-        .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,

Exposing these CPU features to the guest should be the last patch.

Quoted 10 lines
target/mips/cpu.h +2 -0
index ac81470576..98cd7ae83e 100644
@@ -497,6 +497,18 @@ struct TCState {
      target_ulong CP0_TCScheFBack;
      int32_t CP0_Debug_tcstatus;
      target_ulong CP0_UserLocal;
+    target_ulong MPL0;
+    target_ulong MPL1;

target/mips/cpu.h struct TCState { · line 511

Quoted 10 lines
target/mips/cpu.h +10 -0
+    target_ulong MPL2;
+    target_ulong MPL3;
+    target_ulong MPL4;
+    target_ulong MPL5;
+    target_ulong P0;
+    target_ulong P1;
+    target_ulong P2;
+    target_ulong P3;
+    target_ulong P4;
+    target_ulong P5;

This CPU only exists in 64-bit variant, so please directly start with uint64_t (we try to remove target_ulong uses).

We usually model banked registers as array:

   uint64_t MPL[2 * 3];

Although maybe having an octeon field could make the code easier to follow (like the other octeon_crypto structure you introduce):

   struct {
       uint64_t MPL[2 * 3];
       uint64_t P[2 * 3];
   } octeon;
Quoted 148 lines
target/mips/cpu.h +0 -0
      int32_t msacsr;
 
target/mips/helper.h +2 -0
index b6cd53c853..3c9f67ec40 100644
@@ -24,6 +24,8 @@ 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, tl, env, tl, tl)
+DEF_HELPER_3(octeon_v3mulu, tl, env, tl, tl)
 
  /* microMIPS functions */
  DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/system/machine.c +43 -0
index 473d3ab036..1ea5907e55 100644
@@ -118,6 +118,27 @@ 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_UINTTL(MPL0, TCState),
+        VMSTATE_UINTTL(MPL1, TCState),
+        VMSTATE_UINTTL(MPL2, TCState),
+        VMSTATE_UINTTL(MPL3, TCState),
+        VMSTATE_UINTTL(MPL4, TCState),
+        VMSTATE_UINTTL(MPL5, TCState),
+        VMSTATE_UINTTL(P0, TCState),
+        VMSTATE_UINTTL(P1, TCState),
+        VMSTATE_UINTTL(P2, TCState),
+        VMSTATE_UINTTL(P3, TCState),
+        VMSTATE_UINTTL(P4, TCState),
+        VMSTATE_UINTTL(P5, TCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
  /* MVP state */
 
  static const VMStateDescription vmstate_mvp = {
@@ -237,6 +258,27 @@ static const VMStateDescription mips_vmstate_timer = {
      }
  };
 
+static bool mips_octeon_multiplier_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_multiplier_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 +395,7 @@ const VMStateDescription vmstate_mips_cpu = {
      },
      .subsections = (const VMStateDescription * const []) {
          &mips_vmstate_timer,
+        &mips_vmstate_octeon_multiplier,
          NULL
      }
  };
target/mips/tcg/octeon.decode +26 -2
index 102a05860d..d90a194b54 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,12 +40,34 @@ 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
+&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
+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
 
  &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 +2 -0
index b2eca29e06..67b8634dff 100644
@@ -13,6 +13,8 @@
  /* Include the auto-generated decoder.  */
  #include "decode-octeon.c.inc"
 
+typedef void gen_helper_lmi(TCGv, TCGv_ptr, TCGv, TCGv);
+
  static bool trans_BBIT(DisasContext *ctx, arg_BBIT *a)
  {
      TCGv p;

target/mips/tcg/octeon_translate.c static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a) · line 50

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -45,7 +47,7 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
  {
      TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {

Bug fix? Distinct patch please.

Quoted 14 lines
target/mips/tcg/octeon_translate.c +2 -1
          /* nop */
          return true;
      }
@@ -56,7 +58,8 @@ static bool trans_BADDU(DisasContext *ctx, arg_BADDU *a)
      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_andi_tl(t0, t0, 0xff);
+    gen_store_gpr(t0, a->rd);
      return true;
  }
 

target/mips/tcg/octeon_translate.c static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a) · line 70

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -64,7 +67,7 @@ static bool trans_DMUL(DisasContext *ctx, arg_DMUL *a)
  {
      TCGv t0, t1;
 
-    if (a->rt == 0) {
+    if (a->rd == 0) {

Ditto.

Quoted 14 lines
target/mips/tcg/octeon_translate.c +2 -1
          /* nop */
          return true;
      }
@@ -74,7 +77,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_tl(t0, t0, t1);
+    gen_store_gpr(t0, a->rd);
      return true;
  }
 

target/mips/tcg/octeon_translate.c static bool trans_POP(DisasContext *ctx, arg_POP *a) · line 129

Quoted 5 lines
target/mips/tcg/octeon_translate.c +1 -1
@@ -122,14 +126,14 @@ static bool trans_POP(DisasContext *ctx, arg_POP *a)
      t0 = tcg_temp_new();
      gen_load_gpr(t0, a->rs);
      if (!a->dw) {
-        tcg_gen_andi_i64(t0, t0, 0xffffffff);
+        tcg_gen_andi_tl(t0, t0, 0xffffffff);

No, please avoid *_tl() helpers in this 64-bit only translator.

Quoted 33 lines
target/mips/tcg/octeon_translate.c +12 -2
      }
      tcg_gen_ctpop_tl(t0, t0);
      gen_store_gpr(t0, a->rd);
      return true;
  }
 
-static bool trans_SEQNE(DisasContext *ctx, arg_SEQNE *a)
+static bool trans_seqne(DisasContext *ctx, const arg_cmp3 *a)
  {
      TCGv t0, t1;
 
@@ -152,7 +156,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;
 
@@ -175,6 +189,16 @@ static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a)
      return true;
  }

target/mips/tcg/octeon_translate.c static bool trans_SEQNEI(DisasContext *ctx, arg_SEQNEI *a) · line 200

Quoted 10 lines
target/mips/tcg/octeon_translate.c +9 -0
 
+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);
+}

Please move these changes in a preliminary patch to help reviewers.

Quoted 124 lines
target/mips/tcg/octeon_translate.c +113 -0
  static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
  {
      gen_lx(ctx, a->rd, a->base, a->index, mop);
@@ -182,7 +206,132 @@ static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop)
      return true;
  }
 
+static bool trans_saa(DisasContext *ctx, arg_saa *a, MemOp mop)
+{
+    TCGv addr = tcg_temp_new();
+    MemOp amo = mo_endian(ctx) | mop | ctx->default_tcg_memop_mask;
+
+    gen_base_offset_addr(ctx, addr, a->base, 0);
+
+    if (mop == MO_UQ) {
+        TCGv value = tcg_temp_new();
+        TCGv old = tcg_temp_new();
+
+        gen_load_gpr(value, a->rt);
+        tcg_gen_atomic_fetch_add_tl(old, addr, value, ctx->mem_idx, amo);
+    } else {
+        TCGv value = tcg_temp_new();
+        TCGv_i32 value32 = tcg_temp_new_i32();
+        TCGv_i32 old = tcg_temp_new_i32();
+
+        gen_load_gpr(value, a->rt);
+        tcg_gen_trunc_tl_i32(value32, value);
+        tcg_gen_atomic_fetch_add_i32(old, addr, value32, ctx->mem_idx, amo);
+    }
+
+    return true;
+}
+
+static bool trans_ZCB(DisasContext *ctx, arg_zcb *a)
+{
+    TCGv addr = tcg_temp_new();
+    TCGv line = tcg_temp_new();
+    TCGv zero = tcg_constant_tl(0);
+
+    gen_base_offset_addr(ctx, addr, a->base, 0);
+
+    /*
+     * Octeon zcb operates on a cache block. Model it as zeroing the
+     * containing 128-byte line in memory.
+     */
+    tcg_gen_andi_tl(line, addr, ~((target_ulong)127));
+
+    for (int i = 0; i < 16; i++) {
+        TCGv slot = tcg_temp_new();
+
+        tcg_gen_addi_tl(slot, line, i * 8);
+        tcg_gen_qemu_st_tl(zero, slot, ctx->mem_idx, mo_endian(ctx) | MO_UQ);
+    }
+
+    return true;
+}
+
+static void octeon_store_tc_field(ptrdiff_t offset, TCGv value)
+{
+    tcg_gen_st_tl(value, tcg_env, offset);
+}
+
+static void octeon_zero_partial_product_state(void)
+{
+    TCGv zero = tcg_constant_tl(0);
+
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P0), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P1), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P2), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P3), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P4), zero);
+    octeon_store_tc_field(offsetof(CPUMIPSState, active_tc.P5), zero);
+}
+
+static bool trans_mtm(DisasContext *ctx, arg_r2 *a, ptrdiff_t offset,
+                      ptrdiff_t high_offset)
+{
+    TCGv value = tcg_temp_new();
+
+    gen_load_gpr(value, a->rs);
+    octeon_store_tc_field(offset, value);
+    gen_load_gpr(value, a->rt);
+    octeon_store_tc_field(high_offset, value);
+    octeon_zero_partial_product_state();
+    return true;
+}
+
+static bool trans_mtp(DisasContext *ctx, arg_r2 *a, ptrdiff_t offset,
+                      ptrdiff_t high_offset)
+{
+    TCGv value = tcg_temp_new();
+
+    gen_load_gpr(value, a->rs);
+    octeon_store_tc_field(offset, value);
+    gen_load_gpr(value, a->rt);
+    octeon_store_tc_field(high_offset, value);
+    return true;
+}
+
+static bool trans_vmul(DisasContext *ctx, arg_decode_ext_octeon1 *a,
+                       gen_helper_lmi *helper)
+{
+    TCGv lhs = tcg_temp_new();
+    TCGv rhs = tcg_temp_new();
+    TCGv result = tcg_temp_new();
+
+    gen_load_gpr(lhs, a->rs);
+    gen_load_gpr(rhs, a->rt);
+    helper(result, tcg_env, lhs, rhs);
+    gen_store_gpr(result, a->rd);
+    return true;
+}
+
+TRANS(SAA,  trans_saa, MO_UL);
+TRANS(SAAD, trans_saa, MO_UQ);
+TRANS(LBX,  trans_lx, MO_SB);
  TRANS(LBUX, trans_lx, MO_UB);
  TRANS(LHX,  trans_lx, MO_SW);
+TRANS(LHUX, trans_lx, MO_UW);
  TRANS(LWX,  trans_lx, MO_SL);
+TRANS(LWUX, trans_lx, MO_UL);
  TRANS(LDX,  trans_lx, MO_UQ);
+TRANS(MTM0, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL0),
+      offsetof(CPUMIPSState, active_tc.MPL3));

target/mips/tcg/octeon_translate.c static bool trans_lx(DisasContext *ctx, arg_lx *a, MemOp mop) · line 335

Quoted 10 lines
target/mips/tcg/octeon_translate.c +10 -0
+TRANS(MTM1, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL1),
+      offsetof(CPUMIPSState, active_tc.MPL4));
+TRANS(MTM2, trans_mtm, offsetof(CPUMIPSState, active_tc.MPL2),
+      offsetof(CPUMIPSState, active_tc.MPL5));
+TRANS(MTP0, trans_mtp, offsetof(CPUMIPSState, active_tc.P0),
+      offsetof(CPUMIPSState, active_tc.P3));
+TRANS(MTP1, trans_mtp, offsetof(CPUMIPSState, active_tc.P1),
+      offsetof(CPUMIPSState, active_tc.P4));
+TRANS(MTP2, trans_mtp, offsetof(CPUMIPSState, active_tc.P2),
+      offsetof(CPUMIPSState, active_tc.P5));

TRANS() takes a VARARG list, so using the array suggestion we can easily rework the helpers and have simply:

TRANS(MTM0, trans_mtm, 0)
TRANS(MTM1, trans_mtm, 1)
TRANS(MTM2, trans_mtm, 2)
TRANS(MTP0, trans_mtp, 0)
TRANS(MTP1, trans_mtp, 1)
TRANS(MTP2, trans_mtp, 2)

Quoted 16 lines
target/mips/tcg/octeon_translate.c +2 -0
+TRANS(VMULU, trans_vmul, gen_helper_octeon_vmulu);
+TRANS(V3MULU, trans_vmul, gen_helper_octeon_v3mulu);
target/mips/tcg/op_helper.c +6 -0
index 4502ae2b5b..d2398b6426 100644
@@ -144,6 +144,90 @@ target_ulong helper_rotx(target_ulong rs, uint32_t shift, uint32_t shiftx,
      return (int64_t)(int32_t)(uint32_t)tmp5;
  }
 
+static void octeon_addc(uint64_t *res, int count, uint64_t value, int index)
+{
+    while (index < count) {
+        uint64_t old = res[index];
+
+        res[index] += value;

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

Quoted 10 lines
target/mips/tcg/op_helper.c +10 -0
+        if (res[index] >= old) {
+            break;
+        }
+        value = 1;
+        index++;
+    }
+}
+
+target_ulong helper_octeon_vmulu(CPUMIPSState *env, target_ulong arg1,
+                                 target_ulong arg2)

s/target_ulong/uint64_t/

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

Quoted 5 lines
target/mips/tcg/op_helper.c +5 -0
+{
+    uint64_t lo, hi;
+    uint64_t res[3] = {};
+
+    mulu64(&lo, &hi, env->active_tc.MPL0, arg1);

(See, you implicitly use target_ulong as uint64_t)

Quoted 8 lines
target/mips/tcg/op_helper.c +8 -0
+    res[0] = lo;
+    res[1] = hi;
+
+    mulu64(&lo, &hi, env->active_tc.MPL1, arg1);
+    octeon_addc(res, 3, lo, 1);
+    octeon_addc(res, 3, hi, 2);
+
+    octeon_addc(res, 3, arg2, 0);

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

Quoted 10 lines
target/mips/tcg/op_helper.c +10 -0
+    octeon_addc(res, 3, env->active_tc.P0, 0);
+    octeon_addc(res, 3, env->active_tc.P1, 1);
+
+    env->active_tc.P0 = res[1];
+    env->active_tc.P1 = res[2];
+    return res[0];
+}
+
+target_ulong helper_octeon_v3mulu(CPUMIPSState *env, target_ulong arg1,
+                                  target_ulong arg2)

Ditto.

Quoted 48 lines
target/mips/tcg/op_helper.c +45 -0
+{
+    uint64_t lo, hi;
+    uint64_t res[7] = {};
+
+    mulu64(&lo, &hi, env->active_tc.MPL0, arg1);
+    res[0] = lo;
+    res[1] = hi;
+
+    mulu64(&lo, &hi, env->active_tc.MPL1, arg1);
+    octeon_addc(res, 7, lo, 1);
+    octeon_addc(res, 7, hi, 2);
+
+    mulu64(&lo, &hi, env->active_tc.MPL2, arg1);
+    octeon_addc(res, 7, lo, 2);
+    octeon_addc(res, 7, hi, 3);
+
+    mulu64(&lo, &hi, env->active_tc.MPL3, arg1);
+    octeon_addc(res, 7, lo, 3);
+    octeon_addc(res, 7, hi, 4);
+
+    mulu64(&lo, &hi, env->active_tc.MPL4, arg1);
+    octeon_addc(res, 7, lo, 4);
+    octeon_addc(res, 7, hi, 5);
+
+    mulu64(&lo, &hi, env->active_tc.MPL5, arg1);
+    octeon_addc(res, 7, lo, 5);
+    octeon_addc(res, 7, hi, 6);
+
+    octeon_addc(res, 7, arg2, 0);
+    octeon_addc(res, 7, env->active_tc.P0, 0);
+    octeon_addc(res, 7, env->active_tc.P1, 1);
+    octeon_addc(res, 7, env->active_tc.P2, 2);
+    octeon_addc(res, 7, env->active_tc.P3, 3);
+    octeon_addc(res, 7, env->active_tc.P4, 4);
+    octeon_addc(res, 7, env->active_tc.P5, 5);
+
+    env->active_tc.P0 = res[1];
+    env->active_tc.P1 = res[2];
+    env->active_tc.P2 = res[3];
+    env->active_tc.P3 = res[4];
+    env->active_tc.P4 = res[5];
+    env->active_tc.P5 = res[6];
+    return res[0];
+}
+
  /* these crc32 functions are based on target/loongarch/tcg/op_helper.c */
  target_ulong helper_crc32(target_ulong val, target_ulong m, uint32_t sz)
  {
#4 lore raw

PATCH 3/9 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.

---
 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 d90a194b54..0f5fe83609 100644
@@ -59,6 +59,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
 
target/mips/tcg/octeon_translate.c +170 -0

182 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

PATCH 4/9 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 initial hash, AES, CRC, GFM, 3DES, KASUMI, and SNOW3G engines and moves the implementation into octeon_crypto.c to keep the MIPS helper layer manageable.

---
 target/mips/cpu.h                  |  165 +++
 target/mips/helper.h               |    3 +
 target/mips/tcg/meson.build        |    1 +
 target/mips/tcg/octeon.decode      |    2 +
 target/mips/tcg/octeon_crypto.c    | 1628 ++++++++++++++++++++++++++++
 target/mips/tcg/octeon_translate.c |    6 +
 target/mips/tcg/op_helper.c        |   14 +
 target/mips/tcg/translate.c        |  196 ++++
 8 files changed, 2015 insertions(+)
 create mode 100644 target/mips/tcg/octeon_crypto.c
target/mips/cpu.h +165 -0
index 98cd7ae83e..b4b7575ecb 100644
@@ -537,6 +537,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;
+    MIPSOcteonSharedMode 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;
@@ -558,6 +721,8 @@ typedef struct CPUArchState {
 #define MSAIR_ProcID    8
 #define MSAIR_Rev       0
 
+    MIPSOcteonCryptoState octeon_crypto;
+
 /*
  * CP0 Register 0
  */
target/mips/helper.h +3 -0
index 3c9f67ec40..0d5713b705 100644
@@ -25,7 +25,10 @@ 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, tl, env, tl, tl)
+DEF_HELPER_3(octeon_vmm0, tl, env, tl, tl)
 DEF_HELPER_3(octeon_v3mulu, tl, env, tl, tl)
+DEF_HELPER_2(octeon_cop2_dmfc2, tl, env, i32)
+DEF_HELPER_3(octeon_cop2_dmtc2, void, env, tl, i32)
 
 /* microMIPS functions */
 DEF_HELPER_4(lwm, void, env, tl, tl, i32)
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.decode +2 -0
index 0f5fe83609..9dcc5a6ae0 100644
@@ -52,6 +52,7 @@ 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
@@ -78,6 +79,7 @@ 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
 
 &lx          base index rd
 @lx          ...... base:5 index:5 rd:5 ...... ..... &lx
target/mips/tcg/octeon_crypto.c new file +1628 -0

1,628 lines not on this page · raw message

target/mips/tcg/octeon_translate.c +6 -0
index 9ac20196de..2de750e314 100644
@@ -414,6 +414,11 @@ static bool trans_ZCB(DisasContext *ctx, arg_zcb *a)
     return true;
 }
 
+static bool trans_ZCBT(DisasContext *ctx, arg_zcb *a)
+{
+    return trans_ZCB(ctx, a);
+}
+
 static void octeon_store_tc_field(ptrdiff_t offset, TCGv value)
 {
     tcg_gen_st_tl(value, tcg_env, offset);
@@ -504,4 +509,5 @@ TRANS(MTP1, trans_mtp, offsetof(CPUMIPSState, active_tc.P1),
 TRANS(MTP2, trans_mtp, offsetof(CPUMIPSState, active_tc.P2),
       offsetof(CPUMIPSState, active_tc.P5));
 TRANS(VMULU, trans_vmul, gen_helper_octeon_vmulu);
+TRANS(VMM0, trans_vmul, gen_helper_octeon_vmm0);
 TRANS(V3MULU, trans_vmul, gen_helper_octeon_v3mulu);
target/mips/tcg/op_helper.c +14 -0
index d2398b6426..3164166910 100644
@@ -181,6 +181,20 @@ target_ulong helper_octeon_vmulu(CPUMIPSState *env, target_ulong arg1,
     return res[0];
 }
 
+target_ulong helper_octeon_vmm0(CPUMIPSState *env, target_ulong arg1,
+                                target_ulong arg2)
+{
+    uint64_t lo;
+
+    /*
+     * VMM0 is the Montgomery-reduction companion to VMULU: it computes the
+     * low limb of MPL0 * arg1 + arg2 and writes that result back to M0.
+     */
+    lo = env->active_tc.MPL0 * (uint64_t)arg1 + (uint64_t)arg2;
+    env->active_tc.MPL0 = lo;
+    return lo;
+}
+
 target_ulong helper_octeon_v3mulu(CPUMIPSState *env, target_ulong arg1,
                                   target_ulong arg2)
 {
target/mips/tcg/translate.c +196 -0

208 lines not on this page · raw message

Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 4/9] target/mips: add Octeon COP2 crypto core support

Quoted 20 lines · 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 initial hash, AES, CRC, GFM, 3DES, KASUMI, and SNOW3G engines and moves the implementation into octeon_crypto.c to keep the MIPS helper layer manageable.

---
  target/mips/cpu.h                  |  165 +++
  target/mips/helper.h               |    3 +
  target/mips/tcg/meson.build        |    1 +
  target/mips/tcg/octeon.decode      |    2 +
  target/mips/tcg/octeon_crypto.c    | 1628 ++++++++++++++++++++++++++++
  target/mips/tcg/octeon_translate.c |    6 +
  target/mips/tcg/op_helper.c        |   14 +
  target/mips/tcg/translate.c        |  196 ++++
  8 files changed, 2015 insertions(+)
Quoted 1 line

create mode 100644 target/mips/tcg/octeon_crypto.c

Typical patch I'd feel safer to review with something to test in parallel.

No particular comment except s/target_ulong/uint64_t/ mentioned previously.

#7 lore raw

Re: [PATCH 4/9] target/mips: add Octeon COP2 crypto core support

In reply to James Hilliard
Quoted 209 lines · 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 initial hash, AES, CRC, GFM, 3DES, KASUMI, and SNOW3G engines and moves the implementation into octeon_crypto.c to keep the MIPS helper layer manageable.

---
  target/mips/cpu.h                  |  165 +++
  target/mips/helper.h               |    3 +
  target/mips/tcg/meson.build        |    1 +
  target/mips/tcg/octeon.decode      |    2 +
  target/mips/tcg/octeon_crypto.c    | 1628 ++++++++++++++++++++++++++++
  target/mips/tcg/octeon_translate.c |    6 +
  target/mips/tcg/op_helper.c        |   14 +
  target/mips/tcg/translate.c        |  196 ++++
  8 files changed, 2015 insertions(+)

create mode 100644 target/mips/tcg/octeon_crypto.c

target/mips/tcg/translate.c +178 -0
index 2e3fe1fa16..42445e5526 100644
@@ -9127,6 +9127,195 @@ static void gen_cp1(DisasContext *ctx, uint32_t opc, int rt, int fs)
      }
  }
 
+static bool octeon_cop2_is_supported_dmfc2(uint16_t sel)
+{
+    switch (sel) {
+    case OCTEON_COP2_SEL_3DES_KEY0:
+    case OCTEON_COP2_SEL_3DES_KEY1:
+    case OCTEON_COP2_SEL_3DES_KEY2:
+    case OCTEON_COP2_SEL_3DES_IV:
+    case OCTEON_COP2_SEL_3DES_RESULT:
+    case OCTEON_COP2_SEL_3DES_RESULT_IN:
+    case OCTEON_COP2_SEL_AES_RESULT0:
+    case OCTEON_COP2_SEL_AES_RESULT1:
+    case OCTEON_COP2_SEL_CRC_POLYNOMIAL:
+    case OCTEON_COP2_SEL_AES_IV0:
+    case OCTEON_COP2_SEL_AES_IV1:
+    case OCTEON_COP2_SEL_CRC_IV:
+    case OCTEON_COP2_SEL_CRC_LEN:
+    case OCTEON_COP2_SEL_CRC_IV_REFLECT:
+    case OCTEON_COP2_SEL_HSH_DATW0:
+    case OCTEON_COP2_SEL_HSH_DATW1:
+    case OCTEON_COP2_SEL_HSH_DATW2:
+    case OCTEON_COP2_SEL_HSH_DATW3:
+    case OCTEON_COP2_SEL_HSH_DATW4:
+    case OCTEON_COP2_SEL_HSH_DATW5:
+    case OCTEON_COP2_SEL_HSH_DATW6:
+    case OCTEON_COP2_SEL_HSH_DATW7:
+    case OCTEON_COP2_SEL_HSH_IV0:
+    case OCTEON_COP2_SEL_HSH_IV1:
+    case OCTEON_COP2_SEL_HSH_IV2:
+    case OCTEON_COP2_SEL_HSH_IV3:
+    case OCTEON_COP2_SEL_HSH_IVW0:
+    case OCTEON_COP2_SEL_HSH_IVW1:
+    case OCTEON_COP2_SEL_HSH_IVW2:
+    case OCTEON_COP2_SEL_HSH_IVW3:
+    case OCTEON_COP2_SEL_HSH_IVW4:
+    case OCTEON_COP2_SEL_HSH_IVW5:
+    case OCTEON_COP2_SEL_HSH_IVW6:
+    case OCTEON_COP2_SEL_HSH_IVW7:
+    case OCTEON_COP2_SEL_AES_DAT0:
+    case OCTEON_COP2_SEL_GFM_MUL_REFLECT0:
+    case OCTEON_COP2_SEL_GFM_MUL_REFLECT1:
+    case OCTEON_COP2_SEL_GFM_RESINP_REFLECT0:
+    case OCTEON_COP2_SEL_GFM_RESINP_REFLECT1:
+    case OCTEON_COP2_SEL_GFM_MUL0:
+    case OCTEON_COP2_SEL_GFM_MUL1:
+    case OCTEON_COP2_SEL_GFM_RESINP0:
+    case OCTEON_COP2_SEL_GFM_RESINP1:
+    case OCTEON_COP2_SEL_GFM_POLY:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
+{
+    switch (sel) {
+    case OCTEON_COP2_SEL_3DES_KEY0:
+    case OCTEON_COP2_SEL_3DES_KEY1:
+    case OCTEON_COP2_SEL_3DES_KEY2:
+    case OCTEON_COP2_SEL_3DES_IV:
+    case OCTEON_COP2_SEL_3DES_RESULT_IN:
+    case OCTEON_COP2_SEL_3DES_ENC_CBC:
+    case OCTEON_COP2_SEL_KAS_ENC_CBC:
+    case OCTEON_COP2_SEL_3DES_ENC:
+    case OCTEON_COP2_SEL_KAS_ENC:
+    case OCTEON_COP2_SEL_3DES_DEC_CBC:
+    case OCTEON_COP2_SEL_3DES_DEC:
+    case OCTEON_COP2_SEL_AES_IV0:
+    case OCTEON_COP2_SEL_AES_IV1:
+    case OCTEON_COP2_SEL_AES_KEY0:
+    case OCTEON_COP2_SEL_AES_KEY1:
+    case OCTEON_COP2_SEL_AES_KEY2:
+    case OCTEON_COP2_SEL_AES_KEY3:
+    case OCTEON_COP2_SEL_AES_ENC_CBC0:
+    case OCTEON_COP2_SEL_AES_ENC0:
+    case OCTEON_COP2_SEL_AES_DEC_CBC0:
+    case OCTEON_COP2_SEL_AES_DEC0:
+    case OCTEON_COP2_SEL_AES_KEYLENGTH:
+    case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL:
+    case OCTEON_COP2_SEL_CRC_IV:
+    case OCTEON_COP2_SEL_CRC_WRITE_LEN:
+    case OCTEON_COP2_SEL_CRC_WRITE_IV_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_BYTE:
+    case OCTEON_COP2_SEL_CRC_WRITE_HALF:
+    case OCTEON_COP2_SEL_CRC_WRITE_WORD:
+    case OCTEON_COP2_SEL_CRC_WRITE_DWORD:
+    case OCTEON_COP2_SEL_CRC_WRITE_VAR:
+    case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_BYTE_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_HALF_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_WORD_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_DWORD_REFLECT:
+    case OCTEON_COP2_SEL_CRC_WRITE_VAR_REFLECT:
+    case OCTEON_COP2_SEL_HSH_DAT0:
+    case OCTEON_COP2_SEL_HSH_DAT1:
+    case OCTEON_COP2_SEL_HSH_DAT2:
+    case OCTEON_COP2_SEL_HSH_DAT3:
+    case OCTEON_COP2_SEL_HSH_DAT4:
+    case OCTEON_COP2_SEL_HSH_DAT5:
+    case OCTEON_COP2_SEL_HSH_DAT6:
+    case OCTEON_COP2_SEL_HSH_IV0:
+    case OCTEON_COP2_SEL_HSH_IV1:
+    case OCTEON_COP2_SEL_HSH_IV2:
+    case OCTEON_COP2_SEL_HSH_IV3:
+    case OCTEON_COP2_SEL_HSH_DATW0:
+    case OCTEON_COP2_SEL_HSH_DATW1:
+    case OCTEON_COP2_SEL_HSH_DATW2:
+    case OCTEON_COP2_SEL_HSH_DATW3:
+    case OCTEON_COP2_SEL_HSH_DATW4:
+    case OCTEON_COP2_SEL_HSH_DATW5:
+    case OCTEON_COP2_SEL_HSH_DATW6:
+    case OCTEON_COP2_SEL_HSH_DATW7:
+    case OCTEON_COP2_SEL_HSH_DATW8:
+    case OCTEON_COP2_SEL_HSH_DATW9:
+    case OCTEON_COP2_SEL_HSH_DATW10:
+    case OCTEON_COP2_SEL_HSH_DATW11:
+    case OCTEON_COP2_SEL_HSH_DATW12:
+    case OCTEON_COP2_SEL_HSH_DATW13:
+    case OCTEON_COP2_SEL_HSH_DATW14:
+    case OCTEON_COP2_SEL_HSH_DATW15:
+    case OCTEON_COP2_SEL_HSH_IVW0:
+    case OCTEON_COP2_SEL_HSH_IVW1:
+    case OCTEON_COP2_SEL_HSH_IVW2:
+    case OCTEON_COP2_SEL_HSH_IVW3:
+    case OCTEON_COP2_SEL_HSH_IVW4:
+    case OCTEON_COP2_SEL_HSH_IVW5:
+    case OCTEON_COP2_SEL_HSH_IVW6:
+    case OCTEON_COP2_SEL_HSH_IVW7:
+    case OCTEON_COP2_SEL_GFM_MUL_REFLECT0:
+    case OCTEON_COP2_SEL_GFM_MUL_REFLECT1:
+    case OCTEON_COP2_SEL_GFM_XOR0_REFLECT:
+    case OCTEON_COP2_SEL_GFM_MUL0:
+    case OCTEON_COP2_SEL_GFM_MUL1:
+    case OCTEON_COP2_SEL_GFM_RESINP0:
+    case OCTEON_COP2_SEL_GFM_RESINP1:
+    case OCTEON_COP2_SEL_GFM_XOR0:
+    case OCTEON_COP2_SEL_GFM_POLY:
+    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_HSH_STARTSHA1:
+    case OCTEON_COP2_SEL_GFM_XORMUL1_REFLECT:
+    case OCTEON_COP2_SEL_HSH_STARTSHA512:
+    case OCTEON_COP2_SEL_GFM_XORMUL1:
+    case OCTEON_COP2_SEL_AES_ENC_CBC1:
+    case OCTEON_COP2_SEL_AES_ENC1:
+    case OCTEON_COP2_SEL_AES_DEC_CBC1:
+    case OCTEON_COP2_SEL_AES_DEC1:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static bool gen_octeon_cop2(DisasContext *ctx)
+{
+    enum {
+        OCTEON_CP2_RS_DMFC2 = 0x01,
+        OCTEON_CP2_RS_DMTC2 = 0x05,
+    };
+    int rs = extract32(ctx->opcode, 21, 5);
+    int rt = extract32(ctx->opcode, 16, 5);
+    uint16_t sel = ctx->opcode;
+    TCGv t0;
+
+    switch (rs) {
+    case OCTEON_CP2_RS_DMFC2:
+        if (!octeon_cop2_is_supported_dmfc2(sel)) {
+            return false;
+        }
+        t0 = tcg_temp_new();
+        gen_helper_octeon_cop2_dmfc2(t0, tcg_env, tcg_constant_i32(sel));
+        gen_store_gpr(t0, rt);
+        return true;
+    case OCTEON_CP2_RS_DMTC2:
+        if (!octeon_cop2_is_supported_dmtc2(sel)) {

target/mips/tcg/translate.c static void gen_cp1(DisasContext *ctx, uint32_t opc, int rt, int fs) · line 9317

Quoted 10 lines
target/mips/tcg/translate.c +10 -0
+            return false;
+        }
+        t0 = tcg_temp_new();
+        gen_load_gpr(t0, rt);
+        gen_helper_octeon_cop2_dmtc2(tcg_env, t0, tcg_constant_i32(sel));
+        return true;
+    default:
+        return false;
+    }
+}

All this code doesn't belong to translate.c but octeon_translate.c.

target/mips/tcg/translate.c static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx) · line 15053

Quoted 9 lines
target/mips/tcg/translate.c +2 -0
  static void gen_movci(DisasContext *ctx, int rd, int rs, int cc, int tf)
  {
      TCGLabel *l1;
@@ -14860,6 +15049,13 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
          }
          break;
      case OPC_CP2:
+        if (ctx->insn_flags & INSN_OCTEON) {
+            if (gen_octeon_cop2(ctx)) {

Ditto, declare gen_octeon_cop2() prototype in translate.h.

Quoted 8 lines
target/mips/tcg/translate.c +5 -0
+                break;
+            }
+            generate_exception_err(ctx, EXCP_CpU, 2);
+            break;
+        }
          check_insn(ctx, ASE_LMMI);
          /* Note that these instructions use different fields.  */
          gen_loongson_multimedia(ctx, sa, rd, rt);
#8 lore raw

Re: [PATCH 4/9] target/mips: add Octeon COP2 crypto core support

In reply to James Hilliard
Quoted 12 lines · James Hilliard
target/mips/tcg/octeon_crypto.c new file +12 -0
+static uint64_t octeon_gfm_mul64x(uint64_t v, uint8_t c)
+{
+    if (v & 0x8000000000000000ULL) {
+        return (v << 1) ^ c;
+    }
+    return v << 1;
+}
+
+static uint64_t octeon_gfm_mul64(uint64_t v, uint64_t p, uint8_t c)
+{
+    uint64_t result = 0;
+    int i;

target/mips/tcg/octeon_crypto.c line 105

Quoted 10 lines
target/mips/tcg/octeon_crypto.c new file +10 -0
+
+    for (i = 0; i < 64; i++) {
+        if ((p >> i) & 1) {
+            result ^= v;
+        }
+        v = octeon_gfm_mul64x(v, c);
+    }
+
+    return result;
+}

See include/crypto/clmul.h.

r~

#9 lore raw

PATCH 5/9 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.

---
 target/mips/cpu.h               |  18 +++++
 target/mips/tcg/octeon_crypto.c | 114 ++++++++++++++++++++++++++++++++
 target/mips/tcg/translate.c     |   8 +++
 3 files changed, 140 insertions(+)
target/mips/cpu.h +18 -0
index b4b7575ecb..28b3e71793 100644
@@ -593,6 +593,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,
@@ -661,6 +675,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 +114 -0

144 lines not on this page · raw message

target/mips/tcg/translate.c +8 -0
index 42445e5526..c66cb63cfc 100644
@@ -9138,6 +9138,8 @@ static bool octeon_cop2_is_supported_dmfc2(uint16_t sel)
     case OCTEON_COP2_SEL_3DES_RESULT_IN:
     case OCTEON_COP2_SEL_AES_RESULT0:
     case OCTEON_COP2_SEL_AES_RESULT1:
+    case OCTEON_COP2_SEL_AES_KEY0:
+    case OCTEON_COP2_SEL_AES_KEY1:
     case OCTEON_COP2_SEL_CRC_POLYNOMIAL:
     case OCTEON_COP2_SEL_AES_IV0:
     case OCTEON_COP2_SEL_AES_IV1:
@@ -9194,6 +9196,8 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel)
     case OCTEON_COP2_SEL_KAS_ENC:
     case OCTEON_COP2_SEL_3DES_DEC_CBC:
     case OCTEON_COP2_SEL_3DES_DEC:
+    case OCTEON_COP2_SEL_AES_RESULT0:
+    case OCTEON_COP2_SEL_AES_RESULT1:
     case OCTEON_COP2_SEL_AES_IV0:
     case OCTEON_COP2_SEL_AES_IV1:
     case OCTEON_COP2_SEL_AES_KEY0:
@@ -9277,6 +9281,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
#10 lore raw

Re: [PATCH 5/9] target/mips: add Octeon SMS4 crypto support

Quoted 8 lines · 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.

Quoted 6 lines
---
  target/mips/cpu.h               |  18 +++++
  target/mips/tcg/octeon_crypto.c | 114 ++++++++++++++++++++++++++++++++
  target/mips/tcg/translate.c     |   8 +++
  3 files changed, 140 insertions(+)

Tests welcome, otherwise patch LGTM.

#11 lore raw

PATCH 6/9 target/mips: add Octeon SHA3 crypto support

In reply to James Hilliard

Add the Octeon SHA3 register window and STARTOP selector.

Model the dedicated 25-lane Keccak state and the Keccak-f[1600] permutation so the COP2 SHA3 interface follows the hardware behaviour.

---
 target/mips/cpu.h               |  22 +++++
 target/mips/tcg/octeon_crypto.c | 155 ++++++++++++++++++++++++++++++++
 target/mips/tcg/translate.c     |  30 +++++++
 3 files changed, 207 insertions(+)
target/mips/cpu.h +22 -0
index 28b3e71793..dc5223e165 100644
@@ -541,6 +541,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 {
@@ -645,6 +646,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,
@@ -671,6 +673,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,
@@ -683,6 +703,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,
@@ -697,6 +718,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/tcg/octeon_crypto.c +155 -0

210 lines not on this page · raw message

target/mips/tcg/translate.c +30 -0
index c66cb63cfc..be380cab11 100644
@@ -9154,6 +9154,14 @@ static bool octeon_cop2_is_supported_dmfc2(uint16_t sel)
     case OCTEON_COP2_SEL_HSH_DATW5:
     case OCTEON_COP2_SEL_HSH_DATW6:
     case OCTEON_COP2_SEL_HSH_DATW7:
+    case OCTEON_COP2_SEL_HSH_DATW8:
+    case OCTEON_COP2_SEL_HSH_DATW9:
+    case OCTEON_COP2_SEL_HSH_DATW10:
+    case OCTEON_COP2_SEL_HSH_DATW11:
+    case OCTEON_COP2_SEL_HSH_DATW12:
+    case OCTEON_COP2_SEL_HSH_DATW13:
+    case OCTEON_COP2_SEL_HSH_DATW14:
+    case OCTEON_COP2_SEL_SHA3_DAT15_READ:
     case OCTEON_COP2_SEL_HSH_IV0:
     case OCTEON_COP2_SEL_HSH_IV1:
     case OCTEON_COP2_SEL_HSH_IV2:
@@ -9167,6 +9175,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:
@@ -9251,6 +9260,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:
@@ -9268,11 +9279,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
#12 lore raw

Re: [PATCH 6/9] target/mips: add Octeon SHA3 crypto support

Quoted 15 lines · James Hilliard

Add the Octeon SHA3 register window and STARTOP selector.

Model the dedicated 25-lane Keccak state and the Keccak-f[1600] permutation so the COP2 SHA3 interface follows the hardware behaviour.

---
  target/mips/cpu.h               |  22 +++++
  target/mips/tcg/octeon_crypto.c | 155 ++++++++++++++++++++++++++++++++
  target/mips/tcg/translate.c     |  30 +++++++
  3 files changed, 207 insertions(+)
target/mips/tcg/octeon_crypto.c +2 -0
+static int octeon_sha3_dat_pos_from_sel(uint32_t sel)
+{

target/mips/tcg/octeon_crypto.c static void octeon_sha512_transform(MIPSOcteonCryptoState *crypto) · line 570

Quoted 10 lines
target/mips/tcg/octeon_crypto.c +10 -0
+    if (sel >= OCTEON_COP2_SEL_HSH_DATW0 &&
+        sel <= OCTEON_COP2_SEL_HSH_DATW14) {
+        return sel - OCTEON_COP2_SEL_HSH_DATW0;
+    }
+    if (sel >= OCTEON_COP2_SEL_HSH_IVW0 &&
+        sel <= OCTEON_COP2_SEL_HSH_IVW7) {
+        return 16 + (sel - OCTEON_COP2_SEL_HSH_IVW0);
+    }
+
+    switch (sel) {
       case OCTEON_COP2_SEL_HSH_DATW0 ... OCTEON_COP2_SEL_HSH_DATW14:
           return sel - OCTEON_COP2_SEL_HSH_DATW0;
       case OCTEON_COP2_SEL_HSH_IVW0 ... OCTEON_COP2_SEL_HSH_IVW7:
           return 16 + (sel - OCTEON_COP2_SEL_HSH_IVW0);

target/mips/tcg/octeon_crypto.c static void octeon_sha512_transform(MIPSOcteonCryptoState *crypto) · line 581

Quoted 11 lines
target/mips/tcg/octeon_crypto.c +11 -0
+    /*
+     * SHA3_DAT15_READ aliases HSH_DATW15 at 0x024f, so a single case covers
+     * both selector names.
+     */
+    case OCTEON_COP2_SEL_HSH_DATW15:
+    case OCTEON_COP2_SEL_SHA3_DAT15_WRITE:
+        return 15;
+    case OCTEON_COP2_SEL_SHA3_DAT24:
+        return 24;
+    default:
+        return -1;
           g_assert_not_reached();

Review

Quoted 2 lines
(quoted diff) +2 -0
+    }
+}

Otherwise patch LGTM.

#13 lore raw

PATCH 7/9 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.

---
 target/mips/cpu.h               |   7 +
 target/mips/tcg/octeon_crypto.c | 323 ++++++++++++++++++++++++++++++++
 target/mips/tcg/translate.c     |   2 +
 3 files changed, 332 insertions(+)
target/mips/cpu.h +7 -0
index dc5223e165..8249b17e8a 100644
@@ -541,6 +541,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;
 
@@ -704,6 +705,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,
@@ -738,6 +741,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/tcg/octeon_crypto.c +323 -0

347 lines not on this page · raw message

target/mips/tcg/translate.c +2 -0
index be380cab11..39fad07adc 100644
@@ -9303,6 +9303,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
#14 lore raw

Re: [PATCH 7/9] target/mips: add Octeon ZUC crypto support

Quoted 37 lines · 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.

---
  target/mips/cpu.h               |   7 +
  target/mips/tcg/octeon_crypto.c | 323 ++++++++++++++++++++++++++++++++
  target/mips/tcg/translate.c     |   2 +
  3 files changed, 332 insertions(+)
target/mips/tcg/octeon_crypto.c +19 -0
@@ -1487,6 +1732,38 @@ target_ulong helper_octeon_cop2_dmfc2(CPUMIPSState *env, uint32_t sel)
      MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
      int sha3_pos;
 
+    if (crypto->shared_mode == OCTEON_SHARED_MODE_ZUC) {
+        if (sel >= OCTEON_COP2_SEL_HSH_DATW0 &&
+            sel <= OCTEON_COP2_SEL_HSH_DATW7) {
+            unsigned int idx = sel - OCTEON_COP2_SEL_HSH_DATW0;
+
+            return octeon_zuc_pack_pair(crypto->zuc_lfsr[idx * 2],
+                                        crypto->zuc_lfsr[idx * 2 + 1]);
+        }
+        switch (sel) {
+        case OCTEON_COP2_SEL_HSH_DATW8:
+            return octeon_zuc_pack_pair(crypto->zuc_fsm[0], crypto->zuc_fsm[1]);
+        case OCTEON_COP2_SEL_HSH_DATW9:
+        case OCTEON_COP2_SEL_HSH_IVW0:
+            return octeon_zuc_pack_pair(crypto->zuc_window[0],
+                                        crypto->zuc_window[1]);
+        case OCTEON_COP2_SEL_HSH_DATW10:
+            return crypto->zuc_window[2];
+        case OCTEON_COP2_SEL_HSH_DATW11:
+        case OCTEON_COP2_SEL_HSH_IVW3:

target/mips/tcg/octeon_crypto.c target_ulong helper_octeon_cop2_dmfc2(CPUMIPSState *env, uint32_t sel) · line 1763

Quoted 10 lines
target/mips/tcg/octeon_crypto.c +10 -0
+            return crypto->zuc_tresult;
+        case OCTEON_COP2_SEL_SHA3_DAT15_READ:
+        case OCTEON_COP2_SEL_SHA3_DAT24:
+            return 0;
+        case OCTEON_COP2_SEL_HSH_IVW1:
+            return crypto->zuc_fsm[0];
+        case OCTEON_COP2_SEL_HSH_IVW2:
+            return crypto->zuc_fsm[1];
+        default:
+            break;
               g_assert_not_reached();
Quoted 37 lines
target/mips/tcg/octeon_crypto.c +30 -0
+        }
+    }
+
      if (crypto->shared_mode == OCTEON_SHARED_MODE_SNOW3G) {
          if (sel >= OCTEON_COP2_SEL_SNOW3G_LFSR0 &&
              sel <= OCTEON_COP2_SEL_SNOW3G_LFSR7) {
@@ -1606,6 +1883,46 @@ void helper_octeon_cop2_dmtc2(CPUMIPSState *env, target_ulong value,
      uint64_t q = (uint64_t)value;
      int sha3_pos;
 
+    if (crypto->shared_mode == OCTEON_SHARED_MODE_ZUC) {
+        if (sel >= OCTEON_COP2_SEL_HSH_DATW0 &&
+            sel <= OCTEON_COP2_SEL_HSH_DATW7) {
+            unsigned int idx = sel - OCTEON_COP2_SEL_HSH_DATW0;
+
+            crypto->zuc_lfsr[idx * 2] = (q >> 32) & 0x7fffffffU;
+            crypto->zuc_lfsr[idx * 2 + 1] = q & 0x7fffffffU;
+            return;
+        }
+        switch (sel) {
+        case OCTEON_COP2_SEL_HSH_DATW8:
+            crypto->zuc_fsm[0] = q >> 32;
+            crypto->zuc_fsm[1] = q;
+            return;
+        case OCTEON_COP2_SEL_HSH_DATW9:
+        case OCTEON_COP2_SEL_HSH_IVW0:
+            crypto->zuc_window[0] = q >> 32;
+            crypto->zuc_window[1] = q;
+            return;
+        case OCTEON_COP2_SEL_HSH_DATW10:
+            crypto->zuc_window[2] = q;
+            return;
+        case OCTEON_COP2_SEL_HSH_DATW11:
+        case OCTEON_COP2_SEL_HSH_IVW3:
+            crypto->zuc_tresult = q;
+            return;
+        case OCTEON_COP2_SEL_SHA3_DAT15_WRITE:

target/mips/tcg/octeon_crypto.c void helper_octeon_cop2_dmtc2(CPUMIPSState *env, target_ulong value, · line 1922

Quoted 10 lines
target/mips/tcg/octeon_crypto.c +10 -0
+        case OCTEON_COP2_SEL_SHA3_DAT24:
+            return;
+        case OCTEON_COP2_SEL_HSH_IVW1:
+            crypto->zuc_fsm[0] = q;
+            return;
+        case OCTEON_COP2_SEL_HSH_IVW2:
+            crypto->zuc_fsm[1] = q;
+            return;
+        default:
+            break;
               g_assert_not_reached();

Otherwise patch LGTM.

Quoted 2 lines
(quoted diff) +2 -0
+        }
+    }
#15 lore raw

PATCH 8/9 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.

---
 target/mips/cpu.h               |   9 +++
 target/mips/tcg/octeon_crypto.c | 120 ++++++++++++++++++++++++++++++++
 target/mips/tcg/translate.c     |   3 +
 3 files changed, 132 insertions(+)
target/mips/cpu.h +9 -0
index 8249b17e8a..69f96172d8 100644
@@ -595,6 +595,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.
@@ -696,6 +704,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/translate.c +3 -0
index 39fad07adc..625faae9d1 100644
@@ -9218,6 +9218,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:
@@ -9313,6 +9315,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
#16 lore raw

Re: [PATCH 8/9] target/mips: add Octeon Camellia crypto support

Quoted 130 lines · 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.

---
  target/mips/cpu.h               |   9 +++
  target/mips/tcg/octeon_crypto.c | 120 ++++++++++++++++++++++++++++++++
  target/mips/tcg/translate.c     |   3 +
  3 files changed, 132 insertions(+)
target/mips/cpu.h +9 -0
index 8249b17e8a..69f96172d8 100644
@@ -595,6 +595,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.
@@ -696,6 +704,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 +81 -0
index 317dd89cd2..70cd1ddcac 100644
@@ -1584,6 +1584,117 @@ static void octeon_aes_store_block(uint64_t regs[2], const uint8_t *block)
      regs[1] = ldq_be_p(block + 8);
  }
 
+static const uint8_t octeon_camellia_sbox1[256] = {
+    112, 130,  44, 236, 179,  39, 192, 229, 228, 133,  87,  53, 234,  12,
+    174,  65,  35, 239, 107, 147,  69,  25, 165,  33, 237,  14,  79,  78,
+     29, 101, 146, 189, 134, 184, 175, 143, 124, 235,  31, 206,  62,  48,
+    220,  95,  94, 197,  11,  26, 166, 225,  57, 202, 213,  71,  93,  61,
+    217,   1,  90, 214,  81,  86, 108,  77, 139,  13, 154, 102, 251, 204,
+    176,  45, 116,  18,  43,  32, 240, 177, 132, 153, 223,  76, 203, 194,
+     52, 126, 118,   5, 109, 183, 169,  49, 209,  23,   4, 215,  20,  88,
+     58,  97, 222,  27,  17,  28,  50,  15, 156,  22,  83,  24, 242,  34,
+    254,  68, 207, 178, 195, 181, 122, 145,  36,   8, 232, 168,  96, 252,
+    105,  80, 170, 208, 160, 125, 161, 137,  98, 151,  84,  91,  30, 149,
+    224, 255, 100, 210,  16, 196,   0,  72, 163, 247, 117, 219, 138,   3,
+    230, 218,   9,  63, 221, 148, 135,  92, 131,   2, 205,  74, 144,  51,
+    115, 103, 246, 243, 157, 127, 191, 226,  82, 155, 216,  38, 200,  55,
+    198,  59, 129, 150, 111,  75,  19, 190,  99,  46, 233, 121, 167, 140,
+    159, 110, 188, 142,  41, 245, 249, 182,  47, 253, 180,  89, 120, 152,
+      6, 106, 231,  70, 113, 186, 212,  37, 171,  66, 136, 162, 141, 250,
+    114,   7, 185,  85, 248, 238, 172,  10,  54,  73,  42, 104,  60,  56,
+    241, 164,  64,  40, 211, 123, 187, 201,  67, 193,  21, 227, 173, 244,
+    119, 199, 128, 158,
+};
+
+static inline uint8_t octeon_camellia_rotl8(uint8_t v, unsigned int shift)
+{
+    return (v << shift) | (v >> (8 - shift));
+}
+
+static inline uint8_t octeon_camellia_sbox2(uint8_t x)
+{
+    return octeon_camellia_rotl8(octeon_camellia_sbox1[x], 1);
+}
+
+static inline uint8_t octeon_camellia_sbox3(uint8_t x)
+{
+    return octeon_camellia_rotl8(octeon_camellia_sbox1[x], 7);
+}
+
+static inline uint8_t octeon_camellia_sbox4(uint8_t x)
+{
+    return octeon_camellia_sbox1[octeon_camellia_rotl8(x, 1)];
+}
+
+static uint64_t octeon_camellia_f(uint64_t input, uint64_t key)
+{
+    uint64_t x = input ^ key;
+    uint8_t t1 = octeon_camellia_sbox1[x >> 56];
+    uint8_t t2 = octeon_camellia_sbox2((x >> 48) & 0xff);
+    uint8_t t3 = octeon_camellia_sbox3((x >> 40) & 0xff);
+    uint8_t t4 = octeon_camellia_sbox4((x >> 32) & 0xff);
+    uint8_t t5 = octeon_camellia_sbox2((x >> 24) & 0xff);
+    uint8_t t6 = octeon_camellia_sbox3((x >> 16) & 0xff);
+    uint8_t t7 = octeon_camellia_sbox4((x >> 8) & 0xff);
+    uint8_t t8 = octeon_camellia_sbox1[x & 0xff];
+    uint8_t y1 = t1 ^ t3 ^ t4 ^ t6 ^ t7 ^ t8;
+    uint8_t y2 = t1 ^ t2 ^ t4 ^ t5 ^ t7 ^ t8;
+    uint8_t y3 = t1 ^ t2 ^ t3 ^ t5 ^ t6 ^ t8;
+    uint8_t y4 = t2 ^ t3 ^ t4 ^ t5 ^ t6 ^ t7;
+    uint8_t y5 = t1 ^ t2 ^ t6 ^ t7 ^ t8;
+    uint8_t y6 = t2 ^ t3 ^ t5 ^ t7 ^ t8;
+    uint8_t y7 = t3 ^ t4 ^ t5 ^ t6 ^ t8;
+    uint8_t y8 = t1 ^ t4 ^ t5 ^ t6 ^ t7;
+
+    return ((uint64_t)y1 << 56) | ((uint64_t)y2 << 48) |
+           ((uint64_t)y3 << 40) | ((uint64_t)y4 << 32) |
+           ((uint64_t)y5 << 24) | ((uint64_t)y6 << 16) |
+           ((uint64_t)y7 << 8) | y8;
+}
+
+static uint64_t octeon_camellia_fl(uint64_t input, uint64_t key)
+{
+    uint32_t x1 = input >> 32;
+    uint32_t x2 = input;
+    uint32_t k1 = key >> 32;
+    uint32_t k2 = key;
+
+    x2 ^= rol32(x1 & k1, 1);
+    x1 ^= x2 | k2;
+    return ((uint64_t)x1 << 32) | x2;
+}
+
+static uint64_t octeon_camellia_flinv(uint64_t input, uint64_t key)

target/mips/tcg/octeon_crypto.c static void octeon_aes_store_block(uint64_t regs[2], const uint8_t *block) · line 1677

Quoted 10 lines
target/mips/tcg/octeon_crypto.c +10 -0
+{
+    uint32_t y1 = input >> 32;
+    uint32_t y2 = input;
+    uint32_t k1 = key >> 32;
+    uint32_t k2 = key;
+
+    y1 ^= y2 | k2;
+    y2 ^= rol32(y1 & k1, 1);
+    return ((uint64_t)y1 << 32) | y2;
+}

The previous helper aren't Octeon specific. Maybe remove the 'octeon_' prefix in case we reuse them elsewhere?

Otherwise patch LGTM.

Quoted 66 lines
target/mips/tcg/octeon_crypto.c +28 -0
+static void octeon_camellia_round(MIPSOcteonCryptoState *crypto, uint64_t key)
+{
+    uint64_t left = crypto->aes_result[0];
+    uint64_t right = crypto->aes_result[1];
+
+    crypto->aes_result[0] = right ^ octeon_camellia_f(left, key);
+    crypto->aes_result[1] = left;
+}
+
+static void octeon_camellia_fl_layer(MIPSOcteonCryptoState *crypto,
+                                     uint64_t key, bool inverse)
+{
+    uint64_t state = crypto->aes_result[inverse ? 1 : 0];
+
+    crypto->aes_result[inverse ? 1 : 0] = inverse ?
+        octeon_camellia_flinv(state, key) :
+        octeon_camellia_fl(state, key);
+}
+
  static void octeon_sms4_crypt_common(MIPSOcteonCryptoState *crypto,
                                       bool encrypt, bool cbc)
  {
@@ -1977,6 +2088,12 @@ void helper_octeon_cop2_dmtc2(CPUMIPSState *env, target_ulong value,
      case OCTEON_COP2_SEL_AES_KEYLENGTH:
          crypto->aes_keylen = q;
          break;
+    case OCTEON_COP2_SEL_CAMELLIA_FL:
+        octeon_camellia_fl_layer(crypto, q, false);
+        break;
+    case OCTEON_COP2_SEL_CAMELLIA_FLINV:
+        octeon_camellia_fl_layer(crypto, q, true);
+        break;
      case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL:
      case OCTEON_COP2_SEL_CRC_WRITE_POLYNOMIAL_REFLECT:
          crypto->crc_poly = q;
@@ -2164,6 +2281,9 @@ void helper_octeon_cop2_dmtc2(CPUMIPSState *env, target_ulong value,
          crypto->aes_input[1] = q;
          octeon_aes_decrypt_common(crypto, false);
          break;
+    case OCTEON_COP2_SEL_CAMELLIA_ROUND:
+        octeon_camellia_round(crypto, q);
+        break;
      case OCTEON_COP2_SEL_SMS4_ENC_CBC1:
          crypto->aes_input[1] = q;
          octeon_sms4_crypt_common(crypto, true, true);
target/mips/tcg/translate.c +3 -0
index 39fad07adc..625faae9d1 100644
@@ -9218,6 +9218,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:
@@ -9313,6 +9315,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:
#17 lore raw

PATCH 9/9 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.

---
 target/mips/cpu.c               |  28 ++++++++
 target/mips/cpu.h               |  21 ++++++
 target/mips/helper.h            |   1 +
 target/mips/tcg/octeon_crypto.c | 111 ++++++++++++++++++++++++++++++++
 target/mips/tcg/op_helper.c     |   6 ++
 target/mips/tcg/translate.c     |  21 ++++++
 6 files changed, 188 insertions(+)
target/mips/cpu.c +28 -0
index ec70c10985..b098c5030d 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,18 @@ static bool mips_cpu_has_work(CPUState *cs)
 
 #include "cpu-defs.c.inc"
 
+static void mips_cpu_destroy_octeon_state(CPUMIPSState *env)
+{
+    if (env->octeon_crypto.llm_narrow) {
+        q_tree_destroy(env->octeon_crypto.llm_narrow);
+        env->octeon_crypto.llm_narrow = NULL;
+    }
+    if (env->octeon_crypto.llm_wide) {
+        q_tree_destroy(env->octeon_crypto.llm_wide);
+        env->octeon_crypto.llm_wide = NULL;
+    }
+}
+
 static void mips_cpu_reset_hold(Object *obj, ResetType type)
 {
     CPUState *cs = CPU(obj);
@@ -194,6 +207,7 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
         mcc->parent_phases.hold(obj, type);
     }
 
+    mips_cpu_destroy_octeon_state(env);
     memset(env, 0, offsetof(CPUMIPSState, end_reset_fields));
 
     /* Reset registers to their default values */
@@ -248,6 +262,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 +281,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 +442,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_cpu_destroy_octeon_state(&cpu->env);
+}
+
 static void mips_cpu_disas_set_info(const CPUState *cs, disassemble_info *info)
 {
     const MIPSCPU *cpu = MIPS_CPU(cs);
@@ -636,6 +663,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 +21 -0
index 69f96172d8..03ee9181a0 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;
 
@@ -617,6 +618,22 @@ 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,
+    /*
+     * The low-latency memory window shares 0x0400 with the older CHORD
+     * alias for the POW tag-switch completion bit. Newer code reads CHORD
+     * via RDHWR $30, but compatibility still requires DMFC2 0x0400.
+     */
+    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,
@@ -754,6 +771,10 @@ typedef struct MIPSOcteonCryptoState {
     uint32_t zuc_lfsr[16];
     uint32_t zuc_window[3];
     uint32_t zuc_tresult;
+    uint64_t llm_data[2];
+    target_ulong chord;
+    QTree *llm_narrow;
+    QTree *llm_wide;
 } MIPSOcteonCryptoState;
 
 typedef struct CPUArchState {
target/mips/helper.h +1 -0
index 0d5713b705..faa8061dc9 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/tcg/octeon_crypto.c +111 -0

129 lines not on this page · raw message

target/mips/tcg/op_helper.c +6 -0
index 3164166910..eae1ce7083 100644
@@ -353,6 +353,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 +21 -0
index 625faae9d1..6760b816d7 100644
@@ -9185,6 +9185,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;
@@ -9220,6 +9223,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:
@@ -11135,6 +11148,14 @@ void gen_rdhwr(DisasContext *ctx, int rt, int rd, int sel)
         gen_helper_rdhwr_xnp(t0, tcg_env);
         gen_store_gpr(t0, rt);
         break;
+    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;
     case 29:
 #if defined(CONFIG_USER_ONLY)
         tcg_gen_ld_tl(t0, tcg_env,
Signature · 1 line
2.53.0