From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A1BF606A334 for ; Thu, 3 Dec 2026 17:46:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rp+FNgSu Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZAs4-0005m5-M6; Thu, 03 Dec 2026 12:45:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZArz-0005jR-VT for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:45:25 -0500 Received: from mail-dy1-x1334.google.com ([2607:f8b0:4864:20::1334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xZArw-0003jX-SS for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:45:23 -0500 Received: by mail-dy1-x1334.google.com with SMTP id 5a478bee46e88-38190551625so26669717eec.1 for ; Thu, 03 Dec 2026 09:45:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796319919; x=1796924719; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Z3f416o8elP/fJ2vxfV5SKdZzCddOay5EmaHRTIO8N4=; b=rp+FNgSu/WPYtuT+33MWjfb89CJzxLlPisHu7BCEUMmtaPbMlq6SrctMAXdA3M+aGs 0IACaVsokTb9sCfaa51Sk1o6OF7qxmEMCS9yeSnfhSQ1CpYl6IJIra1MCYM8zT218iuy 29/je8GVj0Brz5xOTsKo7x70f8JByKLme0ltNj6B/v5BNbl5RRGKg0WifGOinTMwz6Ws /eDJrXI0hgXnuw3vNclHo/S8mOyL+JDOL8+vsxfcQGE5EL1xQsmH1Vex5ZcQQya01fXS H6uiTgSqCezP01MoIDU4vKyJs+apD5OvEzXBblZb1grXYwdxEMvAXC8MJEwjNM5MYofp Heag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796319919; x=1796924719; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Z3f416o8elP/fJ2vxfV5SKdZzCddOay5EmaHRTIO8N4=; b=aDsgmQN4+RwyipmAPh+v+ynbdkmcUiAHB5HYS0/5Xn6q70rI80qG7f1Z/j68VlSxVM W24Z41ntYjHafgm7OqR6WCXoRu4kwnFF5CALDjxvbhgCbINQHaYLgksmWkyv0TWwghUn /4kt05icoa21KJyDKJqs5/k2S/K0XMEvW0qln5PpETLKA2L858cr3AM5JBLghpkGi1U/ p/AoGZtBjWecme8DZVm7rxtRJjLsZeusHzlNDbla9Lrd8tZHLeGX4vTqD5yGpAA6iFjN UYG/15j/u3hlKz/pqY3LlL1aESRhv81qKO7nzBlCZ5NBUHNL8jffGHCZY4sk+pRly/P1 +U+A== X-Gm-Message-State: APuU4Z+D3qutFN9ykCo/O9nppfoWRAj3RLBcSAnfbuLvsIOoRcdW775Z SHNb/ejcD2tsQHgAFum6Iz5dKSc7QDMGVS20pkzSe7QNzGfJJVFnE4nTO4X16xT81Jnpc49R X-Gm-Gg: AZFdTZf4oBT1MOd3LqG6wFOsqdS4xhHvALhgEeGdk/ECgf+iX25pL4zMHDSev7JL/AJ 6e4QqIbf90IL70hNbQP7xIXipoqx9lVmL/20w9dTFlw8JyViM4dqxPXmdbYLFxTdlN5b62o77R6 AslYs5aCOxpM09os1KMpOfrHMS9AMPNTdnwZvUoabuDQGO592aiQUSSdU5vAEXyrST9A7iWqlsr 4on3VVFAdSlrQFtkh+nCU7vGyuzg3aKxP1zjQEnIcituWcqsIs9xBAAL+w2EWTbafhI5HWTUa4j rpFOHgYuIRkoUsgFizy5jRgETy7z7gpPeJVQd2QVDrtf9+eb8DxVFegd20OozvjY7kUl4Lxm/6V H6hOjaOo+m/f2WGKL9or95pJ4ejtG+FzGQMiPbTghAAeRpSH3eMJptGpOLui92keweLehvLPgs0 8XCCEZxy75jsuBv3t+C9tX2mgVzQn4SUF16NPEOx5LzrXmXkD0xvnxdgYLAL0WPeNBW979SYmIb acyjzmLw/YMaGCEF5mnKYAePk68Rx+o4DFMiBFAcQkYyxuwdF7EwIQ3OZzZrUSletwoVYjF7wBZ IlgmshFEyHTgIGHyJh2eYPgx4dvGBcflnUFzVXg5iKLpa9QnHHoBysahAQ== X-Received: by 2002:a05:7300:10e:b0:387:4409:ae3 with SMTP id 5a478bee46e88-38744090e5emr2253892eec.39.1796319918863; Thu, 03 Dec 2026 09:45:18 -0800 (PST) Received: from canxin-Standard-PC-i440FX-PIIX-1996.lan ([45.59.186.47]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385c6830815sm118514008eec.20.2026.12.03.09.45.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:45:18 -0800 (PST) From: Xudong Zhang To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Peter Maydell , Richard Henderson , Xudong Zhang Subject: [PATCH] accel/tcg, target/arm: Close softmmu MTTCG cross-PE CMODX stale-TB races Date: Fri, 4 Dec 2026 01:45:13 +0800 Message-ID: <20261203174513.167750-1-boxzxd@gmail.com> X-Mailer: git-send-email 2.43.0 X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1334; envelope-from=boxzxd@gmail.com; helo=mail-dy1-x1334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Close the softmmu MTTCG cross-PE concurrent modification and execution of instructions (CMODX) races required by the Arm Architecture Reference Manual for A-profile architecture (Arm DDI 0487, §B2.2.5 "Concurrent modification and execution of instructions" and §B2.4.4 (Issue M.d §B2.7.4) "Synchronization and coherency issues between data and instruction accesses") across three coupled mechanisms without altering guest-visible `CTR_EL0.DIC` live-migration ABI or introducing global stop-the-world `tb_flush()` calls: 1. Defer `TLB_NOTDIRTY` clearing and re-invalidate TBs after RAM stores commit in `accel/tcg/cputlb.c` (normal `do_st_*` and atomic RMW helpers), while synchronizing `physical_memory_is_clean()` checks under `tlb.c.lock` in `tlb_set_page_full()` and `tlb_set_dirty()`. 2. Protect newly translated code pages in `accel/tcg/tb-maint.c` (`tb_lock_page0()` / `tb_lock_page1()`) **before** `setjmp_gen_code()` reads guest instruction bytes, closing the fast-path-store vs. first-TB translation race. 3. Extend `ic_ivau_write()` in `target/arm/helper.c` to softmmu under MTTCG for AArch64 `IC_IVAU` and AArch32 `ICIMVAU`, closing both pre-protect in-flight fast-path stores and direct-RAM `probe_access*()` writes (such as SVE `ST1W`, `DC ZVA`, and MOPS) when followed by architectural `IC IVAU` / `ICIMVAU`. Reproducer: https://gist.github.com/canxin121/34f26564c3667e2b250becdd0b5a331f Resolves: https://gitlab.com/qemu-project/qemu/-/issues/5099 Signed-off-by: Xudong Zhang --- accel/tcg/cputlb.c | 156 ++++++++++++++++++++++++++++++++++--------- accel/tcg/tb-maint.c | 31 +++++++-- target/arm/helper.c | 68 ++++++++++++++----- 3 files changed, 204 insertions(+), 51 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index d79eae58df..73f0d4fd9c 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -26,6 +26,7 @@ #include "accel/tcg/probe.h" #include "exec/page-protection.h" #include "system/memory.h" +#include "system/tcg.h" #include "system/physmem.h" #include "accel/tcg/cpu-ldst-common.h" #include "accel/tcg/cpu-mmu-index.h" @@ -916,22 +917,33 @@ static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry, /* update the TLB corresponding to virtual page vaddr so that it is no longer dirty */ -static void tlb_set_dirty(CPUState *cpu, vaddr addr) +static void tlb_set_dirty(CPUState *cpu, vaddr addr, ram_addr_t ram_addr) { int mmu_idx; + vaddr page_addr = addr & TARGET_PAGE_MASK; assert_cpu_is_self_or_stopped(cpu); - addr &= TARGET_PAGE_MASK; qemu_spin_lock(&cpu->neg.tlb.c.lock); + /* + * Re-check physical_memory_is_clean() under tlb.c.lock so a concurrent + * tlb_protect_code() -> tlb_reset_dirty() cannot race with clearing + * TLB_NOTDIRTY and leave a protected code page unprotected in this TLB. + */ + if (physical_memory_is_clean(ram_addr)) { + qemu_spin_unlock(&cpu->neg.tlb.c.lock); + return; + } + trace_memory_notdirty_set_dirty(addr); for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { - tlb_set_dirty1_locked(tlb_entry(cpu, mmu_idx, addr), addr); + tlb_set_dirty1_locked(tlb_entry(cpu, mmu_idx, page_addr), page_addr); } for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { int k; for (k = 0; k < CPU_VTLB_SIZE; k++) { - tlb_set_dirty1_locked(&cpu->neg.tlb.d[mmu_idx].vtable[k], addr); + tlb_set_dirty1_locked(&cpu->neg.tlb.d[mmu_idx].vtable[k], + page_addr); } } qemu_spin_unlock(&cpu->neg.tlb.c.lock); @@ -1039,16 +1051,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, if (is_ram) { iotlb = memory_region_get_ram_addr(section->mr) + xlat; assert(!(iotlb & ~TARGET_PAGE_MASK)); - /* - * Computing is_clean is expensive; avoid all that unless - * the page is actually writable. - */ - if (prot & PAGE_WRITE) { - if (section->readonly) { - write_flags |= TLB_DISCARD_WRITE; - } else if (physical_memory_is_clean(iotlb)) { - write_flags |= TLB_NOTDIRTY; - } + if ((prot & PAGE_WRITE) && section->readonly) { + write_flags |= TLB_DISCARD_WRITE; } } else { /* I/O or ROMD */ @@ -1079,6 +1083,16 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, */ qemu_spin_lock(&tlb->c.lock); + /* + * Check physical_memory_is_clean() under tlb->c.lock so a concurrent + * tlb_protect_code() -> tlb_reset_dirty() cannot clear DIRTY_MEMORY_CODE + * and scan this TLB before the new writable entry is installed. + */ + if (is_ram && (prot & PAGE_WRITE) && !section->readonly && + physical_memory_is_clean(iotlb)) { + write_flags |= TLB_NOTDIRTY; + } + /* Note that the tlb is no longer clean. */ tlb->c.dirty |= 1 << mmu_idx; @@ -1295,8 +1309,9 @@ static bool victim_tlb_hit(CPUState *cpu, size_t mmu_idx, size_t index, return false; } -static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, - CPUTLBEntryFull *full, uintptr_t retaddr) +static void notdirty_write_impl(CPUState *cpu, vaddr mem_vaddr, unsigned size, + CPUTLBEntryFull *full, uintptr_t retaddr, + bool defer_dirty) { ram_addr_t ram_addr = mem_vaddr + full->xlat_offset; @@ -1313,12 +1328,17 @@ static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE); /* We remove the notdirty callback only if the code has been flushed. */ - if (!physical_memory_is_clean(ram_addr)) { - trace_memory_notdirty_set_dirty(mem_vaddr); - tlb_set_dirty(cpu, mem_vaddr); + if (!defer_dirty) { + tlb_set_dirty(cpu, mem_vaddr, ram_addr); } } +static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, + CPUTLBEntryFull *full, uintptr_t retaddr) +{ + notdirty_write_impl(cpu, mem_vaddr, size, full, retaddr, false); +} + static int probe_access_internal(CPUState *cpu, vaddr addr, int fault_size, MMUAccessType access_type, int mmu_idx, bool nonfault, @@ -1666,12 +1686,48 @@ static void mmu_watch_or_dirty(CPUState *cpu, MMULookupPageData *data, /* Note that notdirty is only set for writes. */ if (flags & TLB_NOTDIRTY) { - notdirty_write(cpu, addr, size, full, ra); - flags &= ~TLB_NOTDIRTY; + bool defer = qemu_tcg_mttcg_enabled(); + notdirty_write_impl(cpu, addr, size, full, ra, defer); + if (!defer) { + flags &= ~TLB_NOTDIRTY; + } } data->flags = flags; } +static void notdirty_write_post_range(CPUState *cpu, vaddr addr, + ram_addr_t ram_addr, unsigned size) +{ + tcg_debug_assert(size > 0 && + ((ram_addr ^ (ram_addr + size - 1)) & + TARGET_PAGE_MASK) == 0); + /* + * Under MTTCG, another vCPU may have translated a TB from pre-store + * bytes between pre-store invalidation and the RAM store. Order the + * RAM store before the unlocked page_find() inside + * tb_invalidate_phys_range_fast(), then re-invalidate before + * clearing TLB_NOTDIRTY. + */ + smp_mb(); + tb_invalidate_phys_range_fast(cpu, ram_addr, size, 0); + tlb_set_dirty(cpu, addr, ram_addr); +} + +static inline void notdirty_write_post(CPUState *cpu, MMULookupPageData *p) +{ + if (unlikely(p->flags & TLB_NOTDIRTY)) { + notdirty_write_post_range(cpu, p->addr, + p->addr + p->full->xlat_offset, p->size); + } +} + +static __thread struct { + ram_addr_t ram_addr; + vaddr addr; + unsigned size; + bool pending; +} atomic_notdirty; + /** * mmu_lookup: translate page(s) * @cpu: generic cpu state @@ -1837,8 +1893,10 @@ static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi, hostaddr = (void *)((uintptr_t)addr + tlbe->addend); + bool defer_notdirty = false; if (unlikely(tlb_addr & TLB_NOTDIRTY)) { - notdirty_write(cpu, addr, size, full, retaddr); + defer_notdirty = qemu_tcg_mttcg_enabled(); + notdirty_write_impl(cpu, addr, size, full, retaddr, defer_notdirty); } if (unlikely(tlb_addr & TLB_WATCHPOINT)) { @@ -1854,6 +1912,13 @@ static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi, full->attrs, wp_flags, retaddr); } + if (unlikely(defer_notdirty)) { + atomic_notdirty.pending = true; + atomic_notdirty.ram_addr = addr + full->xlat_offset; + atomic_notdirty.addr = addr; + atomic_notdirty.size = size; + } + return hostaddr; stop_the_world: @@ -2498,10 +2563,14 @@ static uint64_t do_st_leN(CPUState *cpu, MMULookupPageData *p, * It is a given that we cross a page and therefore there is no atomicity * for the store as a whole, but subobjects may need attention. */ + uint64_t ret; + atom = mop & MO_ATOM_MASK; + switch (atom) { case MO_ATOM_SUBALIGN: - return store_parts_leN(p->haddr, p->size, val_le); + ret = store_parts_leN(p->haddr, p->size, val_le); + break; case MO_ATOM_IFALIGN_PAIR: case MO_ATOM_WITHIN16_PAIR: @@ -2511,18 +2580,22 @@ static uint64_t do_st_leN(CPUState *cpu, MMULookupPageData *p, if (atom == MO_ATOM_IFALIGN_PAIR ? p->size == half_size : p->size >= half_size) { - return store_whole_le8(p->haddr, p->size, val_le); + ret = store_whole_le8(p->haddr, p->size, val_le); + break; } /* fall through */ case MO_ATOM_IFALIGN: case MO_ATOM_WITHIN16: case MO_ATOM_NONE: - return store_bytes_leN(p->haddr, p->size, val_le); + ret = store_bytes_leN(p->haddr, p->size, val_le); + break; default: g_assert_not_reached(); } + notdirty_write_post(cpu, p); + return ret; } /* @@ -2546,19 +2619,23 @@ static uint64_t do_st16_leN(CPUState *cpu, MMULookupPageData *p, * It is a given that we cross a page and therefore there is no atomicity * for the store as a whole, but subobjects may need attention. */ + uint64_t ret; + atom = mop & MO_ATOM_MASK; switch (atom) { case MO_ATOM_SUBALIGN: store_parts_leN(p->haddr, 8, int128_getlo(val_le)); - return store_parts_leN(p->haddr + 8, p->size - 8, - int128_gethi(val_le)); + ret = store_parts_leN(p->haddr + 8, p->size - 8, + int128_gethi(val_le)); + break; case MO_ATOM_WITHIN16_PAIR: /* Since size > 8, this is the half that must be atomic. */ if (!HAVE_CMPXCHG128) { cpu_loop_exit_atomic(cpu, ra); } - return store_whole_le16(p->haddr, p->size, val_le); + ret = store_whole_le16(p->haddr, p->size, val_le); + break; case MO_ATOM_IFALIGN_PAIR: /* @@ -2569,12 +2646,15 @@ static uint64_t do_st16_leN(CPUState *cpu, MMULookupPageData *p, case MO_ATOM_WITHIN16: case MO_ATOM_NONE: stq_le_p(p->haddr, int128_getlo(val_le)); - return store_bytes_leN(p->haddr + 8, p->size - 8, - int128_gethi(val_le)); + ret = store_bytes_leN(p->haddr + 8, p->size - 8, + int128_gethi(val_le)); + break; default: g_assert_not_reached(); } + notdirty_write_post(cpu, p); + return ret; } static void do_st_1(CPUState *cpu, MMULookupPageData *p, uint8_t val, @@ -2586,6 +2666,7 @@ static void do_st_1(CPUState *cpu, MMULookupPageData *p, uint8_t val, /* nothing */ } else { *(uint8_t *)p->haddr = val; + notdirty_write_post(cpu, p); } } @@ -2605,6 +2686,7 @@ static void do_st_2(CPUState *cpu, MMULookupPageData *p, uint16_t val, val = bswap16(val); } store_atom_2(cpu, ra, p->haddr, memop, val); + notdirty_write_post(cpu, p); } } @@ -2624,6 +2706,7 @@ static void do_st_4(CPUState *cpu, MMULookupPageData *p, uint32_t val, val = bswap32(val); } store_atom_4(cpu, ra, p->haddr, memop, val); + notdirty_write_post(cpu, p); } } @@ -2643,6 +2726,7 @@ static void do_st_8(CPUState *cpu, MMULookupPageData *p, uint64_t val, val = bswap64(val); } store_atom_8(cpu, ra, p->haddr, memop, val); + notdirty_write_post(cpu, p); } } @@ -2748,6 +2832,7 @@ static void do_st16_mmu(CPUState *cpu, vaddr addr, Int128 val, val = bswap128(val); } store_atom_16(cpu, ra, l.page[0].haddr, l.memop, val); + notdirty_write_post(cpu, &l.page[0]); } return; } @@ -2792,7 +2877,16 @@ static void do_st16_mmu(CPUState *cpu, vaddr addr, Int128 val, #define ATOMIC_NAME(X) \ glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu) -#define ATOMIC_MMU_CLEANUP +#define ATOMIC_MMU_CLEANUP \ + do { \ + if (unlikely(atomic_notdirty.pending)) { \ + atomic_notdirty.pending = false; \ + notdirty_write_post_range(env_cpu(env), \ + atomic_notdirty.addr, \ + atomic_notdirty.ram_addr, \ + atomic_notdirty.size); \ + } \ + } while (0) #include "atomic_common.c.inc" diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 4313864d2c..6bf126e866 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -406,9 +406,28 @@ static void page_unlock(PageDesc *pd) page_unlock__debug(pd); } +/* + * Protect the physical page before reading guest instructions into a TB so + * concurrent MTTCG stores on other vCPUs see TLB_NOTDIRTY and synchronize on + * pd->lock instead of taking the JIT fast path while setjmp_gen_code() reads + * stale bytes. + */ +static void tb_protect_page_locked(PageDesc *pd, tb_page_addr_t paddr) +{ + assert_page_locked(pd); + if (!pd->first_tb) { + tlb_protect_code(paddr & TARGET_PAGE_MASK); + /* Order code-page invalidation against concurrent store visibility */ + smp_mb(); + } +} + void tb_lock_page0(tb_page_addr_t paddr) { - page_lock(page_find_alloc(paddr >> TARGET_PAGE_BITS, true)); + PageDesc *pd = page_find_alloc(paddr >> TARGET_PAGE_BITS, true); + + page_lock(pd); + tb_protect_page_locked(pd, paddr); } void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1) @@ -426,11 +445,13 @@ void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1) if (pindex0 < pindex1) { /* Correct locking order, we may block. */ page_lock(pd1); + tb_protect_page_locked(pd1, paddr1); return; } /* Incorrect locking order, we cannot block lest we deadlock. */ if (!page_trylock(pd1)) { + tb_protect_page_locked(pd1, paddr1); return; } @@ -442,6 +463,8 @@ void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1) page_unlock(pd0); page_lock(pd1); page_lock(pd0); + tb_protect_page_locked(pd1, paddr1); + tb_protect_page_locked(pd0, paddr0); siglongjmp(tcg_ctx->jmp_trans, -3); } @@ -1203,9 +1226,9 @@ void tb_invalidate_phys_range(CPUState *cpu, tb_page_addr_t start, } /* - * len must be <= 8 and start must be a multiple of len. - * Called via softmmu_template.h when code areas are written to with - * iothread mutex not held. + * Range [start, start + len - 1] must not cross a target page boundary. + * Called from cputlb.c when code areas are written to with + * BQL not held. */ void tb_invalidate_phys_range_fast(CPUState *cpu, ram_addr_t start, unsigned len, uintptr_t ra) diff --git a/target/arm/helper.c b/target/arm/helper.c index f413b15eb7..6c5efa1469 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -20,6 +20,7 @@ #include "qemu/bitops.h" #include "qemu/qemu-print.h" #include "exec/cputlb.h" +#include "exec/target_page.h" #include "exec/translation-block.h" #include "hw/core/irq.h" #include "system/cpu-timers.h" @@ -3615,33 +3616,61 @@ static CPAccessResult access_exlock_el3(CPUARMState *env, return CP_ACCESS_OK; } -#ifdef CONFIG_USER_ONLY +#ifdef CONFIG_TCG /* - * `IC IVAU` is handled to improve compatibility with JITs that dual-map their - * code to get around W^X restrictions, where one region is writable and the - * other is executable. - * - * Since the executable region is never written to we cannot detect code - * changes when running in user mode, and rely on the emulated JIT telling us - * that the code has changed by executing this instruction. + * `IC IVAU` (and AArch32 `ICIMVAU`) is handled: + * - in user mode, to improve compatibility with JITs that dual-map their + * code to get around W^X restrictions, where one region is writable and the + * other is executable; + * - in system mode under MTTCG, to order prior guest stores (including + * fast-path and probe_access* writes) before invalidating any TB covering + * the target I-cache line. */ static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) { - uint64_t icache_line_mask, start_address, end_address; - const ARMCPU *cpu; - - cpu = env_archcpu(env); + uint64_t icache_line_mask, start_address; + const ARMCPU *cpu = env_archcpu(env); icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1; +#ifdef CONFIG_USER_ONLY start_address = value & ~icache_line_mask; - end_address = value | icache_line_mask; mmap_lock(); - tb_invalidate_phys_range(env_cpu(env), start_address, end_address); + tb_invalidate_phys_range(env_cpu(env), start_address, + value | icache_line_mask); mmap_unlock(); +#else + if (qemu_tcg_mttcg_enabled()) { + CPUState *cs = env_cpu(env); + int mmu_idx = arm_env_mmu_index(env); + void *host = NULL; + + icache_line_mask = MIN(icache_line_mask, qemu_target_page_size() - 1); + start_address = value & ~icache_line_mask; + + /* Order store visibility before cross-PE TB invalidation */ + smp_mb(); + probe_access_full_mmu(env, start_address, 0, MMU_DATA_LOAD, + mmu_idx, &host, NULL); + if (!host) { + probe_access_full_mmu(env, start_address, 0, MMU_INST_FETCH, + mmu_idx, &host, NULL); + } + if (host) { + ram_addr_t offset; + MemoryRegion *mr = memory_region_from_host(host, &offset); + + if (mr && memory_region_is_ram(mr)) { + ram_addr_t ram_addr = memory_region_get_ram_addr(mr) + offset; + tb_invalidate_phys_range(cs, ram_addr, + ram_addr + icache_line_mask); + } + } + } +#endif } #endif @@ -3703,7 +3732,7 @@ static const ARMCPRegInfo v8_cp_reginfo[] = { .access = PL0_W, .fgt = FGT_ICIVAU, .accessfn = access_tocu, -#ifdef CONFIG_USER_ONLY +#ifdef CONFIG_TCG .type = ARM_CP_NO_RAW, .writefn = ic_ivau_write #else @@ -3758,7 +3787,14 @@ static const ARMCPRegInfo v8_cp_reginfo[] = { { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, - .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, + .access = PL1_W, .accessfn = access_tocu, +#ifdef CONFIG_TCG + .type = ARM_CP_NO_RAW, + .writefn = ic_ivau_write +#else + .type = ARM_CP_NOP +#endif + }, { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, .type = ARM_CP_NOP, .access = PL1_W }, { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, -- 2.43.0