Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / accel / tcg / tb-maint.c
blobcc0f5afd475d608dd373e8384ed0ac21cffa08cf
1 /*
2 * Translation Block Maintenance
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu/interval-tree.h"
22 #include "qemu/qtree.h"
23 #include "exec/cputlb.h"
24 #include "exec/log.h"
25 #include "exec/exec-all.h"
26 #include "exec/page-protection.h"
27 #include "exec/tb-flush.h"
28 #include "exec/translate-all.h"
29 #include "sysemu/tcg.h"
30 #include "tcg/tcg.h"
31 #include "tb-hash.h"
32 #include "tb-context.h"
33 #include "internal-common.h"
34 #include "internal-target.h"
37 /* List iterators for lists of tagged pointers in TranslationBlock. */
38 #define TB_FOR_EACH_TAGGED(head, tb, n, field) \
39 for (n = (head) & 1, tb = (TranslationBlock *)((head) & ~1); \
40 tb; tb = (TranslationBlock *)tb->field[n], n = (uintptr_t)tb & 1, \
41 tb = (TranslationBlock *)((uintptr_t)tb & ~1))
43 #define TB_FOR_EACH_JMP(head_tb, tb, n) \
44 TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
46 static bool tb_cmp(const void *ap, const void *bp)
48 const TranslationBlock *a = ap;
49 const TranslationBlock *b = bp;
51 return ((tb_cflags(a) & CF_PCREL || a->pc == b->pc) &&
52 a->cs_base == b->cs_base &&
53 a->flags == b->flags &&
54 (tb_cflags(a) & ~CF_INVALID) == (tb_cflags(b) & ~CF_INVALID) &&
55 tb_page_addr0(a) == tb_page_addr0(b) &&
56 tb_page_addr1(a) == tb_page_addr1(b));
59 void tb_htable_init(void)
61 unsigned int mode = QHT_MODE_AUTO_RESIZE;
63 qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
66 typedef struct PageDesc PageDesc;
68 #ifdef CONFIG_USER_ONLY
71 * In user-mode page locks aren't used; mmap_lock is enough.
73 #define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
75 static inline void tb_lock_pages(const TranslationBlock *tb) { }
78 * For user-only, since we are protecting all of memory with a single lock,
79 * and because the two pages of a TranslationBlock are always contiguous,
80 * use a single data structure to record all TranslationBlocks.
82 static IntervalTreeRoot tb_root;
84 static void tb_remove_all(void)
86 assert_memory_lock();
87 memset(&tb_root, 0, sizeof(tb_root));
90 /* Call with mmap_lock held. */
91 static void tb_record(TranslationBlock *tb)
93 vaddr addr;
94 int flags;
96 assert_memory_lock();
97 tb->itree.last = tb->itree.start + tb->size - 1;
99 /* translator_loop() must have made all TB pages non-writable */
100 addr = tb_page_addr0(tb);
101 flags = page_get_flags(addr);
102 assert(!(flags & PAGE_WRITE));
104 addr = tb_page_addr1(tb);
105 if (addr != -1) {
106 flags = page_get_flags(addr);
107 assert(!(flags & PAGE_WRITE));
110 interval_tree_insert(&tb->itree, &tb_root);
113 /* Call with mmap_lock held. */
114 static void tb_remove(TranslationBlock *tb)
116 assert_memory_lock();
117 interval_tree_remove(&tb->itree, &tb_root);
120 /* TODO: For now, still shared with translate-all.c for system mode. */
121 #define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \
122 for (T = foreach_tb_first(start, last), \
123 N = foreach_tb_next(T, start, last); \
124 T != NULL; \
125 T = N, N = foreach_tb_next(N, start, last))
127 typedef TranslationBlock *PageForEachNext;
129 static PageForEachNext foreach_tb_first(tb_page_addr_t start,
130 tb_page_addr_t last)
132 IntervalTreeNode *n = interval_tree_iter_first(&tb_root, start, last);
133 return n ? container_of(n, TranslationBlock, itree) : NULL;
136 static PageForEachNext foreach_tb_next(PageForEachNext tb,
137 tb_page_addr_t start,
138 tb_page_addr_t last)
140 IntervalTreeNode *n;
142 if (tb) {
143 n = interval_tree_iter_next(&tb->itree, start, last);
144 if (n) {
145 return container_of(n, TranslationBlock, itree);
148 return NULL;
151 #else
153 * In system mode we want L1_MAP to be based on ram offsets.
155 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
156 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
157 #else
158 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
159 #endif
161 /* Size of the L2 (and L3, etc) page tables. */
162 #define V_L2_BITS 10
163 #define V_L2_SIZE (1 << V_L2_BITS)
166 * L1 Mapping properties
168 static int v_l1_size;
169 static int v_l1_shift;
170 static int v_l2_levels;
173 * The bottom level has pointers to PageDesc, and is indexed by
174 * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
176 #define V_L1_MIN_BITS 4
177 #define V_L1_MAX_BITS (V_L2_BITS + 3)
178 #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
180 static void *l1_map[V_L1_MAX_SIZE];
182 struct PageDesc {
183 QemuSpin lock;
184 /* list of TBs intersecting this ram page */
185 uintptr_t first_tb;
188 void page_table_config_init(void)
190 uint32_t v_l1_bits;
192 assert(TARGET_PAGE_BITS);
193 /* The bits remaining after N lower levels of page tables. */
194 v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
195 if (v_l1_bits < V_L1_MIN_BITS) {
196 v_l1_bits += V_L2_BITS;
199 v_l1_size = 1 << v_l1_bits;
200 v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
201 v_l2_levels = v_l1_shift / V_L2_BITS - 1;
203 assert(v_l1_bits <= V_L1_MAX_BITS);
204 assert(v_l1_shift % V_L2_BITS == 0);
205 assert(v_l2_levels >= 0);
208 static PageDesc *page_find_alloc(tb_page_addr_t index, bool alloc)
210 PageDesc *pd;
211 void **lp;
213 /* Level 1. Always allocated. */
214 lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
216 /* Level 2..N-1. */
217 for (int i = v_l2_levels; i > 0; i--) {
218 void **p = qatomic_rcu_read(lp);
220 if (p == NULL) {
221 void *existing;
223 if (!alloc) {
224 return NULL;
226 p = g_new0(void *, V_L2_SIZE);
227 existing = qatomic_cmpxchg(lp, NULL, p);
228 if (unlikely(existing)) {
229 g_free(p);
230 p = existing;
234 lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
237 pd = qatomic_rcu_read(lp);
238 if (pd == NULL) {
239 void *existing;
241 if (!alloc) {
242 return NULL;
245 pd = g_new0(PageDesc, V_L2_SIZE);
246 for (int i = 0; i < V_L2_SIZE; i++) {
247 qemu_spin_init(&pd[i].lock);
250 existing = qatomic_cmpxchg(lp, NULL, pd);
251 if (unlikely(existing)) {
252 for (int i = 0; i < V_L2_SIZE; i++) {
253 qemu_spin_destroy(&pd[i].lock);
255 g_free(pd);
256 pd = existing;
260 return pd + (index & (V_L2_SIZE - 1));
263 static inline PageDesc *page_find(tb_page_addr_t index)
265 return page_find_alloc(index, false);
269 * struct page_entry - page descriptor entry
270 * @pd: pointer to the &struct PageDesc of the page this entry represents
271 * @index: page index of the page
272 * @locked: whether the page is locked
274 * This struct helps us keep track of the locked state of a page, without
275 * bloating &struct PageDesc.
277 * A page lock protects accesses to all fields of &struct PageDesc.
279 * See also: &struct page_collection.
281 struct page_entry {
282 PageDesc *pd;
283 tb_page_addr_t index;
284 bool locked;
288 * struct page_collection - tracks a set of pages (i.e. &struct page_entry's)
289 * @tree: Binary search tree (BST) of the pages, with key == page index
290 * @max: Pointer to the page in @tree with the highest page index
292 * To avoid deadlock we lock pages in ascending order of page index.
293 * When operating on a set of pages, we need to keep track of them so that
294 * we can lock them in order and also unlock them later. For this we collect
295 * pages (i.e. &struct page_entry's) in a binary search @tree. Given that the
296 * @tree implementation we use does not provide an O(1) operation to obtain the
297 * highest-ranked element, we use @max to keep track of the inserted page
298 * with the highest index. This is valuable because if a page is not in
299 * the tree and its index is higher than @max's, then we can lock it
300 * without breaking the locking order rule.
302 * Note on naming: 'struct page_set' would be shorter, but we already have a few
303 * page_set_*() helpers, so page_collection is used instead to avoid confusion.
305 * See also: page_collection_lock().
307 struct page_collection {
308 QTree *tree;
309 struct page_entry *max;
312 typedef int PageForEachNext;
313 #define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
314 TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)
316 #ifdef CONFIG_DEBUG_TCG
318 static __thread GHashTable *ht_pages_locked_debug;
320 static void ht_pages_locked_debug_init(void)
322 if (ht_pages_locked_debug) {
323 return;
325 ht_pages_locked_debug = g_hash_table_new(NULL, NULL);
328 static bool page_is_locked(const PageDesc *pd)
330 PageDesc *found;
332 ht_pages_locked_debug_init();
333 found = g_hash_table_lookup(ht_pages_locked_debug, pd);
334 return !!found;
337 static void page_lock__debug(PageDesc *pd)
339 ht_pages_locked_debug_init();
340 g_assert(!page_is_locked(pd));
341 g_hash_table_insert(ht_pages_locked_debug, pd, pd);
344 static void page_unlock__debug(const PageDesc *pd)
346 bool removed;
348 ht_pages_locked_debug_init();
349 g_assert(page_is_locked(pd));
350 removed = g_hash_table_remove(ht_pages_locked_debug, pd);
351 g_assert(removed);
354 static void do_assert_page_locked(const PageDesc *pd,
355 const char *file, int line)
357 if (unlikely(!page_is_locked(pd))) {
358 error_report("assert_page_lock: PageDesc %p not locked @ %s:%d",
359 pd, file, line);
360 abort();
363 #define assert_page_locked(pd) do_assert_page_locked(pd, __FILE__, __LINE__)
365 void assert_no_pages_locked(void)
367 ht_pages_locked_debug_init();
368 g_assert(g_hash_table_size(ht_pages_locked_debug) == 0);
371 #else /* !CONFIG_DEBUG_TCG */
373 static inline void page_lock__debug(const PageDesc *pd) { }
374 static inline void page_unlock__debug(const PageDesc *pd) { }
375 static inline void assert_page_locked(const PageDesc *pd) { }
377 #endif /* CONFIG_DEBUG_TCG */
379 static void page_lock(PageDesc *pd)
381 page_lock__debug(pd);
382 qemu_spin_lock(&pd->lock);
385 /* Like qemu_spin_trylock, returns false on success */
386 static bool page_trylock(PageDesc *pd)
388 bool busy = qemu_spin_trylock(&pd->lock);
389 if (!busy) {
390 page_lock__debug(pd);
392 return busy;
395 static void page_unlock(PageDesc *pd)
397 qemu_spin_unlock(&pd->lock);
398 page_unlock__debug(pd);
401 void tb_lock_page0(tb_page_addr_t paddr)
403 page_lock(page_find_alloc(paddr >> TARGET_PAGE_BITS, true));
406 void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
408 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
409 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
410 PageDesc *pd0, *pd1;
412 if (pindex0 == pindex1) {
413 /* Identical pages, and the first page is already locked. */
414 return;
417 pd1 = page_find_alloc(pindex1, true);
418 if (pindex0 < pindex1) {
419 /* Correct locking order, we may block. */
420 page_lock(pd1);
421 return;
424 /* Incorrect locking order, we cannot block lest we deadlock. */
425 if (!page_trylock(pd1)) {
426 return;
430 * Drop the lock on page0 and get both page locks in the right order.
431 * Restart translation via longjmp.
433 pd0 = page_find_alloc(pindex0, false);
434 page_unlock(pd0);
435 page_lock(pd1);
436 page_lock(pd0);
437 siglongjmp(tcg_ctx->jmp_trans, -3);
440 void tb_unlock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
442 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
443 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
445 if (pindex0 != pindex1) {
446 page_unlock(page_find_alloc(pindex1, false));
450 static void tb_lock_pages(TranslationBlock *tb)
452 tb_page_addr_t paddr0 = tb_page_addr0(tb);
453 tb_page_addr_t paddr1 = tb_page_addr1(tb);
454 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
455 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
457 if (unlikely(paddr0 == -1)) {
458 return;
460 if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
461 if (pindex0 < pindex1) {
462 page_lock(page_find_alloc(pindex0, true));
463 page_lock(page_find_alloc(pindex1, true));
464 return;
466 page_lock(page_find_alloc(pindex1, true));
468 page_lock(page_find_alloc(pindex0, true));
471 void tb_unlock_pages(TranslationBlock *tb)
473 tb_page_addr_t paddr0 = tb_page_addr0(tb);
474 tb_page_addr_t paddr1 = tb_page_addr1(tb);
475 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
476 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
478 if (unlikely(paddr0 == -1)) {
479 return;
481 if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
482 page_unlock(page_find_alloc(pindex1, false));
484 page_unlock(page_find_alloc(pindex0, false));
487 static inline struct page_entry *
488 page_entry_new(PageDesc *pd, tb_page_addr_t index)
490 struct page_entry *pe = g_malloc(sizeof(*pe));
492 pe->index = index;
493 pe->pd = pd;
494 pe->locked = false;
495 return pe;
498 static void page_entry_destroy(gpointer p)
500 struct page_entry *pe = p;
502 g_assert(pe->locked);
503 page_unlock(pe->pd);
504 g_free(pe);
507 /* returns false on success */
508 static bool page_entry_trylock(struct page_entry *pe)
510 bool busy = page_trylock(pe->pd);
511 if (!busy) {
512 g_assert(!pe->locked);
513 pe->locked = true;
515 return busy;
518 static void do_page_entry_lock(struct page_entry *pe)
520 page_lock(pe->pd);
521 g_assert(!pe->locked);
522 pe->locked = true;
525 static gboolean page_entry_lock(gpointer key, gpointer value, gpointer data)
527 struct page_entry *pe = value;
529 do_page_entry_lock(pe);
530 return FALSE;
533 static gboolean page_entry_unlock(gpointer key, gpointer value, gpointer data)
535 struct page_entry *pe = value;
537 if (pe->locked) {
538 pe->locked = false;
539 page_unlock(pe->pd);
541 return FALSE;
545 * Trylock a page, and if successful, add the page to a collection.
546 * Returns true ("busy") if the page could not be locked; false otherwise.
548 static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)
550 tb_page_addr_t index = addr >> TARGET_PAGE_BITS;
551 struct page_entry *pe;
552 PageDesc *pd;
554 pe = q_tree_lookup(set->tree, &index);
555 if (pe) {
556 return false;
559 pd = page_find(index);
560 if (pd == NULL) {
561 return false;
564 pe = page_entry_new(pd, index);
565 q_tree_insert(set->tree, &pe->index, pe);
568 * If this is either (1) the first insertion or (2) a page whose index
569 * is higher than any other so far, just lock the page and move on.
571 if (set->max == NULL || pe->index > set->max->index) {
572 set->max = pe;
573 do_page_entry_lock(pe);
574 return false;
577 * Try to acquire out-of-order lock; if busy, return busy so that we acquire
578 * locks in order.
580 return page_entry_trylock(pe);
583 static gint tb_page_addr_cmp(gconstpointer ap, gconstpointer bp, gpointer udata)
585 tb_page_addr_t a = *(const tb_page_addr_t *)ap;
586 tb_page_addr_t b = *(const tb_page_addr_t *)bp;
588 if (a == b) {
589 return 0;
590 } else if (a < b) {
591 return -1;
593 return 1;
597 * Lock a range of pages ([@start,@last]) as well as the pages of all
598 * intersecting TBs.
599 * Locking order: acquire locks in ascending order of page index.
601 static struct page_collection *page_collection_lock(tb_page_addr_t start,
602 tb_page_addr_t last)
604 struct page_collection *set = g_malloc(sizeof(*set));
605 tb_page_addr_t index;
606 PageDesc *pd;
608 start >>= TARGET_PAGE_BITS;
609 last >>= TARGET_PAGE_BITS;
610 g_assert(start <= last);
612 set->tree = q_tree_new_full(tb_page_addr_cmp, NULL, NULL,
613 page_entry_destroy);
614 set->max = NULL;
615 assert_no_pages_locked();
617 retry:
618 q_tree_foreach(set->tree, page_entry_lock, NULL);
620 for (index = start; index <= last; index++) {
621 TranslationBlock *tb;
622 PageForEachNext n;
624 pd = page_find(index);
625 if (pd == NULL) {
626 continue;
628 if (page_trylock_add(set, index << TARGET_PAGE_BITS)) {
629 q_tree_foreach(set->tree, page_entry_unlock, NULL);
630 goto retry;
632 assert_page_locked(pd);
633 PAGE_FOR_EACH_TB(unused, unused, pd, tb, n) {
634 if (page_trylock_add(set, tb_page_addr0(tb)) ||
635 (tb_page_addr1(tb) != -1 &&
636 page_trylock_add(set, tb_page_addr1(tb)))) {
637 /* drop all locks, and reacquire in order */
638 q_tree_foreach(set->tree, page_entry_unlock, NULL);
639 goto retry;
643 return set;
646 static void page_collection_unlock(struct page_collection *set)
648 /* entries are unlocked and freed via page_entry_destroy */
649 q_tree_destroy(set->tree);
650 g_free(set);
653 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
654 static void tb_remove_all_1(int level, void **lp)
656 int i;
658 if (*lp == NULL) {
659 return;
661 if (level == 0) {
662 PageDesc *pd = *lp;
664 for (i = 0; i < V_L2_SIZE; ++i) {
665 page_lock(&pd[i]);
666 pd[i].first_tb = (uintptr_t)NULL;
667 page_unlock(&pd[i]);
669 } else {
670 void **pp = *lp;
672 for (i = 0; i < V_L2_SIZE; ++i) {
673 tb_remove_all_1(level - 1, pp + i);
678 static void tb_remove_all(void)
680 int i, l1_sz = v_l1_size;
682 for (i = 0; i < l1_sz; i++) {
683 tb_remove_all_1(v_l2_levels, l1_map + i);
688 * Add the tb in the target page and protect it if necessary.
689 * Called with @p->lock held.
691 static void tb_page_add(PageDesc *p, TranslationBlock *tb, unsigned int n)
693 bool page_already_protected;
695 assert_page_locked(p);
697 tb->page_next[n] = p->first_tb;
698 page_already_protected = p->first_tb != 0;
699 p->first_tb = (uintptr_t)tb | n;
702 * If some code is already present, then the pages are already
703 * protected. So we handle the case where only the first TB is
704 * allocated in a physical page.
706 if (!page_already_protected) {
707 tlb_protect_code(tb->page_addr[n] & TARGET_PAGE_MASK);
711 static void tb_record(TranslationBlock *tb)
713 tb_page_addr_t paddr0 = tb_page_addr0(tb);
714 tb_page_addr_t paddr1 = tb_page_addr1(tb);
715 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
716 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
718 assert(paddr0 != -1);
719 if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
720 tb_page_add(page_find_alloc(pindex1, false), tb, 1);
722 tb_page_add(page_find_alloc(pindex0, false), tb, 0);
725 static void tb_page_remove(PageDesc *pd, TranslationBlock *tb)
727 TranslationBlock *tb1;
728 uintptr_t *pprev;
729 PageForEachNext n1;
731 assert_page_locked(pd);
732 pprev = &pd->first_tb;
733 PAGE_FOR_EACH_TB(unused, unused, pd, tb1, n1) {
734 if (tb1 == tb) {
735 *pprev = tb1->page_next[n1];
736 return;
738 pprev = &tb1->page_next[n1];
740 g_assert_not_reached();
743 static void tb_remove(TranslationBlock *tb)
745 tb_page_addr_t paddr0 = tb_page_addr0(tb);
746 tb_page_addr_t paddr1 = tb_page_addr1(tb);
747 tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
748 tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
750 assert(paddr0 != -1);
751 if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
752 tb_page_remove(page_find_alloc(pindex1, false), tb);
754 tb_page_remove(page_find_alloc(pindex0, false), tb);
756 #endif /* CONFIG_USER_ONLY */
758 /* flush all the translation blocks */
759 static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
761 bool did_flush = false;
763 mmap_lock();
764 /* If it is already been done on request of another CPU, just retry. */
765 if (tb_ctx.tb_flush_count != tb_flush_count.host_int) {
766 goto done;
768 did_flush = true;
770 CPU_FOREACH(cpu) {
771 tcg_flush_jmp_cache(cpu);
774 qht_reset_size(&tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
775 tb_remove_all();
777 tcg_region_reset_all();
778 /* XXX: flush processor icache at this point if cache flush is expensive */
779 qatomic_inc(&tb_ctx.tb_flush_count);
781 done:
782 mmap_unlock();
783 if (did_flush) {
784 qemu_plugin_flush_cb();
788 void tb_flush(CPUState *cpu)
790 if (tcg_enabled()) {
791 unsigned tb_flush_count = qatomic_read(&tb_ctx.tb_flush_count);
793 if (cpu_in_serial_context(cpu)) {
794 do_tb_flush(cpu, RUN_ON_CPU_HOST_INT(tb_flush_count));
795 } else {
796 async_safe_run_on_cpu(cpu, do_tb_flush,
797 RUN_ON_CPU_HOST_INT(tb_flush_count));
802 /* remove @orig from its @n_orig-th jump list */
803 static inline void tb_remove_from_jmp_list(TranslationBlock *orig, int n_orig)
805 uintptr_t ptr, ptr_locked;
806 TranslationBlock *dest;
807 TranslationBlock *tb;
808 uintptr_t *pprev;
809 int n;
811 /* mark the LSB of jmp_dest[] so that no further jumps can be inserted */
812 ptr = qatomic_or_fetch(&orig->jmp_dest[n_orig], 1);
813 dest = (TranslationBlock *)(ptr & ~1);
814 if (dest == NULL) {
815 return;
818 qemu_spin_lock(&dest->jmp_lock);
820 * While acquiring the lock, the jump might have been removed if the
821 * destination TB was invalidated; check again.
823 ptr_locked = qatomic_read(&orig->jmp_dest[n_orig]);
824 if (ptr_locked != ptr) {
825 qemu_spin_unlock(&dest->jmp_lock);
827 * The only possibility is that the jump was unlinked via
828 * tb_jump_unlink(dest). Seeing here another destination would be a bug,
829 * because we set the LSB above.
831 g_assert(ptr_locked == 1 && dest->cflags & CF_INVALID);
832 return;
835 * We first acquired the lock, and since the destination pointer matches,
836 * we know for sure that @orig is in the jmp list.
838 pprev = &dest->jmp_list_head;
839 TB_FOR_EACH_JMP(dest, tb, n) {
840 if (tb == orig && n == n_orig) {
841 *pprev = tb->jmp_list_next[n];
842 /* no need to set orig->jmp_dest[n]; setting the LSB was enough */
843 qemu_spin_unlock(&dest->jmp_lock);
844 return;
846 pprev = &tb->jmp_list_next[n];
848 g_assert_not_reached();
852 * Reset the jump entry 'n' of a TB so that it is not chained to another TB.
854 void tb_reset_jump(TranslationBlock *tb, int n)
856 uintptr_t addr = (uintptr_t)(tb->tc.ptr + tb->jmp_reset_offset[n]);
857 tb_set_jmp_target(tb, n, addr);
860 /* remove any jumps to the TB */
861 static inline void tb_jmp_unlink(TranslationBlock *dest)
863 TranslationBlock *tb;
864 int n;
866 qemu_spin_lock(&dest->jmp_lock);
868 TB_FOR_EACH_JMP(dest, tb, n) {
869 tb_reset_jump(tb, n);
870 qatomic_and(&tb->jmp_dest[n], (uintptr_t)NULL | 1);
871 /* No need to clear the list entry; setting the dest ptr is enough */
873 dest->jmp_list_head = (uintptr_t)NULL;
875 qemu_spin_unlock(&dest->jmp_lock);
878 static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
880 CPUState *cpu;
882 if (tb_cflags(tb) & CF_PCREL) {
883 /* A TB may be at any virtual address */
884 CPU_FOREACH(cpu) {
885 tcg_flush_jmp_cache(cpu);
887 } else {
888 uint32_t h = tb_jmp_cache_hash_func(tb->pc);
890 CPU_FOREACH(cpu) {
891 CPUJumpCache *jc = cpu->tb_jmp_cache;
893 if (qatomic_read(&jc->array[h].tb) == tb) {
894 qatomic_set(&jc->array[h].tb, NULL);
901 * In user-mode, call with mmap_lock held.
902 * In !user-mode, if @rm_from_page_list is set, call with the TB's pages'
903 * locks held.
905 static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
907 uint32_t h;
908 tb_page_addr_t phys_pc;
909 uint32_t orig_cflags = tb_cflags(tb);
911 assert_memory_lock();
913 /* make sure no further incoming jumps will be chained to this TB */
914 qemu_spin_lock(&tb->jmp_lock);
915 qatomic_set(&tb->cflags, tb->cflags | CF_INVALID);
916 qemu_spin_unlock(&tb->jmp_lock);
918 /* remove the TB from the hash list */
919 phys_pc = tb_page_addr0(tb);
920 h = tb_hash_func(phys_pc, (orig_cflags & CF_PCREL ? 0 : tb->pc),
921 tb->flags, tb->cs_base, orig_cflags);
922 if (!qht_remove(&tb_ctx.htable, tb, h)) {
923 return;
926 /* remove the TB from the page list */
927 if (rm_from_page_list) {
928 tb_remove(tb);
931 /* remove the TB from the hash list */
932 tb_jmp_cache_inval_tb(tb);
934 /* suppress this TB from the two jump lists */
935 tb_remove_from_jmp_list(tb, 0);
936 tb_remove_from_jmp_list(tb, 1);
938 /* suppress any remaining jumps to this TB */
939 tb_jmp_unlink(tb);
941 qatomic_set(&tb_ctx.tb_phys_invalidate_count,
942 tb_ctx.tb_phys_invalidate_count + 1);
945 static void tb_phys_invalidate__locked(TranslationBlock *tb)
947 qemu_thread_jit_write();
948 do_tb_phys_invalidate(tb, true);
949 qemu_thread_jit_execute();
953 * Invalidate one TB.
954 * Called with mmap_lock held in user-mode.
956 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
958 if (page_addr == -1 && tb_page_addr0(tb) != -1) {
959 tb_lock_pages(tb);
960 do_tb_phys_invalidate(tb, true);
961 tb_unlock_pages(tb);
962 } else {
963 do_tb_phys_invalidate(tb, false);
968 * Add a new TB and link it to the physical page tables.
969 * Called with mmap_lock held for user-mode emulation.
971 * Returns a pointer @tb, or a pointer to an existing TB that matches @tb.
972 * Note that in !user-mode, another thread might have already added a TB
973 * for the same block of guest code that @tb corresponds to. In that case,
974 * the caller should discard the original @tb, and use instead the returned TB.
976 TranslationBlock *tb_link_page(TranslationBlock *tb)
978 void *existing_tb = NULL;
979 uint32_t h;
981 assert_memory_lock();
982 tcg_debug_assert(!(tb->cflags & CF_INVALID));
984 tb_record(tb);
986 /* add in the hash table */
987 h = tb_hash_func(tb_page_addr0(tb), (tb->cflags & CF_PCREL ? 0 : tb->pc),
988 tb->flags, tb->cs_base, tb->cflags);
989 qht_insert(&tb_ctx.htable, tb, h, &existing_tb);
991 /* remove TB from the page(s) if we couldn't insert it */
992 if (unlikely(existing_tb)) {
993 tb_remove(tb);
994 tb_unlock_pages(tb);
995 return existing_tb;
998 tb_unlock_pages(tb);
999 return tb;
1002 #ifdef CONFIG_USER_ONLY
1004 * Invalidate all TBs which intersect with the target address range.
1005 * Called with mmap_lock held for user-mode emulation.
1006 * NOTE: this function must not be called while a TB is running.
1008 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
1010 TranslationBlock *tb;
1011 PageForEachNext n;
1013 assert_memory_lock();
1015 PAGE_FOR_EACH_TB(start, last, unused, tb, n) {
1016 tb_phys_invalidate__locked(tb);
1021 * Invalidate all TBs which intersect with the target address page @addr.
1022 * Called with mmap_lock held for user-mode emulation
1023 * NOTE: this function must not be called while a TB is running.
1025 static void tb_invalidate_phys_page(tb_page_addr_t addr)
1027 tb_page_addr_t start, last;
1029 start = addr & TARGET_PAGE_MASK;
1030 last = addr | ~TARGET_PAGE_MASK;
1031 tb_invalidate_phys_range(start, last);
1035 * Called with mmap_lock held. If pc is not 0 then it indicates the
1036 * host PC of the faulting store instruction that caused this invalidate.
1037 * Returns true if the caller needs to abort execution of the current
1038 * TB (because it was modified by this store and the guest CPU has
1039 * precise-SMC semantics).
1041 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
1043 TranslationBlock *current_tb;
1044 bool current_tb_modified;
1045 TranslationBlock *tb;
1046 PageForEachNext n;
1047 tb_page_addr_t last;
1050 * Without precise smc semantics, or when outside of a TB,
1051 * we can skip to invalidate.
1053 #ifndef TARGET_HAS_PRECISE_SMC
1054 pc = 0;
1055 #endif
1056 if (!pc) {
1057 tb_invalidate_phys_page(addr);
1058 return false;
1061 assert_memory_lock();
1062 current_tb = tcg_tb_lookup(pc);
1064 last = addr | ~TARGET_PAGE_MASK;
1065 addr &= TARGET_PAGE_MASK;
1066 current_tb_modified = false;
1068 PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {
1069 if (current_tb == tb &&
1070 (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
1072 * If we are modifying the current TB, we must stop its
1073 * execution. We could be more precise by checking that
1074 * the modification is after the current PC, but it would
1075 * require a specialized function to partially restore
1076 * the CPU state.
1078 current_tb_modified = true;
1079 cpu_restore_state_from_tb(current_cpu, current_tb, pc);
1081 tb_phys_invalidate__locked(tb);
1084 if (current_tb_modified) {
1085 /* Force execution of one insn next time. */
1086 CPUState *cpu = current_cpu;
1087 cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
1088 return true;
1090 return false;
1092 #else
1094 * @p must be non-NULL.
1095 * Call with all @pages locked.
1097 static void
1098 tb_invalidate_phys_page_range__locked(struct page_collection *pages,
1099 PageDesc *p, tb_page_addr_t start,
1100 tb_page_addr_t last,
1101 uintptr_t retaddr)
1103 TranslationBlock *tb;
1104 PageForEachNext n;
1105 #ifdef TARGET_HAS_PRECISE_SMC
1106 bool current_tb_modified = false;
1107 TranslationBlock *current_tb = retaddr ? tcg_tb_lookup(retaddr) : NULL;
1108 #endif /* TARGET_HAS_PRECISE_SMC */
1110 /* Range may not cross a page. */
1111 tcg_debug_assert(((start ^ last) & TARGET_PAGE_MASK) == 0);
1114 * We remove all the TBs in the range [start, last].
1115 * XXX: see if in some cases it could be faster to invalidate all the code
1117 PAGE_FOR_EACH_TB(start, last, p, tb, n) {
1118 tb_page_addr_t tb_start, tb_last;
1120 /* NOTE: this is subtle as a TB may span two physical pages */
1121 tb_start = tb_page_addr0(tb);
1122 tb_last = tb_start + tb->size - 1;
1123 if (n == 0) {
1124 tb_last = MIN(tb_last, tb_start | ~TARGET_PAGE_MASK);
1125 } else {
1126 tb_start = tb_page_addr1(tb);
1127 tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK);
1129 if (!(tb_last < start || tb_start > last)) {
1130 #ifdef TARGET_HAS_PRECISE_SMC
1131 if (current_tb == tb &&
1132 (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
1134 * If we are modifying the current TB, we must stop
1135 * its execution. We could be more precise by checking
1136 * that the modification is after the current PC, but it
1137 * would require a specialized function to partially
1138 * restore the CPU state.
1140 current_tb_modified = true;
1141 cpu_restore_state_from_tb(current_cpu, current_tb, retaddr);
1143 #endif /* TARGET_HAS_PRECISE_SMC */
1144 tb_phys_invalidate__locked(tb);
1148 /* if no code remaining, no need to continue to use slow writes */
1149 if (!p->first_tb) {
1150 tlb_unprotect_code(start);
1153 #ifdef TARGET_HAS_PRECISE_SMC
1154 if (current_tb_modified) {
1155 page_collection_unlock(pages);
1156 /* Force execution of one insn next time. */
1157 current_cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
1158 mmap_unlock();
1159 cpu_loop_exit_noexc(current_cpu);
1161 #endif
1165 * Invalidate all TBs which intersect with the target physical address range
1166 * [start;last]. NOTE: start and end may refer to *different* physical pages.
1167 * 'is_cpu_write_access' should be true if called from a real cpu write
1168 * access: the virtual CPU will exit the current TB if code is modified inside
1169 * this TB.
1171 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
1173 struct page_collection *pages;
1174 tb_page_addr_t index, index_last;
1176 pages = page_collection_lock(start, last);
1178 index_last = last >> TARGET_PAGE_BITS;
1179 for (index = start >> TARGET_PAGE_BITS; index <= index_last; index++) {
1180 PageDesc *pd = page_find(index);
1181 tb_page_addr_t page_start, page_last;
1183 if (pd == NULL) {
1184 continue;
1186 assert_page_locked(pd);
1187 page_start = index << TARGET_PAGE_BITS;
1188 page_last = page_start | ~TARGET_PAGE_MASK;
1189 page_last = MIN(page_last, last);
1190 tb_invalidate_phys_page_range__locked(pages, pd,
1191 page_start, page_last, 0);
1193 page_collection_unlock(pages);
1197 * Call with all @pages in the range [@start, @start + len[ locked.
1199 static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages,
1200 tb_page_addr_t start,
1201 unsigned len, uintptr_t ra)
1203 PageDesc *p;
1205 p = page_find(start >> TARGET_PAGE_BITS);
1206 if (!p) {
1207 return;
1210 assert_page_locked(p);
1211 tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1, ra);
1215 * len must be <= 8 and start must be a multiple of len.
1216 * Called via softmmu_template.h when code areas are written to with
1217 * iothread mutex not held.
1219 void tb_invalidate_phys_range_fast(ram_addr_t ram_addr,
1220 unsigned size,
1221 uintptr_t retaddr)
1223 struct page_collection *pages;
1225 pages = page_collection_lock(ram_addr, ram_addr + size - 1);
1226 tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr);
1227 page_collection_unlock(pages);
1230 #endif /* CONFIG_USER_ONLY */