[Qemu-devel] [PATCH 2/6] target-mips: implement forbidden slot

2014-07-14 Thread Leon Alrae
When conditional compact branch is encountered decode one more instruction in
current translation block - that will be forbidden slot. Instruction in
forbidden slot will be executed only if conditional compact branch is not taken.

Any control transfer instruction (CTI) which are branches, jumps, ERET,
DERET, WAIT and PAUSE will generate RI exception if executed in forbidden or
delay slot.

Signed-off-by: Leon Alrae 
---
 target-mips/cpu.h   |5 ++-
 target-mips/translate.c |   89 +++---
 2 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/target-mips/cpu.h b/target-mips/cpu.h
index 2a762d2..a35ab9d 100644
--- a/target-mips/cpu.h
+++ b/target-mips/cpu.h
@@ -462,7 +462,7 @@ struct CPUMIPSState {
 #define EXCP_INST_NOTAVAIL 0x2 /* No valid instruction word for BadInstr */
 uint32_t hflags;/* CPU State */
 /* TMASK defines different execution modes */
-#define MIPS_HFLAG_TMASK  0x2C07FF
+#define MIPS_HFLAG_TMASK  0x6C07FF
 #define MIPS_HFLAG_MODE   0x7 /* execution modes*/
 /* The KSU flags must be the lowest bits in hflags. The flag order
must be the same as defined for CP0 Status. This allows to use
@@ -488,7 +488,7 @@ struct CPUMIPSState {
  * the delay slot, record what type of branch it is so that we can
  * resume translation properly.  It might be possible to reduce
  * this from three bits to two.  */
-#define MIPS_HFLAG_BMASK_BASE  0x03800
+#define MIPS_HFLAG_BMASK_BASE  0x403800
 #define MIPS_HFLAG_B  0x00800 /* Unconditional branch   */
 #define MIPS_HFLAG_BC 0x01000 /* Conditional branch */
 #define MIPS_HFLAG_BL 0x01800 /* Likely branch  */
@@ -506,6 +506,7 @@ struct CPUMIPSState {
 /* Extra flag about HWREna register. */
 #define MIPS_HFLAG_HWRENA_ULR 0x10 /* ULR bit from HWREna is set. */
 #define MIPS_HFLAG_SBRI  0x20 /* R6 SDBBP causes RI excpt. in user mode */
+#define MIPS_HFLAG_FBNSLOT 0x40 /* Forbidden slot   */
 target_ulong btarget;/* Jump / branch target   */
 target_ulong bcond;  /* Branch condition (if needed)   */
 
diff --git a/target-mips/translate.c b/target-mips/translate.c
index d0f695a..4ed81fe 100644
--- a/target-mips/translate.c
+++ b/target-mips/translate.c
@@ -7686,12 +7686,20 @@ static void gen_cp0 (CPUMIPSState *env, DisasContext 
*ctx, uint32_t opc, int rt,
 case OPC_ERET:
 opn = "eret";
 check_insn(ctx, ISA_MIPS2);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
 gen_helper_eret(cpu_env);
 ctx->bstate = BS_EXCP;
 break;
 case OPC_DERET:
 opn = "deret";
 check_insn(ctx, ISA_MIPS32);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
 MIPS_INVAL(opn);
 generate_exception(ctx, EXCP_RI);
@@ -7703,6 +7711,10 @@ static void gen_cp0 (CPUMIPSState *env, DisasContext 
*ctx, uint32_t opc, int rt,
 case OPC_WAIT:
 opn = "wait";
 check_insn(ctx, ISA_MIPS3 | ISA_MIPS32);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
 /* If we get an exception, we want to restart at next instruction */
 ctx->pc += 4;
 save_cpu_state(ctx, 1);
@@ -7729,6 +7741,12 @@ static void gen_compute_branch1(DisasContext *ctx, 
uint32_t op,
 const char *opn = "cp1 cond branch";
 TCGv_i32 t0 = tcg_temp_new_i32();
 
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+generate_exception(ctx, EXCP_RI);
+goto out;
+}
+
 if (cc != 0)
 check_insn(ctx, ISA_MIPS4 | ISA_MIPS32);
 
@@ -10299,6 +10317,10 @@ static void gen_branch(DisasContext *ctx, int 
insn_bytes)
 save_cpu_state(ctx, 0);
 /* FIXME: Need to clear can_do_io.  */
 switch (proc_hflags & MIPS_HFLAG_BMASK_BASE) {
+case MIPS_HFLAG_FBNSLOT:
+MIPS_DEBUG("forbidden slot");
+gen_goto_tb(ctx, 0, ctx->pc + insn_bytes);
+break;
 case MIPS_HFLAG_B:
 /* unconditional branch */
 MIPS_DEBUG("unconditional branch");
@@ -15711,56 +15733,56 @@ static void gen_compute_compact_branch(DisasContext 
*ctx, uint32_t opc,
 gen_branch(ctx, 4);
 } else {
 /* Conditional compact branch */
-int l1 = gen_new_label();
+int fs = gen_new_label();
 save_cpu_state(ctx, 0);
 
 switch (opc) {
 case OPC_BLEZALC: /* OPC_BGEZALC, OPC

Re: [Qemu-devel] [PATCH 2/6] target-mips: implement forbidden slot

2014-10-20 Thread Yongbok Kim

On 14/07/2014 17:19, Leon Alrae wrote:

When conditional compact branch is encountered decode one more instruction in
current translation block - that will be forbidden slot. Instruction in
forbidden slot will be executed only if conditional compact branch is not taken.

Any control transfer instruction (CTI) which are branches, jumps, ERET,
DERET, WAIT and PAUSE will generate RI exception if executed in forbidden or
delay slot.

Signed-off-by: Leon Alrae 
---
  target-mips/cpu.h   |5 ++-
  target-mips/translate.c |   89 +++---
  2 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/target-mips/cpu.h b/target-mips/cpu.h
index 2a762d2..a35ab9d 100644
--- a/target-mips/cpu.h
+++ b/target-mips/cpu.h
@@ -462,7 +462,7 @@ struct CPUMIPSState {
  #define EXCP_INST_NOTAVAIL 0x2 /* No valid instruction word for BadInstr */
  uint32_t hflags;/* CPU State */
  /* TMASK defines different execution modes */
-#define MIPS_HFLAG_TMASK  0x2C07FF
+#define MIPS_HFLAG_TMASK  0x6C07FF
  #define MIPS_HFLAG_MODE   0x7 /* execution modes*/
  /* The KSU flags must be the lowest bits in hflags. The flag order
 must be the same as defined for CP0 Status. This allows to use
@@ -488,7 +488,7 @@ struct CPUMIPSState {
   * the delay slot, record what type of branch it is so that we can
   * resume translation properly.  It might be possible to reduce
   * this from three bits to two.  */
-#define MIPS_HFLAG_BMASK_BASE  0x03800
+#define MIPS_HFLAG_BMASK_BASE  0x403800
  #define MIPS_HFLAG_B  0x00800 /* Unconditional branch   */
  #define MIPS_HFLAG_BC 0x01000 /* Conditional branch */
  #define MIPS_HFLAG_BL 0x01800 /* Likely branch  */
@@ -506,6 +506,7 @@ struct CPUMIPSState {
  /* Extra flag about HWREna register. */
  #define MIPS_HFLAG_HWRENA_ULR 0x10 /* ULR bit from HWREna is set. */
  #define MIPS_HFLAG_SBRI  0x20 /* R6 SDBBP causes RI excpt. in user mode */
+#define MIPS_HFLAG_FBNSLOT 0x40 /* Forbidden slot   */
  target_ulong btarget;/* Jump / branch target   */
  target_ulong bcond;  /* Branch condition (if needed)   */
  
diff --git a/target-mips/translate.c b/target-mips/translate.c

index d0f695a..4ed81fe 100644
--- a/target-mips/translate.c
+++ b/target-mips/translate.c
@@ -7686,12 +7686,20 @@ static void gen_cp0 (CPUMIPSState *env, DisasContext 
*ctx, uint32_t opc, int rt,
  case OPC_ERET:
  opn = "eret";
  check_insn(ctx, ISA_MIPS2);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
  gen_helper_eret(cpu_env);
  ctx->bstate = BS_EXCP;
  break;
  case OPC_DERET:
  opn = "deret";
  check_insn(ctx, ISA_MIPS32);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
  if (!(ctx->hflags & MIPS_HFLAG_DM)) {
  MIPS_INVAL(opn);
  generate_exception(ctx, EXCP_RI);
@@ -7703,6 +7711,10 @@ static void gen_cp0 (CPUMIPSState *env, DisasContext 
*ctx, uint32_t opc, int rt,
  case OPC_WAIT:
  opn = "wait";
  check_insn(ctx, ISA_MIPS3 | ISA_MIPS32);
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {
+MIPS_DEBUG("CTI in delay / forbidden slot");
+goto die;
+}
  /* If we get an exception, we want to restart at next instruction */
  ctx->pc += 4;
  save_cpu_state(ctx, 1);
@@ -7729,6 +7741,12 @@ static void gen_compute_branch1(DisasContext *ctx, 
uint32_t op,
  const char *opn = "cp1 cond branch";
  TCGv_i32 t0 = tcg_temp_new_i32();
  
+if (ctx->insn_flags & ISA_MIPS32R6 && ctx->hflags & MIPS_HFLAG_BMASK) {

+MIPS_DEBUG("CTI in delay / forbidden slot");
+generate_exception(ctx, EXCP_RI);
+goto out;
+}
+
  if (cc != 0)
  check_insn(ctx, ISA_MIPS4 | ISA_MIPS32);
  
@@ -10299,6 +10317,10 @@ static void gen_branch(DisasContext *ctx, int insn_bytes)

  save_cpu_state(ctx, 0);
  /* FIXME: Need to clear can_do_io.  */
  switch (proc_hflags & MIPS_HFLAG_BMASK_BASE) {
+case MIPS_HFLAG_FBNSLOT:
+MIPS_DEBUG("forbidden slot");
+gen_goto_tb(ctx, 0, ctx->pc + insn_bytes);
+break;
  case MIPS_HFLAG_B:
  /* unconditional branch */
  MIPS_DEBUG("unconditional branch");
@@ -15711,56 +15733,56 @@ static void gen_compute_compact_branch(DisasContext 
*ctx, uint32_t opc,
  gen_branch(ctx, 4);
  } else {
  /* Conditional compact branch */
-int l1 = gen_new_label();
+int fs = gen_new_label();