All of lore.kernel.org
 help / color / mirror / Atom feed
From: Aurelien Jarno <aurelien@aurel32.net>
To: Richard Henderson <rth@twiddle.net>
Cc: qemu-devel@nongnu.org, laurent@vivier.eu, bruno@clisp.org,
	glaubitz@debian.org
Subject: Re: [Qemu-devel] [PATCH v2 07/27] target/sh4: Recognize common gUSA sequences
Date: Mon, 17 Jul 2017 16:10:09 +0200	[thread overview]
Message-ID: <20170717141009.na4es4x3dcugri23@aurel32.net> (raw)
In-Reply-To: <20170707022111.21836-8-rth@twiddle.net>

On 2017-07-06 16:20, Richard Henderson wrote:
> For many of the sequences produced by gcc or glibc,
> we can translate these as host atomic operations.
> Which saves the need to acquire the exclusive lock.
> 
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  target/sh4/translate.c | 316 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 316 insertions(+)
> 
> diff --git a/target/sh4/translate.c b/target/sh4/translate.c
> index 653c06c..73b3e02 100644
> --- a/target/sh4/translate.c
> +++ b/target/sh4/translate.c
> @@ -1894,10 +1894,17 @@ static void decode_opc(DisasContext * ctx)
>  */
>  static int decode_gusa(DisasContext *ctx, CPUSH4State *env, int *pmax_insns)
>  {
> +    uint16_t insns[5];
> +    int ld_adr, ld_dst, ld_mop;
> +    int op_dst, op_src, op_opc;
> +    int mv_src, mt_dst, st_src, st_mop;
> +    TCGv op_arg;
> +
>      uint32_t pc = ctx->pc;
>      uint32_t pc_end = ctx->tb->cs_base;
>      int backup = sextract32(ctx->tbflags, GUSA_SHIFT, 8);
>      int max_insns = (pc_end - pc) / 2;
> +    int i;
>  
>      if (pc != pc_end + backup || max_insns < 2) {
>          /* This is a malformed gUSA region.  Don't do anything special,
> @@ -1914,6 +1921,315 @@ static int decode_gusa(DisasContext *ctx, CPUSH4State *env, int *pmax_insns)
>          return 0;
>      }
>  
> +    /* The state machine below will consume only a few insns.
> +       If there are more than that in a region, fail now.  */
> +    if (max_insns > ARRAY_SIZE(insns)) {
> +        goto fail;
> +    }
> +
> +    /* Read all of the insns for the region.  */
> +    for (i = 0; i < max_insns; ++i) {
> +        insns[i] = cpu_lduw_code(env, pc + i * 2);
> +    }
> +
> +    ld_adr = ld_dst = ld_mop = -1;
> +    mv_src = -1;
> +    op_dst = op_src = op_opc = -1;
> +    mt_dst = -1;
> +    st_src = st_mop = -1;
> +    TCGV_UNUSED(op_arg);
> +    i = 0;
> +
> +#define NEXT_INSN \
> +    do { if (i >= max_insns) goto fail; ctx->opcode = insns[i++]; } while (0)
> +
> +    /*
> +     * Expect a load to begin the region.
> +     */
> +    NEXT_INSN;
> +    switch (ctx->opcode & 0xf00f) {
> +    case 0x6000: /* mov.b @Rm,Rn */
> +        ld_mop = MO_SB;
> +        break;
> +    case 0x6001: /* mov.w @Rm,Rn */
> +        ld_mop = MO_TESW;
> +        break;
> +    case 0x6002: /* mov.l @Rm,Rn */
> +        ld_mop = MO_TESL;
> +        break;
> +    default:
> +        goto fail;
> +    }
> +    ld_adr = B7_4;
> +    ld_dst = B11_8;
> +    if (ld_adr == ld_dst) {
> +        goto fail;
> +    }
> +    /* Unless we see a mov, any two-operand operation must use ld_dst.  */
> +    op_dst = ld_dst;
> +
> +    /*
> +     * Expect an optional register move.
> +     */
> +    NEXT_INSN;
> +    switch (ctx->opcode & 0xf00f) {
> +    case 0x6003: /* mov Rm,Rn */
> +        /* Here we want to recognize ld_dst being saved for later consumtion,
> +           or for another input register being copied so that ld_dst need not
> +           be clobbered during the operation.  */
> +        op_dst = B11_8;
> +        mv_src = B7_4;
> +        if (op_dst == ld_dst) {
> +            /* Overwriting the load output.  */
> +            goto fail;
> +        }
> +        if (mv_src != ld_dst) {
> +            /* Copying a new input; constrain op_src to match the load.  */
> +            op_src = ld_dst;
> +        }
> +        break;
> +
> +    default:
> +        /* Put back and re-examine as operation.  */
> +        --i;
> +    }
> +
> +    /*
> +     * Expect the operation.
> +     */
> +    NEXT_INSN;
> +    switch (ctx->opcode & 0xf00f) {
> +    case 0x300c: /* add Rm,Rn */
> +        op_opc = INDEX_op_add_i32;
> +        goto do_reg_op;
> +    case 0x2009: /* and Rm,Rn */
> +        op_opc = INDEX_op_and_i32;
> +        goto do_reg_op;
> +    case 0x200a: /* xor Rm,Rn */
> +        op_opc = INDEX_op_xor_i32;
> +        goto do_reg_op;
> +    case 0x200b: /* or Rm,Rn */
> +        op_opc = INDEX_op_or_i32;
> +    do_reg_op:
> +        /* The operation register should be as expected, and the
> +           other input cannot depend on the load.  */
> +        if (op_dst != B11_8) {
> +            goto fail;
> +        }
> +        if (op_src < 0) {
> +            /* Unconstrainted input.  */
> +            op_src = B7_4;
> +        } else if (op_src == B7_4) {
> +            /* Constrained input matched load.  All operations are
> +               commutative; "swap" them by "moving" the load output
> +               to the (implicit) first argument and the move source
> +               to the (explicit) second argument.  */
> +            op_src = mv_src;
> +        } else {
> +            goto fail;
> +        }
> +        op_arg = REG(op_src);
> +        break;
> +
> +    case 0x6007: /* not Rm,Rn */
> +        if (ld_dst != B7_4 || mv_src >= 0) {
> +            goto fail;
> +        }
> +        op_dst = B11_8;
> +        op_opc = INDEX_op_xor_i32;
> +        op_arg = tcg_const_i32(-1);

This temp is never freed. Same for a few others below.

Overall, parsing the atomic sequence ends up being complex. I have
verified the most common sequences from GCC or GLIBC, and your code
seems fine for at least those cases.

-- 
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
aurelien@aurel32.net                 http://www.aurel32.net

  reply	other threads:[~2017-07-17 14:10 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-07  2:20 [Qemu-devel] [PATCH v2 00/27] target/sh4 improvements Richard Henderson
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 01/27] target/sh4: Use cmpxchg for movco Richard Henderson
2017-07-15 23:22   ` Aurelien Jarno
2017-07-16 21:55     ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 02/27] target/sh4: Consolidate end-of-TB tests Richard Henderson
2017-07-07 21:42   ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 03/27] target/sh4: Introduce TB_FLAG_ENVFLAGS_MASK Richard Henderson
2017-07-07 21:42   ` Aurelien Jarno
2017-07-08 16:29   ` Philippe Mathieu-Daudé
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 04/27] target/sh4: Keep env->flags clean Richard Henderson
2017-07-07 21:42   ` Aurelien Jarno
2017-07-08 16:31   ` Philippe Mathieu-Daudé
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 05/27] target/sh4: Adjust TB_FLAG_PENDING_MOVCA Richard Henderson
2017-07-07 21:42   ` Aurelien Jarno
2017-07-08 16:31   ` Philippe Mathieu-Daudé
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 06/27] target/sh4: Handle user-space atomics Richard Henderson
2017-07-15 22:14   ` Aurelien Jarno
2017-07-15 22:16     ` John Paul Adrian Glaubitz
2017-07-16  2:30     ` Richard Henderson
2017-07-16 15:18       ` Aurelien Jarno
2017-07-16 19:35         ` Richard Henderson
2017-07-16 21:43           ` Aurelien Jarno
2017-07-16 21:59             ` Richard Henderson
2017-07-16 22:16               ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 07/27] target/sh4: Recognize common gUSA sequences Richard Henderson
2017-07-17 14:10   ` Aurelien Jarno [this message]
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 08/27] linux-user/sh4: Notice gUSA regions during signal delivery Richard Henderson
2017-07-07  7:25   ` John Paul Adrian Glaubitz
2017-07-07  8:20     ` Richard Henderson
2017-07-07  8:30       ` John Paul Adrian Glaubitz
2017-07-07  8:35         ` John Paul Adrian Glaubitz
2017-07-07 16:22           ` Richard Henderson
2017-07-13  9:09             ` John Paul Adrian Glaubitz
2017-07-13 10:56               ` John Paul Adrian Glaubitz
2017-07-13 21:37                 ` Richard Henderson
2017-07-13 21:42                   ` John Paul Adrian Glaubitz
     [not found]           ` <20170707163826.22631-1-rth@twiddle.net>
2017-07-07 17:57             ` [Qemu-devel] Fwd: [PATCH v2.5] fixup! " Richard Henderson
2017-07-07 19:00               ` Richard Henderson
2017-07-17 14:15                 ` Aurelien Jarno
2017-07-07  9:05   ` [Qemu-devel] [PATCH v2 08/27] " Laurent Vivier
2017-07-07  9:09     ` Laurent Vivier
2017-07-07  9:13     ` John Paul Adrian Glaubitz
2017-07-15 22:52   ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 09/27] linux-user/sh4: Clean env->flags on signal boundaries Richard Henderson
2017-07-15 22:59   ` Aurelien Jarno
2017-07-16  2:33     ` Richard Henderson
2017-07-16 15:18       ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 10/27] target/sh4: Hoist register bank selection Richard Henderson
2017-07-07 21:48   ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 11/27] target/sh4: Unify cpu_fregs into FREG Richard Henderson
2017-07-07 21:54   ` Aurelien Jarno
2017-07-08 16:54   ` Philippe Mathieu-Daudé
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 12/27] target/sh4: Pass DisasContext to fpr64 routines Richard Henderson
2017-07-07 21:55   ` Aurelien Jarno
2017-07-08 16:56   ` Philippe Mathieu-Daudé
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 13/27] target/sh4: Hoist fp register bank selection Richard Henderson
2017-07-07 21:57   ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 14/27] target/sh4: Eliminate unused XREG macro Richard Henderson
2017-07-07 21:59   ` Aurelien Jarno
2017-07-07  2:20 ` [Qemu-devel] [PATCH v2 15/27] target/sh4: Merge DREG into fpr64 routines Richard Henderson
2017-07-07 22:06   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 16/27] target/sh4: Load/store Dr as 64-bit quantities Richard Henderson
2017-07-07 22:14   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 17/27] target/sh4: Simplify 64-bit fp reg-reg move Richard Henderson
2017-07-07 22:15   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 18/27] target/sh4: Unify code for CHECK_NOT_DELAY_SLOT Richard Henderson
2017-07-07 22:17   ` Aurelien Jarno
2017-07-08 16:59   ` Philippe Mathieu-Daudé
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 19/27] target/sh4: Unify code for CHECK_PRIVILEGED Richard Henderson
2017-07-07 22:17   ` Aurelien Jarno
2017-07-08 17:00   ` Philippe Mathieu-Daudé
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 20/27] target/sh4: Unify code for CHECK_FPU_ENABLED Richard Henderson
2017-07-07 22:18   ` Aurelien Jarno
2017-07-08 17:01   ` Philippe Mathieu-Daudé
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 21/27] target/sh4: Tidy misc illegal insn checks Richard Henderson
2017-07-07 22:18   ` Aurelien Jarno
2017-07-08 17:02   ` Philippe Mathieu-Daudé
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 22/27] target/sh4: Introduce CHECK_FPSCR_PR_* Richard Henderson
2017-07-07 22:20   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 23/27] target/sh4: Introduce CHECK_SH4A Richard Henderson
2017-07-07 22:21   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 24/27] target/sh4: Implement fpchg Richard Henderson
2017-07-07 22:23   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 25/27] target/sh4: Add missing FPSCR.PR == 0 checks Richard Henderson
2017-07-07 22:24   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 26/27] target/sh4: Implement fsrra Richard Henderson
2017-07-07 22:27   ` Aurelien Jarno
2017-07-07  2:21 ` [Qemu-devel] [PATCH v2 27/27] target/sh4: Use tcg_gen_lookup_and_goto_ptr Richard Henderson
2017-07-18  7:51 ` [Qemu-devel] [PATCH v2 00/27] target/sh4 improvements Aurelien Jarno

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170717141009.na4es4x3dcugri23@aurel32.net \
    --to=aurelien@aurel32.net \
    --cc=bruno@clisp.org \
    --cc=glaubitz@debian.org \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.