Received: from eggs.gnu.org ([2001:4830:134:3::10]:36071) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHdx0-0005XO-Pf for qemu-devel@nongnu.org; Mon, 27 Jun 2016 17:20:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bHdww-0006h4-GX for qemu-devel@nongnu.org; Mon, 27 Jun 2016 17:20:13 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:38232) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHdwu-0006d3-7I for qemu-devel@nongnu.org; Mon, 27 Jun 2016 17:20:10 -0400 Date: Mon, 27 Jun 2016 17:19:58 -0400 From: "Emilio G. Cota" Message-ID: <20160627211958.GB15173@flamenco> References: <1467054136-10430-1-git-send-email-cota@braap.org> <1467054136-10430-2-git-send-email-cota@braap.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [RFC 01/30] softmmu: add cmpxchg helpers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Richard Henderson Cc: QEMU Developers , MTTCG Devel , Peter Maydell , Alvise Rigo , Sergey Fedorov , Paolo Bonzini , Alex =?iso-8859-1?Q?Benn=E9e?= On Mon, Jun 27, 2016 at 13:11:28 -0700, Richard Henderson wrote: > On 06/27/2016 12:01 PM, Emilio G. Cota wrote: > >Signed-off-by: Emilio G. Cota > >--- > > softmmu_template.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > tcg/tcg.h | 16 +++++++++++++++ > > 2 files changed, 74 insertions(+) > > > >diff --git a/softmmu_template.h b/softmmu_template.h > >index 208f808..7b519dc 100644 > >--- a/softmmu_template.h > >+++ b/softmmu_template.h > >@@ -548,6 +548,64 @@ void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx, > > } > > } > > #endif > >+ > >+DATA_TYPE > >+glue(glue(helper_cmpxchg, SUFFIX), > >+ MMUSUFFIX)(CPUArchState *env, target_ulong addr, DATA_TYPE old, > >+ DATA_TYPE new, TCGMemOpIdx oi, uintptr_t retaddr) > >+{ > >+ unsigned mmu_idx = get_mmuidx(oi); > >+ int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); > >+ target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write; > >+ uintptr_t haddr; > >+ > >+ /* Adjust the given return address. */ > >+ retaddr -= GETPC_ADJ; > >+ > >+ /* If the TLB entry is for a different page, reload and try again. */ > >+ if ((addr & TARGET_PAGE_MASK) > >+ != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { > >+ if (unlikely((addr & (DATA_SIZE - 1)) != 0 > >+ && (get_memop(oi) & MO_AMASK) == MO_ALIGN)) { > >+ cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE, > >+ mmu_idx, retaddr); > >+ } > >+ if (!VICTIM_TLB_HIT(addr_write)) { > >+ tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr); > >+ } > >+ tlb_addr = env->tlb_table[mmu_idx][index].addr_write; > >+ } > > You need to verify that addr_read == addr_write as well, so that tlb_fill > can signal an exception in the rare case of a guest attempting a cmpxchg on > a write-only page. Will do. > >+ /* > >+ * If the host allows unaligned accesses, then let the compiler > >+ * do its thing when performing the access on the host. > >+ */ > >+ haddr = addr + env->tlb_table[mmu_idx][index].addend; > >+ return atomic_cmpxchg((DATA_TYPE *)haddr, old, new); > > Host endian operation? I forgot to add byte ordering in the cover letter under "why this is an RFC" -- I admit I'm confused by all the macro trickery done for regular loads and stores. We store data in memory as per the guests' byte ordering, right? If so, I don't see how it would be possible to leverage the host compiler for things like atomic_add -- we'd increment garbage, not a meaningful value. Emilio