2 * Translation Block Maintaince
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"
25 #include "exec/exec-all.h"
26 #include "exec/tb-flush.h"
27 #include "exec/translate-all.h"
28 #include "sysemu/tcg.h"
31 #include "tb-context.h"
35 /* List iterators for lists of tagged pointers in TranslationBlock. */
36 #define TB_FOR_EACH_TAGGED(head, tb, n, field) \
37 for (n = (head) & 1, tb = (TranslationBlock *)((head) & ~1); \
38 tb; tb = (TranslationBlock *)tb->field[n], n = (uintptr_t)tb & 1, \
39 tb = (TranslationBlock *)((uintptr_t)tb & ~1))
41 #define TB_FOR_EACH_JMP(head_tb, tb, n) \
42 TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
44 static bool tb_cmp(const void *ap
, const void *bp
)
46 const TranslationBlock
*a
= ap
;
47 const TranslationBlock
*b
= bp
;
49 return ((tb_cflags(a
) & CF_PCREL
|| a
->pc
== b
->pc
) &&
50 a
->cs_base
== b
->cs_base
&&
51 a
->flags
== b
->flags
&&
52 (tb_cflags(a
) & ~CF_INVALID
) == (tb_cflags(b
) & ~CF_INVALID
) &&
53 a
->trace_vcpu_dstate
== b
->trace_vcpu_dstate
&&
54 tb_page_addr0(a
) == tb_page_addr0(b
) &&
55 tb_page_addr1(a
) == tb_page_addr1(b
));
58 void tb_htable_init(void)
60 unsigned int mode
= QHT_MODE_AUTO_RESIZE
;
62 qht_init(&tb_ctx
.htable
, tb_cmp
, CODE_GEN_HTABLE_SIZE
, mode
);
65 typedef struct PageDesc PageDesc
;
67 #ifdef CONFIG_USER_ONLY
70 * In user-mode page locks aren't used; mmap_lock is enough.
72 #define assert_page_locked(pd) tcg_debug_assert(have_mmap_lock())
74 static inline void page_lock_pair(PageDesc
**ret_p1
, tb_page_addr_t phys1
,
75 PageDesc
**ret_p2
, tb_page_addr_t phys2
,
82 static inline void page_unlock(PageDesc
*pd
) { }
83 static inline void page_lock_tb(const TranslationBlock
*tb
) { }
84 static inline void page_unlock_tb(const TranslationBlock
*tb
) { }
87 * For user-only, since we are protecting all of memory with a single lock,
88 * and because the two pages of a TranslationBlock are always contiguous,
89 * use a single data structure to record all TranslationBlocks.
91 static IntervalTreeRoot tb_root
;
93 static void tb_remove_all(void)
96 memset(&tb_root
, 0, sizeof(tb_root
));
99 /* Call with mmap_lock held. */
100 static void tb_record(TranslationBlock
*tb
, PageDesc
*p1
, PageDesc
*p2
)
105 assert_memory_lock();
106 tb
->itree
.last
= tb
->itree
.start
+ tb
->size
- 1;
108 /* translator_loop() must have made all TB pages non-writable */
109 addr
= tb_page_addr0(tb
);
110 flags
= page_get_flags(addr
);
111 assert(!(flags
& PAGE_WRITE
));
113 addr
= tb_page_addr1(tb
);
115 flags
= page_get_flags(addr
);
116 assert(!(flags
& PAGE_WRITE
));
119 interval_tree_insert(&tb
->itree
, &tb_root
);
122 /* Call with mmap_lock held. */
123 static void tb_remove(TranslationBlock
*tb
)
125 assert_memory_lock();
126 interval_tree_remove(&tb
->itree
, &tb_root
);
129 /* TODO: For now, still shared with translate-all.c for system mode. */
130 #define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \
131 for (T = foreach_tb_first(start, last), \
132 N = foreach_tb_next(T, start, last); \
134 T = N, N = foreach_tb_next(N, start, last))
136 typedef TranslationBlock
*PageForEachNext
;
138 static PageForEachNext
foreach_tb_first(tb_page_addr_t start
,
141 IntervalTreeNode
*n
= interval_tree_iter_first(&tb_root
, start
, last
);
142 return n
? container_of(n
, TranslationBlock
, itree
) : NULL
;
145 static PageForEachNext
foreach_tb_next(PageForEachNext tb
,
146 tb_page_addr_t start
,
152 n
= interval_tree_iter_next(&tb
->itree
, start
, last
);
154 return container_of(n
, TranslationBlock
, itree
);
162 * In system mode we want L1_MAP to be based on ram offsets.
164 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
165 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
167 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
170 /* Size of the L2 (and L3, etc) page tables. */
172 #define V_L2_SIZE (1 << V_L2_BITS)
175 * L1 Mapping properties
177 static int v_l1_size
;
178 static int v_l1_shift
;
179 static int v_l2_levels
;
182 * The bottom level has pointers to PageDesc, and is indexed by
183 * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
185 #define V_L1_MIN_BITS 4
186 #define V_L1_MAX_BITS (V_L2_BITS + 3)
187 #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
189 static void *l1_map
[V_L1_MAX_SIZE
];
193 /* list of TBs intersecting this ram page */
197 void page_table_config_init(void)
201 assert(TARGET_PAGE_BITS
);
202 /* The bits remaining after N lower levels of page tables. */
203 v_l1_bits
= (L1_MAP_ADDR_SPACE_BITS
- TARGET_PAGE_BITS
) % V_L2_BITS
;
204 if (v_l1_bits
< V_L1_MIN_BITS
) {
205 v_l1_bits
+= V_L2_BITS
;
208 v_l1_size
= 1 << v_l1_bits
;
209 v_l1_shift
= L1_MAP_ADDR_SPACE_BITS
- TARGET_PAGE_BITS
- v_l1_bits
;
210 v_l2_levels
= v_l1_shift
/ V_L2_BITS
- 1;
212 assert(v_l1_bits
<= V_L1_MAX_BITS
);
213 assert(v_l1_shift
% V_L2_BITS
== 0);
214 assert(v_l2_levels
>= 0);
217 static PageDesc
*page_find_alloc(tb_page_addr_t index
, bool alloc
)
223 /* Level 1. Always allocated. */
224 lp
= l1_map
+ ((index
>> v_l1_shift
) & (v_l1_size
- 1));
227 for (i
= v_l2_levels
; i
> 0; i
--) {
228 void **p
= qatomic_rcu_read(lp
);
236 p
= g_new0(void *, V_L2_SIZE
);
237 existing
= qatomic_cmpxchg(lp
, NULL
, p
);
238 if (unlikely(existing
)) {
244 lp
= p
+ ((index
>> (i
* V_L2_BITS
)) & (V_L2_SIZE
- 1));
247 pd
= qatomic_rcu_read(lp
);
255 pd
= g_new0(PageDesc
, V_L2_SIZE
);
256 for (int i
= 0; i
< V_L2_SIZE
; i
++) {
257 qemu_spin_init(&pd
[i
].lock
);
260 existing
= qatomic_cmpxchg(lp
, NULL
, pd
);
261 if (unlikely(existing
)) {
262 for (int i
= 0; i
< V_L2_SIZE
; i
++) {
263 qemu_spin_destroy(&pd
[i
].lock
);
270 return pd
+ (index
& (V_L2_SIZE
- 1));
273 static inline PageDesc
*page_find(tb_page_addr_t index
)
275 return page_find_alloc(index
, false);
279 * struct page_entry - page descriptor entry
280 * @pd: pointer to the &struct PageDesc of the page this entry represents
281 * @index: page index of the page
282 * @locked: whether the page is locked
284 * This struct helps us keep track of the locked state of a page, without
285 * bloating &struct PageDesc.
287 * A page lock protects accesses to all fields of &struct PageDesc.
289 * See also: &struct page_collection.
293 tb_page_addr_t index
;
298 * struct page_collection - tracks a set of pages (i.e. &struct page_entry's)
299 * @tree: Binary search tree (BST) of the pages, with key == page index
300 * @max: Pointer to the page in @tree with the highest page index
302 * To avoid deadlock we lock pages in ascending order of page index.
303 * When operating on a set of pages, we need to keep track of them so that
304 * we can lock them in order and also unlock them later. For this we collect
305 * pages (i.e. &struct page_entry's) in a binary search @tree. Given that the
306 * @tree implementation we use does not provide an O(1) operation to obtain the
307 * highest-ranked element, we use @max to keep track of the inserted page
308 * with the highest index. This is valuable because if a page is not in
309 * the tree and its index is higher than @max's, then we can lock it
310 * without breaking the locking order rule.
312 * Note on naming: 'struct page_set' would be shorter, but we already have a few
313 * page_set_*() helpers, so page_collection is used instead to avoid confusion.
315 * See also: page_collection_lock().
317 struct page_collection
{
319 struct page_entry
*max
;
322 typedef int PageForEachNext
;
323 #define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \
324 TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)
326 #ifdef CONFIG_DEBUG_TCG
328 static __thread GHashTable
*ht_pages_locked_debug
;
330 static void ht_pages_locked_debug_init(void)
332 if (ht_pages_locked_debug
) {
335 ht_pages_locked_debug
= g_hash_table_new(NULL
, NULL
);
338 static bool page_is_locked(const PageDesc
*pd
)
342 ht_pages_locked_debug_init();
343 found
= g_hash_table_lookup(ht_pages_locked_debug
, pd
);
347 static void page_lock__debug(PageDesc
*pd
)
349 ht_pages_locked_debug_init();
350 g_assert(!page_is_locked(pd
));
351 g_hash_table_insert(ht_pages_locked_debug
, pd
, pd
);
354 static void page_unlock__debug(const PageDesc
*pd
)
358 ht_pages_locked_debug_init();
359 g_assert(page_is_locked(pd
));
360 removed
= g_hash_table_remove(ht_pages_locked_debug
, pd
);
364 static void do_assert_page_locked(const PageDesc
*pd
,
365 const char *file
, int line
)
367 if (unlikely(!page_is_locked(pd
))) {
368 error_report("assert_page_lock: PageDesc %p not locked @ %s:%d",
373 #define assert_page_locked(pd) do_assert_page_locked(pd, __FILE__, __LINE__)
375 void assert_no_pages_locked(void)
377 ht_pages_locked_debug_init();
378 g_assert(g_hash_table_size(ht_pages_locked_debug
) == 0);
381 #else /* !CONFIG_DEBUG_TCG */
383 static inline void page_lock__debug(const PageDesc
*pd
) { }
384 static inline void page_unlock__debug(const PageDesc
*pd
) { }
385 static inline void assert_page_locked(const PageDesc
*pd
) { }
387 #endif /* CONFIG_DEBUG_TCG */
389 static void page_lock(PageDesc
*pd
)
391 page_lock__debug(pd
);
392 qemu_spin_lock(&pd
->lock
);
395 static void page_unlock(PageDesc
*pd
)
397 qemu_spin_unlock(&pd
->lock
);
398 page_unlock__debug(pd
);
401 static inline struct page_entry
*
402 page_entry_new(PageDesc
*pd
, tb_page_addr_t index
)
404 struct page_entry
*pe
= g_malloc(sizeof(*pe
));
412 static void page_entry_destroy(gpointer p
)
414 struct page_entry
*pe
= p
;
416 g_assert(pe
->locked
);
421 /* returns false on success */
422 static bool page_entry_trylock(struct page_entry
*pe
)
426 busy
= qemu_spin_trylock(&pe
->pd
->lock
);
428 g_assert(!pe
->locked
);
430 page_lock__debug(pe
->pd
);
435 static void do_page_entry_lock(struct page_entry
*pe
)
438 g_assert(!pe
->locked
);
442 static gboolean
page_entry_lock(gpointer key
, gpointer value
, gpointer data
)
444 struct page_entry
*pe
= value
;
446 do_page_entry_lock(pe
);
450 static gboolean
page_entry_unlock(gpointer key
, gpointer value
, gpointer data
)
452 struct page_entry
*pe
= value
;
462 * Trylock a page, and if successful, add the page to a collection.
463 * Returns true ("busy") if the page could not be locked; false otherwise.
465 static bool page_trylock_add(struct page_collection
*set
, tb_page_addr_t addr
)
467 tb_page_addr_t index
= addr
>> TARGET_PAGE_BITS
;
468 struct page_entry
*pe
;
471 pe
= q_tree_lookup(set
->tree
, &index
);
476 pd
= page_find(index
);
481 pe
= page_entry_new(pd
, index
);
482 q_tree_insert(set
->tree
, &pe
->index
, pe
);
485 * If this is either (1) the first insertion or (2) a page whose index
486 * is higher than any other so far, just lock the page and move on.
488 if (set
->max
== NULL
|| pe
->index
> set
->max
->index
) {
490 do_page_entry_lock(pe
);
494 * Try to acquire out-of-order lock; if busy, return busy so that we acquire
497 return page_entry_trylock(pe
);
500 static gint
tb_page_addr_cmp(gconstpointer ap
, gconstpointer bp
, gpointer udata
)
502 tb_page_addr_t a
= *(const tb_page_addr_t
*)ap
;
503 tb_page_addr_t b
= *(const tb_page_addr_t
*)bp
;
514 * Lock a range of pages ([@start,@last]) as well as the pages of all
516 * Locking order: acquire locks in ascending order of page index.
518 static struct page_collection
*page_collection_lock(tb_page_addr_t start
,
521 struct page_collection
*set
= g_malloc(sizeof(*set
));
522 tb_page_addr_t index
;
525 start
>>= TARGET_PAGE_BITS
;
526 last
>>= TARGET_PAGE_BITS
;
527 g_assert(start
<= last
);
529 set
->tree
= q_tree_new_full(tb_page_addr_cmp
, NULL
, NULL
,
532 assert_no_pages_locked();
535 q_tree_foreach(set
->tree
, page_entry_lock
, NULL
);
537 for (index
= start
; index
<= last
; index
++) {
538 TranslationBlock
*tb
;
541 pd
= page_find(index
);
545 if (page_trylock_add(set
, index
<< TARGET_PAGE_BITS
)) {
546 q_tree_foreach(set
->tree
, page_entry_unlock
, NULL
);
549 assert_page_locked(pd
);
550 PAGE_FOR_EACH_TB(unused
, unused
, pd
, tb
, n
) {
551 if (page_trylock_add(set
, tb_page_addr0(tb
)) ||
552 (tb_page_addr1(tb
) != -1 &&
553 page_trylock_add(set
, tb_page_addr1(tb
)))) {
554 /* drop all locks, and reacquire in order */
555 q_tree_foreach(set
->tree
, page_entry_unlock
, NULL
);
563 static void page_collection_unlock(struct page_collection
*set
)
565 /* entries are unlocked and freed via page_entry_destroy */
566 q_tree_destroy(set
->tree
);
570 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
571 static void tb_remove_all_1(int level
, void **lp
)
581 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
583 pd
[i
].first_tb
= (uintptr_t)NULL
;
589 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
590 tb_remove_all_1(level
- 1, pp
+ i
);
595 static void tb_remove_all(void)
597 int i
, l1_sz
= v_l1_size
;
599 for (i
= 0; i
< l1_sz
; i
++) {
600 tb_remove_all_1(v_l2_levels
, l1_map
+ i
);
605 * Add the tb in the target page and protect it if necessary.
606 * Called with @p->lock held.
608 static inline void tb_page_add(PageDesc
*p
, TranslationBlock
*tb
,
611 bool page_already_protected
;
613 assert_page_locked(p
);
615 tb
->page_next
[n
] = p
->first_tb
;
616 page_already_protected
= p
->first_tb
!= 0;
617 p
->first_tb
= (uintptr_t)tb
| n
;
620 * If some code is already present, then the pages are already
621 * protected. So we handle the case where only the first TB is
622 * allocated in a physical page.
624 if (!page_already_protected
) {
625 tlb_protect_code(tb
->page_addr
[n
] & TARGET_PAGE_MASK
);
629 static void tb_record(TranslationBlock
*tb
, PageDesc
*p1
, PageDesc
*p2
)
631 tb_page_add(p1
, tb
, 0);
633 tb_page_add(p2
, tb
, 1);
637 static inline void tb_page_remove(PageDesc
*pd
, TranslationBlock
*tb
)
639 TranslationBlock
*tb1
;
643 assert_page_locked(pd
);
644 pprev
= &pd
->first_tb
;
645 PAGE_FOR_EACH_TB(unused
, unused
, pd
, tb1
, n1
) {
647 *pprev
= tb1
->page_next
[n1
];
650 pprev
= &tb1
->page_next
[n1
];
652 g_assert_not_reached();
655 static void tb_remove(TranslationBlock
*tb
)
659 pd
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
660 tb_page_remove(pd
, tb
);
661 if (unlikely(tb
->page_addr
[1] != -1)) {
662 pd
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
663 tb_page_remove(pd
, tb
);
667 static void page_lock_pair(PageDesc
**ret_p1
, tb_page_addr_t phys1
,
668 PageDesc
**ret_p2
, tb_page_addr_t phys2
, bool alloc
)
671 tb_page_addr_t page1
;
672 tb_page_addr_t page2
;
674 assert_memory_lock();
675 g_assert(phys1
!= -1);
677 page1
= phys1
>> TARGET_PAGE_BITS
;
678 page2
= phys2
>> TARGET_PAGE_BITS
;
680 p1
= page_find_alloc(page1
, alloc
);
684 if (likely(phys2
== -1)) {
687 } else if (page1
== page2
) {
694 p2
= page_find_alloc(page2
, alloc
);
707 /* lock the page(s) of a TB in the correct acquisition order */
708 static void page_lock_tb(const TranslationBlock
*tb
)
710 page_lock_pair(NULL
, tb_page_addr0(tb
), NULL
, tb_page_addr1(tb
), false);
713 static void page_unlock_tb(const TranslationBlock
*tb
)
715 PageDesc
*p1
= page_find(tb_page_addr0(tb
) >> TARGET_PAGE_BITS
);
718 if (unlikely(tb_page_addr1(tb
) != -1)) {
719 PageDesc
*p2
= page_find(tb_page_addr1(tb
) >> TARGET_PAGE_BITS
);
726 #endif /* CONFIG_USER_ONLY */
728 /* flush all the translation blocks */
729 static void do_tb_flush(CPUState
*cpu
, run_on_cpu_data tb_flush_count
)
731 bool did_flush
= false;
734 /* If it is already been done on request of another CPU, just retry. */
735 if (tb_ctx
.tb_flush_count
!= tb_flush_count
.host_int
) {
741 tcg_flush_jmp_cache(cpu
);
744 qht_reset_size(&tb_ctx
.htable
, CODE_GEN_HTABLE_SIZE
);
747 tcg_region_reset_all();
748 /* XXX: flush processor icache at this point if cache flush is expensive */
749 qatomic_mb_set(&tb_ctx
.tb_flush_count
, tb_ctx
.tb_flush_count
+ 1);
754 qemu_plugin_flush_cb();
758 void tb_flush(CPUState
*cpu
)
761 unsigned tb_flush_count
= qatomic_mb_read(&tb_ctx
.tb_flush_count
);
763 if (cpu_in_exclusive_context(cpu
)) {
764 do_tb_flush(cpu
, RUN_ON_CPU_HOST_INT(tb_flush_count
));
766 async_safe_run_on_cpu(cpu
, do_tb_flush
,
767 RUN_ON_CPU_HOST_INT(tb_flush_count
));
772 /* remove @orig from its @n_orig-th jump list */
773 static inline void tb_remove_from_jmp_list(TranslationBlock
*orig
, int n_orig
)
775 uintptr_t ptr
, ptr_locked
;
776 TranslationBlock
*dest
;
777 TranslationBlock
*tb
;
781 /* mark the LSB of jmp_dest[] so that no further jumps can be inserted */
782 ptr
= qatomic_or_fetch(&orig
->jmp_dest
[n_orig
], 1);
783 dest
= (TranslationBlock
*)(ptr
& ~1);
788 qemu_spin_lock(&dest
->jmp_lock
);
790 * While acquiring the lock, the jump might have been removed if the
791 * destination TB was invalidated; check again.
793 ptr_locked
= qatomic_read(&orig
->jmp_dest
[n_orig
]);
794 if (ptr_locked
!= ptr
) {
795 qemu_spin_unlock(&dest
->jmp_lock
);
797 * The only possibility is that the jump was unlinked via
798 * tb_jump_unlink(dest). Seeing here another destination would be a bug,
799 * because we set the LSB above.
801 g_assert(ptr_locked
== 1 && dest
->cflags
& CF_INVALID
);
805 * We first acquired the lock, and since the destination pointer matches,
806 * we know for sure that @orig is in the jmp list.
808 pprev
= &dest
->jmp_list_head
;
809 TB_FOR_EACH_JMP(dest
, tb
, n
) {
810 if (tb
== orig
&& n
== n_orig
) {
811 *pprev
= tb
->jmp_list_next
[n
];
812 /* no need to set orig->jmp_dest[n]; setting the LSB was enough */
813 qemu_spin_unlock(&dest
->jmp_lock
);
816 pprev
= &tb
->jmp_list_next
[n
];
818 g_assert_not_reached();
822 * Reset the jump entry 'n' of a TB so that it is not chained to another TB.
824 void tb_reset_jump(TranslationBlock
*tb
, int n
)
826 uintptr_t addr
= (uintptr_t)(tb
->tc
.ptr
+ tb
->jmp_reset_offset
[n
]);
827 tb_set_jmp_target(tb
, n
, addr
);
830 /* remove any jumps to the TB */
831 static inline void tb_jmp_unlink(TranslationBlock
*dest
)
833 TranslationBlock
*tb
;
836 qemu_spin_lock(&dest
->jmp_lock
);
838 TB_FOR_EACH_JMP(dest
, tb
, n
) {
839 tb_reset_jump(tb
, n
);
840 qatomic_and(&tb
->jmp_dest
[n
], (uintptr_t)NULL
| 1);
841 /* No need to clear the list entry; setting the dest ptr is enough */
843 dest
->jmp_list_head
= (uintptr_t)NULL
;
845 qemu_spin_unlock(&dest
->jmp_lock
);
848 static void tb_jmp_cache_inval_tb(TranslationBlock
*tb
)
852 if (tb_cflags(tb
) & CF_PCREL
) {
853 /* A TB may be at any virtual address */
855 tcg_flush_jmp_cache(cpu
);
858 uint32_t h
= tb_jmp_cache_hash_func(tb
->pc
);
861 CPUJumpCache
*jc
= cpu
->tb_jmp_cache
;
863 if (qatomic_read(&jc
->array
[h
].tb
) == tb
) {
864 qatomic_set(&jc
->array
[h
].tb
, NULL
);
871 * In user-mode, call with mmap_lock held.
872 * In !user-mode, if @rm_from_page_list is set, call with the TB's pages'
875 static void do_tb_phys_invalidate(TranslationBlock
*tb
, bool rm_from_page_list
)
878 tb_page_addr_t phys_pc
;
879 uint32_t orig_cflags
= tb_cflags(tb
);
881 assert_memory_lock();
883 /* make sure no further incoming jumps will be chained to this TB */
884 qemu_spin_lock(&tb
->jmp_lock
);
885 qatomic_set(&tb
->cflags
, tb
->cflags
| CF_INVALID
);
886 qemu_spin_unlock(&tb
->jmp_lock
);
888 /* remove the TB from the hash list */
889 phys_pc
= tb_page_addr0(tb
);
890 h
= tb_hash_func(phys_pc
, (orig_cflags
& CF_PCREL
? 0 : tb
->pc
),
891 tb
->flags
, orig_cflags
, tb
->trace_vcpu_dstate
);
892 if (!qht_remove(&tb_ctx
.htable
, tb
, h
)) {
896 /* remove the TB from the page list */
897 if (rm_from_page_list
) {
901 /* remove the TB from the hash list */
902 tb_jmp_cache_inval_tb(tb
);
904 /* suppress this TB from the two jump lists */
905 tb_remove_from_jmp_list(tb
, 0);
906 tb_remove_from_jmp_list(tb
, 1);
908 /* suppress any remaining jumps to this TB */
911 qatomic_set(&tb_ctx
.tb_phys_invalidate_count
,
912 tb_ctx
.tb_phys_invalidate_count
+ 1);
915 static void tb_phys_invalidate__locked(TranslationBlock
*tb
)
917 qemu_thread_jit_write();
918 do_tb_phys_invalidate(tb
, true);
919 qemu_thread_jit_execute();
924 * Called with mmap_lock held in user-mode.
926 void tb_phys_invalidate(TranslationBlock
*tb
, tb_page_addr_t page_addr
)
928 if (page_addr
== -1 && tb_page_addr0(tb
) != -1) {
930 do_tb_phys_invalidate(tb
, true);
933 do_tb_phys_invalidate(tb
, false);
938 * Add a new TB and link it to the physical page tables. phys_page2 is
939 * (-1) to indicate that only one page contains the TB.
941 * Called with mmap_lock held for user-mode emulation.
943 * Returns a pointer @tb, or a pointer to an existing TB that matches @tb.
944 * Note that in !user-mode, another thread might have already added a TB
945 * for the same block of guest code that @tb corresponds to. In that case,
946 * the caller should discard the original @tb, and use instead the returned TB.
948 TranslationBlock
*tb_link_page(TranslationBlock
*tb
, tb_page_addr_t phys_pc
,
949 tb_page_addr_t phys_page2
)
953 void *existing_tb
= NULL
;
956 assert_memory_lock();
957 tcg_debug_assert(!(tb
->cflags
& CF_INVALID
));
960 * Add the TB to the page list, acquiring first the pages's locks.
961 * We keep the locks held until after inserting the TB in the hash table,
962 * so that if the insertion fails we know for sure that the TBs are still
963 * in the page descriptors.
964 * Note that inserting into the hash table first isn't an option, since
965 * we can only insert TBs that are fully initialized.
967 page_lock_pair(&p
, phys_pc
, &p2
, phys_page2
, true);
968 tb_record(tb
, p
, p2
);
970 /* add in the hash table */
971 h
= tb_hash_func(phys_pc
, (tb
->cflags
& CF_PCREL
? 0 : tb
->pc
),
972 tb
->flags
, tb
->cflags
, tb
->trace_vcpu_dstate
);
973 qht_insert(&tb_ctx
.htable
, tb
, h
, &existing_tb
);
975 /* remove TB from the page(s) if we couldn't insert it */
976 if (unlikely(existing_tb
)) {
988 #ifdef CONFIG_USER_ONLY
990 * Invalidate all TBs which intersect with the target address range.
991 * Called with mmap_lock held for user-mode emulation.
992 * NOTE: this function must not be called while a TB is running.
994 void tb_invalidate_phys_range(tb_page_addr_t start
, tb_page_addr_t last
)
996 TranslationBlock
*tb
;
999 assert_memory_lock();
1001 PAGE_FOR_EACH_TB(start
, last
, unused
, tb
, n
) {
1002 tb_phys_invalidate__locked(tb
);
1007 * Invalidate all TBs which intersect with the target address page @addr.
1008 * Called with mmap_lock held for user-mode emulation
1009 * NOTE: this function must not be called while a TB is running.
1011 void tb_invalidate_phys_page(tb_page_addr_t addr
)
1013 tb_page_addr_t start
, last
;
1015 start
= addr
& TARGET_PAGE_MASK
;
1016 last
= addr
| ~TARGET_PAGE_MASK
;
1017 tb_invalidate_phys_range(start
, last
);
1021 * Called with mmap_lock held. If pc is not 0 then it indicates the
1022 * host PC of the faulting store instruction that caused this invalidate.
1023 * Returns true if the caller needs to abort execution of the current
1024 * TB (because it was modified by this store and the guest CPU has
1025 * precise-SMC semantics).
1027 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr
, uintptr_t pc
)
1029 TranslationBlock
*current_tb
;
1030 bool current_tb_modified
;
1031 TranslationBlock
*tb
;
1033 tb_page_addr_t last
;
1036 * Without precise smc semantics, or when outside of a TB,
1037 * we can skip to invalidate.
1039 #ifndef TARGET_HAS_PRECISE_SMC
1043 tb_invalidate_phys_page(addr
);
1047 assert_memory_lock();
1048 current_tb
= tcg_tb_lookup(pc
);
1050 last
= addr
| ~TARGET_PAGE_MASK
;
1051 addr
&= TARGET_PAGE_MASK
;
1052 current_tb_modified
= false;
1054 PAGE_FOR_EACH_TB(addr
, last
, unused
, tb
, n
) {
1055 if (current_tb
== tb
&&
1056 (tb_cflags(current_tb
) & CF_COUNT_MASK
) != 1) {
1058 * If we are modifying the current TB, we must stop its
1059 * execution. We could be more precise by checking that
1060 * the modification is after the current PC, but it would
1061 * require a specialized function to partially restore
1064 current_tb_modified
= true;
1065 cpu_restore_state_from_tb(current_cpu
, current_tb
, pc
);
1067 tb_phys_invalidate__locked(tb
);
1070 if (current_tb_modified
) {
1071 /* Force execution of one insn next time. */
1072 CPUState
*cpu
= current_cpu
;
1073 cpu
->cflags_next_tb
= 1 | CF_NOIRQ
| curr_cflags(current_cpu
);
1080 * @p must be non-NULL.
1081 * Call with all @pages locked.
1084 tb_invalidate_phys_page_range__locked(struct page_collection
*pages
,
1085 PageDesc
*p
, tb_page_addr_t start
,
1086 tb_page_addr_t last
,
1089 TranslationBlock
*tb
;
1091 #ifdef TARGET_HAS_PRECISE_SMC
1092 bool current_tb_modified
= false;
1093 TranslationBlock
*current_tb
= retaddr
? tcg_tb_lookup(retaddr
) : NULL
;
1094 #endif /* TARGET_HAS_PRECISE_SMC */
1097 * We remove all the TBs in the range [start, last].
1098 * XXX: see if in some cases it could be faster to invalidate all the code
1100 PAGE_FOR_EACH_TB(start
, last
, p
, tb
, n
) {
1101 tb_page_addr_t tb_start
, tb_last
;
1103 /* NOTE: this is subtle as a TB may span two physical pages */
1104 tb_start
= tb_page_addr0(tb
);
1105 tb_last
= tb_start
+ tb
->size
- 1;
1107 tb_last
= MIN(tb_last
, tb_start
| ~TARGET_PAGE_MASK
);
1109 tb_start
= tb_page_addr1(tb
);
1110 tb_last
= tb_start
+ (tb_last
& ~TARGET_PAGE_MASK
);
1112 if (!(tb_last
< start
|| tb_start
> last
)) {
1113 #ifdef TARGET_HAS_PRECISE_SMC
1114 if (current_tb
== tb
&&
1115 (tb_cflags(current_tb
) & CF_COUNT_MASK
) != 1) {
1117 * If we are modifying the current TB, we must stop
1118 * its execution. We could be more precise by checking
1119 * that the modification is after the current PC, but it
1120 * would require a specialized function to partially
1121 * restore the CPU state.
1123 current_tb_modified
= true;
1124 cpu_restore_state_from_tb(current_cpu
, current_tb
, retaddr
);
1126 #endif /* TARGET_HAS_PRECISE_SMC */
1127 tb_phys_invalidate__locked(tb
);
1131 /* if no code remaining, no need to continue to use slow writes */
1133 tlb_unprotect_code(start
);
1136 #ifdef TARGET_HAS_PRECISE_SMC
1137 if (current_tb_modified
) {
1138 page_collection_unlock(pages
);
1139 /* Force execution of one insn next time. */
1140 current_cpu
->cflags_next_tb
= 1 | CF_NOIRQ
| curr_cflags(current_cpu
);
1142 cpu_loop_exit_noexc(current_cpu
);
1148 * Invalidate all TBs which intersect with the target physical
1149 * address page @addr.
1151 void tb_invalidate_phys_page(tb_page_addr_t addr
)
1153 struct page_collection
*pages
;
1154 tb_page_addr_t start
, last
;
1157 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1162 start
= addr
& TARGET_PAGE_MASK
;
1163 last
= addr
| ~TARGET_PAGE_MASK
;
1164 pages
= page_collection_lock(start
, last
);
1165 tb_invalidate_phys_page_range__locked(pages
, p
, start
, last
, 0);
1166 page_collection_unlock(pages
);
1170 * Invalidate all TBs which intersect with the target physical address range
1171 * [start;last]. NOTE: start and end may refer to *different* physical pages.
1172 * 'is_cpu_write_access' should be true if called from a real cpu write
1173 * access: the virtual CPU will exit the current TB if code is modified inside
1176 void tb_invalidate_phys_range(tb_page_addr_t start
, tb_page_addr_t last
)
1178 struct page_collection
*pages
;
1179 tb_page_addr_t index
, index_last
;
1181 pages
= page_collection_lock(start
, last
);
1183 index_last
= last
>> TARGET_PAGE_BITS
;
1184 for (index
= start
>> TARGET_PAGE_BITS
; index
<= index_last
; index
++) {
1185 PageDesc
*pd
= page_find(index
);
1186 tb_page_addr_t bound
;
1191 assert_page_locked(pd
);
1192 bound
= (index
<< TARGET_PAGE_BITS
) | ~TARGET_PAGE_MASK
;
1193 bound
= MIN(bound
, last
);
1194 tb_invalidate_phys_page_range__locked(pages
, pd
, start
, bound
, 0);
1196 page_collection_unlock(pages
);
1200 * Call with all @pages in the range [@start, @start + len[ locked.
1202 static void tb_invalidate_phys_page_fast__locked(struct page_collection
*pages
,
1203 tb_page_addr_t start
,
1204 unsigned len
, uintptr_t ra
)
1208 p
= page_find(start
>> TARGET_PAGE_BITS
);
1213 assert_page_locked(p
);
1214 tb_invalidate_phys_page_range__locked(pages
, p
, start
, start
+ len
- 1, ra
);
1218 * len must be <= 8 and start must be a multiple of len.
1219 * Called via softmmu_template.h when code areas are written to with
1220 * iothread mutex not held.
1222 void tb_invalidate_phys_range_fast(ram_addr_t ram_addr
,
1226 struct page_collection
*pages
;
1228 pages
= page_collection_lock(ram_addr
, ram_addr
+ size
- 1);
1229 tb_invalidate_phys_page_fast__locked(pages
, ram_addr
, size
, retaddr
);
1230 page_collection_unlock(pages
);
1233 #endif /* CONFIG_USER_ONLY */