2022-09-20 08:17:44 +03:00
|
|
|
/*
|
2023-08-23 09:53:30 +03:00
|
|
|
* Translation Block Maintenance
|
2022-09-20 08:17:44 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2022-10-01 23:36:33 +03:00
|
|
|
#include "qemu/interval-tree.h"
|
2023-02-05 19:37:58 +03:00
|
|
|
#include "qemu/qtree.h"
|
2022-09-20 08:17:44 +03:00
|
|
|
#include "exec/cputlb.h"
|
|
|
|
#include "exec/log.h"
|
2022-10-05 23:50:32 +03:00
|
|
|
#include "exec/exec-all.h"
|
2023-12-06 22:27:32 +03:00
|
|
|
#include "exec/page-protection.h"
|
2023-03-03 05:57:43 +03:00
|
|
|
#include "exec/tb-flush.h"
|
2022-09-20 08:17:44 +03:00
|
|
|
#include "exec/translate-all.h"
|
|
|
|
#include "sysemu/tcg.h"
|
|
|
|
#include "tcg/tcg.h"
|
|
|
|
#include "tb-hash.h"
|
|
|
|
#include "tb-context.h"
|
2023-09-14 21:57:17 +03:00
|
|
|
#include "internal-common.h"
|
2023-09-14 21:57:14 +03:00
|
|
|
#include "internal-target.h"
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
/* List iterators for lists of tagged pointers in TranslationBlock. */
|
|
|
|
#define TB_FOR_EACH_TAGGED(head, tb, n, field) \
|
|
|
|
for (n = (head) & 1, tb = (TranslationBlock *)((head) & ~1); \
|
|
|
|
tb; tb = (TranslationBlock *)tb->field[n], n = (uintptr_t)tb & 1, \
|
|
|
|
tb = (TranslationBlock *)((uintptr_t)tb & ~1))
|
|
|
|
|
|
|
|
#define TB_FOR_EACH_JMP(head_tb, tb, n) \
|
|
|
|
TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
|
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
static bool tb_cmp(const void *ap, const void *bp)
|
|
|
|
{
|
|
|
|
const TranslationBlock *a = ap;
|
|
|
|
const TranslationBlock *b = bp;
|
|
|
|
|
2023-02-27 16:51:47 +03:00
|
|
|
return ((tb_cflags(a) & CF_PCREL || a->pc == b->pc) &&
|
2022-09-20 08:17:44 +03:00
|
|
|
a->cs_base == b->cs_base &&
|
|
|
|
a->flags == b->flags &&
|
|
|
|
(tb_cflags(a) & ~CF_INVALID) == (tb_cflags(b) & ~CF_INVALID) &&
|
2022-09-20 14:21:40 +03:00
|
|
|
tb_page_addr0(a) == tb_page_addr0(b) &&
|
|
|
|
tb_page_addr1(a) == tb_page_addr1(b));
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void tb_htable_init(void)
|
|
|
|
{
|
|
|
|
unsigned int mode = QHT_MODE_AUTO_RESIZE;
|
|
|
|
|
|
|
|
qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
|
|
|
|
}
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
typedef struct PageDesc PageDesc;
|
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
2022-10-06 04:06:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In user-mode page locks aren't used; mmap_lock is enough.
|
|
|
|
*/
|
|
|
|
#define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
static inline void tb_lock_pages(const TranslationBlock *tb) { }
|
2022-10-06 04:06:29 +03:00
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
/*
|
|
|
|
* For user-only, since we are protecting all of memory with a single lock,
|
|
|
|
* and because the two pages of a TranslationBlock are always contiguous,
|
|
|
|
* use a single data structure to record all TranslationBlocks.
|
|
|
|
*/
|
|
|
|
static IntervalTreeRoot tb_root;
|
|
|
|
|
|
|
|
static void tb_remove_all(void)
|
|
|
|
{
|
|
|
|
assert_memory_lock();
|
|
|
|
memset(&tb_root, 0, sizeof(tb_root));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with mmap_lock held. */
|
2023-07-06 19:55:48 +03:00
|
|
|
static void tb_record(TranslationBlock *tb)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
2023-06-21 16:56:22 +03:00
|
|
|
vaddr addr;
|
2022-10-05 17:34:51 +03:00
|
|
|
int flags;
|
2022-10-01 23:36:33 +03:00
|
|
|
|
|
|
|
assert_memory_lock();
|
|
|
|
tb->itree.last = tb->itree.start + tb->size - 1;
|
2022-10-05 17:34:51 +03:00
|
|
|
|
|
|
|
/* translator_loop() must have made all TB pages non-writable */
|
|
|
|
addr = tb_page_addr0(tb);
|
|
|
|
flags = page_get_flags(addr);
|
|
|
|
assert(!(flags & PAGE_WRITE));
|
|
|
|
|
|
|
|
addr = tb_page_addr1(tb);
|
|
|
|
if (addr != -1) {
|
|
|
|
flags = page_get_flags(addr);
|
|
|
|
assert(!(flags & PAGE_WRITE));
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
interval_tree_insert(&tb->itree, &tb_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with mmap_lock held. */
|
|
|
|
static void tb_remove(TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
assert_memory_lock();
|
|
|
|
interval_tree_remove(&tb->itree, &tb_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: For now, still shared with translate-all.c for system mode. */
|
2023-03-06 03:20:46 +03:00
|
|
|
#define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \
|
|
|
|
for (T = foreach_tb_first(start, last), \
|
|
|
|
N = foreach_tb_next(T, start, last); \
|
2022-10-01 23:36:33 +03:00
|
|
|
T != NULL; \
|
2023-03-06 03:20:46 +03:00
|
|
|
T = N, N = foreach_tb_next(N, start, last))
|
2022-10-01 23:36:33 +03:00
|
|
|
|
|
|
|
typedef TranslationBlock *PageForEachNext;
|
|
|
|
|
|
|
|
static PageForEachNext foreach_tb_first(tb_page_addr_t start,
|
2023-03-06 03:20:46 +03:00
|
|
|
tb_page_addr_t last)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
2023-03-06 03:20:46 +03:00
|
|
|
IntervalTreeNode *n = interval_tree_iter_first(&tb_root, start, last);
|
2022-10-01 23:36:33 +03:00
|
|
|
return n ? container_of(n, TranslationBlock, itree) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PageForEachNext foreach_tb_next(PageForEachNext tb,
|
|
|
|
tb_page_addr_t start,
|
2023-03-06 03:20:46 +03:00
|
|
|
tb_page_addr_t last)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
|
|
|
IntervalTreeNode *n;
|
|
|
|
|
|
|
|
if (tb) {
|
2023-03-06 03:20:46 +03:00
|
|
|
n = interval_tree_iter_next(&tb->itree, start, last);
|
2022-10-01 23:36:33 +03:00
|
|
|
if (n) {
|
|
|
|
return container_of(n, TranslationBlock, itree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2022-10-06 03:22:42 +03:00
|
|
|
/*
|
|
|
|
* In system mode we want L1_MAP to be based on ram offsets.
|
|
|
|
*/
|
|
|
|
#if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
|
|
|
|
# define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
|
|
|
|
#else
|
|
|
|
# define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Size of the L2 (and L3, etc) page tables. */
|
|
|
|
#define V_L2_BITS 10
|
|
|
|
#define V_L2_SIZE (1 << V_L2_BITS)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* L1 Mapping properties
|
|
|
|
*/
|
|
|
|
static int v_l1_size;
|
|
|
|
static int v_l1_shift;
|
|
|
|
static int v_l2_levels;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The bottom level has pointers to PageDesc, and is indexed by
|
|
|
|
* anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
|
|
|
|
*/
|
|
|
|
#define V_L1_MIN_BITS 4
|
|
|
|
#define V_L1_MAX_BITS (V_L2_BITS + 3)
|
|
|
|
#define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
|
|
|
|
|
|
|
|
static void *l1_map[V_L1_MAX_SIZE];
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
struct PageDesc {
|
|
|
|
QemuSpin lock;
|
|
|
|
/* list of TBs intersecting this ram page */
|
|
|
|
uintptr_t first_tb;
|
|
|
|
};
|
|
|
|
|
2022-10-06 03:22:42 +03:00
|
|
|
void page_table_config_init(void)
|
|
|
|
{
|
|
|
|
uint32_t v_l1_bits;
|
|
|
|
|
|
|
|
assert(TARGET_PAGE_BITS);
|
|
|
|
/* The bits remaining after N lower levels of page tables. */
|
|
|
|
v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
|
|
|
|
if (v_l1_bits < V_L1_MIN_BITS) {
|
|
|
|
v_l1_bits += V_L2_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
v_l1_size = 1 << v_l1_bits;
|
|
|
|
v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
|
|
|
|
v_l2_levels = v_l1_shift / V_L2_BITS - 1;
|
|
|
|
|
|
|
|
assert(v_l1_bits <= V_L1_MAX_BITS);
|
|
|
|
assert(v_l1_shift % V_L2_BITS == 0);
|
|
|
|
assert(v_l2_levels >= 0);
|
|
|
|
}
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
static PageDesc *page_find_alloc(tb_page_addr_t index, bool alloc)
|
2022-10-06 03:22:42 +03:00
|
|
|
{
|
|
|
|
PageDesc *pd;
|
|
|
|
void **lp;
|
|
|
|
|
|
|
|
/* Level 1. Always allocated. */
|
|
|
|
lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
|
|
|
|
|
|
|
|
/* Level 2..N-1. */
|
2023-09-04 19:12:13 +03:00
|
|
|
for (int i = v_l2_levels; i > 0; i--) {
|
2022-10-06 03:22:42 +03:00
|
|
|
void **p = qatomic_rcu_read(lp);
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
void *existing;
|
|
|
|
|
|
|
|
if (!alloc) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = g_new0(void *, V_L2_SIZE);
|
|
|
|
existing = qatomic_cmpxchg(lp, NULL, p);
|
|
|
|
if (unlikely(existing)) {
|
|
|
|
g_free(p);
|
|
|
|
p = existing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pd = qatomic_rcu_read(lp);
|
|
|
|
if (pd == NULL) {
|
|
|
|
void *existing;
|
|
|
|
|
|
|
|
if (!alloc) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pd = g_new0(PageDesc, V_L2_SIZE);
|
|
|
|
for (int i = 0; i < V_L2_SIZE; i++) {
|
|
|
|
qemu_spin_init(&pd[i].lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
existing = qatomic_cmpxchg(lp, NULL, pd);
|
|
|
|
if (unlikely(existing)) {
|
|
|
|
for (int i = 0; i < V_L2_SIZE; i++) {
|
|
|
|
qemu_spin_destroy(&pd[i].lock);
|
|
|
|
}
|
|
|
|
g_free(pd);
|
|
|
|
pd = existing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pd + (index & (V_L2_SIZE - 1));
|
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
static inline PageDesc *page_find(tb_page_addr_t index)
|
|
|
|
{
|
|
|
|
return page_find_alloc(index, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct page_entry - page descriptor entry
|
|
|
|
* @pd: pointer to the &struct PageDesc of the page this entry represents
|
|
|
|
* @index: page index of the page
|
|
|
|
* @locked: whether the page is locked
|
|
|
|
*
|
|
|
|
* This struct helps us keep track of the locked state of a page, without
|
|
|
|
* bloating &struct PageDesc.
|
|
|
|
*
|
|
|
|
* A page lock protects accesses to all fields of &struct PageDesc.
|
|
|
|
*
|
|
|
|
* See also: &struct page_collection.
|
|
|
|
*/
|
|
|
|
struct page_entry {
|
|
|
|
PageDesc *pd;
|
|
|
|
tb_page_addr_t index;
|
|
|
|
bool locked;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct page_collection - tracks a set of pages (i.e. &struct page_entry's)
|
|
|
|
* @tree: Binary search tree (BST) of the pages, with key == page index
|
|
|
|
* @max: Pointer to the page in @tree with the highest page index
|
|
|
|
*
|
|
|
|
* To avoid deadlock we lock pages in ascending order of page index.
|
|
|
|
* When operating on a set of pages, we need to keep track of them so that
|
|
|
|
* we can lock them in order and also unlock them later. For this we collect
|
|
|
|
* pages (i.e. &struct page_entry's) in a binary search @tree. Given that the
|
|
|
|
* @tree implementation we use does not provide an O(1) operation to obtain the
|
|
|
|
* highest-ranked element, we use @max to keep track of the inserted page
|
|
|
|
* with the highest index. This is valuable because if a page is not in
|
|
|
|
* the tree and its index is higher than @max's, then we can lock it
|
|
|
|
* without breaking the locking order rule.
|
|
|
|
*
|
|
|
|
* Note on naming: 'struct page_set' would be shorter, but we already have a few
|
|
|
|
* page_set_*() helpers, so page_collection is used instead to avoid confusion.
|
|
|
|
*
|
|
|
|
* See also: page_collection_lock().
|
|
|
|
*/
|
|
|
|
struct page_collection {
|
2023-02-05 19:37:58 +03:00
|
|
|
QTree *tree;
|
2022-10-06 04:06:29 +03:00
|
|
|
struct page_entry *max;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int PageForEachNext;
|
2023-03-06 03:20:46 +03:00
|
|
|
#define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
|
2022-10-06 04:06:29 +03:00
|
|
|
TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_TCG
|
|
|
|
|
|
|
|
static __thread GHashTable *ht_pages_locked_debug;
|
|
|
|
|
|
|
|
static void ht_pages_locked_debug_init(void)
|
|
|
|
{
|
|
|
|
if (ht_pages_locked_debug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ht_pages_locked_debug = g_hash_table_new(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool page_is_locked(const PageDesc *pd)
|
|
|
|
{
|
|
|
|
PageDesc *found;
|
|
|
|
|
|
|
|
ht_pages_locked_debug_init();
|
|
|
|
found = g_hash_table_lookup(ht_pages_locked_debug, pd);
|
|
|
|
return !!found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void page_lock__debug(PageDesc *pd)
|
|
|
|
{
|
|
|
|
ht_pages_locked_debug_init();
|
|
|
|
g_assert(!page_is_locked(pd));
|
|
|
|
g_hash_table_insert(ht_pages_locked_debug, pd, pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void page_unlock__debug(const PageDesc *pd)
|
|
|
|
{
|
|
|
|
bool removed;
|
|
|
|
|
|
|
|
ht_pages_locked_debug_init();
|
|
|
|
g_assert(page_is_locked(pd));
|
|
|
|
removed = g_hash_table_remove(ht_pages_locked_debug, pd);
|
|
|
|
g_assert(removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_assert_page_locked(const PageDesc *pd,
|
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
if (unlikely(!page_is_locked(pd))) {
|
|
|
|
error_report("assert_page_lock: PageDesc %p not locked @ %s:%d",
|
|
|
|
pd, file, line);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define assert_page_locked(pd) do_assert_page_locked(pd, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
void assert_no_pages_locked(void)
|
|
|
|
{
|
|
|
|
ht_pages_locked_debug_init();
|
|
|
|
g_assert(g_hash_table_size(ht_pages_locked_debug) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !CONFIG_DEBUG_TCG */
|
|
|
|
|
|
|
|
static inline void page_lock__debug(const PageDesc *pd) { }
|
|
|
|
static inline void page_unlock__debug(const PageDesc *pd) { }
|
|
|
|
static inline void assert_page_locked(const PageDesc *pd) { }
|
|
|
|
|
|
|
|
#endif /* CONFIG_DEBUG_TCG */
|
|
|
|
|
|
|
|
static void page_lock(PageDesc *pd)
|
|
|
|
{
|
|
|
|
page_lock__debug(pd);
|
|
|
|
qemu_spin_lock(&pd->lock);
|
|
|
|
}
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
/* Like qemu_spin_trylock, returns false on success */
|
|
|
|
static bool page_trylock(PageDesc *pd)
|
|
|
|
{
|
|
|
|
bool busy = qemu_spin_trylock(&pd->lock);
|
|
|
|
if (!busy) {
|
|
|
|
page_lock__debug(pd);
|
|
|
|
}
|
|
|
|
return busy;
|
|
|
|
}
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
static void page_unlock(PageDesc *pd)
|
|
|
|
{
|
|
|
|
qemu_spin_unlock(&pd->lock);
|
|
|
|
page_unlock__debug(pd);
|
|
|
|
}
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
void tb_lock_page0(tb_page_addr_t paddr)
|
|
|
|
{
|
|
|
|
page_lock(page_find_alloc(paddr >> TARGET_PAGE_BITS, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
|
|
|
|
{
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
|
|
|
PageDesc *pd0, *pd1;
|
|
|
|
|
|
|
|
if (pindex0 == pindex1) {
|
|
|
|
/* Identical pages, and the first page is already locked. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pd1 = page_find_alloc(pindex1, true);
|
|
|
|
if (pindex0 < pindex1) {
|
|
|
|
/* Correct locking order, we may block. */
|
|
|
|
page_lock(pd1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Incorrect locking order, we cannot block lest we deadlock. */
|
|
|
|
if (!page_trylock(pd1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop the lock on page0 and get both page locks in the right order.
|
|
|
|
* Restart translation via longjmp.
|
|
|
|
*/
|
|
|
|
pd0 = page_find_alloc(pindex0, false);
|
|
|
|
page_unlock(pd0);
|
|
|
|
page_lock(pd1);
|
|
|
|
page_lock(pd0);
|
|
|
|
siglongjmp(tcg_ctx->jmp_trans, -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tb_unlock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
|
|
|
|
{
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
|
|
|
|
|
|
|
if (pindex0 != pindex1) {
|
|
|
|
page_unlock(page_find_alloc(pindex1, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tb_lock_pages(TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
tb_page_addr_t paddr0 = tb_page_addr0(tb);
|
|
|
|
tb_page_addr_t paddr1 = tb_page_addr1(tb);
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
|
|
|
|
|
|
|
if (unlikely(paddr0 == -1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
|
|
|
|
if (pindex0 < pindex1) {
|
|
|
|
page_lock(page_find_alloc(pindex0, true));
|
|
|
|
page_lock(page_find_alloc(pindex1, true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
page_lock(page_find_alloc(pindex1, true));
|
|
|
|
}
|
|
|
|
page_lock(page_find_alloc(pindex0, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tb_unlock_pages(TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
tb_page_addr_t paddr0 = tb_page_addr0(tb);
|
|
|
|
tb_page_addr_t paddr1 = tb_page_addr1(tb);
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
|
|
|
|
|
|
|
if (unlikely(paddr0 == -1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
|
|
|
|
page_unlock(page_find_alloc(pindex1, false));
|
|
|
|
}
|
|
|
|
page_unlock(page_find_alloc(pindex0, false));
|
|
|
|
}
|
|
|
|
|
2022-10-06 04:06:29 +03:00
|
|
|
static inline struct page_entry *
|
|
|
|
page_entry_new(PageDesc *pd, tb_page_addr_t index)
|
|
|
|
{
|
|
|
|
struct page_entry *pe = g_malloc(sizeof(*pe));
|
|
|
|
|
|
|
|
pe->index = index;
|
|
|
|
pe->pd = pd;
|
|
|
|
pe->locked = false;
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void page_entry_destroy(gpointer p)
|
|
|
|
{
|
|
|
|
struct page_entry *pe = p;
|
|
|
|
|
|
|
|
g_assert(pe->locked);
|
|
|
|
page_unlock(pe->pd);
|
|
|
|
g_free(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns false on success */
|
|
|
|
static bool page_entry_trylock(struct page_entry *pe)
|
|
|
|
{
|
2023-07-06 19:55:48 +03:00
|
|
|
bool busy = page_trylock(pe->pd);
|
2022-10-06 04:06:29 +03:00
|
|
|
if (!busy) {
|
|
|
|
g_assert(!pe->locked);
|
|
|
|
pe->locked = true;
|
|
|
|
}
|
|
|
|
return busy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_page_entry_lock(struct page_entry *pe)
|
|
|
|
{
|
|
|
|
page_lock(pe->pd);
|
|
|
|
g_assert(!pe->locked);
|
|
|
|
pe->locked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean page_entry_lock(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
struct page_entry *pe = value;
|
|
|
|
|
|
|
|
do_page_entry_lock(pe);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean page_entry_unlock(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
struct page_entry *pe = value;
|
|
|
|
|
|
|
|
if (pe->locked) {
|
|
|
|
pe->locked = false;
|
|
|
|
page_unlock(pe->pd);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trylock a page, and if successful, add the page to a collection.
|
|
|
|
* Returns true ("busy") if the page could not be locked; false otherwise.
|
|
|
|
*/
|
|
|
|
static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)
|
|
|
|
{
|
|
|
|
tb_page_addr_t index = addr >> TARGET_PAGE_BITS;
|
|
|
|
struct page_entry *pe;
|
|
|
|
PageDesc *pd;
|
|
|
|
|
2023-02-05 19:37:58 +03:00
|
|
|
pe = q_tree_lookup(set->tree, &index);
|
2022-10-06 04:06:29 +03:00
|
|
|
if (pe) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pd = page_find(index);
|
|
|
|
if (pd == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pe = page_entry_new(pd, index);
|
2023-02-05 19:37:58 +03:00
|
|
|
q_tree_insert(set->tree, &pe->index, pe);
|
2022-10-06 04:06:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is either (1) the first insertion or (2) a page whose index
|
|
|
|
* is higher than any other so far, just lock the page and move on.
|
|
|
|
*/
|
|
|
|
if (set->max == NULL || pe->index > set->max->index) {
|
|
|
|
set->max = pe;
|
|
|
|
do_page_entry_lock(pe);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Try to acquire out-of-order lock; if busy, return busy so that we acquire
|
|
|
|
* locks in order.
|
|
|
|
*/
|
|
|
|
return page_entry_trylock(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint tb_page_addr_cmp(gconstpointer ap, gconstpointer bp, gpointer udata)
|
|
|
|
{
|
|
|
|
tb_page_addr_t a = *(const tb_page_addr_t *)ap;
|
|
|
|
tb_page_addr_t b = *(const tb_page_addr_t *)bp;
|
|
|
|
|
|
|
|
if (a == b) {
|
|
|
|
return 0;
|
|
|
|
} else if (a < b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-06 03:42:12 +03:00
|
|
|
* Lock a range of pages ([@start,@last]) as well as the pages of all
|
2022-10-06 04:06:29 +03:00
|
|
|
* intersecting TBs.
|
|
|
|
* Locking order: acquire locks in ascending order of page index.
|
|
|
|
*/
|
2022-12-09 12:36:49 +03:00
|
|
|
static struct page_collection *page_collection_lock(tb_page_addr_t start,
|
2023-03-06 03:42:12 +03:00
|
|
|
tb_page_addr_t last)
|
2022-10-06 04:06:29 +03:00
|
|
|
{
|
|
|
|
struct page_collection *set = g_malloc(sizeof(*set));
|
|
|
|
tb_page_addr_t index;
|
|
|
|
PageDesc *pd;
|
|
|
|
|
|
|
|
start >>= TARGET_PAGE_BITS;
|
2023-03-06 03:42:12 +03:00
|
|
|
last >>= TARGET_PAGE_BITS;
|
|
|
|
g_assert(start <= last);
|
2022-10-06 04:06:29 +03:00
|
|
|
|
2023-02-05 19:37:58 +03:00
|
|
|
set->tree = q_tree_new_full(tb_page_addr_cmp, NULL, NULL,
|
2022-10-06 04:06:29 +03:00
|
|
|
page_entry_destroy);
|
|
|
|
set->max = NULL;
|
|
|
|
assert_no_pages_locked();
|
|
|
|
|
|
|
|
retry:
|
2023-02-05 19:37:58 +03:00
|
|
|
q_tree_foreach(set->tree, page_entry_lock, NULL);
|
2022-10-06 04:06:29 +03:00
|
|
|
|
2023-03-06 03:42:12 +03:00
|
|
|
for (index = start; index <= last; index++) {
|
2022-10-06 04:06:29 +03:00
|
|
|
TranslationBlock *tb;
|
|
|
|
PageForEachNext n;
|
|
|
|
|
|
|
|
pd = page_find(index);
|
|
|
|
if (pd == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (page_trylock_add(set, index << TARGET_PAGE_BITS)) {
|
2023-02-05 19:37:58 +03:00
|
|
|
q_tree_foreach(set->tree, page_entry_unlock, NULL);
|
2022-10-06 04:06:29 +03:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
assert_page_locked(pd);
|
|
|
|
PAGE_FOR_EACH_TB(unused, unused, pd, tb, n) {
|
|
|
|
if (page_trylock_add(set, tb_page_addr0(tb)) ||
|
|
|
|
(tb_page_addr1(tb) != -1 &&
|
|
|
|
page_trylock_add(set, tb_page_addr1(tb)))) {
|
|
|
|
/* drop all locks, and reacquire in order */
|
2023-02-05 19:37:58 +03:00
|
|
|
q_tree_foreach(set->tree, page_entry_unlock, NULL);
|
2022-10-06 04:06:29 +03:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2022-12-09 12:36:49 +03:00
|
|
|
static void page_collection_unlock(struct page_collection *set)
|
2022-10-06 04:06:29 +03:00
|
|
|
{
|
|
|
|
/* entries are unlocked and freed via page_entry_destroy */
|
2023-02-05 19:37:58 +03:00
|
|
|
q_tree_destroy(set->tree);
|
2022-10-06 04:06:29 +03:00
|
|
|
g_free(set);
|
|
|
|
}
|
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
/* Set to NULL all the 'first_tb' fields in all PageDescs. */
|
2022-11-01 02:09:54 +03:00
|
|
|
static void tb_remove_all_1(int level, void **lp)
|
2022-09-20 08:17:44 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*lp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (level == 0) {
|
|
|
|
PageDesc *pd = *lp;
|
|
|
|
|
|
|
|
for (i = 0; i < V_L2_SIZE; ++i) {
|
|
|
|
page_lock(&pd[i]);
|
|
|
|
pd[i].first_tb = (uintptr_t)NULL;
|
|
|
|
page_unlock(&pd[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
void **pp = *lp;
|
|
|
|
|
|
|
|
for (i = 0; i < V_L2_SIZE; ++i) {
|
2022-11-01 02:09:54 +03:00
|
|
|
tb_remove_all_1(level - 1, pp + i);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 02:09:54 +03:00
|
|
|
static void tb_remove_all(void)
|
2022-09-20 08:17:44 +03:00
|
|
|
{
|
|
|
|
int i, l1_sz = v_l1_size;
|
|
|
|
|
|
|
|
for (i = 0; i < l1_sz; i++) {
|
2022-11-01 02:09:54 +03:00
|
|
|
tb_remove_all_1(v_l2_levels, l1_map + i);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
/*
|
|
|
|
* Add the tb in the target page and protect it if necessary.
|
|
|
|
* Called with @p->lock held.
|
|
|
|
*/
|
2023-07-06 19:55:48 +03:00
|
|
|
static void tb_page_add(PageDesc *p, TranslationBlock *tb, unsigned int n)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
|
|
|
bool page_already_protected;
|
|
|
|
|
|
|
|
assert_page_locked(p);
|
|
|
|
|
|
|
|
tb->page_next[n] = p->first_tb;
|
|
|
|
page_already_protected = p->first_tb != 0;
|
|
|
|
p->first_tb = (uintptr_t)tb | n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If some code is already present, then the pages are already
|
|
|
|
* protected. So we handle the case where only the first TB is
|
|
|
|
* allocated in a physical page.
|
|
|
|
*/
|
|
|
|
if (!page_already_protected) {
|
|
|
|
tlb_protect_code(tb->page_addr[n] & TARGET_PAGE_MASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
static void tb_record(TranslationBlock *tb)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_page_addr_t paddr0 = tb_page_addr0(tb);
|
|
|
|
tb_page_addr_t paddr1 = tb_page_addr1(tb);
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
2024-06-12 16:30:31 +03:00
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
2023-07-06 19:55:48 +03:00
|
|
|
|
|
|
|
assert(paddr0 != -1);
|
|
|
|
if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
|
|
|
|
tb_page_add(page_find_alloc(pindex1, false), tb, 1);
|
2022-10-01 23:36:33 +03:00
|
|
|
}
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_page_add(page_find_alloc(pindex0, false), tb, 0);
|
2022-10-01 23:36:33 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
static void tb_page_remove(PageDesc *pd, TranslationBlock *tb)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
|
|
|
TranslationBlock *tb1;
|
|
|
|
uintptr_t *pprev;
|
|
|
|
PageForEachNext n1;
|
|
|
|
|
|
|
|
assert_page_locked(pd);
|
|
|
|
pprev = &pd->first_tb;
|
|
|
|
PAGE_FOR_EACH_TB(unused, unused, pd, tb1, n1) {
|
|
|
|
if (tb1 == tb) {
|
|
|
|
*pprev = tb1->page_next[n1];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pprev = &tb1->page_next[n1];
|
|
|
|
}
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tb_remove(TranslationBlock *tb)
|
|
|
|
{
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_page_addr_t paddr0 = tb_page_addr0(tb);
|
|
|
|
tb_page_addr_t paddr1 = tb_page_addr1(tb);
|
|
|
|
tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
|
2024-06-12 16:30:31 +03:00
|
|
|
tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
|
2023-07-06 19:55:48 +03:00
|
|
|
|
|
|
|
assert(paddr0 != -1);
|
|
|
|
if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
|
|
|
|
tb_page_remove(page_find_alloc(pindex1, false), tb);
|
2022-10-06 04:06:29 +03:00
|
|
|
}
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_page_remove(page_find_alloc(pindex0, false), tb);
|
2022-10-06 04:06:29 +03:00
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
#endif /* CONFIG_USER_ONLY */
|
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
/* flush all the translation blocks */
|
|
|
|
static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
|
|
|
|
{
|
|
|
|
bool did_flush = false;
|
|
|
|
|
|
|
|
mmap_lock();
|
|
|
|
/* If it is already been done on request of another CPU, just retry. */
|
|
|
|
if (tb_ctx.tb_flush_count != tb_flush_count.host_int) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
did_flush = true;
|
|
|
|
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
tcg_flush_jmp_cache(cpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
qht_reset_size(&tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
|
2022-11-01 02:09:54 +03:00
|
|
|
tb_remove_all();
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
tcg_region_reset_all();
|
|
|
|
/* XXX: flush processor icache at this point if cache flush is expensive */
|
2023-03-03 15:12:50 +03:00
|
|
|
qatomic_inc(&tb_ctx.tb_flush_count);
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
done:
|
|
|
|
mmap_unlock();
|
|
|
|
if (did_flush) {
|
|
|
|
qemu_plugin_flush_cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tb_flush(CPUState *cpu)
|
|
|
|
{
|
|
|
|
if (tcg_enabled()) {
|
2023-03-03 15:12:50 +03:00
|
|
|
unsigned tb_flush_count = qatomic_read(&tb_ctx.tb_flush_count);
|
2022-09-20 08:17:44 +03:00
|
|
|
|
2022-10-26 07:58:09 +03:00
|
|
|
if (cpu_in_serial_context(cpu)) {
|
2022-09-20 08:17:44 +03:00
|
|
|
do_tb_flush(cpu, RUN_ON_CPU_HOST_INT(tb_flush_count));
|
|
|
|
} else {
|
|
|
|
async_safe_run_on_cpu(cpu, do_tb_flush,
|
|
|
|
RUN_ON_CPU_HOST_INT(tb_flush_count));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove @orig from its @n_orig-th jump list */
|
|
|
|
static inline void tb_remove_from_jmp_list(TranslationBlock *orig, int n_orig)
|
|
|
|
{
|
|
|
|
uintptr_t ptr, ptr_locked;
|
|
|
|
TranslationBlock *dest;
|
|
|
|
TranslationBlock *tb;
|
|
|
|
uintptr_t *pprev;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* mark the LSB of jmp_dest[] so that no further jumps can be inserted */
|
|
|
|
ptr = qatomic_or_fetch(&orig->jmp_dest[n_orig], 1);
|
|
|
|
dest = (TranslationBlock *)(ptr & ~1);
|
|
|
|
if (dest == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_spin_lock(&dest->jmp_lock);
|
|
|
|
/*
|
|
|
|
* While acquiring the lock, the jump might have been removed if the
|
|
|
|
* destination TB was invalidated; check again.
|
|
|
|
*/
|
|
|
|
ptr_locked = qatomic_read(&orig->jmp_dest[n_orig]);
|
|
|
|
if (ptr_locked != ptr) {
|
|
|
|
qemu_spin_unlock(&dest->jmp_lock);
|
|
|
|
/*
|
|
|
|
* The only possibility is that the jump was unlinked via
|
|
|
|
* tb_jump_unlink(dest). Seeing here another destination would be a bug,
|
|
|
|
* because we set the LSB above.
|
|
|
|
*/
|
|
|
|
g_assert(ptr_locked == 1 && dest->cflags & CF_INVALID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We first acquired the lock, and since the destination pointer matches,
|
|
|
|
* we know for sure that @orig is in the jmp list.
|
|
|
|
*/
|
|
|
|
pprev = &dest->jmp_list_head;
|
|
|
|
TB_FOR_EACH_JMP(dest, tb, n) {
|
|
|
|
if (tb == orig && n == n_orig) {
|
|
|
|
*pprev = tb->jmp_list_next[n];
|
|
|
|
/* no need to set orig->jmp_dest[n]; setting the LSB was enough */
|
|
|
|
qemu_spin_unlock(&dest->jmp_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pprev = &tb->jmp_list_next[n];
|
|
|
|
}
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the jump entry 'n' of a TB so that it is not chained to another TB.
|
|
|
|
*/
|
|
|
|
void tb_reset_jump(TranslationBlock *tb, int n)
|
|
|
|
{
|
|
|
|
uintptr_t addr = (uintptr_t)(tb->tc.ptr + tb->jmp_reset_offset[n]);
|
|
|
|
tb_set_jmp_target(tb, n, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove any jumps to the TB */
|
|
|
|
static inline void tb_jmp_unlink(TranslationBlock *dest)
|
|
|
|
{
|
|
|
|
TranslationBlock *tb;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
qemu_spin_lock(&dest->jmp_lock);
|
|
|
|
|
|
|
|
TB_FOR_EACH_JMP(dest, tb, n) {
|
|
|
|
tb_reset_jump(tb, n);
|
|
|
|
qatomic_and(&tb->jmp_dest[n], (uintptr_t)NULL | 1);
|
|
|
|
/* No need to clear the list entry; setting the dest ptr is enough */
|
|
|
|
}
|
|
|
|
dest->jmp_list_head = (uintptr_t)NULL;
|
|
|
|
|
|
|
|
qemu_spin_unlock(&dest->jmp_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
CPUState *cpu;
|
|
|
|
|
2023-02-27 16:51:39 +03:00
|
|
|
if (tb_cflags(tb) & CF_PCREL) {
|
2022-09-20 08:17:44 +03:00
|
|
|
/* A TB may be at any virtual address */
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
tcg_flush_jmp_cache(cpu);
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-27 16:51:47 +03:00
|
|
|
uint32_t h = tb_jmp_cache_hash_func(tb->pc);
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
CPUJumpCache *jc = cpu->tb_jmp_cache;
|
|
|
|
|
|
|
|
if (qatomic_read(&jc->array[h].tb) == tb) {
|
|
|
|
qatomic_set(&jc->array[h].tb, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In user-mode, call with mmap_lock held.
|
|
|
|
* In !user-mode, if @rm_from_page_list is set, call with the TB's pages'
|
|
|
|
* locks held.
|
|
|
|
*/
|
|
|
|
static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
|
|
|
|
{
|
|
|
|
uint32_t h;
|
|
|
|
tb_page_addr_t phys_pc;
|
|
|
|
uint32_t orig_cflags = tb_cflags(tb);
|
|
|
|
|
|
|
|
assert_memory_lock();
|
|
|
|
|
|
|
|
/* make sure no further incoming jumps will be chained to this TB */
|
|
|
|
qemu_spin_lock(&tb->jmp_lock);
|
|
|
|
qatomic_set(&tb->cflags, tb->cflags | CF_INVALID);
|
|
|
|
qemu_spin_unlock(&tb->jmp_lock);
|
|
|
|
|
|
|
|
/* remove the TB from the hash list */
|
2022-09-20 14:21:40 +03:00
|
|
|
phys_pc = tb_page_addr0(tb);
|
2023-02-27 16:51:47 +03:00
|
|
|
h = tb_hash_func(phys_pc, (orig_cflags & CF_PCREL ? 0 : tb->pc),
|
accel/tcg: include cs_base in our hash calculations
We weren't using cs_base in the hash calculations before. Since the
arm front end moved a chunk of flags in a378206a20 (target/arm: Move
mode specific TB flags to tb->cs_base) they comprise of an important
part of the execution state.
Widen the tb_hash_func to include cs_base and expand to qemu_xxhash8()
to accommodate it.
My initial benchmark shows very little difference in the
runtime.
Before:
armhf
➜ hyperfine -w 2 -m 20 "./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot"
Benchmark 1: ./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot
Time (mean ± σ): 24.627 s ± 2.708 s [User: 34.309 s, System: 1.797 s]
Range (min … max): 22.345 s … 29.864 s 20 runs
arm64
➜ hyperfine -w 2 -n 20 "./qemu-system-aarch64 -cpu max,pauth-impdef=on -machine type=virt,virtualization=on,gic-version=3 -display none -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22,hostfwd=tcp::1234-:1234 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-arm64 -device scsi-hd,drive=hd -smp 4 -kernel ~/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image.gz -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark-pigz.service' -snapshot"
Benchmark 1: 20
Time (mean ± σ): 62.559 s ± 2.917 s [User: 189.115 s, System: 4.089 s]
Range (min … max): 59.997 s … 70.153 s 10 runs
After:
armhf
Benchmark 1: ./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot
Time (mean ± σ): 24.223 s ± 2.151 s [User: 34.284 s, System: 1.906 s]
Range (min … max): 22.000 s … 28.476 s 20 runs
arm64
hyperfine -w 2 -n 20 "./qemu-system-aarch64 -cpu max,pauth-impdef=on -machine type=virt,virtualization=on,gic-version=3 -display none -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22,hostfwd=tcp::1234-:1234 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-arm64 -device scsi-hd,drive=hd -smp 4 -kernel ~/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image.gz -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark-pigz.service' -snapshot"
Benchmark 1: 20
Time (mean ± σ): 62.769 s ± 1.978 s [User: 188.431 s, System: 5.269 s]
Range (min … max): 60.285 s … 66.868 s 10 runs
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-id: 20230526165401.574474-12-alex.bennee@linaro.org
Message-Id: <20230524133952.3971948-11-alex.bennee@linaro.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2023-05-26 19:54:01 +03:00
|
|
|
tb->flags, tb->cs_base, orig_cflags);
|
2022-09-20 08:17:44 +03:00
|
|
|
if (!qht_remove(&tb_ctx.htable, tb, h)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the TB from the page list */
|
|
|
|
if (rm_from_page_list) {
|
2022-10-01 23:36:33 +03:00
|
|
|
tb_remove(tb);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the TB from the hash list */
|
|
|
|
tb_jmp_cache_inval_tb(tb);
|
|
|
|
|
|
|
|
/* suppress this TB from the two jump lists */
|
|
|
|
tb_remove_from_jmp_list(tb, 0);
|
|
|
|
tb_remove_from_jmp_list(tb, 1);
|
|
|
|
|
|
|
|
/* suppress any remaining jumps to this TB */
|
|
|
|
tb_jmp_unlink(tb);
|
|
|
|
|
|
|
|
qatomic_set(&tb_ctx.tb_phys_invalidate_count,
|
|
|
|
tb_ctx.tb_phys_invalidate_count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tb_phys_invalidate__locked(TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
qemu_thread_jit_write();
|
|
|
|
do_tb_phys_invalidate(tb, true);
|
|
|
|
qemu_thread_jit_execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invalidate one TB.
|
|
|
|
* Called with mmap_lock held in user-mode.
|
|
|
|
*/
|
|
|
|
void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
|
|
|
|
{
|
2022-09-20 14:21:40 +03:00
|
|
|
if (page_addr == -1 && tb_page_addr0(tb) != -1) {
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_lock_pages(tb);
|
2022-09-20 08:17:44 +03:00
|
|
|
do_tb_phys_invalidate(tb, true);
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_unlock_pages(tb);
|
2022-09-20 08:17:44 +03:00
|
|
|
} else {
|
|
|
|
do_tb_phys_invalidate(tb, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-07-06 19:55:48 +03:00
|
|
|
* Add a new TB and link it to the physical page tables.
|
2022-09-20 08:17:44 +03:00
|
|
|
* Called with mmap_lock held for user-mode emulation.
|
|
|
|
*
|
|
|
|
* Returns a pointer @tb, or a pointer to an existing TB that matches @tb.
|
|
|
|
* Note that in !user-mode, another thread might have already added a TB
|
|
|
|
* for the same block of guest code that @tb corresponds to. In that case,
|
|
|
|
* the caller should discard the original @tb, and use instead the returned TB.
|
|
|
|
*/
|
2023-07-06 19:55:48 +03:00
|
|
|
TranslationBlock *tb_link_page(TranslationBlock *tb)
|
2022-09-20 08:17:44 +03:00
|
|
|
{
|
|
|
|
void *existing_tb = NULL;
|
|
|
|
uint32_t h;
|
|
|
|
|
|
|
|
assert_memory_lock();
|
|
|
|
tcg_debug_assert(!(tb->cflags & CF_INVALID));
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_record(tb);
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
/* add in the hash table */
|
2023-07-06 19:55:48 +03:00
|
|
|
h = tb_hash_func(tb_page_addr0(tb), (tb->cflags & CF_PCREL ? 0 : tb->pc),
|
accel/tcg: include cs_base in our hash calculations
We weren't using cs_base in the hash calculations before. Since the
arm front end moved a chunk of flags in a378206a20 (target/arm: Move
mode specific TB flags to tb->cs_base) they comprise of an important
part of the execution state.
Widen the tb_hash_func to include cs_base and expand to qemu_xxhash8()
to accommodate it.
My initial benchmark shows very little difference in the
runtime.
Before:
armhf
➜ hyperfine -w 2 -m 20 "./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot"
Benchmark 1: ./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot
Time (mean ± σ): 24.627 s ± 2.708 s [User: 34.309 s, System: 1.797 s]
Range (min … max): 22.345 s … 29.864 s 20 runs
arm64
➜ hyperfine -w 2 -n 20 "./qemu-system-aarch64 -cpu max,pauth-impdef=on -machine type=virt,virtualization=on,gic-version=3 -display none -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22,hostfwd=tcp::1234-:1234 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-arm64 -device scsi-hd,drive=hd -smp 4 -kernel ~/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image.gz -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark-pigz.service' -snapshot"
Benchmark 1: 20
Time (mean ± σ): 62.559 s ± 2.917 s [User: 189.115 s, System: 4.089 s]
Range (min … max): 59.997 s … 70.153 s 10 runs
After:
armhf
Benchmark 1: ./arm-softmmu/qemu-system-arm -cpu cortex-a15 -machine type=virt,highmem=off -display none -m 2048 -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-armhf -device scsi-hd,drive=hd -smp 4 -kernel /home/alex/lsrc/linux.git/builds/arm/arch/arm/boot/zImage -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark.service' -snapshot
Time (mean ± σ): 24.223 s ± 2.151 s [User: 34.284 s, System: 1.906 s]
Range (min … max): 22.000 s … 28.476 s 20 runs
arm64
hyperfine -w 2 -n 20 "./qemu-system-aarch64 -cpu max,pauth-impdef=on -machine type=virt,virtualization=on,gic-version=3 -display none -serial mon:stdio -netdev user,id=unet,hostfwd=tcp::2222-:22,hostfwd=tcp::1234-:1234 -device virtio-net-pci,netdev=unet -device virtio-scsi-pci -blockdev driver=raw,node-name=hd,discard=unmap,file.driver=host_device,file.filename=/dev/zen-disk/debian-bullseye-arm64 -device scsi-hd,drive=hd -smp 4 -kernel ~/lsrc/linux.git/builds/arm64/arch/arm64/boot/Image.gz -append 'console=ttyAMA0 root=/dev/sda2 systemd.unit=benchmark-pigz.service' -snapshot"
Benchmark 1: 20
Time (mean ± σ): 62.769 s ± 1.978 s [User: 188.431 s, System: 5.269 s]
Range (min … max): 60.285 s … 66.868 s 10 runs
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-id: 20230526165401.574474-12-alex.bennee@linaro.org
Message-Id: <20230524133952.3971948-11-alex.bennee@linaro.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2023-05-26 19:54:01 +03:00
|
|
|
tb->flags, tb->cs_base, tb->cflags);
|
2022-09-20 08:17:44 +03:00
|
|
|
qht_insert(&tb_ctx.htable, tb, h, &existing_tb);
|
|
|
|
|
|
|
|
/* remove TB from the page(s) if we couldn't insert it */
|
|
|
|
if (unlikely(existing_tb)) {
|
2022-10-01 23:36:33 +03:00
|
|
|
tb_remove(tb);
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_unlock_pages(tb);
|
|
|
|
return existing_tb;
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 19:55:48 +03:00
|
|
|
tb_unlock_pages(tb);
|
2022-09-20 08:17:44 +03:00
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
/*
|
|
|
|
* Invalidate all TBs which intersect with the target address range.
|
|
|
|
* Called with mmap_lock held for user-mode emulation.
|
|
|
|
* NOTE: this function must not be called while a TB is running.
|
|
|
|
*/
|
2023-03-06 04:30:11 +03:00
|
|
|
void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
|
|
|
TranslationBlock *tb;
|
|
|
|
PageForEachNext n;
|
|
|
|
|
|
|
|
assert_memory_lock();
|
|
|
|
|
2023-03-06 03:20:46 +03:00
|
|
|
PAGE_FOR_EACH_TB(start, last, unused, tb, n) {
|
2022-10-01 23:36:33 +03:00
|
|
|
tb_phys_invalidate__locked(tb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invalidate all TBs which intersect with the target address page @addr.
|
|
|
|
* Called with mmap_lock held for user-mode emulation
|
|
|
|
* NOTE: this function must not be called while a TB is running.
|
|
|
|
*/
|
2023-11-30 23:53:13 +03:00
|
|
|
static void tb_invalidate_phys_page(tb_page_addr_t addr)
|
2022-10-01 23:36:33 +03:00
|
|
|
{
|
2023-03-06 04:30:11 +03:00
|
|
|
tb_page_addr_t start, last;
|
2022-10-01 23:36:33 +03:00
|
|
|
|
|
|
|
start = addr & TARGET_PAGE_MASK;
|
2023-03-06 04:30:11 +03:00
|
|
|
last = addr | ~TARGET_PAGE_MASK;
|
|
|
|
tb_invalidate_phys_range(start, last);
|
2022-10-01 23:36:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called with mmap_lock held. If pc is not 0 then it indicates the
|
|
|
|
* host PC of the faulting store instruction that caused this invalidate.
|
|
|
|
* Returns true if the caller needs to abort execution of the current
|
|
|
|
* TB (because it was modified by this store and the guest CPU has
|
|
|
|
* precise-SMC semantics).
|
|
|
|
*/
|
|
|
|
bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
|
|
|
|
{
|
2022-12-24 15:35:22 +03:00
|
|
|
TranslationBlock *current_tb;
|
|
|
|
bool current_tb_modified;
|
|
|
|
TranslationBlock *tb;
|
|
|
|
PageForEachNext n;
|
2023-03-06 03:20:46 +03:00
|
|
|
tb_page_addr_t last;
|
2022-10-01 23:36:33 +03:00
|
|
|
|
2022-12-24 15:35:22 +03:00
|
|
|
/*
|
|
|
|
* Without precise smc semantics, or when outside of a TB,
|
|
|
|
* we can skip to invalidate.
|
|
|
|
*/
|
|
|
|
#ifndef TARGET_HAS_PRECISE_SMC
|
|
|
|
pc = 0;
|
|
|
|
#endif
|
|
|
|
if (!pc) {
|
|
|
|
tb_invalidate_phys_page(addr);
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
|
2022-12-24 15:35:22 +03:00
|
|
|
assert_memory_lock();
|
|
|
|
current_tb = tcg_tb_lookup(pc);
|
|
|
|
|
2023-03-06 03:20:46 +03:00
|
|
|
last = addr | ~TARGET_PAGE_MASK;
|
2022-12-24 15:35:22 +03:00
|
|
|
addr &= TARGET_PAGE_MASK;
|
|
|
|
current_tb_modified = false;
|
|
|
|
|
2023-03-06 03:20:46 +03:00
|
|
|
PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {
|
2022-12-24 15:35:22 +03:00
|
|
|
if (current_tb == tb &&
|
|
|
|
(tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
|
|
|
|
/*
|
|
|
|
* If we are modifying the current TB, we must stop its
|
|
|
|
* execution. We could be more precise by checking that
|
|
|
|
* the modification is after the current PC, but it would
|
|
|
|
* require a specialized function to partially restore
|
|
|
|
* the CPU state.
|
|
|
|
*/
|
|
|
|
current_tb_modified = true;
|
|
|
|
cpu_restore_state_from_tb(current_cpu, current_tb, pc);
|
2022-10-01 23:36:33 +03:00
|
|
|
}
|
2022-12-24 15:35:22 +03:00
|
|
|
tb_phys_invalidate__locked(tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_tb_modified) {
|
|
|
|
/* Force execution of one insn next time. */
|
|
|
|
CPUState *cpu = current_cpu;
|
2023-11-10 19:21:23 +03:00
|
|
|
cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
|
2022-12-24 15:35:22 +03:00
|
|
|
return true;
|
2022-10-01 23:36:33 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2022-09-20 08:17:44 +03:00
|
|
|
/*
|
|
|
|
* @p must be non-NULL.
|
2022-10-06 04:06:29 +03:00
|
|
|
* Call with all @pages locked.
|
2022-09-20 08:17:44 +03:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tb_invalidate_phys_page_range__locked(struct page_collection *pages,
|
|
|
|
PageDesc *p, tb_page_addr_t start,
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_page_addr_t last,
|
2022-09-20 08:17:44 +03:00
|
|
|
uintptr_t retaddr)
|
|
|
|
{
|
|
|
|
TranslationBlock *tb;
|
2022-10-01 23:36:33 +03:00
|
|
|
PageForEachNext n;
|
2022-09-20 08:17:44 +03:00
|
|
|
#ifdef TARGET_HAS_PRECISE_SMC
|
|
|
|
bool current_tb_modified = false;
|
2022-10-01 23:36:33 +03:00
|
|
|
TranslationBlock *current_tb = retaddr ? tcg_tb_lookup(retaddr) : NULL;
|
2022-09-20 08:17:44 +03:00
|
|
|
#endif /* TARGET_HAS_PRECISE_SMC */
|
|
|
|
|
2023-06-29 11:25:22 +03:00
|
|
|
/* Range may not cross a page. */
|
|
|
|
tcg_debug_assert(((start ^ last) & TARGET_PAGE_MASK) == 0);
|
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
/*
|
2023-03-06 04:18:34 +03:00
|
|
|
* We remove all the TBs in the range [start, last].
|
2022-09-20 08:17:44 +03:00
|
|
|
* XXX: see if in some cases it could be faster to invalidate all the code
|
|
|
|
*/
|
2023-03-06 03:20:46 +03:00
|
|
|
PAGE_FOR_EACH_TB(start, last, p, tb, n) {
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_page_addr_t tb_start, tb_last;
|
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
/* NOTE: this is subtle as a TB may span two physical pages */
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_start = tb_page_addr0(tb);
|
|
|
|
tb_last = tb_start + tb->size - 1;
|
2022-09-20 08:17:44 +03:00
|
|
|
if (n == 0) {
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_last = MIN(tb_last, tb_start | ~TARGET_PAGE_MASK);
|
2022-09-20 08:17:44 +03:00
|
|
|
} else {
|
2022-09-20 14:21:40 +03:00
|
|
|
tb_start = tb_page_addr1(tb);
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
2023-03-06 04:18:34 +03:00
|
|
|
if (!(tb_last < start || tb_start > last)) {
|
2022-09-20 08:17:44 +03:00
|
|
|
#ifdef TARGET_HAS_PRECISE_SMC
|
|
|
|
if (current_tb == tb &&
|
|
|
|
(tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
|
|
|
|
/*
|
|
|
|
* If we are modifying the current TB, we must stop
|
|
|
|
* its execution. We could be more precise by checking
|
|
|
|
* that the modification is after the current PC, but it
|
|
|
|
* would require a specialized function to partially
|
|
|
|
* restore the CPU state.
|
|
|
|
*/
|
|
|
|
current_tb_modified = true;
|
2022-10-01 23:36:33 +03:00
|
|
|
cpu_restore_state_from_tb(current_cpu, current_tb, retaddr);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
#endif /* TARGET_HAS_PRECISE_SMC */
|
|
|
|
tb_phys_invalidate__locked(tb);
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
/* if no code remaining, no need to continue to use slow writes */
|
|
|
|
if (!p->first_tb) {
|
|
|
|
tlb_unprotect_code(start);
|
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
|
2022-09-20 08:17:44 +03:00
|
|
|
#ifdef TARGET_HAS_PRECISE_SMC
|
|
|
|
if (current_tb_modified) {
|
|
|
|
page_collection_unlock(pages);
|
|
|
|
/* Force execution of one insn next time. */
|
2023-11-10 19:21:23 +03:00
|
|
|
current_cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
|
2022-09-20 08:17:44 +03:00
|
|
|
mmap_unlock();
|
2022-10-01 23:36:33 +03:00
|
|
|
cpu_loop_exit_noexc(current_cpu);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invalidate all TBs which intersect with the target physical address range
|
2023-03-06 04:30:11 +03:00
|
|
|
* [start;last]. NOTE: start and end may refer to *different* physical pages.
|
2022-09-20 08:17:44 +03:00
|
|
|
* 'is_cpu_write_access' should be true if called from a real cpu write
|
|
|
|
* access: the virtual CPU will exit the current TB if code is modified inside
|
|
|
|
* this TB.
|
|
|
|
*/
|
2023-03-06 04:30:11 +03:00
|
|
|
void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
|
2022-09-20 08:17:44 +03:00
|
|
|
{
|
|
|
|
struct page_collection *pages;
|
2023-03-06 04:30:11 +03:00
|
|
|
tb_page_addr_t index, index_last;
|
|
|
|
|
|
|
|
pages = page_collection_lock(start, last);
|
2022-09-20 08:17:44 +03:00
|
|
|
|
2023-03-06 04:30:11 +03:00
|
|
|
index_last = last >> TARGET_PAGE_BITS;
|
|
|
|
for (index = start >> TARGET_PAGE_BITS; index <= index_last; index++) {
|
|
|
|
PageDesc *pd = page_find(index);
|
2023-06-29 11:25:21 +03:00
|
|
|
tb_page_addr_t page_start, page_last;
|
2022-09-20 08:17:44 +03:00
|
|
|
|
|
|
|
if (pd == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-01 23:36:33 +03:00
|
|
|
assert_page_locked(pd);
|
2023-06-29 11:25:21 +03:00
|
|
|
page_start = index << TARGET_PAGE_BITS;
|
|
|
|
page_last = page_start | ~TARGET_PAGE_MASK;
|
|
|
|
page_last = MIN(page_last, last);
|
|
|
|
tb_invalidate_phys_page_range__locked(pages, pd,
|
|
|
|
page_start, page_last, 0);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
|
|
|
page_collection_unlock(pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call with all @pages in the range [@start, @start + len[ locked.
|
|
|
|
*/
|
2022-12-09 12:36:49 +03:00
|
|
|
static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages,
|
|
|
|
tb_page_addr_t start,
|
|
|
|
unsigned len, uintptr_t ra)
|
2022-09-20 08:17:44 +03:00
|
|
|
{
|
|
|
|
PageDesc *p;
|
|
|
|
|
|
|
|
p = page_find(start >> TARGET_PAGE_BITS);
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_page_locked(p);
|
2023-03-06 04:18:34 +03:00
|
|
|
tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1, ra);
|
2022-09-20 08:17:44 +03:00
|
|
|
}
|
2022-12-09 12:36:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void tb_invalidate_phys_range_fast(ram_addr_t ram_addr,
|
|
|
|
unsigned size,
|
|
|
|
uintptr_t retaddr)
|
|
|
|
{
|
|
|
|
struct page_collection *pages;
|
|
|
|
|
2023-03-06 03:42:12 +03:00
|
|
|
pages = page_collection_lock(ram_addr, ram_addr + size - 1);
|
2022-12-09 12:36:48 +03:00
|
|
|
tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr);
|
|
|
|
page_collection_unlock(pages);
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:36:33 +03:00
|
|
|
#endif /* CONFIG_USER_ONLY */
|