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