From patchwork Thu Jul 6 00:23:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 9827353 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4CBA960361 for ; Thu, 6 Jul 2017 00:27:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 444892858E for ; Thu, 6 Jul 2017 00:27:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 345BF285B0; Thu, 6 Jul 2017 00:27:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9F59C2858E for ; Thu, 6 Jul 2017 00:27:29 +0000 (UTC) Received: from localhost ([::1]:48636 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSudk-0000PF-RV for patchwork-qemu-devel@patchwork.kernel.org; Wed, 05 Jul 2017 20:27:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59577) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dSuan-0007Ni-Bd for qemu-devel@nongnu.org; Wed, 05 Jul 2017 20:24:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dSual-0002LN-MK for qemu-devel@nongnu.org; Wed, 05 Jul 2017 20:24:25 -0400 Received: from mail-qt0-x244.google.com ([2607:f8b0:400d:c0d::244]:35853) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dSual-0002L9-I7 for qemu-devel@nongnu.org; Wed, 05 Jul 2017 20:24:23 -0400 Received: by mail-qt0-x244.google.com with SMTP id v31so638284qtb.3 for ; Wed, 05 Jul 2017 17:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=ep4es793wXbFPhzqDI4BTeE541BKnHdvpWGJ4L+4VSI=; b=N/MRRMHw4VXvhW1QqYW734k81dE37kqdYAWikVPJbwWVE9uCeFjx+4kEywHpmpvnKg AgvviMQB2JI11Qmm2YN4F8d8bT7ThxvmCF4PXJN/q9NXgPPPRsbvRFUuP7OK53/1Ej7g iFj7Lk46up+47fhQ3QnG2JmCmY1AjhXH0XXDjiNuNA9uP9AaiyJGDLx7b2eJ8W2Q5JzP lr5cIZMgIZWRF96978GIUMROoQ+v0V61R9Z8V7vfaQM9A0Q6jOtAqcNlOZ4TGaiOxwqV lD3FWIF2dag7/46CAkWC9pMHmPfXXMKoOuaxqqZtHdn3FJKLy677CNvhP6dI6CE9H8KT FWoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=ep4es793wXbFPhzqDI4BTeE541BKnHdvpWGJ4L+4VSI=; b=brcapKB42CCGMHffTgEmmwSePbXUvcr7PBHdy64/Du3hgQivCzj/VED148PUF16ISM KI4LG0cBVWKJLeMoBfwpJ4TY4CFbeA46KTc2w7KmRk94SMZbevWFIa7EB74y5ahJ4GJd LcuGgLATBxug8xUsmgkS5LXeeDgQ7VA1ZfcptSpgnyfrKbLXL7rChr/VW+NPFHopKIkM OKHwH8XOwwz36j9OfTLZfhSdu8kgenzphtCKH1V+8BFlTOwE/asQW2j8s9gbbM5qWWFk RTvPHlCP3ykjeR4pV/ix0g1AtV1pc83kZov0PAWmApYMyx6oWn6XRBRacylTFa/L6MkD sIig== X-Gm-Message-State: AIVw1119iCAVpbo3YUWHcsWrZ7+ZoRMBggblHSSxGasKSe+8aZViR4vg bYjYG0si3zs6RP+TTmg= X-Received: by 10.237.54.42 with SMTP id e39mr21614097qtb.30.1499300662803; Wed, 05 Jul 2017 17:24:22 -0700 (PDT) Received: from bigtime.twiddle.net.com (rrcs-66-91-136-156.west.biz.rr.com. [66.91.136.156]) by smtp.gmail.com with ESMTPSA id u85sm371825qku.42.2017.07.05.17.24.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jul 2017 17:24:22 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jul 2017 14:23:51 -1000 Message-Id: <20170706002401.10507-2-rth@twiddle.net> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170706002401.10507-1-rth@twiddle.net> References: <20170706002401.10507-1-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::244 Subject: [Qemu-devel] [PATCH 01/11] target/sh4: Use cmpxchg for movco X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bruno@clisp.org, aurelien@aurel32.net Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP As for other targets, cmpxchg isn't quite right for ll/sc, suffering from an ABA race, but is sufficient to implement portable atomic operations. Signed-off-by: Richard Henderson --- target/sh4/cpu.h | 3 ++- target/sh4/translate.c | 56 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/target/sh4/cpu.h b/target/sh4/cpu.h index ffb9168..b15116e 100644 --- a/target/sh4/cpu.h +++ b/target/sh4/cpu.h @@ -169,7 +169,8 @@ typedef struct CPUSH4State { tlb_t itlb[ITLB_SIZE]; /* instruction translation table */ tlb_t utlb[UTLB_SIZE]; /* unified translation table */ - uint32_t ldst; + uint32_t lock_addr; + uint32_t lock_value; /* Fields up to this point are cleared by a CPU reset */ struct {} end_reset_fields; diff --git a/target/sh4/translate.c b/target/sh4/translate.c index 8bc132b..6b247fa 100644 --- a/target/sh4/translate.c +++ b/target/sh4/translate.c @@ -68,7 +68,8 @@ static TCGv cpu_gregs[24]; static TCGv cpu_sr, cpu_sr_m, cpu_sr_q, cpu_sr_t; static TCGv cpu_pc, cpu_ssr, cpu_spc, cpu_gbr; static TCGv cpu_vbr, cpu_sgr, cpu_dbr, cpu_mach, cpu_macl; -static TCGv cpu_pr, cpu_fpscr, cpu_fpul, cpu_ldst; +static TCGv cpu_pr, cpu_fpscr, cpu_fpul; +static TCGv cpu_lock_addr, cpu_lock_value; static TCGv cpu_fregs[32]; /* internal register indexes */ @@ -151,8 +152,12 @@ void sh4_translate_init(void) offsetof(CPUSH4State, delayed_cond), "_delayed_cond_"); - cpu_ldst = tcg_global_mem_new_i32(cpu_env, - offsetof(CPUSH4State, ldst), "_ldst_"); + cpu_lock_addr = tcg_global_mem_new_i32(cpu_env, + offsetof(CPUSH4State, lock_addr), + "_lock_addr_"); + cpu_lock_value = tcg_global_mem_new_i32(cpu_env, + offsetof(CPUSH4State, lock_value), + "_lock_value_"); for (i = 0; i < 32; i++) cpu_fregs[i] = tcg_global_mem_new_i32(cpu_env, @@ -1526,20 +1531,32 @@ static void _decode_opc(DisasContext * ctx) return; case 0x0073: /* MOVCO.L - LDST -> T + LDST -> T If (T == 1) R0 -> (Rn) 0 -> LDST */ if (ctx->features & SH_FEATURE_SH4A) { - TCGLabel *label = gen_new_label(); - tcg_gen_mov_i32(cpu_sr_t, cpu_ldst); - tcg_gen_brcondi_i32(TCG_COND_EQ, cpu_ldst, 0, label); - tcg_gen_qemu_st_i32(REG(0), REG(B11_8), ctx->memidx, MO_TEUL); - gen_set_label(label); - tcg_gen_movi_i32(cpu_ldst, 0); - return; - } else - break; + TCGLabel *fail = gen_new_label(); + TCGLabel *done = gen_new_label(); + TCGv tmp; + + tcg_gen_brcond_i32(TCG_COND_NE, REG(B11_8), cpu_lock_addr, fail); + + tmp = tcg_temp_new(); + tcg_gen_atomic_cmpxchg_i32(tmp, REG(B11_8), cpu_lock_value, + REG(0), ctx->memidx, MO_TEUL); + tcg_gen_setcond_i32(TCG_COND_EQ, cpu_sr_t, tmp, cpu_lock_value); + tcg_temp_free(tmp); + tcg_gen_br(done); + + gen_set_label(fail); + tcg_gen_movi_i32(cpu_sr_t, 0); + + gen_set_label(done); + return; + } else { + break; + } case 0x0063: /* MOVLI.L @Rm,R0 1 -> LDST @@ -1547,13 +1564,14 @@ static void _decode_opc(DisasContext * ctx) When interrupt/exception occurred 0 -> LDST */ - if (ctx->features & SH_FEATURE_SH4A) { - tcg_gen_movi_i32(cpu_ldst, 0); + if (ctx->features & SH_FEATURE_SH4A) { tcg_gen_qemu_ld_i32(REG(0), REG(B11_8), ctx->memidx, MO_TESL); - tcg_gen_movi_i32(cpu_ldst, 1); - return; - } else - break; + tcg_gen_mov_i32(cpu_lock_addr, REG(B11_8)); + tcg_gen_mov_i32(cpu_lock_value, REG(0)); + return; + } else { + break; + } case 0x0093: /* ocbi @Rn */ { gen_helper_ocbi(cpu_env, REG(B11_8));