xref: /qemu/accel/tcg/tb-maint.c (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
1 /*
2  * Translation Block Maintenance
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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 "tb-internal.h"
29 #include "system/tcg.h"
30 #include "tcg/tcg.h"
31 #include "tb-hash.h"
32 #include "tb-context.h"
33 #include "tb-internal.h"
34 #include "internal-common.h"
35 #include "internal-target.h"
36 #ifdef CONFIG_USER_ONLY
37 #include "user/page-protection.h"
38 #endif
39 
40 
41 /* List iterators for lists of tagged pointers in TranslationBlock. */
42 #define TB_FOR_EACH_TAGGED(head, tb, n, field)                          \
43     for (n = (head) & 1, tb = (TranslationBlock *)((head) & ~1);        \
44          tb; tb = (TranslationBlock *)tb->field[n], n = (uintptr_t)tb & 1, \
45              tb = (TranslationBlock *)((uintptr_t)tb & ~1))
46 
47 #define TB_FOR_EACH_JMP(head_tb, tb, n)                                 \
48     TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
49 
50 static bool tb_cmp(const void *ap, const void *bp)
51 {
52     const TranslationBlock *a = ap;
53     const TranslationBlock *b = bp;
54 
55     return ((tb_cflags(a) & CF_PCREL || a->pc == b->pc) &&
56             a->cs_base == b->cs_base &&
57             a->flags == b->flags &&
58             (tb_cflags(a) & ~CF_INVALID) == (tb_cflags(b) & ~CF_INVALID) &&
59             tb_page_addr0(a) == tb_page_addr0(b) &&
60             tb_page_addr1(a) == tb_page_addr1(b));
61 }
62 
63 void tb_htable_init(void)
64 {
65     unsigned int mode = QHT_MODE_AUTO_RESIZE;
66 
67     qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
68 }
69 
70 typedef struct PageDesc PageDesc;
71 
72 #ifdef CONFIG_USER_ONLY
73 
74 /*
75  * In user-mode page locks aren't used; mmap_lock is enough.
76  */
77 #define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
78 
79 static inline void tb_lock_pages(const TranslationBlock *tb) { }
80 
81 /*
82  * For user-only, since we are protecting all of memory with a single lock,
83  * and because the two pages of a TranslationBlock are always contiguous,
84  * use a single data structure to record all TranslationBlocks.
85  */
86 static IntervalTreeRoot tb_root;
87 
88 static void tb_remove_all(void)
89 {
90     assert_memory_lock();
91     memset(&tb_root, 0, sizeof(tb_root));
92 }
93 
94 /* Call with mmap_lock held. */
95 static void tb_record(TranslationBlock *tb)
96 {
97     vaddr addr;
98     int flags;
99 
100     assert_memory_lock();
101     tb->itree.last = tb->itree.start + tb->size - 1;
102 
103     /* translator_loop() must have made all TB pages non-writable */
104     addr = tb_page_addr0(tb);
105     flags = page_get_flags(addr);
106     assert(!(flags & PAGE_WRITE));
107 
108     addr = tb_page_addr1(tb);
109     if (addr != -1) {
110         flags = page_get_flags(addr);
111         assert(!(flags & PAGE_WRITE));
112     }
113 
114     interval_tree_insert(&tb->itree, &tb_root);
115 }
116 
117 /* Call with mmap_lock held. */
118 static void tb_remove(TranslationBlock *tb)
119 {
120     assert_memory_lock();
121     interval_tree_remove(&tb->itree, &tb_root);
122 }
123 
124 /* TODO: For now, still shared with translate-all.c for system mode. */
125 #define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N)   \
126     for (T = foreach_tb_first(start, last),             \
127          N = foreach_tb_next(T, start, last);           \
128          T != NULL;                                     \
129          T = N, N = foreach_tb_next(N, start, last))
130 
131 typedef TranslationBlock *PageForEachNext;
132 
133 static PageForEachNext foreach_tb_first(tb_page_addr_t start,
134                                         tb_page_addr_t last)
135 {
136     IntervalTreeNode *n = interval_tree_iter_first(&tb_root, start, last);
137     return n ? container_of(n, TranslationBlock, itree) : NULL;
138 }
139 
140 static PageForEachNext foreach_tb_next(PageForEachNext tb,
141                                        tb_page_addr_t start,
142                                        tb_page_addr_t last)
143 {
144     IntervalTreeNode *n;
145 
146     if (tb) {
147         n = interval_tree_iter_next(&tb->itree, start, last);
148         if (n) {
149             return container_of(n, TranslationBlock, itree);
150         }
151     }
152     return NULL;
153 }
154 
155 #else
156 /*
157  * In system mode we want L1_MAP to be based on ram offsets.
158  */
159 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
160 # define L1_MAP_ADDR_SPACE_BITS  HOST_LONG_BITS
161 #else
162 # define L1_MAP_ADDR_SPACE_BITS  TARGET_PHYS_ADDR_SPACE_BITS
163 #endif
164 
165 /* Size of the L2 (and L3, etc) page tables.  */
166 #define V_L2_BITS 10
167 #define V_L2_SIZE (1 << V_L2_BITS)
168 
169 /*
170  * L1 Mapping properties
171  */
172 static int v_l1_size;
173 static int v_l1_shift;
174 static int v_l2_levels;
175 
176 /*
177  * The bottom level has pointers to PageDesc, and is indexed by
178  * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
179  */
180 #define V_L1_MIN_BITS 4
181 #define V_L1_MAX_BITS (V_L2_BITS + 3)
182 #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
183 
184 static void *l1_map[V_L1_MAX_SIZE];
185 
186 struct PageDesc {
187     QemuSpin lock;
188     /* list of TBs intersecting this ram page */
189     uintptr_t first_tb;
190 };
191 
192 void page_table_config_init(void)
193 {
194     uint32_t v_l1_bits;
195 
196     assert(TARGET_PAGE_BITS);
197     /* The bits remaining after N lower levels of page tables.  */
198     v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
199     if (v_l1_bits < V_L1_MIN_BITS) {
200         v_l1_bits += V_L2_BITS;
201     }
202 
203     v_l1_size = 1 << v_l1_bits;
204     v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
205     v_l2_levels = v_l1_shift / V_L2_BITS - 1;
206 
207     assert(v_l1_bits <= V_L1_MAX_BITS);
208     assert(v_l1_shift % V_L2_BITS == 0);
209     assert(v_l2_levels >= 0);
210 }
211 
212 static PageDesc *page_find_alloc(tb_page_addr_t index, bool alloc)
213 {
214     PageDesc *pd;
215     void **lp;
216 
217     /* Level 1.  Always allocated.  */
218     lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
219 
220     /* Level 2..N-1.  */
221     for (int i = v_l2_levels; i > 0; i--) {
222         void **p = qatomic_rcu_read(lp);
223 
224         if (p == NULL) {
225             void *existing;
226 
227             if (!alloc) {
228                 return NULL;
229             }
230             p = g_new0(void *, V_L2_SIZE);
231             existing = qatomic_cmpxchg(lp, NULL, p);
232             if (unlikely(existing)) {
233                 g_free(p);
234                 p = existing;
235             }
236         }
237 
238         lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
239     }
240 
241     pd = qatomic_rcu_read(lp);
242     if (pd == NULL) {
243         void *existing;
244 
245         if (!alloc) {
246             return NULL;
247         }
248 
249         pd = g_new0(PageDesc, V_L2_SIZE);
250         for (int i = 0; i < V_L2_SIZE; i++) {
251             qemu_spin_init(&pd[i].lock);
252         }
253 
254         existing = qatomic_cmpxchg(lp, NULL, pd);
255         if (unlikely(existing)) {
256             for (int i = 0; i < V_L2_SIZE; i++) {
257                 qemu_spin_destroy(&pd[i].lock);
258             }
259             g_free(pd);
260             pd = existing;
261         }
262     }
263 
264     return pd + (index & (V_L2_SIZE - 1));
265 }
266 
267 static inline PageDesc *page_find(tb_page_addr_t index)
268 {
269     return page_find_alloc(index, false);
270 }
271 
272 /**
273  * struct page_entry - page descriptor entry
274  * @pd:     pointer to the &struct PageDesc of the page this entry represents
275  * @index:  page index of the page
276  * @locked: whether the page is locked
277  *
278  * This struct helps us keep track of the locked state of a page, without
279  * bloating &struct PageDesc.
280  *
281  * A page lock protects accesses to all fields of &struct PageDesc.
282  *
283  * See also: &struct page_collection.
284  */
285 struct page_entry {
286     PageDesc *pd;
287     tb_page_addr_t index;
288     bool locked;
289 };
290 
291 /**
292  * struct page_collection - tracks a set of pages (i.e. &struct page_entry's)
293  * @tree:   Binary search tree (BST) of the pages, with key == page index
294  * @max:    Pointer to the page in @tree with the highest page index
295  *
296  * To avoid deadlock we lock pages in ascending order of page index.
297  * When operating on a set of pages, we need to keep track of them so that
298  * we can lock them in order and also unlock them later. For this we collect
299  * pages (i.e. &struct page_entry's) in a binary search @tree. Given that the
300  * @tree implementation we use does not provide an O(1) operation to obtain the
301  * highest-ranked element, we use @max to keep track of the inserted page
302  * with the highest index. This is valuable because if a page is not in
303  * the tree and its index is higher than @max's, then we can lock it
304  * without breaking the locking order rule.
305  *
306  * Note on naming: 'struct page_set' would be shorter, but we already have a few
307  * page_set_*() helpers, so page_collection is used instead to avoid confusion.
308  *
309  * See also: page_collection_lock().
310  */
311 struct page_collection {
312     QTree *tree;
313     struct page_entry *max;
314 };
315 
316 typedef int PageForEachNext;
317 #define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
318     TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)
319 
320 #ifdef CONFIG_DEBUG_TCG
321 
322 static __thread GHashTable *ht_pages_locked_debug;
323 
324 static void ht_pages_locked_debug_init(void)
325 {
326     if (ht_pages_locked_debug) {
327         return;
328     }
329     ht_pages_locked_debug = g_hash_table_new(NULL, NULL);
330 }
331 
332 static bool page_is_locked(const PageDesc *pd)
333 {
334     PageDesc *found;
335 
336     ht_pages_locked_debug_init();
337     found = g_hash_table_lookup(ht_pages_locked_debug, pd);
338     return !!found;
339 }
340 
341 static void page_lock__debug(PageDesc *pd)
342 {
343     ht_pages_locked_debug_init();
344     g_assert(!page_is_locked(pd));
345     g_hash_table_insert(ht_pages_locked_debug, pd, pd);
346 }
347 
348 static void page_unlock__debug(const PageDesc *pd)
349 {
350     bool removed;
351 
352     ht_pages_locked_debug_init();
353     g_assert(page_is_locked(pd));
354     removed = g_hash_table_remove(ht_pages_locked_debug, pd);
355     g_assert(removed);
356 }
357 
358 static void do_assert_page_locked(const PageDesc *pd,
359                                   const char *file, int line)
360 {
361     if (unlikely(!page_is_locked(pd))) {
362         error_report("assert_page_lock: PageDesc %p not locked @ %s:%d",
363                      pd, file, line);
364         abort();
365     }
366 }
367 #define assert_page_locked(pd) do_assert_page_locked(pd, __FILE__, __LINE__)
368 
369 void assert_no_pages_locked(void)
370 {
371     ht_pages_locked_debug_init();
372     g_assert(g_hash_table_size(ht_pages_locked_debug) == 0);
373 }
374 
375 #else /* !CONFIG_DEBUG_TCG */
376 
377 static inline void page_lock__debug(const PageDesc *pd) { }
378 static inline void page_unlock__debug(const PageDesc *pd) { }
379 static inline void assert_page_locked(const PageDesc *pd) { }
380 
381 #endif /* CONFIG_DEBUG_TCG */
382 
383 static void page_lock(PageDesc *pd)
384 {
385     page_lock__debug(pd);
386     qemu_spin_lock(&pd->lock);
387 }
388 
389 /* Like qemu_spin_trylock, returns false on success */
390 static bool page_trylock(PageDesc *pd)
391 {
392     bool busy = qemu_spin_trylock(&pd->lock);
393     if (!busy) {
394         page_lock__debug(pd);
395     }
396     return busy;
397 }
398 
399 static void page_unlock(PageDesc *pd)
400 {
401     qemu_spin_unlock(&pd->lock);
402     page_unlock__debug(pd);
403 }
404 
405 void tb_lock_page0(tb_page_addr_t paddr)
406 {
407     page_lock(page_find_alloc(paddr >> TARGET_PAGE_BITS, true));
408 }
409 
410 void tb_lock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
411 {
412     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
413     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
414     PageDesc *pd0, *pd1;
415 
416     if (pindex0 == pindex1) {
417         /* Identical pages, and the first page is already locked. */
418         return;
419     }
420 
421     pd1 = page_find_alloc(pindex1, true);
422     if (pindex0 < pindex1) {
423         /* Correct locking order, we may block. */
424         page_lock(pd1);
425         return;
426     }
427 
428     /* Incorrect locking order, we cannot block lest we deadlock. */
429     if (!page_trylock(pd1)) {
430         return;
431     }
432 
433     /*
434      * Drop the lock on page0 and get both page locks in the right order.
435      * Restart translation via longjmp.
436      */
437     pd0 = page_find_alloc(pindex0, false);
438     page_unlock(pd0);
439     page_lock(pd1);
440     page_lock(pd0);
441     siglongjmp(tcg_ctx->jmp_trans, -3);
442 }
443 
444 void tb_unlock_page1(tb_page_addr_t paddr0, tb_page_addr_t paddr1)
445 {
446     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
447     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
448 
449     if (pindex0 != pindex1) {
450         page_unlock(page_find_alloc(pindex1, false));
451     }
452 }
453 
454 static void tb_lock_pages(TranslationBlock *tb)
455 {
456     tb_page_addr_t paddr0 = tb_page_addr0(tb);
457     tb_page_addr_t paddr1 = tb_page_addr1(tb);
458     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
459     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
460 
461     if (unlikely(paddr0 == -1)) {
462         return;
463     }
464     if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
465         if (pindex0 < pindex1) {
466             page_lock(page_find_alloc(pindex0, true));
467             page_lock(page_find_alloc(pindex1, true));
468             return;
469         }
470         page_lock(page_find_alloc(pindex1, true));
471     }
472     page_lock(page_find_alloc(pindex0, true));
473 }
474 
475 void tb_unlock_pages(TranslationBlock *tb)
476 {
477     tb_page_addr_t paddr0 = tb_page_addr0(tb);
478     tb_page_addr_t paddr1 = tb_page_addr1(tb);
479     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
480     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
481 
482     if (unlikely(paddr0 == -1)) {
483         return;
484     }
485     if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
486         page_unlock(page_find_alloc(pindex1, false));
487     }
488     page_unlock(page_find_alloc(pindex0, false));
489 }
490 
491 static inline struct page_entry *
492 page_entry_new(PageDesc *pd, tb_page_addr_t index)
493 {
494     struct page_entry *pe = g_malloc(sizeof(*pe));
495 
496     pe->index = index;
497     pe->pd = pd;
498     pe->locked = false;
499     return pe;
500 }
501 
502 static void page_entry_destroy(gpointer p)
503 {
504     struct page_entry *pe = p;
505 
506     g_assert(pe->locked);
507     page_unlock(pe->pd);
508     g_free(pe);
509 }
510 
511 /* returns false on success */
512 static bool page_entry_trylock(struct page_entry *pe)
513 {
514     bool busy = page_trylock(pe->pd);
515     if (!busy) {
516         g_assert(!pe->locked);
517         pe->locked = true;
518     }
519     return busy;
520 }
521 
522 static void do_page_entry_lock(struct page_entry *pe)
523 {
524     page_lock(pe->pd);
525     g_assert(!pe->locked);
526     pe->locked = true;
527 }
528 
529 static gboolean page_entry_lock(gpointer key, gpointer value, gpointer data)
530 {
531     struct page_entry *pe = value;
532 
533     do_page_entry_lock(pe);
534     return FALSE;
535 }
536 
537 static gboolean page_entry_unlock(gpointer key, gpointer value, gpointer data)
538 {
539     struct page_entry *pe = value;
540 
541     if (pe->locked) {
542         pe->locked = false;
543         page_unlock(pe->pd);
544     }
545     return FALSE;
546 }
547 
548 /*
549  * Trylock a page, and if successful, add the page to a collection.
550  * Returns true ("busy") if the page could not be locked; false otherwise.
551  */
552 static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)
553 {
554     tb_page_addr_t index = addr >> TARGET_PAGE_BITS;
555     struct page_entry *pe;
556     PageDesc *pd;
557 
558     pe = q_tree_lookup(set->tree, &index);
559     if (pe) {
560         return false;
561     }
562 
563     pd = page_find(index);
564     if (pd == NULL) {
565         return false;
566     }
567 
568     pe = page_entry_new(pd, index);
569     q_tree_insert(set->tree, &pe->index, pe);
570 
571     /*
572      * If this is either (1) the first insertion or (2) a page whose index
573      * is higher than any other so far, just lock the page and move on.
574      */
575     if (set->max == NULL || pe->index > set->max->index) {
576         set->max = pe;
577         do_page_entry_lock(pe);
578         return false;
579     }
580     /*
581      * Try to acquire out-of-order lock; if busy, return busy so that we acquire
582      * locks in order.
583      */
584     return page_entry_trylock(pe);
585 }
586 
587 static gint tb_page_addr_cmp(gconstpointer ap, gconstpointer bp, gpointer udata)
588 {
589     tb_page_addr_t a = *(const tb_page_addr_t *)ap;
590     tb_page_addr_t b = *(const tb_page_addr_t *)bp;
591 
592     if (a == b) {
593         return 0;
594     } else if (a < b) {
595         return -1;
596     }
597     return 1;
598 }
599 
600 /*
601  * Lock a range of pages ([@start,@last]) as well as the pages of all
602  * intersecting TBs.
603  * Locking order: acquire locks in ascending order of page index.
604  */
605 static struct page_collection *page_collection_lock(tb_page_addr_t start,
606                                                     tb_page_addr_t last)
607 {
608     struct page_collection *set = g_malloc(sizeof(*set));
609     tb_page_addr_t index;
610     PageDesc *pd;
611 
612     start >>= TARGET_PAGE_BITS;
613     last >>= TARGET_PAGE_BITS;
614     g_assert(start <= last);
615 
616     set->tree = q_tree_new_full(tb_page_addr_cmp, NULL, NULL,
617                                 page_entry_destroy);
618     set->max = NULL;
619     assert_no_pages_locked();
620 
621  retry:
622     q_tree_foreach(set->tree, page_entry_lock, NULL);
623 
624     for (index = start; index <= last; index++) {
625         TranslationBlock *tb;
626         PageForEachNext n;
627 
628         pd = page_find(index);
629         if (pd == NULL) {
630             continue;
631         }
632         if (page_trylock_add(set, index << TARGET_PAGE_BITS)) {
633             q_tree_foreach(set->tree, page_entry_unlock, NULL);
634             goto retry;
635         }
636         assert_page_locked(pd);
637         PAGE_FOR_EACH_TB(unused, unused, pd, tb, n) {
638             if (page_trylock_add(set, tb_page_addr0(tb)) ||
639                 (tb_page_addr1(tb) != -1 &&
640                  page_trylock_add(set, tb_page_addr1(tb)))) {
641                 /* drop all locks, and reacquire in order */
642                 q_tree_foreach(set->tree, page_entry_unlock, NULL);
643                 goto retry;
644             }
645         }
646     }
647     return set;
648 }
649 
650 static void page_collection_unlock(struct page_collection *set)
651 {
652     /* entries are unlocked and freed via page_entry_destroy */
653     q_tree_destroy(set->tree);
654     g_free(set);
655 }
656 
657 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
658 static void tb_remove_all_1(int level, void **lp)
659 {
660     int i;
661 
662     if (*lp == NULL) {
663         return;
664     }
665     if (level == 0) {
666         PageDesc *pd = *lp;
667 
668         for (i = 0; i < V_L2_SIZE; ++i) {
669             page_lock(&pd[i]);
670             pd[i].first_tb = (uintptr_t)NULL;
671             page_unlock(&pd[i]);
672         }
673     } else {
674         void **pp = *lp;
675 
676         for (i = 0; i < V_L2_SIZE; ++i) {
677             tb_remove_all_1(level - 1, pp + i);
678         }
679     }
680 }
681 
682 static void tb_remove_all(void)
683 {
684     int i, l1_sz = v_l1_size;
685 
686     for (i = 0; i < l1_sz; i++) {
687         tb_remove_all_1(v_l2_levels, l1_map + i);
688     }
689 }
690 
691 /*
692  * Add the tb in the target page and protect it if necessary.
693  * Called with @p->lock held.
694  */
695 static void tb_page_add(PageDesc *p, TranslationBlock *tb, unsigned int n)
696 {
697     bool page_already_protected;
698 
699     assert_page_locked(p);
700 
701     tb->page_next[n] = p->first_tb;
702     page_already_protected = p->first_tb != 0;
703     p->first_tb = (uintptr_t)tb | n;
704 
705     /*
706      * If some code is already present, then the pages are already
707      * protected. So we handle the case where only the first TB is
708      * allocated in a physical page.
709      */
710     if (!page_already_protected) {
711         tlb_protect_code(tb->page_addr[n] & TARGET_PAGE_MASK);
712     }
713 }
714 
715 static void tb_record(TranslationBlock *tb)
716 {
717     tb_page_addr_t paddr0 = tb_page_addr0(tb);
718     tb_page_addr_t paddr1 = tb_page_addr1(tb);
719     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
720     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
721 
722     assert(paddr0 != -1);
723     if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
724         tb_page_add(page_find_alloc(pindex1, false), tb, 1);
725     }
726     tb_page_add(page_find_alloc(pindex0, false), tb, 0);
727 }
728 
729 static void tb_page_remove(PageDesc *pd, TranslationBlock *tb)
730 {
731     TranslationBlock *tb1;
732     uintptr_t *pprev;
733     PageForEachNext n1;
734 
735     assert_page_locked(pd);
736     pprev = &pd->first_tb;
737     PAGE_FOR_EACH_TB(unused, unused, pd, tb1, n1) {
738         if (tb1 == tb) {
739             *pprev = tb1->page_next[n1];
740             return;
741         }
742         pprev = &tb1->page_next[n1];
743     }
744     g_assert_not_reached();
745 }
746 
747 static void tb_remove(TranslationBlock *tb)
748 {
749     tb_page_addr_t paddr0 = tb_page_addr0(tb);
750     tb_page_addr_t paddr1 = tb_page_addr1(tb);
751     tb_page_addr_t pindex0 = paddr0 >> TARGET_PAGE_BITS;
752     tb_page_addr_t pindex1 = paddr1 >> TARGET_PAGE_BITS;
753 
754     assert(paddr0 != -1);
755     if (unlikely(paddr1 != -1) && pindex0 != pindex1) {
756         tb_page_remove(page_find_alloc(pindex1, false), tb);
757     }
758     tb_page_remove(page_find_alloc(pindex0, false), tb);
759 }
760 #endif /* CONFIG_USER_ONLY */
761 
762 /* flush all the translation blocks */
763 static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
764 {
765     bool did_flush = false;
766 
767     mmap_lock();
768     /* If it is already been done on request of another CPU, just retry. */
769     if (tb_ctx.tb_flush_count != tb_flush_count.host_int) {
770         goto done;
771     }
772     did_flush = true;
773 
774     CPU_FOREACH(cpu) {
775         tcg_flush_jmp_cache(cpu);
776     }
777 
778     qht_reset_size(&tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
779     tb_remove_all();
780 
781     tcg_region_reset_all();
782     /* XXX: flush processor icache at this point if cache flush is expensive */
783     qatomic_inc(&tb_ctx.tb_flush_count);
784 
785 done:
786     mmap_unlock();
787     if (did_flush) {
788         qemu_plugin_flush_cb();
789     }
790 }
791 
792 void tb_flush(CPUState *cpu)
793 {
794     if (tcg_enabled()) {
795         unsigned tb_flush_count = qatomic_read(&tb_ctx.tb_flush_count);
796 
797         if (cpu_in_serial_context(cpu)) {
798             do_tb_flush(cpu, RUN_ON_CPU_HOST_INT(tb_flush_count));
799         } else {
800             async_safe_run_on_cpu(cpu, do_tb_flush,
801                                   RUN_ON_CPU_HOST_INT(tb_flush_count));
802         }
803     }
804 }
805 
806 /* remove @orig from its @n_orig-th jump list */
807 static inline void tb_remove_from_jmp_list(TranslationBlock *orig, int n_orig)
808 {
809     uintptr_t ptr, ptr_locked;
810     TranslationBlock *dest;
811     TranslationBlock *tb;
812     uintptr_t *pprev;
813     int n;
814 
815     /* mark the LSB of jmp_dest[] so that no further jumps can be inserted */
816     ptr = qatomic_or_fetch(&orig->jmp_dest[n_orig], 1);
817     dest = (TranslationBlock *)(ptr & ~1);
818     if (dest == NULL) {
819         return;
820     }
821 
822     qemu_spin_lock(&dest->jmp_lock);
823     /*
824      * While acquiring the lock, the jump might have been removed if the
825      * destination TB was invalidated; check again.
826      */
827     ptr_locked = qatomic_read(&orig->jmp_dest[n_orig]);
828     if (ptr_locked != ptr) {
829         qemu_spin_unlock(&dest->jmp_lock);
830         /*
831          * The only possibility is that the jump was unlinked via
832          * tb_jump_unlink(dest). Seeing here another destination would be a bug,
833          * because we set the LSB above.
834          */
835         g_assert(ptr_locked == 1 && dest->cflags & CF_INVALID);
836         return;
837     }
838     /*
839      * We first acquired the lock, and since the destination pointer matches,
840      * we know for sure that @orig is in the jmp list.
841      */
842     pprev = &dest->jmp_list_head;
843     TB_FOR_EACH_JMP(dest, tb, n) {
844         if (tb == orig && n == n_orig) {
845             *pprev = tb->jmp_list_next[n];
846             /* no need to set orig->jmp_dest[n]; setting the LSB was enough */
847             qemu_spin_unlock(&dest->jmp_lock);
848             return;
849         }
850         pprev = &tb->jmp_list_next[n];
851     }
852     g_assert_not_reached();
853 }
854 
855 /*
856  * Reset the jump entry 'n' of a TB so that it is not chained to another TB.
857  */
858 void tb_reset_jump(TranslationBlock *tb, int n)
859 {
860     uintptr_t addr = (uintptr_t)(tb->tc.ptr + tb->jmp_reset_offset[n]);
861     tb_set_jmp_target(tb, n, addr);
862 }
863 
864 /* remove any jumps to the TB */
865 static inline void tb_jmp_unlink(TranslationBlock *dest)
866 {
867     TranslationBlock *tb;
868     int n;
869 
870     qemu_spin_lock(&dest->jmp_lock);
871 
872     TB_FOR_EACH_JMP(dest, tb, n) {
873         tb_reset_jump(tb, n);
874         qatomic_and(&tb->jmp_dest[n], (uintptr_t)NULL | 1);
875         /* No need to clear the list entry; setting the dest ptr is enough */
876     }
877     dest->jmp_list_head = (uintptr_t)NULL;
878 
879     qemu_spin_unlock(&dest->jmp_lock);
880 }
881 
882 static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
883 {
884     CPUState *cpu;
885 
886     if (tb_cflags(tb) & CF_PCREL) {
887         /* A TB may be at any virtual address */
888         CPU_FOREACH(cpu) {
889             tcg_flush_jmp_cache(cpu);
890         }
891     } else {
892         uint32_t h = tb_jmp_cache_hash_func(tb->pc);
893 
894         CPU_FOREACH(cpu) {
895             CPUJumpCache *jc = cpu->tb_jmp_cache;
896 
897             if (qatomic_read(&jc->array[h].tb) == tb) {
898                 qatomic_set(&jc->array[h].tb, NULL);
899             }
900         }
901     }
902 }
903 
904 /*
905  * In user-mode, call with mmap_lock held.
906  * In !user-mode, if @rm_from_page_list is set, call with the TB's pages'
907  * locks held.
908  */
909 static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
910 {
911     uint32_t h;
912     tb_page_addr_t phys_pc;
913     uint32_t orig_cflags = tb_cflags(tb);
914 
915     assert_memory_lock();
916 
917     /* make sure no further incoming jumps will be chained to this TB */
918     qemu_spin_lock(&tb->jmp_lock);
919     qatomic_set(&tb->cflags, tb->cflags | CF_INVALID);
920     qemu_spin_unlock(&tb->jmp_lock);
921 
922     /* remove the TB from the hash list */
923     phys_pc = tb_page_addr0(tb);
924     h = tb_hash_func(phys_pc, (orig_cflags & CF_PCREL ? 0 : tb->pc),
925                      tb->flags, tb->cs_base, orig_cflags);
926     if (!qht_remove(&tb_ctx.htable, tb, h)) {
927         return;
928     }
929 
930     /* remove the TB from the page list */
931     if (rm_from_page_list) {
932         tb_remove(tb);
933     }
934 
935     /* remove the TB from the hash list */
936     tb_jmp_cache_inval_tb(tb);
937 
938     /* suppress this TB from the two jump lists */
939     tb_remove_from_jmp_list(tb, 0);
940     tb_remove_from_jmp_list(tb, 1);
941 
942     /* suppress any remaining jumps to this TB */
943     tb_jmp_unlink(tb);
944 
945     qatomic_set(&tb_ctx.tb_phys_invalidate_count,
946                 tb_ctx.tb_phys_invalidate_count + 1);
947 }
948 
949 static void tb_phys_invalidate__locked(TranslationBlock *tb)
950 {
951     qemu_thread_jit_write();
952     do_tb_phys_invalidate(tb, true);
953     qemu_thread_jit_execute();
954 }
955 
956 /*
957  * Invalidate one TB.
958  * Called with mmap_lock held in user-mode.
959  */
960 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
961 {
962     if (page_addr == -1 && tb_page_addr0(tb) != -1) {
963         tb_lock_pages(tb);
964         do_tb_phys_invalidate(tb, true);
965         tb_unlock_pages(tb);
966     } else {
967         do_tb_phys_invalidate(tb, false);
968     }
969 }
970 
971 /*
972  * Add a new TB and link it to the physical page tables.
973  * Called with mmap_lock held for user-mode emulation.
974  *
975  * Returns a pointer @tb, or a pointer to an existing TB that matches @tb.
976  * Note that in !user-mode, another thread might have already added a TB
977  * for the same block of guest code that @tb corresponds to. In that case,
978  * the caller should discard the original @tb, and use instead the returned TB.
979  */
980 TranslationBlock *tb_link_page(TranslationBlock *tb)
981 {
982     void *existing_tb = NULL;
983     uint32_t h;
984 
985     assert_memory_lock();
986     tcg_debug_assert(!(tb->cflags & CF_INVALID));
987 
988     tb_record(tb);
989 
990     /* add in the hash table */
991     h = tb_hash_func(tb_page_addr0(tb), (tb->cflags & CF_PCREL ? 0 : tb->pc),
992                      tb->flags, tb->cs_base, tb->cflags);
993     qht_insert(&tb_ctx.htable, tb, h, &existing_tb);
994 
995     /* remove TB from the page(s) if we couldn't insert it */
996     if (unlikely(existing_tb)) {
997         tb_remove(tb);
998         tb_unlock_pages(tb);
999         return existing_tb;
1000     }
1001 
1002     tb_unlock_pages(tb);
1003     return tb;
1004 }
1005 
1006 #ifdef CONFIG_USER_ONLY
1007 /*
1008  * Invalidate all TBs which intersect with the target address range.
1009  * Called with mmap_lock held for user-mode emulation.
1010  * NOTE: this function must not be called while a TB is running.
1011  */
1012 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
1013 {
1014     TranslationBlock *tb;
1015     PageForEachNext n;
1016 
1017     assert_memory_lock();
1018 
1019     PAGE_FOR_EACH_TB(start, last, unused, tb, n) {
1020         tb_phys_invalidate__locked(tb);
1021     }
1022 }
1023 
1024 /*
1025  * Invalidate all TBs which intersect with the target address page @addr.
1026  * Called with mmap_lock held for user-mode emulation
1027  * NOTE: this function must not be called while a TB is running.
1028  */
1029 static void tb_invalidate_phys_page(tb_page_addr_t addr)
1030 {
1031     tb_page_addr_t start, last;
1032 
1033     start = addr & TARGET_PAGE_MASK;
1034     last = addr | ~TARGET_PAGE_MASK;
1035     tb_invalidate_phys_range(start, last);
1036 }
1037 
1038 /*
1039  * Called with mmap_lock held. If pc is not 0 then it indicates the
1040  * host PC of the faulting store instruction that caused this invalidate.
1041  * Returns true if the caller needs to abort execution of the current
1042  * TB (because it was modified by this store and the guest CPU has
1043  * precise-SMC semantics).
1044  */
1045 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
1046 {
1047     TranslationBlock *current_tb;
1048     bool current_tb_modified;
1049     TranslationBlock *tb;
1050     PageForEachNext n;
1051     tb_page_addr_t last;
1052 
1053     /*
1054      * Without precise smc semantics, or when outside of a TB,
1055      * we can skip to invalidate.
1056      */
1057 #ifndef TARGET_HAS_PRECISE_SMC
1058     pc = 0;
1059 #endif
1060     if (!pc) {
1061         tb_invalidate_phys_page(addr);
1062         return false;
1063     }
1064 
1065     assert_memory_lock();
1066     current_tb = tcg_tb_lookup(pc);
1067 
1068     last = addr | ~TARGET_PAGE_MASK;
1069     addr &= TARGET_PAGE_MASK;
1070     current_tb_modified = false;
1071 
1072     PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {
1073         if (current_tb == tb &&
1074             (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
1075             /*
1076              * If we are modifying the current TB, we must stop its
1077              * execution. We could be more precise by checking that
1078              * the modification is after the current PC, but it would
1079              * require a specialized function to partially restore
1080              * the CPU state.
1081              */
1082             current_tb_modified = true;
1083             cpu_restore_state_from_tb(current_cpu, current_tb, pc);
1084         }
1085         tb_phys_invalidate__locked(tb);
1086     }
1087 
1088     if (current_tb_modified) {
1089         /* Force execution of one insn next time.  */
1090         CPUState *cpu = current_cpu;
1091         cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
1092         return true;
1093     }
1094     return false;
1095 }
1096 #else
1097 /*
1098  * @p must be non-NULL.
1099  * Call with all @pages locked.
1100  */
1101 static void
1102 tb_invalidate_phys_page_range__locked(struct page_collection *pages,
1103                                       PageDesc *p, tb_page_addr_t start,
1104                                       tb_page_addr_t last,
1105                                       uintptr_t retaddr)
1106 {
1107     TranslationBlock *tb;
1108     PageForEachNext n;
1109 #ifdef TARGET_HAS_PRECISE_SMC
1110     bool current_tb_modified = false;
1111     TranslationBlock *current_tb = retaddr ? tcg_tb_lookup(retaddr) : NULL;
1112 #endif /* TARGET_HAS_PRECISE_SMC */
1113 
1114     /* Range may not cross a page. */
1115     tcg_debug_assert(((start ^ last) & TARGET_PAGE_MASK) == 0);
1116 
1117     /*
1118      * We remove all the TBs in the range [start, last].
1119      * XXX: see if in some cases it could be faster to invalidate all the code
1120      */
1121     PAGE_FOR_EACH_TB(start, last, p, tb, n) {
1122         tb_page_addr_t tb_start, tb_last;
1123 
1124         /* NOTE: this is subtle as a TB may span two physical pages */
1125         tb_start = tb_page_addr0(tb);
1126         tb_last = tb_start + tb->size - 1;
1127         if (n == 0) {
1128             tb_last = MIN(tb_last, tb_start | ~TARGET_PAGE_MASK);
1129         } else {
1130             tb_start = tb_page_addr1(tb);
1131             tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK);
1132         }
1133         if (!(tb_last < start || tb_start > last)) {
1134 #ifdef TARGET_HAS_PRECISE_SMC
1135             if (current_tb == tb &&
1136                 (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
1137                 /*
1138                  * If we are modifying the current TB, we must stop
1139                  * its execution. We could be more precise by checking
1140                  * that the modification is after the current PC, but it
1141                  * would require a specialized function to partially
1142                  * restore the CPU state.
1143                  */
1144                 current_tb_modified = true;
1145                 cpu_restore_state_from_tb(current_cpu, current_tb, retaddr);
1146             }
1147 #endif /* TARGET_HAS_PRECISE_SMC */
1148             tb_phys_invalidate__locked(tb);
1149         }
1150     }
1151 
1152     /* if no code remaining, no need to continue to use slow writes */
1153     if (!p->first_tb) {
1154         tlb_unprotect_code(start);
1155     }
1156 
1157 #ifdef TARGET_HAS_PRECISE_SMC
1158     if (current_tb_modified) {
1159         page_collection_unlock(pages);
1160         /* Force execution of one insn next time.  */
1161         current_cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
1162         mmap_unlock();
1163         cpu_loop_exit_noexc(current_cpu);
1164     }
1165 #endif
1166 }
1167 
1168 /*
1169  * Invalidate all TBs which intersect with the target physical address range
1170  * [start;last]. NOTE: start and end may refer to *different* physical pages.
1171  * 'is_cpu_write_access' should be true if called from a real cpu write
1172  * access: the virtual CPU will exit the current TB if code is modified inside
1173  * this TB.
1174  */
1175 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last)
1176 {
1177     struct page_collection *pages;
1178     tb_page_addr_t index, index_last;
1179 
1180     pages = page_collection_lock(start, last);
1181 
1182     index_last = last >> TARGET_PAGE_BITS;
1183     for (index = start >> TARGET_PAGE_BITS; index <= index_last; index++) {
1184         PageDesc *pd = page_find(index);
1185         tb_page_addr_t page_start, page_last;
1186 
1187         if (pd == NULL) {
1188             continue;
1189         }
1190         assert_page_locked(pd);
1191         page_start = index << TARGET_PAGE_BITS;
1192         page_last = page_start | ~TARGET_PAGE_MASK;
1193         page_last = MIN(page_last, last);
1194         tb_invalidate_phys_page_range__locked(pages, pd,
1195                                               page_start, page_last, 0);
1196     }
1197     page_collection_unlock(pages);
1198 }
1199 
1200 /*
1201  * Call with all @pages in the range [@start, @start + len[ locked.
1202  */
1203 static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages,
1204                                                  tb_page_addr_t start,
1205                                                  unsigned len, uintptr_t ra)
1206 {
1207     PageDesc *p;
1208 
1209     p = page_find(start >> TARGET_PAGE_BITS);
1210     if (!p) {
1211         return;
1212     }
1213 
1214     assert_page_locked(p);
1215     tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1, ra);
1216 }
1217 
1218 /*
1219  * len must be <= 8 and start must be a multiple of len.
1220  * Called via softmmu_template.h when code areas are written to with
1221  * iothread mutex not held.
1222  */
1223 void tb_invalidate_phys_range_fast(ram_addr_t ram_addr,
1224                                    unsigned size,
1225                                    uintptr_t retaddr)
1226 {
1227     struct page_collection *pages;
1228 
1229     pages = page_collection_lock(ram_addr, ram_addr + size - 1);
1230     tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr);
1231     page_collection_unlock(pages);
1232 }
1233 
1234 #endif /* CONFIG_USER_ONLY */
1235