123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230 |
- /*
- * Translation Block Maintenance
- *
- * 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"
- #include "qemu/interval-tree.h"
- #include "qemu/qtree.h"
- #include "exec/cputlb.h"
- #include "exec/log.h"
- #include "exec/exec-all.h"
- #include "exec/page-protection.h"
- #include "exec/tb-flush.h"
- #include "exec/translate-all.h"
- #include "system/tcg.h"
- #include "tcg/tcg.h"
- #include "tb-hash.h"
- #include "tb-context.h"
- #include "internal-common.h"
- #include "internal-target.h"
- /* 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)
- static bool tb_cmp(const void *ap, const void *bp)
- {
- const TranslationBlock *a = ap;
- const TranslationBlock *b = bp;
- return ((tb_cflags(a) & CF_PCREL || a->pc == b->pc) &&
- a->cs_base == b->cs_base &&
- a->flags == b->flags &&
- (tb_cflags(a) & ~CF_INVALID) == (tb_cflags(b) & ~CF_INVALID) &&
- tb_page_addr0(a) == tb_page_addr0(b) &&
- tb_page_addr1(a) == tb_page_addr1(b));
- }
- void tb_htable_init(void)
- {
- unsigned int mode = QHT_MODE_AUTO_RESIZE;
- qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
- }
- typedef struct PageDesc PageDesc;
- #ifdef CONFIG_USER_ONLY
- /*
- * In user-mode page locks aren't used; mmap_lock is enough.
- */
- #define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
- static inline void tb_lock_pages(const TranslationBlock *tb) { }
- /*
- * 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. */
- static void tb_record(TranslationBlock *tb)
- {
- vaddr addr;
- int flags;
- assert_memory_lock();
- tb->itree.last = tb->itree.start + tb->size - 1;
- /* 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));
- }
- 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. */
- #define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \
- for (T = foreach_tb_first(start, last), \
- N = foreach_tb_next(T, start, last); \
- T != NULL; \
- T = N, N = foreach_tb_next(N, start, last))
- typedef TranslationBlock *PageForEachNext;
- static PageForEachNext foreach_tb_first(tb_page_addr_t start,
- tb_page_addr_t last)
- {
- IntervalTreeNode *n = interval_tree_iter_first(&tb_root, start, last);
- return n ? container_of(n, TranslationBlock, itree) : NULL;
- }
- static PageForEachNext foreach_tb_next(PageForEachNext tb,
- tb_page_addr_t start,
- tb_page_addr_t last)
- {
- IntervalTreeNode *n;
- if (tb) {
- n = interval_tree_iter_next(&tb->itree, start, last);
- if (n) {
- return container_of(n, TranslationBlock, itree);
- }
- }
- return NULL;
- }
- #else
- /*
- * 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];
- struct PageDesc {
- QemuSpin lock;
- /* list of TBs intersecting this ram page */
- uintptr_t first_tb;
- };
- 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);
- }
- static PageDesc *page_find_alloc(tb_page_addr_t index, bool alloc)
- {
- PageDesc *pd;
- void **lp;
- /* Level 1. Always allocated. */
- lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
- /* Level 2..N-1. */
- for (int i = v_l2_levels; i > 0; i--) {
- 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));
- }
- 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 {
- QTree *tree;
- struct page_entry *max;
- };
- typedef int PageForEachNext;
- #define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
- 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);
- }
- /* 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;
- }
- static void page_unlock(PageDesc *pd)
- {
- qemu_spin_unlock(&pd->lock);
- page_unlock__debug(pd);
- }
- 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));
- }
- 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)
- {
- bool busy = page_trylock(pe->pd);
- 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;
- pe = q_tree_lookup(set->tree, &index);
- if (pe) {
- return false;
- }
- pd = page_find(index);
- if (pd == NULL) {
- return false;
- }
- pe = page_entry_new(pd, index);
- q_tree_insert(set->tree, &pe->index, pe);
- /*
- * 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;
- }
- /*
- * Lock a range of pages ([@start,@last]) as well as the pages of all
- * intersecting TBs.
- * Locking order: acquire locks in ascending order of page index.
- */
- static struct page_collection *page_collection_lock(tb_page_addr_t start,
- tb_page_addr_t last)
- {
- struct page_collection *set = g_malloc(sizeof(*set));
- tb_page_addr_t index;
- PageDesc *pd;
- start >>= TARGET_PAGE_BITS;
- last >>= TARGET_PAGE_BITS;
- g_assert(start <= last);
- set->tree = q_tree_new_full(tb_page_addr_cmp, NULL, NULL,
- page_entry_destroy);
- set->max = NULL;
- assert_no_pages_locked();
- retry:
- q_tree_foreach(set->tree, page_entry_lock, NULL);
- for (index = start; index <= last; index++) {
- TranslationBlock *tb;
- PageForEachNext n;
- pd = page_find(index);
- if (pd == NULL) {
- continue;
- }
- if (page_trylock_add(set, index << TARGET_PAGE_BITS)) {
- q_tree_foreach(set->tree, page_entry_unlock, NULL);
- 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 */
- q_tree_foreach(set->tree, page_entry_unlock, NULL);
- goto retry;
- }
- }
- }
- return set;
- }
- static void page_collection_unlock(struct page_collection *set)
- {
- /* entries are unlocked and freed via page_entry_destroy */
- q_tree_destroy(set->tree);
- g_free(set);
- }
- /* Set to NULL all the 'first_tb' fields in all PageDescs. */
- static void tb_remove_all_1(int level, void **lp)
- {
- 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) {
- tb_remove_all_1(level - 1, pp + i);
- }
- }
- }
- static void tb_remove_all(void)
- {
- int i, l1_sz = v_l1_size;
- for (i = 0; i < l1_sz; i++) {
- tb_remove_all_1(v_l2_levels, l1_map + i);
- }
- }
- /*
- * Add the tb in the target page and protect it if necessary.
- * Called with @p->lock held.
- */
- static void tb_page_add(PageDesc *p, TranslationBlock *tb, unsigned int n)
- {
- 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);
- }
- }
- static void tb_record(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;
- assert(paddr0 != -1);
- if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
- tb_page_add(page_find_alloc(pindex1, false), tb, 1);
- }
- tb_page_add(page_find_alloc(pindex0, false), tb, 0);
- }
- static void tb_page_remove(PageDesc *pd, TranslationBlock *tb)
- {
- 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)
- {
- 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;
- assert(paddr0 != -1);
- if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
- tb_page_remove(page_find_alloc(pindex1, false), tb);
- }
- tb_page_remove(page_find_alloc(pindex0, false), tb);
- }
- #endif /* CONFIG_USER_ONLY */
- /* 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);
- tb_remove_all();
- tcg_region_reset_all();
- /* XXX: flush processor icache at this point if cache flush is expensive */
- qatomic_inc(&tb_ctx.tb_flush_count);
- done:
- mmap_unlock();
- if (did_flush) {
- qemu_plugin_flush_cb();
- }
- }
- void tb_flush(CPUState *cpu)
- {
- if (tcg_enabled()) {
- unsigned tb_flush_count = qatomic_read(&tb_ctx.tb_flush_count);
- if (cpu_in_serial_context(cpu)) {
- 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;
- if (tb_cflags(tb) & CF_PCREL) {
- /* A TB may be at any virtual address */
- CPU_FOREACH(cpu) {
- tcg_flush_jmp_cache(cpu);
- }
- } else {
- uint32_t h = tb_jmp_cache_hash_func(tb->pc);
- 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 */
- phys_pc = tb_page_addr0(tb);
- h = tb_hash_func(phys_pc, (orig_cflags & CF_PCREL ? 0 : tb->pc),
- tb->flags, tb->cs_base, orig_cflags);
- if (!qht_remove(&tb_ctx.htable, tb, h)) {
- return;
- }
- /* remove the TB from the page list */
- if (rm_from_page_list) {
- tb_remove(tb);
- }
- /* 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)
- {
- if (page_addr == -1 && tb_page_addr0(tb) != -1) {
- tb_lock_pages(tb);
- do_tb_phys_invalidate(tb, true);
- tb_unlock_pages(tb);
- } else {
- do_tb_phys_invalidate(tb, false);
- }
- }
- /*
- * Add a new TB and link it to the physical page tables.
- * 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.
- */
- TranslationBlock *tb_link_page(TranslationBlock *tb)
- {
- void *existing_tb = NULL;
- uint32_t h;
- assert_memory_lock();
- tcg_debug_assert(!(tb->cflags & CF_INVALID));
- tb_record(tb);
- /* add in the hash table */
- h = tb_hash_func(tb_page_addr0(tb), (tb->cflags & CF_PCREL ? 0 : tb->pc),
- tb->flags, tb->cs_base, tb->cflags);
- 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)) {
- tb_remove(tb);
- tb_unlock_pages(tb);
- return existing_tb;
- }
- tb_unlock_pages(tb);
- return tb;
- }
- #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.
- */
- void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
- {
- TranslationBlock *tb;
- PageForEachNext n;
- assert_memory_lock();
- PAGE_FOR_EACH_TB(start, last, unused, tb, n) {
- 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.
- */
- static void tb_invalidate_phys_page(tb_page_addr_t addr)
- {
- tb_page_addr_t start, last;
- start = addr & TARGET_PAGE_MASK;
- last = addr | ~TARGET_PAGE_MASK;
- tb_invalidate_phys_range(start, last);
- }
- /*
- * 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)
- {
- TranslationBlock *current_tb;
- bool current_tb_modified;
- TranslationBlock *tb;
- PageForEachNext n;
- tb_page_addr_t last;
- /*
- * 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;
- }
- assert_memory_lock();
- current_tb = tcg_tb_lookup(pc);
- last = addr | ~TARGET_PAGE_MASK;
- addr &= TARGET_PAGE_MASK;
- current_tb_modified = false;
- PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {
- 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);
- }
- tb_phys_invalidate__locked(tb);
- }
- if (current_tb_modified) {
- /* Force execution of one insn next time. */
- CPUState *cpu = current_cpu;
- cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
- return true;
- }
- return false;
- }
- #else
- /*
- * @p must be non-NULL.
- * Call with all @pages locked.
- */
- static void
- tb_invalidate_phys_page_range__locked(struct page_collection *pages,
- PageDesc *p, tb_page_addr_t start,
- tb_page_addr_t last,
- uintptr_t retaddr)
- {
- TranslationBlock *tb;
- PageForEachNext n;
- #ifdef TARGET_HAS_PRECISE_SMC
- bool current_tb_modified = false;
- TranslationBlock *current_tb = retaddr ? tcg_tb_lookup(retaddr) : NULL;
- #endif /* TARGET_HAS_PRECISE_SMC */
- /* Range may not cross a page. */
- tcg_debug_assert(((start ^ last) & TARGET_PAGE_MASK) == 0);
- /*
- * We remove all the TBs in the range [start, last].
- * XXX: see if in some cases it could be faster to invalidate all the code
- */
- PAGE_FOR_EACH_TB(start, last, p, tb, n) {
- tb_page_addr_t tb_start, tb_last;
- /* NOTE: this is subtle as a TB may span two physical pages */
- tb_start = tb_page_addr0(tb);
- tb_last = tb_start + tb->size - 1;
- if (n == 0) {
- tb_last = MIN(tb_last, tb_start | ~TARGET_PAGE_MASK);
- } else {
- tb_start = tb_page_addr1(tb);
- tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK);
- }
- if (!(tb_last < start || tb_start > last)) {
- #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;
- cpu_restore_state_from_tb(current_cpu, current_tb, retaddr);
- }
- #endif /* TARGET_HAS_PRECISE_SMC */
- tb_phys_invalidate__locked(tb);
- }
- }
- /* if no code remaining, no need to continue to use slow writes */
- if (!p->first_tb) {
- tlb_unprotect_code(start);
- }
- #ifdef TARGET_HAS_PRECISE_SMC
- if (current_tb_modified) {
- page_collection_unlock(pages);
- /* Force execution of one insn next time. */
- current_cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
- mmap_unlock();
- cpu_loop_exit_noexc(current_cpu);
- }
- #endif
- }
- /*
- * Invalidate all TBs which intersect with the target physical address range
- * [start;last]. NOTE: start and end may refer to *different* physical pages.
- * '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.
- */
- void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
- {
- struct page_collection *pages;
- tb_page_addr_t index, index_last;
- pages = page_collection_lock(start, last);
- index_last = last >> TARGET_PAGE_BITS;
- for (index = start >> TARGET_PAGE_BITS; index <= index_last; index++) {
- PageDesc *pd = page_find(index);
- tb_page_addr_t page_start, page_last;
- if (pd == NULL) {
- continue;
- }
- assert_page_locked(pd);
- 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);
- }
- page_collection_unlock(pages);
- }
- /*
- * Call with all @pages in the range [@start, @start + len[ locked.
- */
- static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages,
- tb_page_addr_t start,
- unsigned len, uintptr_t ra)
- {
- PageDesc *p;
- p = page_find(start >> TARGET_PAGE_BITS);
- if (!p) {
- return;
- }
- assert_page_locked(p);
- tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1, ra);
- }
- /*
- * 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;
- pages = page_collection_lock(ram_addr, ram_addr + size - 1);
- tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr);
- page_collection_unlock(pages);
- }
- #endif /* CONFIG_USER_ONLY */
|