1 /* 2 * Common CPU TLB handling 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/main-loop.h" 22 #include "hw/core/tcg-cpu-ops.h" 23 #include "exec/exec-all.h" 24 #include "exec/memory.h" 25 #include "exec/cpu_ldst.h" 26 #include "exec/cputlb.h" 27 #include "exec/memory-internal.h" 28 #include "exec/ram_addr.h" 29 #include "tcg/tcg.h" 30 #include "qemu/error-report.h" 31 #include "exec/log.h" 32 #include "exec/helper-proto.h" 33 #include "qemu/atomic.h" 34 #include "qemu/atomic128.h" 35 #include "exec/translate-all.h" 36 #include "trace.h" 37 #include "tb-hash.h" 38 #include "internal.h" 39 #ifdef CONFIG_PLUGIN 40 #include "qemu/plugin-memory.h" 41 #endif 42 #include "tcg/tcg-ldst.h" 43 #include "exec/helper-proto.h" 44 45 /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */ 46 /* #define DEBUG_TLB */ 47 /* #define DEBUG_TLB_LOG */ 48 49 #ifdef DEBUG_TLB 50 # define DEBUG_TLB_GATE 1 51 # ifdef DEBUG_TLB_LOG 52 # define DEBUG_TLB_LOG_GATE 1 53 # else 54 # define DEBUG_TLB_LOG_GATE 0 55 # endif 56 #else 57 # define DEBUG_TLB_GATE 0 58 # define DEBUG_TLB_LOG_GATE 0 59 #endif 60 61 #define tlb_debug(fmt, ...) do { \ 62 if (DEBUG_TLB_LOG_GATE) { \ 63 qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \ 64 ## __VA_ARGS__); \ 65 } else if (DEBUG_TLB_GATE) { \ 66 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 67 } \ 68 } while (0) 69 70 #define assert_cpu_is_self(cpu) do { \ 71 if (DEBUG_TLB_GATE) { \ 72 g_assert(!(cpu)->created || qemu_cpu_is_self(cpu)); \ 73 } \ 74 } while (0) 75 76 /* run_on_cpu_data.target_ptr should always be big enough for a 77 * target_ulong even on 32 bit builds */ 78 QEMU_BUILD_BUG_ON(sizeof(target_ulong) > sizeof(run_on_cpu_data)); 79 80 /* We currently can't handle more than 16 bits in the MMUIDX bitmask. 81 */ 82 QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16); 83 #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1) 84 85 static inline size_t tlb_n_entries(CPUTLBDescFast *fast) 86 { 87 return (fast->mask >> CPU_TLB_ENTRY_BITS) + 1; 88 } 89 90 static inline size_t sizeof_tlb(CPUTLBDescFast *fast) 91 { 92 return fast->mask + (1 << CPU_TLB_ENTRY_BITS); 93 } 94 95 static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns, 96 size_t max_entries) 97 { 98 desc->window_begin_ns = ns; 99 desc->window_max_entries = max_entries; 100 } 101 102 static void tb_jmp_cache_clear_page(CPUState *cpu, target_ulong page_addr) 103 { 104 CPUJumpCache *jc = cpu->tb_jmp_cache; 105 int i, i0; 106 107 if (unlikely(!jc)) { 108 return; 109 } 110 111 i0 = tb_jmp_cache_hash_page(page_addr); 112 for (i = 0; i < TB_JMP_PAGE_SIZE; i++) { 113 qatomic_set(&jc->array[i0 + i].tb, NULL); 114 } 115 } 116 117 /** 118 * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary 119 * @desc: The CPUTLBDesc portion of the TLB 120 * @fast: The CPUTLBDescFast portion of the same TLB 121 * 122 * Called with tlb_lock_held. 123 * 124 * We have two main constraints when resizing a TLB: (1) we only resize it 125 * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing 126 * the array or unnecessarily flushing it), which means we do not control how 127 * frequently the resizing can occur; (2) we don't have access to the guest's 128 * future scheduling decisions, and therefore have to decide the magnitude of 129 * the resize based on past observations. 130 * 131 * In general, a memory-hungry process can benefit greatly from an appropriately 132 * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that 133 * we just have to make the TLB as large as possible; while an oversized TLB 134 * results in minimal TLB miss rates, it also takes longer to be flushed 135 * (flushes can be _very_ frequent), and the reduced locality can also hurt 136 * performance. 137 * 138 * To achieve near-optimal performance for all kinds of workloads, we: 139 * 140 * 1. Aggressively increase the size of the TLB when the use rate of the 141 * TLB being flushed is high, since it is likely that in the near future this 142 * memory-hungry process will execute again, and its memory hungriness will 143 * probably be similar. 144 * 145 * 2. Slowly reduce the size of the TLB as the use rate declines over a 146 * reasonably large time window. The rationale is that if in such a time window 147 * we have not observed a high TLB use rate, it is likely that we won't observe 148 * it in the near future. In that case, once a time window expires we downsize 149 * the TLB to match the maximum use rate observed in the window. 150 * 151 * 3. Try to keep the maximum use rate in a time window in the 30-70% range, 152 * since in that range performance is likely near-optimal. Recall that the TLB 153 * is direct mapped, so we want the use rate to be low (or at least not too 154 * high), since otherwise we are likely to have a significant amount of 155 * conflict misses. 156 */ 157 static void tlb_mmu_resize_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast, 158 int64_t now) 159 { 160 size_t old_size = tlb_n_entries(fast); 161 size_t rate; 162 size_t new_size = old_size; 163 int64_t window_len_ms = 100; 164 int64_t window_len_ns = window_len_ms * 1000 * 1000; 165 bool window_expired = now > desc->window_begin_ns + window_len_ns; 166 167 if (desc->n_used_entries > desc->window_max_entries) { 168 desc->window_max_entries = desc->n_used_entries; 169 } 170 rate = desc->window_max_entries * 100 / old_size; 171 172 if (rate > 70) { 173 new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS); 174 } else if (rate < 30 && window_expired) { 175 size_t ceil = pow2ceil(desc->window_max_entries); 176 size_t expected_rate = desc->window_max_entries * 100 / ceil; 177 178 /* 179 * Avoid undersizing when the max number of entries seen is just below 180 * a pow2. For instance, if max_entries == 1025, the expected use rate 181 * would be 1025/2048==50%. However, if max_entries == 1023, we'd get 182 * 1023/1024==99.9% use rate, so we'd likely end up doubling the size 183 * later. Thus, make sure that the expected use rate remains below 70%. 184 * (and since we double the size, that means the lowest rate we'd 185 * expect to get is 35%, which is still in the 30-70% range where 186 * we consider that the size is appropriate.) 187 */ 188 if (expected_rate > 70) { 189 ceil *= 2; 190 } 191 new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS); 192 } 193 194 if (new_size == old_size) { 195 if (window_expired) { 196 tlb_window_reset(desc, now, desc->n_used_entries); 197 } 198 return; 199 } 200 201 g_free(fast->table); 202 g_free(desc->fulltlb); 203 204 tlb_window_reset(desc, now, 0); 205 /* desc->n_used_entries is cleared by the caller */ 206 fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS; 207 fast->table = g_try_new(CPUTLBEntry, new_size); 208 desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size); 209 210 /* 211 * If the allocations fail, try smaller sizes. We just freed some 212 * memory, so going back to half of new_size has a good chance of working. 213 * Increased memory pressure elsewhere in the system might cause the 214 * allocations to fail though, so we progressively reduce the allocation 215 * size, aborting if we cannot even allocate the smallest TLB we support. 216 */ 217 while (fast->table == NULL || desc->fulltlb == NULL) { 218 if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) { 219 error_report("%s: %s", __func__, strerror(errno)); 220 abort(); 221 } 222 new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS); 223 fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS; 224 225 g_free(fast->table); 226 g_free(desc->fulltlb); 227 fast->table = g_try_new(CPUTLBEntry, new_size); 228 desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size); 229 } 230 } 231 232 static void tlb_mmu_flush_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast) 233 { 234 desc->n_used_entries = 0; 235 desc->large_page_addr = -1; 236 desc->large_page_mask = -1; 237 desc->vindex = 0; 238 memset(fast->table, -1, sizeof_tlb(fast)); 239 memset(desc->vtable, -1, sizeof(desc->vtable)); 240 } 241 242 static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx, 243 int64_t now) 244 { 245 CPUTLBDesc *desc = &env_tlb(env)->d[mmu_idx]; 246 CPUTLBDescFast *fast = &env_tlb(env)->f[mmu_idx]; 247 248 tlb_mmu_resize_locked(desc, fast, now); 249 tlb_mmu_flush_locked(desc, fast); 250 } 251 252 static void tlb_mmu_init(CPUTLBDesc *desc, CPUTLBDescFast *fast, int64_t now) 253 { 254 size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS; 255 256 tlb_window_reset(desc, now, 0); 257 desc->n_used_entries = 0; 258 fast->mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS; 259 fast->table = g_new(CPUTLBEntry, n_entries); 260 desc->fulltlb = g_new(CPUTLBEntryFull, n_entries); 261 tlb_mmu_flush_locked(desc, fast); 262 } 263 264 static inline void tlb_n_used_entries_inc(CPUArchState *env, uintptr_t mmu_idx) 265 { 266 env_tlb(env)->d[mmu_idx].n_used_entries++; 267 } 268 269 static inline void tlb_n_used_entries_dec(CPUArchState *env, uintptr_t mmu_idx) 270 { 271 env_tlb(env)->d[mmu_idx].n_used_entries--; 272 } 273 274 void tlb_init(CPUState *cpu) 275 { 276 CPUArchState *env = cpu->env_ptr; 277 int64_t now = get_clock_realtime(); 278 int i; 279 280 qemu_spin_init(&env_tlb(env)->c.lock); 281 282 /* All tlbs are initialized flushed. */ 283 env_tlb(env)->c.dirty = 0; 284 285 for (i = 0; i < NB_MMU_MODES; i++) { 286 tlb_mmu_init(&env_tlb(env)->d[i], &env_tlb(env)->f[i], now); 287 } 288 } 289 290 void tlb_destroy(CPUState *cpu) 291 { 292 CPUArchState *env = cpu->env_ptr; 293 int i; 294 295 qemu_spin_destroy(&env_tlb(env)->c.lock); 296 for (i = 0; i < NB_MMU_MODES; i++) { 297 CPUTLBDesc *desc = &env_tlb(env)->d[i]; 298 CPUTLBDescFast *fast = &env_tlb(env)->f[i]; 299 300 g_free(fast->table); 301 g_free(desc->fulltlb); 302 } 303 } 304 305 /* flush_all_helper: run fn across all cpus 306 * 307 * If the wait flag is set then the src cpu's helper will be queued as 308 * "safe" work and the loop exited creating a synchronisation point 309 * where all queued work will be finished before execution starts 310 * again. 311 */ 312 static void flush_all_helper(CPUState *src, run_on_cpu_func fn, 313 run_on_cpu_data d) 314 { 315 CPUState *cpu; 316 317 CPU_FOREACH(cpu) { 318 if (cpu != src) { 319 async_run_on_cpu(cpu, fn, d); 320 } 321 } 322 } 323 324 void tlb_flush_counts(size_t *pfull, size_t *ppart, size_t *pelide) 325 { 326 CPUState *cpu; 327 size_t full = 0, part = 0, elide = 0; 328 329 CPU_FOREACH(cpu) { 330 CPUArchState *env = cpu->env_ptr; 331 332 full += qatomic_read(&env_tlb(env)->c.full_flush_count); 333 part += qatomic_read(&env_tlb(env)->c.part_flush_count); 334 elide += qatomic_read(&env_tlb(env)->c.elide_flush_count); 335 } 336 *pfull = full; 337 *ppart = part; 338 *pelide = elide; 339 } 340 341 static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data) 342 { 343 CPUArchState *env = cpu->env_ptr; 344 uint16_t asked = data.host_int; 345 uint16_t all_dirty, work, to_clean; 346 int64_t now = get_clock_realtime(); 347 348 assert_cpu_is_self(cpu); 349 350 tlb_debug("mmu_idx:0x%04" PRIx16 "\n", asked); 351 352 qemu_spin_lock(&env_tlb(env)->c.lock); 353 354 all_dirty = env_tlb(env)->c.dirty; 355 to_clean = asked & all_dirty; 356 all_dirty &= ~to_clean; 357 env_tlb(env)->c.dirty = all_dirty; 358 359 for (work = to_clean; work != 0; work &= work - 1) { 360 int mmu_idx = ctz32(work); 361 tlb_flush_one_mmuidx_locked(env, mmu_idx, now); 362 } 363 364 qemu_spin_unlock(&env_tlb(env)->c.lock); 365 366 tcg_flush_jmp_cache(cpu); 367 368 if (to_clean == ALL_MMUIDX_BITS) { 369 qatomic_set(&env_tlb(env)->c.full_flush_count, 370 env_tlb(env)->c.full_flush_count + 1); 371 } else { 372 qatomic_set(&env_tlb(env)->c.part_flush_count, 373 env_tlb(env)->c.part_flush_count + ctpop16(to_clean)); 374 if (to_clean != asked) { 375 qatomic_set(&env_tlb(env)->c.elide_flush_count, 376 env_tlb(env)->c.elide_flush_count + 377 ctpop16(asked & ~to_clean)); 378 } 379 } 380 } 381 382 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap) 383 { 384 tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap); 385 386 if (cpu->created && !qemu_cpu_is_self(cpu)) { 387 async_run_on_cpu(cpu, tlb_flush_by_mmuidx_async_work, 388 RUN_ON_CPU_HOST_INT(idxmap)); 389 } else { 390 tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap)); 391 } 392 } 393 394 void tlb_flush(CPUState *cpu) 395 { 396 tlb_flush_by_mmuidx(cpu, ALL_MMUIDX_BITS); 397 } 398 399 void tlb_flush_by_mmuidx_all_cpus(CPUState *src_cpu, uint16_t idxmap) 400 { 401 const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work; 402 403 tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap); 404 405 flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 406 fn(src_cpu, RUN_ON_CPU_HOST_INT(idxmap)); 407 } 408 409 void tlb_flush_all_cpus(CPUState *src_cpu) 410 { 411 tlb_flush_by_mmuidx_all_cpus(src_cpu, ALL_MMUIDX_BITS); 412 } 413 414 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu, uint16_t idxmap) 415 { 416 const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work; 417 418 tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap); 419 420 flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 421 async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 422 } 423 424 void tlb_flush_all_cpus_synced(CPUState *src_cpu) 425 { 426 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS); 427 } 428 429 static bool tlb_hit_page_mask_anyprot(CPUTLBEntry *tlb_entry, 430 target_ulong page, target_ulong mask) 431 { 432 page &= mask; 433 mask &= TARGET_PAGE_MASK | TLB_INVALID_MASK; 434 435 return (page == (tlb_entry->addr_read & mask) || 436 page == (tlb_addr_write(tlb_entry) & mask) || 437 page == (tlb_entry->addr_code & mask)); 438 } 439 440 static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry, 441 target_ulong page) 442 { 443 return tlb_hit_page_mask_anyprot(tlb_entry, page, -1); 444 } 445 446 /** 447 * tlb_entry_is_empty - return true if the entry is not in use 448 * @te: pointer to CPUTLBEntry 449 */ 450 static inline bool tlb_entry_is_empty(const CPUTLBEntry *te) 451 { 452 return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1; 453 } 454 455 /* Called with tlb_c.lock held */ 456 static bool tlb_flush_entry_mask_locked(CPUTLBEntry *tlb_entry, 457 target_ulong page, 458 target_ulong mask) 459 { 460 if (tlb_hit_page_mask_anyprot(tlb_entry, page, mask)) { 461 memset(tlb_entry, -1, sizeof(*tlb_entry)); 462 return true; 463 } 464 return false; 465 } 466 467 static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry, 468 target_ulong page) 469 { 470 return tlb_flush_entry_mask_locked(tlb_entry, page, -1); 471 } 472 473 /* Called with tlb_c.lock held */ 474 static void tlb_flush_vtlb_page_mask_locked(CPUArchState *env, int mmu_idx, 475 target_ulong page, 476 target_ulong mask) 477 { 478 CPUTLBDesc *d = &env_tlb(env)->d[mmu_idx]; 479 int k; 480 481 assert_cpu_is_self(env_cpu(env)); 482 for (k = 0; k < CPU_VTLB_SIZE; k++) { 483 if (tlb_flush_entry_mask_locked(&d->vtable[k], page, mask)) { 484 tlb_n_used_entries_dec(env, mmu_idx); 485 } 486 } 487 } 488 489 static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx, 490 target_ulong page) 491 { 492 tlb_flush_vtlb_page_mask_locked(env, mmu_idx, page, -1); 493 } 494 495 static void tlb_flush_page_locked(CPUArchState *env, int midx, 496 target_ulong page) 497 { 498 target_ulong lp_addr = env_tlb(env)->d[midx].large_page_addr; 499 target_ulong lp_mask = env_tlb(env)->d[midx].large_page_mask; 500 501 /* Check if we need to flush due to large pages. */ 502 if ((page & lp_mask) == lp_addr) { 503 tlb_debug("forcing full flush midx %d (" 504 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", 505 midx, lp_addr, lp_mask); 506 tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 507 } else { 508 if (tlb_flush_entry_locked(tlb_entry(env, midx, page), page)) { 509 tlb_n_used_entries_dec(env, midx); 510 } 511 tlb_flush_vtlb_page_locked(env, midx, page); 512 } 513 } 514 515 /** 516 * tlb_flush_page_by_mmuidx_async_0: 517 * @cpu: cpu on which to flush 518 * @addr: page of virtual address to flush 519 * @idxmap: set of mmu_idx to flush 520 * 521 * Helper for tlb_flush_page_by_mmuidx and friends, flush one page 522 * at @addr from the tlbs indicated by @idxmap from @cpu. 523 */ 524 static void tlb_flush_page_by_mmuidx_async_0(CPUState *cpu, 525 target_ulong addr, 526 uint16_t idxmap) 527 { 528 CPUArchState *env = cpu->env_ptr; 529 int mmu_idx; 530 531 assert_cpu_is_self(cpu); 532 533 tlb_debug("page addr:" TARGET_FMT_lx " mmu_map:0x%x\n", addr, idxmap); 534 535 qemu_spin_lock(&env_tlb(env)->c.lock); 536 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 537 if ((idxmap >> mmu_idx) & 1) { 538 tlb_flush_page_locked(env, mmu_idx, addr); 539 } 540 } 541 qemu_spin_unlock(&env_tlb(env)->c.lock); 542 543 /* 544 * Discard jump cache entries for any tb which might potentially 545 * overlap the flushed page, which includes the previous. 546 */ 547 tb_jmp_cache_clear_page(cpu, addr - TARGET_PAGE_SIZE); 548 tb_jmp_cache_clear_page(cpu, addr); 549 } 550 551 /** 552 * tlb_flush_page_by_mmuidx_async_1: 553 * @cpu: cpu on which to flush 554 * @data: encoded addr + idxmap 555 * 556 * Helper for tlb_flush_page_by_mmuidx and friends, called through 557 * async_run_on_cpu. The idxmap parameter is encoded in the page 558 * offset of the target_ptr field. This limits the set of mmu_idx 559 * that can be passed via this method. 560 */ 561 static void tlb_flush_page_by_mmuidx_async_1(CPUState *cpu, 562 run_on_cpu_data data) 563 { 564 target_ulong addr_and_idxmap = (target_ulong) data.target_ptr; 565 target_ulong addr = addr_and_idxmap & TARGET_PAGE_MASK; 566 uint16_t idxmap = addr_and_idxmap & ~TARGET_PAGE_MASK; 567 568 tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap); 569 } 570 571 typedef struct { 572 target_ulong addr; 573 uint16_t idxmap; 574 } TLBFlushPageByMMUIdxData; 575 576 /** 577 * tlb_flush_page_by_mmuidx_async_2: 578 * @cpu: cpu on which to flush 579 * @data: allocated addr + idxmap 580 * 581 * Helper for tlb_flush_page_by_mmuidx and friends, called through 582 * async_run_on_cpu. The addr+idxmap parameters are stored in a 583 * TLBFlushPageByMMUIdxData structure that has been allocated 584 * specifically for this helper. Free the structure when done. 585 */ 586 static void tlb_flush_page_by_mmuidx_async_2(CPUState *cpu, 587 run_on_cpu_data data) 588 { 589 TLBFlushPageByMMUIdxData *d = data.host_ptr; 590 591 tlb_flush_page_by_mmuidx_async_0(cpu, d->addr, d->idxmap); 592 g_free(d); 593 } 594 595 void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, uint16_t idxmap) 596 { 597 tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%" PRIx16 "\n", addr, idxmap); 598 599 /* This should already be page aligned */ 600 addr &= TARGET_PAGE_MASK; 601 602 if (qemu_cpu_is_self(cpu)) { 603 tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap); 604 } else if (idxmap < TARGET_PAGE_SIZE) { 605 /* 606 * Most targets have only a few mmu_idx. In the case where 607 * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid 608 * allocating memory for this operation. 609 */ 610 async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_1, 611 RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 612 } else { 613 TLBFlushPageByMMUIdxData *d = g_new(TLBFlushPageByMMUIdxData, 1); 614 615 /* Otherwise allocate a structure, freed by the worker. */ 616 d->addr = addr; 617 d->idxmap = idxmap; 618 async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_2, 619 RUN_ON_CPU_HOST_PTR(d)); 620 } 621 } 622 623 void tlb_flush_page(CPUState *cpu, target_ulong addr) 624 { 625 tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS); 626 } 627 628 void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, target_ulong addr, 629 uint16_t idxmap) 630 { 631 tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap); 632 633 /* This should already be page aligned */ 634 addr &= TARGET_PAGE_MASK; 635 636 /* 637 * Allocate memory to hold addr+idxmap only when needed. 638 * See tlb_flush_page_by_mmuidx for details. 639 */ 640 if (idxmap < TARGET_PAGE_SIZE) { 641 flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1, 642 RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 643 } else { 644 CPUState *dst_cpu; 645 646 /* Allocate a separate data block for each destination cpu. */ 647 CPU_FOREACH(dst_cpu) { 648 if (dst_cpu != src_cpu) { 649 TLBFlushPageByMMUIdxData *d 650 = g_new(TLBFlushPageByMMUIdxData, 1); 651 652 d->addr = addr; 653 d->idxmap = idxmap; 654 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2, 655 RUN_ON_CPU_HOST_PTR(d)); 656 } 657 } 658 } 659 660 tlb_flush_page_by_mmuidx_async_0(src_cpu, addr, idxmap); 661 } 662 663 void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr) 664 { 665 tlb_flush_page_by_mmuidx_all_cpus(src, addr, ALL_MMUIDX_BITS); 666 } 667 668 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 669 target_ulong addr, 670 uint16_t idxmap) 671 { 672 tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap); 673 674 /* This should already be page aligned */ 675 addr &= TARGET_PAGE_MASK; 676 677 /* 678 * Allocate memory to hold addr+idxmap only when needed. 679 * See tlb_flush_page_by_mmuidx for details. 680 */ 681 if (idxmap < TARGET_PAGE_SIZE) { 682 flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1, 683 RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 684 async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_1, 685 RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 686 } else { 687 CPUState *dst_cpu; 688 TLBFlushPageByMMUIdxData *d; 689 690 /* Allocate a separate data block for each destination cpu. */ 691 CPU_FOREACH(dst_cpu) { 692 if (dst_cpu != src_cpu) { 693 d = g_new(TLBFlushPageByMMUIdxData, 1); 694 d->addr = addr; 695 d->idxmap = idxmap; 696 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2, 697 RUN_ON_CPU_HOST_PTR(d)); 698 } 699 } 700 701 d = g_new(TLBFlushPageByMMUIdxData, 1); 702 d->addr = addr; 703 d->idxmap = idxmap; 704 async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_2, 705 RUN_ON_CPU_HOST_PTR(d)); 706 } 707 } 708 709 void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr) 710 { 711 tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS); 712 } 713 714 static void tlb_flush_range_locked(CPUArchState *env, int midx, 715 target_ulong addr, target_ulong len, 716 unsigned bits) 717 { 718 CPUTLBDesc *d = &env_tlb(env)->d[midx]; 719 CPUTLBDescFast *f = &env_tlb(env)->f[midx]; 720 target_ulong mask = MAKE_64BIT_MASK(0, bits); 721 722 /* 723 * If @bits is smaller than the tlb size, there may be multiple entries 724 * within the TLB; otherwise all addresses that match under @mask hit 725 * the same TLB entry. 726 * TODO: Perhaps allow bits to be a few bits less than the size. 727 * For now, just flush the entire TLB. 728 * 729 * If @len is larger than the tlb size, then it will take longer to 730 * test all of the entries in the TLB than it will to flush it all. 731 */ 732 if (mask < f->mask || len > f->mask) { 733 tlb_debug("forcing full flush midx %d (" 734 TARGET_FMT_lx "/" TARGET_FMT_lx "+" TARGET_FMT_lx ")\n", 735 midx, addr, mask, len); 736 tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 737 return; 738 } 739 740 /* 741 * Check if we need to flush due to large pages. 742 * Because large_page_mask contains all 1's from the msb, 743 * we only need to test the end of the range. 744 */ 745 if (((addr + len - 1) & d->large_page_mask) == d->large_page_addr) { 746 tlb_debug("forcing full flush midx %d (" 747 TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", 748 midx, d->large_page_addr, d->large_page_mask); 749 tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 750 return; 751 } 752 753 for (target_ulong i = 0; i < len; i += TARGET_PAGE_SIZE) { 754 target_ulong page = addr + i; 755 CPUTLBEntry *entry = tlb_entry(env, midx, page); 756 757 if (tlb_flush_entry_mask_locked(entry, page, mask)) { 758 tlb_n_used_entries_dec(env, midx); 759 } 760 tlb_flush_vtlb_page_mask_locked(env, midx, page, mask); 761 } 762 } 763 764 typedef struct { 765 target_ulong addr; 766 target_ulong len; 767 uint16_t idxmap; 768 uint16_t bits; 769 } TLBFlushRangeData; 770 771 static void tlb_flush_range_by_mmuidx_async_0(CPUState *cpu, 772 TLBFlushRangeData d) 773 { 774 CPUArchState *env = cpu->env_ptr; 775 int mmu_idx; 776 777 assert_cpu_is_self(cpu); 778 779 tlb_debug("range:" TARGET_FMT_lx "/%u+" TARGET_FMT_lx " mmu_map:0x%x\n", 780 d.addr, d.bits, d.len, d.idxmap); 781 782 qemu_spin_lock(&env_tlb(env)->c.lock); 783 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 784 if ((d.idxmap >> mmu_idx) & 1) { 785 tlb_flush_range_locked(env, mmu_idx, d.addr, d.len, d.bits); 786 } 787 } 788 qemu_spin_unlock(&env_tlb(env)->c.lock); 789 790 /* 791 * If the length is larger than the jump cache size, then it will take 792 * longer to clear each entry individually than it will to clear it all. 793 */ 794 if (d.len >= (TARGET_PAGE_SIZE * TB_JMP_CACHE_SIZE)) { 795 tcg_flush_jmp_cache(cpu); 796 return; 797 } 798 799 /* 800 * Discard jump cache entries for any tb which might potentially 801 * overlap the flushed pages, which includes the previous. 802 */ 803 d.addr -= TARGET_PAGE_SIZE; 804 for (target_ulong i = 0, n = d.len / TARGET_PAGE_SIZE + 1; i < n; i++) { 805 tb_jmp_cache_clear_page(cpu, d.addr); 806 d.addr += TARGET_PAGE_SIZE; 807 } 808 } 809 810 static void tlb_flush_range_by_mmuidx_async_1(CPUState *cpu, 811 run_on_cpu_data data) 812 { 813 TLBFlushRangeData *d = data.host_ptr; 814 tlb_flush_range_by_mmuidx_async_0(cpu, *d); 815 g_free(d); 816 } 817 818 void tlb_flush_range_by_mmuidx(CPUState *cpu, target_ulong addr, 819 target_ulong len, uint16_t idxmap, 820 unsigned bits) 821 { 822 TLBFlushRangeData d; 823 824 /* 825 * If all bits are significant, and len is small, 826 * this devolves to tlb_flush_page. 827 */ 828 if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 829 tlb_flush_page_by_mmuidx(cpu, addr, idxmap); 830 return; 831 } 832 /* If no page bits are significant, this devolves to tlb_flush. */ 833 if (bits < TARGET_PAGE_BITS) { 834 tlb_flush_by_mmuidx(cpu, idxmap); 835 return; 836 } 837 838 /* This should already be page aligned */ 839 d.addr = addr & TARGET_PAGE_MASK; 840 d.len = len; 841 d.idxmap = idxmap; 842 d.bits = bits; 843 844 if (qemu_cpu_is_self(cpu)) { 845 tlb_flush_range_by_mmuidx_async_0(cpu, d); 846 } else { 847 /* Otherwise allocate a structure, freed by the worker. */ 848 TLBFlushRangeData *p = g_memdup(&d, sizeof(d)); 849 async_run_on_cpu(cpu, tlb_flush_range_by_mmuidx_async_1, 850 RUN_ON_CPU_HOST_PTR(p)); 851 } 852 } 853 854 void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, target_ulong addr, 855 uint16_t idxmap, unsigned bits) 856 { 857 tlb_flush_range_by_mmuidx(cpu, addr, TARGET_PAGE_SIZE, idxmap, bits); 858 } 859 860 void tlb_flush_range_by_mmuidx_all_cpus(CPUState *src_cpu, 861 target_ulong addr, target_ulong len, 862 uint16_t idxmap, unsigned bits) 863 { 864 TLBFlushRangeData d; 865 CPUState *dst_cpu; 866 867 /* 868 * If all bits are significant, and len is small, 869 * this devolves to tlb_flush_page. 870 */ 871 if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 872 tlb_flush_page_by_mmuidx_all_cpus(src_cpu, addr, idxmap); 873 return; 874 } 875 /* If no page bits are significant, this devolves to tlb_flush. */ 876 if (bits < TARGET_PAGE_BITS) { 877 tlb_flush_by_mmuidx_all_cpus(src_cpu, idxmap); 878 return; 879 } 880 881 /* This should already be page aligned */ 882 d.addr = addr & TARGET_PAGE_MASK; 883 d.len = len; 884 d.idxmap = idxmap; 885 d.bits = bits; 886 887 /* Allocate a separate data block for each destination cpu. */ 888 CPU_FOREACH(dst_cpu) { 889 if (dst_cpu != src_cpu) { 890 TLBFlushRangeData *p = g_memdup(&d, sizeof(d)); 891 async_run_on_cpu(dst_cpu, 892 tlb_flush_range_by_mmuidx_async_1, 893 RUN_ON_CPU_HOST_PTR(p)); 894 } 895 } 896 897 tlb_flush_range_by_mmuidx_async_0(src_cpu, d); 898 } 899 900 void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState *src_cpu, 901 target_ulong addr, 902 uint16_t idxmap, unsigned bits) 903 { 904 tlb_flush_range_by_mmuidx_all_cpus(src_cpu, addr, TARGET_PAGE_SIZE, 905 idxmap, bits); 906 } 907 908 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 909 target_ulong addr, 910 target_ulong len, 911 uint16_t idxmap, 912 unsigned bits) 913 { 914 TLBFlushRangeData d, *p; 915 CPUState *dst_cpu; 916 917 /* 918 * If all bits are significant, and len is small, 919 * this devolves to tlb_flush_page. 920 */ 921 if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 922 tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu, addr, idxmap); 923 return; 924 } 925 /* If no page bits are significant, this devolves to tlb_flush. */ 926 if (bits < TARGET_PAGE_BITS) { 927 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, idxmap); 928 return; 929 } 930 931 /* This should already be page aligned */ 932 d.addr = addr & TARGET_PAGE_MASK; 933 d.len = len; 934 d.idxmap = idxmap; 935 d.bits = bits; 936 937 /* Allocate a separate data block for each destination cpu. */ 938 CPU_FOREACH(dst_cpu) { 939 if (dst_cpu != src_cpu) { 940 p = g_memdup(&d, sizeof(d)); 941 async_run_on_cpu(dst_cpu, tlb_flush_range_by_mmuidx_async_1, 942 RUN_ON_CPU_HOST_PTR(p)); 943 } 944 } 945 946 p = g_memdup(&d, sizeof(d)); 947 async_safe_run_on_cpu(src_cpu, tlb_flush_range_by_mmuidx_async_1, 948 RUN_ON_CPU_HOST_PTR(p)); 949 } 950 951 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 952 target_ulong addr, 953 uint16_t idxmap, 954 unsigned bits) 955 { 956 tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu, addr, TARGET_PAGE_SIZE, 957 idxmap, bits); 958 } 959 960 /* update the TLBs so that writes to code in the virtual page 'addr' 961 can be detected */ 962 void tlb_protect_code(ram_addr_t ram_addr) 963 { 964 cpu_physical_memory_test_and_clear_dirty(ram_addr & TARGET_PAGE_MASK, 965 TARGET_PAGE_SIZE, 966 DIRTY_MEMORY_CODE); 967 } 968 969 /* update the TLB so that writes in physical page 'phys_addr' are no longer 970 tested for self modifying code */ 971 void tlb_unprotect_code(ram_addr_t ram_addr) 972 { 973 cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE); 974 } 975 976 977 /* 978 * Dirty write flag handling 979 * 980 * When the TCG code writes to a location it looks up the address in 981 * the TLB and uses that data to compute the final address. If any of 982 * the lower bits of the address are set then the slow path is forced. 983 * There are a number of reasons to do this but for normal RAM the 984 * most usual is detecting writes to code regions which may invalidate 985 * generated code. 986 * 987 * Other vCPUs might be reading their TLBs during guest execution, so we update 988 * te->addr_write with qatomic_set. We don't need to worry about this for 989 * oversized guests as MTTCG is disabled for them. 990 * 991 * Called with tlb_c.lock held. 992 */ 993 static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry, 994 uintptr_t start, uintptr_t length) 995 { 996 uintptr_t addr = tlb_entry->addr_write; 997 998 if ((addr & (TLB_INVALID_MASK | TLB_MMIO | 999 TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) { 1000 addr &= TARGET_PAGE_MASK; 1001 addr += tlb_entry->addend; 1002 if ((addr - start) < length) { 1003 #if TCG_OVERSIZED_GUEST 1004 tlb_entry->addr_write |= TLB_NOTDIRTY; 1005 #else 1006 qatomic_set(&tlb_entry->addr_write, 1007 tlb_entry->addr_write | TLB_NOTDIRTY); 1008 #endif 1009 } 1010 } 1011 } 1012 1013 /* 1014 * Called with tlb_c.lock held. 1015 * Called only from the vCPU context, i.e. the TLB's owner thread. 1016 */ 1017 static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s) 1018 { 1019 *d = *s; 1020 } 1021 1022 /* This is a cross vCPU call (i.e. another vCPU resetting the flags of 1023 * the target vCPU). 1024 * We must take tlb_c.lock to avoid racing with another vCPU update. The only 1025 * thing actually updated is the target TLB entry ->addr_write flags. 1026 */ 1027 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length) 1028 { 1029 CPUArchState *env; 1030 1031 int mmu_idx; 1032 1033 env = cpu->env_ptr; 1034 qemu_spin_lock(&env_tlb(env)->c.lock); 1035 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1036 unsigned int i; 1037 unsigned int n = tlb_n_entries(&env_tlb(env)->f[mmu_idx]); 1038 1039 for (i = 0; i < n; i++) { 1040 tlb_reset_dirty_range_locked(&env_tlb(env)->f[mmu_idx].table[i], 1041 start1, length); 1042 } 1043 1044 for (i = 0; i < CPU_VTLB_SIZE; i++) { 1045 tlb_reset_dirty_range_locked(&env_tlb(env)->d[mmu_idx].vtable[i], 1046 start1, length); 1047 } 1048 } 1049 qemu_spin_unlock(&env_tlb(env)->c.lock); 1050 } 1051 1052 /* Called with tlb_c.lock held */ 1053 static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry, 1054 target_ulong vaddr) 1055 { 1056 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) { 1057 tlb_entry->addr_write = vaddr; 1058 } 1059 } 1060 1061 /* update the TLB corresponding to virtual page vaddr 1062 so that it is no longer dirty */ 1063 void tlb_set_dirty(CPUState *cpu, target_ulong vaddr) 1064 { 1065 CPUArchState *env = cpu->env_ptr; 1066 int mmu_idx; 1067 1068 assert_cpu_is_self(cpu); 1069 1070 vaddr &= TARGET_PAGE_MASK; 1071 qemu_spin_lock(&env_tlb(env)->c.lock); 1072 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1073 tlb_set_dirty1_locked(tlb_entry(env, mmu_idx, vaddr), vaddr); 1074 } 1075 1076 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1077 int k; 1078 for (k = 0; k < CPU_VTLB_SIZE; k++) { 1079 tlb_set_dirty1_locked(&env_tlb(env)->d[mmu_idx].vtable[k], vaddr); 1080 } 1081 } 1082 qemu_spin_unlock(&env_tlb(env)->c.lock); 1083 } 1084 1085 /* Our TLB does not support large pages, so remember the area covered by 1086 large pages and trigger a full TLB flush if these are invalidated. */ 1087 static void tlb_add_large_page(CPUArchState *env, int mmu_idx, 1088 target_ulong vaddr, target_ulong size) 1089 { 1090 target_ulong lp_addr = env_tlb(env)->d[mmu_idx].large_page_addr; 1091 target_ulong lp_mask = ~(size - 1); 1092 1093 if (lp_addr == (target_ulong)-1) { 1094 /* No previous large page. */ 1095 lp_addr = vaddr; 1096 } else { 1097 /* Extend the existing region to include the new page. 1098 This is a compromise between unnecessary flushes and 1099 the cost of maintaining a full variable size TLB. */ 1100 lp_mask &= env_tlb(env)->d[mmu_idx].large_page_mask; 1101 while (((lp_addr ^ vaddr) & lp_mask) != 0) { 1102 lp_mask <<= 1; 1103 } 1104 } 1105 env_tlb(env)->d[mmu_idx].large_page_addr = lp_addr & lp_mask; 1106 env_tlb(env)->d[mmu_idx].large_page_mask = lp_mask; 1107 } 1108 1109 /* 1110 * Add a new TLB entry. At most one entry for a given virtual address 1111 * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the 1112 * supplied size is only used by tlb_flush_page. 1113 * 1114 * Called from TCG-generated code, which is under an RCU read-side 1115 * critical section. 1116 */ 1117 void tlb_set_page_full(CPUState *cpu, int mmu_idx, 1118 target_ulong vaddr, CPUTLBEntryFull *full) 1119 { 1120 CPUArchState *env = cpu->env_ptr; 1121 CPUTLB *tlb = env_tlb(env); 1122 CPUTLBDesc *desc = &tlb->d[mmu_idx]; 1123 MemoryRegionSection *section; 1124 unsigned int index; 1125 target_ulong address; 1126 target_ulong write_address; 1127 uintptr_t addend; 1128 CPUTLBEntry *te, tn; 1129 hwaddr iotlb, xlat, sz, paddr_page; 1130 target_ulong vaddr_page; 1131 int asidx, wp_flags, prot; 1132 bool is_ram, is_romd; 1133 1134 assert_cpu_is_self(cpu); 1135 1136 if (full->lg_page_size <= TARGET_PAGE_BITS) { 1137 sz = TARGET_PAGE_SIZE; 1138 } else { 1139 sz = (hwaddr)1 << full->lg_page_size; 1140 tlb_add_large_page(env, mmu_idx, vaddr, sz); 1141 } 1142 vaddr_page = vaddr & TARGET_PAGE_MASK; 1143 paddr_page = full->phys_addr & TARGET_PAGE_MASK; 1144 1145 prot = full->prot; 1146 asidx = cpu_asidx_from_attrs(cpu, full->attrs); 1147 section = address_space_translate_for_iotlb(cpu, asidx, paddr_page, 1148 &xlat, &sz, full->attrs, &prot); 1149 assert(sz >= TARGET_PAGE_SIZE); 1150 1151 tlb_debug("vaddr=" TARGET_FMT_lx " paddr=0x" HWADDR_FMT_plx 1152 " prot=%x idx=%d\n", 1153 vaddr, full->phys_addr, prot, mmu_idx); 1154 1155 address = vaddr_page; 1156 if (full->lg_page_size < TARGET_PAGE_BITS) { 1157 /* Repeat the MMU check and TLB fill on every access. */ 1158 address |= TLB_INVALID_MASK; 1159 } 1160 if (full->attrs.byte_swap) { 1161 address |= TLB_BSWAP; 1162 } 1163 1164 is_ram = memory_region_is_ram(section->mr); 1165 is_romd = memory_region_is_romd(section->mr); 1166 1167 if (is_ram || is_romd) { 1168 /* RAM and ROMD both have associated host memory. */ 1169 addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat; 1170 } else { 1171 /* I/O does not; force the host address to NULL. */ 1172 addend = 0; 1173 } 1174 1175 write_address = address; 1176 if (is_ram) { 1177 iotlb = memory_region_get_ram_addr(section->mr) + xlat; 1178 /* 1179 * Computing is_clean is expensive; avoid all that unless 1180 * the page is actually writable. 1181 */ 1182 if (prot & PAGE_WRITE) { 1183 if (section->readonly) { 1184 write_address |= TLB_DISCARD_WRITE; 1185 } else if (cpu_physical_memory_is_clean(iotlb)) { 1186 write_address |= TLB_NOTDIRTY; 1187 } 1188 } 1189 } else { 1190 /* I/O or ROMD */ 1191 iotlb = memory_region_section_get_iotlb(cpu, section) + xlat; 1192 /* 1193 * Writes to romd devices must go through MMIO to enable write. 1194 * Reads to romd devices go through the ram_ptr found above, 1195 * but of course reads to I/O must go through MMIO. 1196 */ 1197 write_address |= TLB_MMIO; 1198 if (!is_romd) { 1199 address = write_address; 1200 } 1201 } 1202 1203 wp_flags = cpu_watchpoint_address_matches(cpu, vaddr_page, 1204 TARGET_PAGE_SIZE); 1205 1206 index = tlb_index(env, mmu_idx, vaddr_page); 1207 te = tlb_entry(env, mmu_idx, vaddr_page); 1208 1209 /* 1210 * Hold the TLB lock for the rest of the function. We could acquire/release 1211 * the lock several times in the function, but it is faster to amortize the 1212 * acquisition cost by acquiring it just once. Note that this leads to 1213 * a longer critical section, but this is not a concern since the TLB lock 1214 * is unlikely to be contended. 1215 */ 1216 qemu_spin_lock(&tlb->c.lock); 1217 1218 /* Note that the tlb is no longer clean. */ 1219 tlb->c.dirty |= 1 << mmu_idx; 1220 1221 /* Make sure there's no cached translation for the new page. */ 1222 tlb_flush_vtlb_page_locked(env, mmu_idx, vaddr_page); 1223 1224 /* 1225 * Only evict the old entry to the victim tlb if it's for a 1226 * different page; otherwise just overwrite the stale data. 1227 */ 1228 if (!tlb_hit_page_anyprot(te, vaddr_page) && !tlb_entry_is_empty(te)) { 1229 unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE; 1230 CPUTLBEntry *tv = &desc->vtable[vidx]; 1231 1232 /* Evict the old entry into the victim tlb. */ 1233 copy_tlb_helper_locked(tv, te); 1234 desc->vfulltlb[vidx] = desc->fulltlb[index]; 1235 tlb_n_used_entries_dec(env, mmu_idx); 1236 } 1237 1238 /* refill the tlb */ 1239 /* 1240 * At this point iotlb contains a physical section number in the lower 1241 * TARGET_PAGE_BITS, and either 1242 * + the ram_addr_t of the page base of the target RAM (RAM) 1243 * + the offset within section->mr of the page base (I/O, ROMD) 1244 * We subtract the vaddr_page (which is page aligned and thus won't 1245 * disturb the low bits) to give an offset which can be added to the 1246 * (non-page-aligned) vaddr of the eventual memory access to get 1247 * the MemoryRegion offset for the access. Note that the vaddr we 1248 * subtract here is that of the page base, and not the same as the 1249 * vaddr we add back in io_readx()/io_writex()/get_page_addr_code(). 1250 */ 1251 desc->fulltlb[index] = *full; 1252 desc->fulltlb[index].xlat_section = iotlb - vaddr_page; 1253 desc->fulltlb[index].phys_addr = paddr_page; 1254 1255 /* Now calculate the new entry */ 1256 tn.addend = addend - vaddr_page; 1257 if (prot & PAGE_READ) { 1258 tn.addr_read = address; 1259 if (wp_flags & BP_MEM_READ) { 1260 tn.addr_read |= TLB_WATCHPOINT; 1261 } 1262 } else { 1263 tn.addr_read = -1; 1264 } 1265 1266 if (prot & PAGE_EXEC) { 1267 tn.addr_code = address; 1268 } else { 1269 tn.addr_code = -1; 1270 } 1271 1272 tn.addr_write = -1; 1273 if (prot & PAGE_WRITE) { 1274 tn.addr_write = write_address; 1275 if (prot & PAGE_WRITE_INV) { 1276 tn.addr_write |= TLB_INVALID_MASK; 1277 } 1278 if (wp_flags & BP_MEM_WRITE) { 1279 tn.addr_write |= TLB_WATCHPOINT; 1280 } 1281 } 1282 1283 copy_tlb_helper_locked(te, &tn); 1284 tlb_n_used_entries_inc(env, mmu_idx); 1285 qemu_spin_unlock(&tlb->c.lock); 1286 } 1287 1288 void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, 1289 hwaddr paddr, MemTxAttrs attrs, int prot, 1290 int mmu_idx, target_ulong size) 1291 { 1292 CPUTLBEntryFull full = { 1293 .phys_addr = paddr, 1294 .attrs = attrs, 1295 .prot = prot, 1296 .lg_page_size = ctz64(size) 1297 }; 1298 1299 assert(is_power_of_2(size)); 1300 tlb_set_page_full(cpu, mmu_idx, vaddr, &full); 1301 } 1302 1303 void tlb_set_page(CPUState *cpu, target_ulong vaddr, 1304 hwaddr paddr, int prot, 1305 int mmu_idx, target_ulong size) 1306 { 1307 tlb_set_page_with_attrs(cpu, vaddr, paddr, MEMTXATTRS_UNSPECIFIED, 1308 prot, mmu_idx, size); 1309 } 1310 1311 /* 1312 * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the 1313 * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must 1314 * be discarded and looked up again (e.g. via tlb_entry()). 1315 */ 1316 static void tlb_fill(CPUState *cpu, target_ulong addr, int size, 1317 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 1318 { 1319 bool ok; 1320 1321 /* 1322 * This is not a probe, so only valid return is success; failure 1323 * should result in exception + longjmp to the cpu loop. 1324 */ 1325 ok = cpu->cc->tcg_ops->tlb_fill(cpu, addr, size, 1326 access_type, mmu_idx, false, retaddr); 1327 assert(ok); 1328 } 1329 1330 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, 1331 MMUAccessType access_type, 1332 int mmu_idx, uintptr_t retaddr) 1333 { 1334 cpu->cc->tcg_ops->do_unaligned_access(cpu, addr, access_type, 1335 mmu_idx, retaddr); 1336 } 1337 1338 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr, 1339 vaddr addr, unsigned size, 1340 MMUAccessType access_type, 1341 int mmu_idx, MemTxAttrs attrs, 1342 MemTxResult response, 1343 uintptr_t retaddr) 1344 { 1345 CPUClass *cc = CPU_GET_CLASS(cpu); 1346 1347 if (!cpu->ignore_memory_transaction_failures && 1348 cc->tcg_ops->do_transaction_failed) { 1349 cc->tcg_ops->do_transaction_failed(cpu, physaddr, addr, size, 1350 access_type, mmu_idx, attrs, 1351 response, retaddr); 1352 } 1353 } 1354 1355 static uint64_t io_readx(CPUArchState *env, CPUTLBEntryFull *full, 1356 int mmu_idx, target_ulong addr, uintptr_t retaddr, 1357 MMUAccessType access_type, MemOp op) 1358 { 1359 CPUState *cpu = env_cpu(env); 1360 hwaddr mr_offset; 1361 MemoryRegionSection *section; 1362 MemoryRegion *mr; 1363 uint64_t val; 1364 MemTxResult r; 1365 1366 section = iotlb_to_section(cpu, full->xlat_section, full->attrs); 1367 mr = section->mr; 1368 mr_offset = (full->xlat_section & TARGET_PAGE_MASK) + addr; 1369 cpu->mem_io_pc = retaddr; 1370 if (!cpu->can_do_io) { 1371 cpu_io_recompile(cpu, retaddr); 1372 } 1373 1374 { 1375 QEMU_IOTHREAD_LOCK_GUARD(); 1376 r = memory_region_dispatch_read(mr, mr_offset, &val, op, full->attrs); 1377 } 1378 1379 if (r != MEMTX_OK) { 1380 hwaddr physaddr = mr_offset + 1381 section->offset_within_address_space - 1382 section->offset_within_region; 1383 1384 cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), access_type, 1385 mmu_idx, full->attrs, r, retaddr); 1386 } 1387 return val; 1388 } 1389 1390 /* 1391 * Save a potentially trashed CPUTLBEntryFull for later lookup by plugin. 1392 * This is read by tlb_plugin_lookup if the fulltlb entry doesn't match 1393 * because of the side effect of io_writex changing memory layout. 1394 */ 1395 static void save_iotlb_data(CPUState *cs, MemoryRegionSection *section, 1396 hwaddr mr_offset) 1397 { 1398 #ifdef CONFIG_PLUGIN 1399 SavedIOTLB *saved = &cs->saved_iotlb; 1400 saved->section = section; 1401 saved->mr_offset = mr_offset; 1402 #endif 1403 } 1404 1405 static void io_writex(CPUArchState *env, CPUTLBEntryFull *full, 1406 int mmu_idx, uint64_t val, target_ulong addr, 1407 uintptr_t retaddr, MemOp op) 1408 { 1409 CPUState *cpu = env_cpu(env); 1410 hwaddr mr_offset; 1411 MemoryRegionSection *section; 1412 MemoryRegion *mr; 1413 MemTxResult r; 1414 1415 section = iotlb_to_section(cpu, full->xlat_section, full->attrs); 1416 mr = section->mr; 1417 mr_offset = (full->xlat_section & TARGET_PAGE_MASK) + addr; 1418 if (!cpu->can_do_io) { 1419 cpu_io_recompile(cpu, retaddr); 1420 } 1421 cpu->mem_io_pc = retaddr; 1422 1423 /* 1424 * The memory_region_dispatch may trigger a flush/resize 1425 * so for plugins we save the iotlb_data just in case. 1426 */ 1427 save_iotlb_data(cpu, section, mr_offset); 1428 1429 { 1430 QEMU_IOTHREAD_LOCK_GUARD(); 1431 r = memory_region_dispatch_write(mr, mr_offset, val, op, full->attrs); 1432 } 1433 1434 if (r != MEMTX_OK) { 1435 hwaddr physaddr = mr_offset + 1436 section->offset_within_address_space - 1437 section->offset_within_region; 1438 1439 cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), 1440 MMU_DATA_STORE, mmu_idx, full->attrs, r, 1441 retaddr); 1442 } 1443 } 1444 1445 /* Return true if ADDR is present in the victim tlb, and has been copied 1446 back to the main tlb. */ 1447 static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index, 1448 MMUAccessType access_type, target_ulong page) 1449 { 1450 size_t vidx; 1451 1452 assert_cpu_is_self(env_cpu(env)); 1453 for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) { 1454 CPUTLBEntry *vtlb = &env_tlb(env)->d[mmu_idx].vtable[vidx]; 1455 target_ulong cmp = tlb_read_idx(vtlb, access_type); 1456 1457 if (cmp == page) { 1458 /* Found entry in victim tlb, swap tlb and iotlb. */ 1459 CPUTLBEntry tmptlb, *tlb = &env_tlb(env)->f[mmu_idx].table[index]; 1460 1461 qemu_spin_lock(&env_tlb(env)->c.lock); 1462 copy_tlb_helper_locked(&tmptlb, tlb); 1463 copy_tlb_helper_locked(tlb, vtlb); 1464 copy_tlb_helper_locked(vtlb, &tmptlb); 1465 qemu_spin_unlock(&env_tlb(env)->c.lock); 1466 1467 CPUTLBEntryFull *f1 = &env_tlb(env)->d[mmu_idx].fulltlb[index]; 1468 CPUTLBEntryFull *f2 = &env_tlb(env)->d[mmu_idx].vfulltlb[vidx]; 1469 CPUTLBEntryFull tmpf; 1470 tmpf = *f1; *f1 = *f2; *f2 = tmpf; 1471 return true; 1472 } 1473 } 1474 return false; 1475 } 1476 1477 static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, 1478 CPUTLBEntryFull *full, uintptr_t retaddr) 1479 { 1480 ram_addr_t ram_addr = mem_vaddr + full->xlat_section; 1481 1482 trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size); 1483 1484 if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) { 1485 tb_invalidate_phys_range_fast(ram_addr, size, retaddr); 1486 } 1487 1488 /* 1489 * Set both VGA and migration bits for simplicity and to remove 1490 * the notdirty callback faster. 1491 */ 1492 cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE); 1493 1494 /* We remove the notdirty callback only if the code has been flushed. */ 1495 if (!cpu_physical_memory_is_clean(ram_addr)) { 1496 trace_memory_notdirty_set_dirty(mem_vaddr); 1497 tlb_set_dirty(cpu, mem_vaddr); 1498 } 1499 } 1500 1501 static int probe_access_internal(CPUArchState *env, target_ulong addr, 1502 int fault_size, MMUAccessType access_type, 1503 int mmu_idx, bool nonfault, 1504 void **phost, CPUTLBEntryFull **pfull, 1505 uintptr_t retaddr) 1506 { 1507 uintptr_t index = tlb_index(env, mmu_idx, addr); 1508 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr); 1509 target_ulong tlb_addr = tlb_read_idx(entry, access_type); 1510 target_ulong page_addr = addr & TARGET_PAGE_MASK; 1511 int flags = TLB_FLAGS_MASK; 1512 1513 if (!tlb_hit_page(tlb_addr, page_addr)) { 1514 if (!victim_tlb_hit(env, mmu_idx, index, access_type, page_addr)) { 1515 CPUState *cs = env_cpu(env); 1516 1517 if (!cs->cc->tcg_ops->tlb_fill(cs, addr, fault_size, access_type, 1518 mmu_idx, nonfault, retaddr)) { 1519 /* Non-faulting page table read failed. */ 1520 *phost = NULL; 1521 *pfull = NULL; 1522 return TLB_INVALID_MASK; 1523 } 1524 1525 /* TLB resize via tlb_fill may have moved the entry. */ 1526 index = tlb_index(env, mmu_idx, addr); 1527 entry = tlb_entry(env, mmu_idx, addr); 1528 1529 /* 1530 * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately, 1531 * to force the next access through tlb_fill. We've just 1532 * called tlb_fill, so we know that this entry *is* valid. 1533 */ 1534 flags &= ~TLB_INVALID_MASK; 1535 } 1536 tlb_addr = tlb_read_idx(entry, access_type); 1537 } 1538 flags &= tlb_addr; 1539 1540 *pfull = &env_tlb(env)->d[mmu_idx].fulltlb[index]; 1541 1542 /* Fold all "mmio-like" bits into TLB_MMIO. This is not RAM. */ 1543 if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY))) { 1544 *phost = NULL; 1545 return TLB_MMIO; 1546 } 1547 1548 /* Everything else is RAM. */ 1549 *phost = (void *)((uintptr_t)addr + entry->addend); 1550 return flags; 1551 } 1552 1553 int probe_access_full(CPUArchState *env, target_ulong addr, int size, 1554 MMUAccessType access_type, int mmu_idx, 1555 bool nonfault, void **phost, CPUTLBEntryFull **pfull, 1556 uintptr_t retaddr) 1557 { 1558 int flags = probe_access_internal(env, addr, size, access_type, mmu_idx, 1559 nonfault, phost, pfull, retaddr); 1560 1561 /* Handle clean RAM pages. */ 1562 if (unlikely(flags & TLB_NOTDIRTY)) { 1563 notdirty_write(env_cpu(env), addr, 1, *pfull, retaddr); 1564 flags &= ~TLB_NOTDIRTY; 1565 } 1566 1567 return flags; 1568 } 1569 1570 int probe_access_flags(CPUArchState *env, target_ulong addr, int size, 1571 MMUAccessType access_type, int mmu_idx, 1572 bool nonfault, void **phost, uintptr_t retaddr) 1573 { 1574 CPUTLBEntryFull *full; 1575 int flags; 1576 1577 g_assert(-(addr | TARGET_PAGE_MASK) >= size); 1578 1579 flags = probe_access_internal(env, addr, size, access_type, mmu_idx, 1580 nonfault, phost, &full, retaddr); 1581 1582 /* Handle clean RAM pages. */ 1583 if (unlikely(flags & TLB_NOTDIRTY)) { 1584 notdirty_write(env_cpu(env), addr, 1, full, retaddr); 1585 flags &= ~TLB_NOTDIRTY; 1586 } 1587 1588 return flags; 1589 } 1590 1591 void *probe_access(CPUArchState *env, target_ulong addr, int size, 1592 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 1593 { 1594 CPUTLBEntryFull *full; 1595 void *host; 1596 int flags; 1597 1598 g_assert(-(addr | TARGET_PAGE_MASK) >= size); 1599 1600 flags = probe_access_internal(env, addr, size, access_type, mmu_idx, 1601 false, &host, &full, retaddr); 1602 1603 /* Per the interface, size == 0 merely faults the access. */ 1604 if (size == 0) { 1605 return NULL; 1606 } 1607 1608 if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) { 1609 /* Handle watchpoints. */ 1610 if (flags & TLB_WATCHPOINT) { 1611 int wp_access = (access_type == MMU_DATA_STORE 1612 ? BP_MEM_WRITE : BP_MEM_READ); 1613 cpu_check_watchpoint(env_cpu(env), addr, size, 1614 full->attrs, wp_access, retaddr); 1615 } 1616 1617 /* Handle clean RAM pages. */ 1618 if (flags & TLB_NOTDIRTY) { 1619 notdirty_write(env_cpu(env), addr, 1, full, retaddr); 1620 } 1621 } 1622 1623 return host; 1624 } 1625 1626 void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr, 1627 MMUAccessType access_type, int mmu_idx) 1628 { 1629 CPUTLBEntryFull *full; 1630 void *host; 1631 int flags; 1632 1633 flags = probe_access_internal(env, addr, 0, access_type, 1634 mmu_idx, true, &host, &full, 0); 1635 1636 /* No combination of flags are expected by the caller. */ 1637 return flags ? NULL : host; 1638 } 1639 1640 /* 1641 * Return a ram_addr_t for the virtual address for execution. 1642 * 1643 * Return -1 if we can't translate and execute from an entire page 1644 * of RAM. This will force us to execute by loading and translating 1645 * one insn at a time, without caching. 1646 * 1647 * NOTE: This function will trigger an exception if the page is 1648 * not executable. 1649 */ 1650 tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr, 1651 void **hostp) 1652 { 1653 CPUTLBEntryFull *full; 1654 void *p; 1655 1656 (void)probe_access_internal(env, addr, 1, MMU_INST_FETCH, 1657 cpu_mmu_index(env, true), false, &p, &full, 0); 1658 if (p == NULL) { 1659 return -1; 1660 } 1661 1662 if (full->lg_page_size < TARGET_PAGE_BITS) { 1663 return -1; 1664 } 1665 1666 if (hostp) { 1667 *hostp = p; 1668 } 1669 return qemu_ram_addr_from_host_nofail(p); 1670 } 1671 1672 /* Load/store with atomicity primitives. */ 1673 #include "ldst_atomicity.c.inc" 1674 1675 #ifdef CONFIG_PLUGIN 1676 /* 1677 * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure. 1678 * This should be a hot path as we will have just looked this path up 1679 * in the softmmu lookup code (or helper). We don't handle re-fills or 1680 * checking the victim table. This is purely informational. 1681 * 1682 * This almost never fails as the memory access being instrumented 1683 * should have just filled the TLB. The one corner case is io_writex 1684 * which can cause TLB flushes and potential resizing of the TLBs 1685 * losing the information we need. In those cases we need to recover 1686 * data from a copy of the CPUTLBEntryFull. As long as this always occurs 1687 * from the same thread (which a mem callback will be) this is safe. 1688 */ 1689 1690 bool tlb_plugin_lookup(CPUState *cpu, target_ulong addr, int mmu_idx, 1691 bool is_store, struct qemu_plugin_hwaddr *data) 1692 { 1693 CPUArchState *env = cpu->env_ptr; 1694 CPUTLBEntry *tlbe = tlb_entry(env, mmu_idx, addr); 1695 uintptr_t index = tlb_index(env, mmu_idx, addr); 1696 target_ulong tlb_addr = is_store ? tlb_addr_write(tlbe) : tlbe->addr_read; 1697 1698 if (likely(tlb_hit(tlb_addr, addr))) { 1699 /* We must have an iotlb entry for MMIO */ 1700 if (tlb_addr & TLB_MMIO) { 1701 CPUTLBEntryFull *full; 1702 full = &env_tlb(env)->d[mmu_idx].fulltlb[index]; 1703 data->is_io = true; 1704 data->v.io.section = 1705 iotlb_to_section(cpu, full->xlat_section, full->attrs); 1706 data->v.io.offset = (full->xlat_section & TARGET_PAGE_MASK) + addr; 1707 } else { 1708 data->is_io = false; 1709 data->v.ram.hostaddr = (void *)((uintptr_t)addr + tlbe->addend); 1710 } 1711 return true; 1712 } else { 1713 SavedIOTLB *saved = &cpu->saved_iotlb; 1714 data->is_io = true; 1715 data->v.io.section = saved->section; 1716 data->v.io.offset = saved->mr_offset; 1717 return true; 1718 } 1719 } 1720 1721 #endif 1722 1723 /* 1724 * Probe for a load/store operation. 1725 * Return the host address and into @flags. 1726 */ 1727 1728 typedef struct MMULookupPageData { 1729 CPUTLBEntryFull *full; 1730 void *haddr; 1731 target_ulong addr; 1732 int flags; 1733 int size; 1734 } MMULookupPageData; 1735 1736 typedef struct MMULookupLocals { 1737 MMULookupPageData page[2]; 1738 MemOp memop; 1739 int mmu_idx; 1740 } MMULookupLocals; 1741 1742 /** 1743 * mmu_lookup1: translate one page 1744 * @env: cpu context 1745 * @data: lookup parameters 1746 * @mmu_idx: virtual address context 1747 * @access_type: load/store/code 1748 * @ra: return address into tcg generated code, or 0 1749 * 1750 * Resolve the translation for the one page at @data.addr, filling in 1751 * the rest of @data with the results. If the translation fails, 1752 * tlb_fill will longjmp out. Return true if the softmmu tlb for 1753 * @mmu_idx may have resized. 1754 */ 1755 static bool mmu_lookup1(CPUArchState *env, MMULookupPageData *data, 1756 int mmu_idx, MMUAccessType access_type, uintptr_t ra) 1757 { 1758 target_ulong addr = data->addr; 1759 uintptr_t index = tlb_index(env, mmu_idx, addr); 1760 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr); 1761 target_ulong tlb_addr = tlb_read_idx(entry, access_type); 1762 bool maybe_resized = false; 1763 1764 /* If the TLB entry is for a different page, reload and try again. */ 1765 if (!tlb_hit(tlb_addr, addr)) { 1766 if (!victim_tlb_hit(env, mmu_idx, index, access_type, 1767 addr & TARGET_PAGE_MASK)) { 1768 tlb_fill(env_cpu(env), addr, data->size, access_type, mmu_idx, ra); 1769 maybe_resized = true; 1770 index = tlb_index(env, mmu_idx, addr); 1771 entry = tlb_entry(env, mmu_idx, addr); 1772 } 1773 tlb_addr = tlb_read_idx(entry, access_type) & ~TLB_INVALID_MASK; 1774 } 1775 1776 data->flags = tlb_addr & TLB_FLAGS_MASK; 1777 data->full = &env_tlb(env)->d[mmu_idx].fulltlb[index]; 1778 /* Compute haddr speculatively; depending on flags it might be invalid. */ 1779 data->haddr = (void *)((uintptr_t)addr + entry->addend); 1780 1781 return maybe_resized; 1782 } 1783 1784 /** 1785 * mmu_watch_or_dirty 1786 * @env: cpu context 1787 * @data: lookup parameters 1788 * @access_type: load/store/code 1789 * @ra: return address into tcg generated code, or 0 1790 * 1791 * Trigger watchpoints for @data.addr:@data.size; 1792 * record writes to protected clean pages. 1793 */ 1794 static void mmu_watch_or_dirty(CPUArchState *env, MMULookupPageData *data, 1795 MMUAccessType access_type, uintptr_t ra) 1796 { 1797 CPUTLBEntryFull *full = data->full; 1798 target_ulong addr = data->addr; 1799 int flags = data->flags; 1800 int size = data->size; 1801 1802 /* On watchpoint hit, this will longjmp out. */ 1803 if (flags & TLB_WATCHPOINT) { 1804 int wp = access_type == MMU_DATA_STORE ? BP_MEM_WRITE : BP_MEM_READ; 1805 cpu_check_watchpoint(env_cpu(env), addr, size, full->attrs, wp, ra); 1806 flags &= ~TLB_WATCHPOINT; 1807 } 1808 1809 /* Note that notdirty is only set for writes. */ 1810 if (flags & TLB_NOTDIRTY) { 1811 notdirty_write(env_cpu(env), addr, size, full, ra); 1812 flags &= ~TLB_NOTDIRTY; 1813 } 1814 data->flags = flags; 1815 } 1816 1817 /** 1818 * mmu_lookup: translate page(s) 1819 * @env: cpu context 1820 * @addr: virtual address 1821 * @oi: combined mmu_idx and MemOp 1822 * @ra: return address into tcg generated code, or 0 1823 * @access_type: load/store/code 1824 * @l: output result 1825 * 1826 * Resolve the translation for the page(s) beginning at @addr, for MemOp.size 1827 * bytes. Return true if the lookup crosses a page boundary. 1828 */ 1829 static bool mmu_lookup(CPUArchState *env, target_ulong addr, MemOpIdx oi, 1830 uintptr_t ra, MMUAccessType type, MMULookupLocals *l) 1831 { 1832 unsigned a_bits; 1833 bool crosspage; 1834 int flags; 1835 1836 l->memop = get_memop(oi); 1837 l->mmu_idx = get_mmuidx(oi); 1838 1839 tcg_debug_assert(l->mmu_idx < NB_MMU_MODES); 1840 1841 /* Handle CPU specific unaligned behaviour */ 1842 a_bits = get_alignment_bits(l->memop); 1843 if (addr & ((1 << a_bits) - 1)) { 1844 cpu_unaligned_access(env_cpu(env), addr, type, l->mmu_idx, ra); 1845 } 1846 1847 l->page[0].addr = addr; 1848 l->page[0].size = memop_size(l->memop); 1849 l->page[1].addr = (addr + l->page[0].size - 1) & TARGET_PAGE_MASK; 1850 l->page[1].size = 0; 1851 crosspage = (addr ^ l->page[1].addr) & TARGET_PAGE_MASK; 1852 1853 if (likely(!crosspage)) { 1854 mmu_lookup1(env, &l->page[0], l->mmu_idx, type, ra); 1855 1856 flags = l->page[0].flags; 1857 if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) { 1858 mmu_watch_or_dirty(env, &l->page[0], type, ra); 1859 } 1860 if (unlikely(flags & TLB_BSWAP)) { 1861 l->memop ^= MO_BSWAP; 1862 } 1863 } else { 1864 /* Finish compute of page crossing. */ 1865 int size0 = l->page[1].addr - addr; 1866 l->page[1].size = l->page[0].size - size0; 1867 l->page[0].size = size0; 1868 1869 /* 1870 * Lookup both pages, recognizing exceptions from either. If the 1871 * second lookup potentially resized, refresh first CPUTLBEntryFull. 1872 */ 1873 mmu_lookup1(env, &l->page[0], l->mmu_idx, type, ra); 1874 if (mmu_lookup1(env, &l->page[1], l->mmu_idx, type, ra)) { 1875 uintptr_t index = tlb_index(env, l->mmu_idx, addr); 1876 l->page[0].full = &env_tlb(env)->d[l->mmu_idx].fulltlb[index]; 1877 } 1878 1879 flags = l->page[0].flags | l->page[1].flags; 1880 if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) { 1881 mmu_watch_or_dirty(env, &l->page[0], type, ra); 1882 mmu_watch_or_dirty(env, &l->page[1], type, ra); 1883 } 1884 1885 /* 1886 * Since target/sparc is the only user of TLB_BSWAP, and all 1887 * Sparc accesses are aligned, any treatment across two pages 1888 * would be arbitrary. Refuse it until there's a use. 1889 */ 1890 tcg_debug_assert((flags & TLB_BSWAP) == 0); 1891 } 1892 1893 return crosspage; 1894 } 1895 1896 /* 1897 * Probe for an atomic operation. Do not allow unaligned operations, 1898 * or io operations to proceed. Return the host address. 1899 */ 1900 static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr, 1901 MemOpIdx oi, int size, uintptr_t retaddr) 1902 { 1903 uintptr_t mmu_idx = get_mmuidx(oi); 1904 MemOp mop = get_memop(oi); 1905 int a_bits = get_alignment_bits(mop); 1906 uintptr_t index; 1907 CPUTLBEntry *tlbe; 1908 target_ulong tlb_addr; 1909 void *hostaddr; 1910 CPUTLBEntryFull *full; 1911 1912 tcg_debug_assert(mmu_idx < NB_MMU_MODES); 1913 1914 /* Adjust the given return address. */ 1915 retaddr -= GETPC_ADJ; 1916 1917 /* Enforce guest required alignment. */ 1918 if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) { 1919 /* ??? Maybe indicate atomic op to cpu_unaligned_access */ 1920 cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE, 1921 mmu_idx, retaddr); 1922 } 1923 1924 /* Enforce qemu required alignment. */ 1925 if (unlikely(addr & (size - 1))) { 1926 /* We get here if guest alignment was not requested, 1927 or was not enforced by cpu_unaligned_access above. 1928 We might widen the access and emulate, but for now 1929 mark an exception and exit the cpu loop. */ 1930 goto stop_the_world; 1931 } 1932 1933 index = tlb_index(env, mmu_idx, addr); 1934 tlbe = tlb_entry(env, mmu_idx, addr); 1935 1936 /* Check TLB entry and enforce page permissions. */ 1937 tlb_addr = tlb_addr_write(tlbe); 1938 if (!tlb_hit(tlb_addr, addr)) { 1939 if (!victim_tlb_hit(env, mmu_idx, index, MMU_DATA_STORE, 1940 addr & TARGET_PAGE_MASK)) { 1941 tlb_fill(env_cpu(env), addr, size, 1942 MMU_DATA_STORE, mmu_idx, retaddr); 1943 index = tlb_index(env, mmu_idx, addr); 1944 tlbe = tlb_entry(env, mmu_idx, addr); 1945 } 1946 tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK; 1947 } 1948 1949 /* 1950 * Let the guest notice RMW on a write-only page. 1951 * We have just verified that the page is writable. 1952 * Subpage lookups may have left TLB_INVALID_MASK set, 1953 * but addr_read will only be -1 if PAGE_READ was unset. 1954 */ 1955 if (unlikely(tlbe->addr_read == -1)) { 1956 tlb_fill(env_cpu(env), addr, size, MMU_DATA_LOAD, mmu_idx, retaddr); 1957 /* 1958 * Since we don't support reads and writes to different 1959 * addresses, and we do have the proper page loaded for 1960 * write, this shouldn't ever return. But just in case, 1961 * handle via stop-the-world. 1962 */ 1963 goto stop_the_world; 1964 } 1965 /* Collect TLB_WATCHPOINT for read. */ 1966 tlb_addr |= tlbe->addr_read; 1967 1968 /* Notice an IO access or a needs-MMU-lookup access */ 1969 if (unlikely(tlb_addr & (TLB_MMIO | TLB_DISCARD_WRITE))) { 1970 /* There's really nothing that can be done to 1971 support this apart from stop-the-world. */ 1972 goto stop_the_world; 1973 } 1974 1975 hostaddr = (void *)((uintptr_t)addr + tlbe->addend); 1976 full = &env_tlb(env)->d[mmu_idx].fulltlb[index]; 1977 1978 if (unlikely(tlb_addr & TLB_NOTDIRTY)) { 1979 notdirty_write(env_cpu(env), addr, size, full, retaddr); 1980 } 1981 1982 if (unlikely(tlb_addr & TLB_WATCHPOINT)) { 1983 cpu_check_watchpoint(env_cpu(env), addr, size, full->attrs, 1984 BP_MEM_READ | BP_MEM_WRITE, retaddr); 1985 } 1986 1987 return hostaddr; 1988 1989 stop_the_world: 1990 cpu_loop_exit_atomic(env_cpu(env), retaddr); 1991 } 1992 1993 /* 1994 * Load Helpers 1995 * 1996 * We support two different access types. SOFTMMU_CODE_ACCESS is 1997 * specifically for reading instructions from system memory. It is 1998 * called by the translation loop and in some helpers where the code 1999 * is disassembled. It shouldn't be called directly by guest code. 2000 * 2001 * For the benefit of TCG generated code, we want to avoid the 2002 * complication of ABI-specific return type promotion and always 2003 * return a value extended to the register size of the host. This is 2004 * tcg_target_long, except in the case of a 32-bit host and 64-bit 2005 * data, and for that we always have uint64_t. 2006 * 2007 * We don't bother with this widened value for SOFTMMU_CODE_ACCESS. 2008 */ 2009 2010 /** 2011 * do_ld_mmio_beN: 2012 * @env: cpu context 2013 * @p: translation parameters 2014 * @ret_be: accumulated data 2015 * @mmu_idx: virtual address context 2016 * @ra: return address into tcg generated code, or 0 2017 * 2018 * Load @p->size bytes from @p->addr, which is memory-mapped i/o. 2019 * The bytes are concatenated in big-endian order with @ret_be. 2020 */ 2021 static uint64_t do_ld_mmio_beN(CPUArchState *env, MMULookupPageData *p, 2022 uint64_t ret_be, int mmu_idx, 2023 MMUAccessType type, uintptr_t ra) 2024 { 2025 CPUTLBEntryFull *full = p->full; 2026 target_ulong addr = p->addr; 2027 int i, size = p->size; 2028 2029 QEMU_IOTHREAD_LOCK_GUARD(); 2030 for (i = 0; i < size; i++) { 2031 uint8_t x = io_readx(env, full, mmu_idx, addr + i, ra, type, MO_UB); 2032 ret_be = (ret_be << 8) | x; 2033 } 2034 return ret_be; 2035 } 2036 2037 /** 2038 * do_ld_bytes_beN 2039 * @p: translation parameters 2040 * @ret_be: accumulated data 2041 * 2042 * Load @p->size bytes from @p->haddr, which is RAM. 2043 * The bytes to concatenated in big-endian order with @ret_be. 2044 */ 2045 static uint64_t do_ld_bytes_beN(MMULookupPageData *p, uint64_t ret_be) 2046 { 2047 uint8_t *haddr = p->haddr; 2048 int i, size = p->size; 2049 2050 for (i = 0; i < size; i++) { 2051 ret_be = (ret_be << 8) | haddr[i]; 2052 } 2053 return ret_be; 2054 } 2055 2056 /** 2057 * do_ld_parts_beN 2058 * @p: translation parameters 2059 * @ret_be: accumulated data 2060 * 2061 * As do_ld_bytes_beN, but atomically on each aligned part. 2062 */ 2063 static uint64_t do_ld_parts_beN(MMULookupPageData *p, uint64_t ret_be) 2064 { 2065 void *haddr = p->haddr; 2066 int size = p->size; 2067 2068 do { 2069 uint64_t x; 2070 int n; 2071 2072 /* 2073 * Find minimum of alignment and size. 2074 * This is slightly stronger than required by MO_ATOM_SUBALIGN, which 2075 * would have only checked the low bits of addr|size once at the start, 2076 * but is just as easy. 2077 */ 2078 switch (((uintptr_t)haddr | size) & 7) { 2079 case 4: 2080 x = cpu_to_be32(load_atomic4(haddr)); 2081 ret_be = (ret_be << 32) | x; 2082 n = 4; 2083 break; 2084 case 2: 2085 case 6: 2086 x = cpu_to_be16(load_atomic2(haddr)); 2087 ret_be = (ret_be << 16) | x; 2088 n = 2; 2089 break; 2090 default: 2091 x = *(uint8_t *)haddr; 2092 ret_be = (ret_be << 8) | x; 2093 n = 1; 2094 break; 2095 case 0: 2096 g_assert_not_reached(); 2097 } 2098 haddr += n; 2099 size -= n; 2100 } while (size != 0); 2101 return ret_be; 2102 } 2103 2104 /** 2105 * do_ld_parts_be4 2106 * @p: translation parameters 2107 * @ret_be: accumulated data 2108 * 2109 * As do_ld_bytes_beN, but with one atomic load. 2110 * Four aligned bytes are guaranteed to cover the load. 2111 */ 2112 static uint64_t do_ld_whole_be4(MMULookupPageData *p, uint64_t ret_be) 2113 { 2114 int o = p->addr & 3; 2115 uint32_t x = load_atomic4(p->haddr - o); 2116 2117 x = cpu_to_be32(x); 2118 x <<= o * 8; 2119 x >>= (4 - p->size) * 8; 2120 return (ret_be << (p->size * 8)) | x; 2121 } 2122 2123 /** 2124 * do_ld_parts_be8 2125 * @p: translation parameters 2126 * @ret_be: accumulated data 2127 * 2128 * As do_ld_bytes_beN, but with one atomic load. 2129 * Eight aligned bytes are guaranteed to cover the load. 2130 */ 2131 static uint64_t do_ld_whole_be8(CPUArchState *env, uintptr_t ra, 2132 MMULookupPageData *p, uint64_t ret_be) 2133 { 2134 int o = p->addr & 7; 2135 uint64_t x = load_atomic8_or_exit(env, ra, p->haddr - o); 2136 2137 x = cpu_to_be64(x); 2138 x <<= o * 8; 2139 x >>= (8 - p->size) * 8; 2140 return (ret_be << (p->size * 8)) | x; 2141 } 2142 2143 /** 2144 * do_ld_parts_be16 2145 * @p: translation parameters 2146 * @ret_be: accumulated data 2147 * 2148 * As do_ld_bytes_beN, but with one atomic load. 2149 * 16 aligned bytes are guaranteed to cover the load. 2150 */ 2151 static Int128 do_ld_whole_be16(CPUArchState *env, uintptr_t ra, 2152 MMULookupPageData *p, uint64_t ret_be) 2153 { 2154 int o = p->addr & 15; 2155 Int128 x, y = load_atomic16_or_exit(env, ra, p->haddr - o); 2156 int size = p->size; 2157 2158 if (!HOST_BIG_ENDIAN) { 2159 y = bswap128(y); 2160 } 2161 y = int128_lshift(y, o * 8); 2162 y = int128_urshift(y, (16 - size) * 8); 2163 x = int128_make64(ret_be); 2164 x = int128_lshift(x, size * 8); 2165 return int128_or(x, y); 2166 } 2167 2168 /* 2169 * Wrapper for the above. 2170 */ 2171 static uint64_t do_ld_beN(CPUArchState *env, MMULookupPageData *p, 2172 uint64_t ret_be, int mmu_idx, MMUAccessType type, 2173 MemOp mop, uintptr_t ra) 2174 { 2175 MemOp atom; 2176 unsigned tmp, half_size; 2177 2178 if (unlikely(p->flags & TLB_MMIO)) { 2179 return do_ld_mmio_beN(env, p, ret_be, mmu_idx, type, ra); 2180 } 2181 2182 /* 2183 * It is a given that we cross a page and therefore there is no 2184 * atomicity for the load as a whole, but subobjects may need attention. 2185 */ 2186 atom = mop & MO_ATOM_MASK; 2187 switch (atom) { 2188 case MO_ATOM_SUBALIGN: 2189 return do_ld_parts_beN(p, ret_be); 2190 2191 case MO_ATOM_IFALIGN_PAIR: 2192 case MO_ATOM_WITHIN16_PAIR: 2193 tmp = mop & MO_SIZE; 2194 tmp = tmp ? tmp - 1 : 0; 2195 half_size = 1 << tmp; 2196 if (atom == MO_ATOM_IFALIGN_PAIR 2197 ? p->size == half_size 2198 : p->size >= half_size) { 2199 if (!HAVE_al8_fast && p->size < 4) { 2200 return do_ld_whole_be4(p, ret_be); 2201 } else { 2202 return do_ld_whole_be8(env, ra, p, ret_be); 2203 } 2204 } 2205 /* fall through */ 2206 2207 case MO_ATOM_IFALIGN: 2208 case MO_ATOM_WITHIN16: 2209 case MO_ATOM_NONE: 2210 return do_ld_bytes_beN(p, ret_be); 2211 2212 default: 2213 g_assert_not_reached(); 2214 } 2215 } 2216 2217 /* 2218 * Wrapper for the above, for 8 < size < 16. 2219 */ 2220 static Int128 do_ld16_beN(CPUArchState *env, MMULookupPageData *p, 2221 uint64_t a, int mmu_idx, MemOp mop, uintptr_t ra) 2222 { 2223 int size = p->size; 2224 uint64_t b; 2225 MemOp atom; 2226 2227 if (unlikely(p->flags & TLB_MMIO)) { 2228 p->size = size - 8; 2229 a = do_ld_mmio_beN(env, p, a, mmu_idx, MMU_DATA_LOAD, ra); 2230 p->addr += p->size; 2231 p->size = 8; 2232 b = do_ld_mmio_beN(env, p, 0, mmu_idx, MMU_DATA_LOAD, ra); 2233 return int128_make128(b, a); 2234 } 2235 2236 /* 2237 * It is a given that we cross a page and therefore there is no 2238 * atomicity for the load as a whole, but subobjects may need attention. 2239 */ 2240 atom = mop & MO_ATOM_MASK; 2241 switch (atom) { 2242 case MO_ATOM_SUBALIGN: 2243 p->size = size - 8; 2244 a = do_ld_parts_beN(p, a); 2245 p->haddr += size - 8; 2246 p->size = 8; 2247 b = do_ld_parts_beN(p, 0); 2248 break; 2249 2250 case MO_ATOM_WITHIN16_PAIR: 2251 /* Since size > 8, this is the half that must be atomic. */ 2252 return do_ld_whole_be16(env, ra, p, a); 2253 2254 case MO_ATOM_IFALIGN_PAIR: 2255 /* 2256 * Since size > 8, both halves are misaligned, 2257 * and so neither is atomic. 2258 */ 2259 case MO_ATOM_IFALIGN: 2260 case MO_ATOM_WITHIN16: 2261 case MO_ATOM_NONE: 2262 p->size = size - 8; 2263 a = do_ld_bytes_beN(p, a); 2264 b = ldq_be_p(p->haddr + size - 8); 2265 break; 2266 2267 default: 2268 g_assert_not_reached(); 2269 } 2270 2271 return int128_make128(b, a); 2272 } 2273 2274 static uint8_t do_ld_1(CPUArchState *env, MMULookupPageData *p, int mmu_idx, 2275 MMUAccessType type, uintptr_t ra) 2276 { 2277 if (unlikely(p->flags & TLB_MMIO)) { 2278 return io_readx(env, p->full, mmu_idx, p->addr, ra, type, MO_UB); 2279 } else { 2280 return *(uint8_t *)p->haddr; 2281 } 2282 } 2283 2284 static uint16_t do_ld_2(CPUArchState *env, MMULookupPageData *p, int mmu_idx, 2285 MMUAccessType type, MemOp memop, uintptr_t ra) 2286 { 2287 uint64_t ret; 2288 2289 if (unlikely(p->flags & TLB_MMIO)) { 2290 return io_readx(env, p->full, mmu_idx, p->addr, ra, type, memop); 2291 } 2292 2293 /* Perform the load host endian, then swap if necessary. */ 2294 ret = load_atom_2(env, ra, p->haddr, memop); 2295 if (memop & MO_BSWAP) { 2296 ret = bswap16(ret); 2297 } 2298 return ret; 2299 } 2300 2301 static uint32_t do_ld_4(CPUArchState *env, MMULookupPageData *p, int mmu_idx, 2302 MMUAccessType type, MemOp memop, uintptr_t ra) 2303 { 2304 uint32_t ret; 2305 2306 if (unlikely(p->flags & TLB_MMIO)) { 2307 return io_readx(env, p->full, mmu_idx, p->addr, ra, type, memop); 2308 } 2309 2310 /* Perform the load host endian. */ 2311 ret = load_atom_4(env, ra, p->haddr, memop); 2312 if (memop & MO_BSWAP) { 2313 ret = bswap32(ret); 2314 } 2315 return ret; 2316 } 2317 2318 static uint64_t do_ld_8(CPUArchState *env, MMULookupPageData *p, int mmu_idx, 2319 MMUAccessType type, MemOp memop, uintptr_t ra) 2320 { 2321 uint64_t ret; 2322 2323 if (unlikely(p->flags & TLB_MMIO)) { 2324 return io_readx(env, p->full, mmu_idx, p->addr, ra, type, memop); 2325 } 2326 2327 /* Perform the load host endian. */ 2328 ret = load_atom_8(env, ra, p->haddr, memop); 2329 if (memop & MO_BSWAP) { 2330 ret = bswap64(ret); 2331 } 2332 return ret; 2333 } 2334 2335 static uint8_t do_ld1_mmu(CPUArchState *env, target_ulong addr, MemOpIdx oi, 2336 uintptr_t ra, MMUAccessType access_type) 2337 { 2338 MMULookupLocals l; 2339 bool crosspage; 2340 2341 crosspage = mmu_lookup(env, addr, oi, ra, access_type, &l); 2342 tcg_debug_assert(!crosspage); 2343 2344 return do_ld_1(env, &l.page[0], l.mmu_idx, access_type, ra); 2345 } 2346 2347 tcg_target_ulong helper_ldub_mmu(CPUArchState *env, uint64_t addr, 2348 MemOpIdx oi, uintptr_t retaddr) 2349 { 2350 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_8); 2351 return do_ld1_mmu(env, addr, oi, retaddr, MMU_DATA_LOAD); 2352 } 2353 2354 static uint16_t do_ld2_mmu(CPUArchState *env, target_ulong addr, MemOpIdx oi, 2355 uintptr_t ra, MMUAccessType access_type) 2356 { 2357 MMULookupLocals l; 2358 bool crosspage; 2359 uint16_t ret; 2360 uint8_t a, b; 2361 2362 crosspage = mmu_lookup(env, addr, oi, ra, access_type, &l); 2363 if (likely(!crosspage)) { 2364 return do_ld_2(env, &l.page[0], l.mmu_idx, access_type, l.memop, ra); 2365 } 2366 2367 a = do_ld_1(env, &l.page[0], l.mmu_idx, access_type, ra); 2368 b = do_ld_1(env, &l.page[1], l.mmu_idx, access_type, ra); 2369 2370 if ((l.memop & MO_BSWAP) == MO_LE) { 2371 ret = a | (b << 8); 2372 } else { 2373 ret = b | (a << 8); 2374 } 2375 return ret; 2376 } 2377 2378 tcg_target_ulong helper_lduw_mmu(CPUArchState *env, uint64_t addr, 2379 MemOpIdx oi, uintptr_t retaddr) 2380 { 2381 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_16); 2382 return do_ld2_mmu(env, addr, oi, retaddr, MMU_DATA_LOAD); 2383 } 2384 2385 static uint32_t do_ld4_mmu(CPUArchState *env, target_ulong addr, MemOpIdx oi, 2386 uintptr_t ra, MMUAccessType access_type) 2387 { 2388 MMULookupLocals l; 2389 bool crosspage; 2390 uint32_t ret; 2391 2392 crosspage = mmu_lookup(env, addr, oi, ra, access_type, &l); 2393 if (likely(!crosspage)) { 2394 return do_ld_4(env, &l.page[0], l.mmu_idx, access_type, l.memop, ra); 2395 } 2396 2397 ret = do_ld_beN(env, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra); 2398 ret = do_ld_beN(env, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra); 2399 if ((l.memop & MO_BSWAP) == MO_LE) { 2400 ret = bswap32(ret); 2401 } 2402 return ret; 2403 } 2404 2405 tcg_target_ulong helper_ldul_mmu(CPUArchState *env, uint64_t addr, 2406 MemOpIdx oi, uintptr_t retaddr) 2407 { 2408 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_32); 2409 return do_ld4_mmu(env, addr, oi, retaddr, MMU_DATA_LOAD); 2410 } 2411 2412 static uint64_t do_ld8_mmu(CPUArchState *env, target_ulong addr, MemOpIdx oi, 2413 uintptr_t ra, MMUAccessType access_type) 2414 { 2415 MMULookupLocals l; 2416 bool crosspage; 2417 uint64_t ret; 2418 2419 crosspage = mmu_lookup(env, addr, oi, ra, access_type, &l); 2420 if (likely(!crosspage)) { 2421 return do_ld_8(env, &l.page[0], l.mmu_idx, access_type, l.memop, ra); 2422 } 2423 2424 ret = do_ld_beN(env, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra); 2425 ret = do_ld_beN(env, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra); 2426 if ((l.memop & MO_BSWAP) == MO_LE) { 2427 ret = bswap64(ret); 2428 } 2429 return ret; 2430 } 2431 2432 uint64_t helper_ldq_mmu(CPUArchState *env, uint64_t addr, 2433 MemOpIdx oi, uintptr_t retaddr) 2434 { 2435 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_64); 2436 return do_ld8_mmu(env, addr, oi, retaddr, MMU_DATA_LOAD); 2437 } 2438 2439 /* 2440 * Provide signed versions of the load routines as well. We can of course 2441 * avoid this for 64-bit data, or for 32-bit data on 32-bit host. 2442 */ 2443 2444 tcg_target_ulong helper_ldsb_mmu(CPUArchState *env, uint64_t addr, 2445 MemOpIdx oi, uintptr_t retaddr) 2446 { 2447 return (int8_t)helper_ldub_mmu(env, addr, oi, retaddr); 2448 } 2449 2450 tcg_target_ulong helper_ldsw_mmu(CPUArchState *env, uint64_t addr, 2451 MemOpIdx oi, uintptr_t retaddr) 2452 { 2453 return (int16_t)helper_lduw_mmu(env, addr, oi, retaddr); 2454 } 2455 2456 tcg_target_ulong helper_ldsl_mmu(CPUArchState *env, uint64_t addr, 2457 MemOpIdx oi, uintptr_t retaddr) 2458 { 2459 return (int32_t)helper_ldul_mmu(env, addr, oi, retaddr); 2460 } 2461 2462 static Int128 do_ld16_mmu(CPUArchState *env, target_ulong addr, 2463 MemOpIdx oi, uintptr_t ra) 2464 { 2465 MMULookupLocals l; 2466 bool crosspage; 2467 uint64_t a, b; 2468 Int128 ret; 2469 int first; 2470 2471 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD, &l); 2472 if (likely(!crosspage)) { 2473 /* Perform the load host endian. */ 2474 if (unlikely(l.page[0].flags & TLB_MMIO)) { 2475 QEMU_IOTHREAD_LOCK_GUARD(); 2476 a = io_readx(env, l.page[0].full, l.mmu_idx, addr, 2477 ra, MMU_DATA_LOAD, MO_64); 2478 b = io_readx(env, l.page[0].full, l.mmu_idx, addr + 8, 2479 ra, MMU_DATA_LOAD, MO_64); 2480 ret = int128_make128(HOST_BIG_ENDIAN ? b : a, 2481 HOST_BIG_ENDIAN ? a : b); 2482 } else { 2483 ret = load_atom_16(env, ra, l.page[0].haddr, l.memop); 2484 } 2485 if (l.memop & MO_BSWAP) { 2486 ret = bswap128(ret); 2487 } 2488 return ret; 2489 } 2490 2491 first = l.page[0].size; 2492 if (first == 8) { 2493 MemOp mop8 = (l.memop & ~MO_SIZE) | MO_64; 2494 2495 a = do_ld_8(env, &l.page[0], l.mmu_idx, MMU_DATA_LOAD, mop8, ra); 2496 b = do_ld_8(env, &l.page[1], l.mmu_idx, MMU_DATA_LOAD, mop8, ra); 2497 if ((mop8 & MO_BSWAP) == MO_LE) { 2498 ret = int128_make128(a, b); 2499 } else { 2500 ret = int128_make128(b, a); 2501 } 2502 return ret; 2503 } 2504 2505 if (first < 8) { 2506 a = do_ld_beN(env, &l.page[0], 0, l.mmu_idx, 2507 MMU_DATA_LOAD, l.memop, ra); 2508 ret = do_ld16_beN(env, &l.page[1], a, l.mmu_idx, l.memop, ra); 2509 } else { 2510 ret = do_ld16_beN(env, &l.page[0], 0, l.mmu_idx, l.memop, ra); 2511 b = int128_getlo(ret); 2512 ret = int128_lshift(ret, l.page[1].size * 8); 2513 a = int128_gethi(ret); 2514 b = do_ld_beN(env, &l.page[1], b, l.mmu_idx, 2515 MMU_DATA_LOAD, l.memop, ra); 2516 ret = int128_make128(b, a); 2517 } 2518 if ((l.memop & MO_BSWAP) == MO_LE) { 2519 ret = bswap128(ret); 2520 } 2521 return ret; 2522 } 2523 2524 Int128 helper_ld16_mmu(CPUArchState *env, uint64_t addr, 2525 uint32_t oi, uintptr_t retaddr) 2526 { 2527 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_128); 2528 return do_ld16_mmu(env, addr, oi, retaddr); 2529 } 2530 2531 Int128 helper_ld_i128(CPUArchState *env, uint64_t addr, uint32_t oi) 2532 { 2533 return helper_ld16_mmu(env, addr, oi, GETPC()); 2534 } 2535 2536 /* 2537 * Load helpers for cpu_ldst.h. 2538 */ 2539 2540 static void plugin_load_cb(CPUArchState *env, abi_ptr addr, MemOpIdx oi) 2541 { 2542 qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 2543 } 2544 2545 uint8_t cpu_ldb_mmu(CPUArchState *env, abi_ptr addr, MemOpIdx oi, uintptr_t ra) 2546 { 2547 uint8_t ret; 2548 2549 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_UB); 2550 ret = do_ld1_mmu(env, addr, oi, ra, MMU_DATA_LOAD); 2551 plugin_load_cb(env, addr, oi); 2552 return ret; 2553 } 2554 2555 uint16_t cpu_ldw_mmu(CPUArchState *env, abi_ptr addr, 2556 MemOpIdx oi, uintptr_t ra) 2557 { 2558 uint16_t ret; 2559 2560 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_16); 2561 ret = do_ld2_mmu(env, addr, oi, ra, MMU_DATA_LOAD); 2562 plugin_load_cb(env, addr, oi); 2563 return ret; 2564 } 2565 2566 uint32_t cpu_ldl_mmu(CPUArchState *env, abi_ptr addr, 2567 MemOpIdx oi, uintptr_t ra) 2568 { 2569 uint32_t ret; 2570 2571 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_32); 2572 ret = do_ld4_mmu(env, addr, oi, ra, MMU_DATA_LOAD); 2573 plugin_load_cb(env, addr, oi); 2574 return ret; 2575 } 2576 2577 uint64_t cpu_ldq_mmu(CPUArchState *env, abi_ptr addr, 2578 MemOpIdx oi, uintptr_t ra) 2579 { 2580 uint64_t ret; 2581 2582 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_64); 2583 ret = do_ld8_mmu(env, addr, oi, ra, MMU_DATA_LOAD); 2584 plugin_load_cb(env, addr, oi); 2585 return ret; 2586 } 2587 2588 Int128 cpu_ld16_mmu(CPUArchState *env, abi_ptr addr, 2589 MemOpIdx oi, uintptr_t ra) 2590 { 2591 Int128 ret; 2592 2593 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_128); 2594 ret = do_ld16_mmu(env, addr, oi, ra); 2595 plugin_load_cb(env, addr, oi); 2596 return ret; 2597 } 2598 2599 /* 2600 * Store Helpers 2601 */ 2602 2603 /** 2604 * do_st_mmio_leN: 2605 * @env: cpu context 2606 * @p: translation parameters 2607 * @val_le: data to store 2608 * @mmu_idx: virtual address context 2609 * @ra: return address into tcg generated code, or 0 2610 * 2611 * Store @p->size bytes at @p->addr, which is memory-mapped i/o. 2612 * The bytes to store are extracted in little-endian order from @val_le; 2613 * return the bytes of @val_le beyond @p->size that have not been stored. 2614 */ 2615 static uint64_t do_st_mmio_leN(CPUArchState *env, MMULookupPageData *p, 2616 uint64_t val_le, int mmu_idx, uintptr_t ra) 2617 { 2618 CPUTLBEntryFull *full = p->full; 2619 target_ulong addr = p->addr; 2620 int i, size = p->size; 2621 2622 QEMU_IOTHREAD_LOCK_GUARD(); 2623 for (i = 0; i < size; i++, val_le >>= 8) { 2624 io_writex(env, full, mmu_idx, val_le, addr + i, ra, MO_UB); 2625 } 2626 return val_le; 2627 } 2628 2629 /* 2630 * Wrapper for the above. 2631 */ 2632 static uint64_t do_st_leN(CPUArchState *env, MMULookupPageData *p, 2633 uint64_t val_le, int mmu_idx, 2634 MemOp mop, uintptr_t ra) 2635 { 2636 MemOp atom; 2637 unsigned tmp, half_size; 2638 2639 if (unlikely(p->flags & TLB_MMIO)) { 2640 return do_st_mmio_leN(env, p, val_le, mmu_idx, ra); 2641 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2642 return val_le >> (p->size * 8); 2643 } 2644 2645 /* 2646 * It is a given that we cross a page and therefore there is no atomicity 2647 * for the store as a whole, but subobjects may need attention. 2648 */ 2649 atom = mop & MO_ATOM_MASK; 2650 switch (atom) { 2651 case MO_ATOM_SUBALIGN: 2652 return store_parts_leN(p->haddr, p->size, val_le); 2653 2654 case MO_ATOM_IFALIGN_PAIR: 2655 case MO_ATOM_WITHIN16_PAIR: 2656 tmp = mop & MO_SIZE; 2657 tmp = tmp ? tmp - 1 : 0; 2658 half_size = 1 << tmp; 2659 if (atom == MO_ATOM_IFALIGN_PAIR 2660 ? p->size == half_size 2661 : p->size >= half_size) { 2662 if (!HAVE_al8_fast && p->size <= 4) { 2663 return store_whole_le4(p->haddr, p->size, val_le); 2664 } else if (HAVE_al8) { 2665 return store_whole_le8(p->haddr, p->size, val_le); 2666 } else { 2667 cpu_loop_exit_atomic(env_cpu(env), ra); 2668 } 2669 } 2670 /* fall through */ 2671 2672 case MO_ATOM_IFALIGN: 2673 case MO_ATOM_WITHIN16: 2674 case MO_ATOM_NONE: 2675 return store_bytes_leN(p->haddr, p->size, val_le); 2676 2677 default: 2678 g_assert_not_reached(); 2679 } 2680 } 2681 2682 /* 2683 * Wrapper for the above, for 8 < size < 16. 2684 */ 2685 static uint64_t do_st16_leN(CPUArchState *env, MMULookupPageData *p, 2686 Int128 val_le, int mmu_idx, 2687 MemOp mop, uintptr_t ra) 2688 { 2689 int size = p->size; 2690 MemOp atom; 2691 2692 if (unlikely(p->flags & TLB_MMIO)) { 2693 p->size = 8; 2694 do_st_mmio_leN(env, p, int128_getlo(val_le), mmu_idx, ra); 2695 p->size = size - 8; 2696 p->addr += 8; 2697 return do_st_mmio_leN(env, p, int128_gethi(val_le), mmu_idx, ra); 2698 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2699 return int128_gethi(val_le) >> ((size - 8) * 8); 2700 } 2701 2702 /* 2703 * It is a given that we cross a page and therefore there is no atomicity 2704 * for the store as a whole, but subobjects may need attention. 2705 */ 2706 atom = mop & MO_ATOM_MASK; 2707 switch (atom) { 2708 case MO_ATOM_SUBALIGN: 2709 store_parts_leN(p->haddr, 8, int128_getlo(val_le)); 2710 return store_parts_leN(p->haddr + 8, p->size - 8, 2711 int128_gethi(val_le)); 2712 2713 case MO_ATOM_WITHIN16_PAIR: 2714 /* Since size > 8, this is the half that must be atomic. */ 2715 if (!HAVE_ATOMIC128_RW) { 2716 cpu_loop_exit_atomic(env_cpu(env), ra); 2717 } 2718 return store_whole_le16(p->haddr, p->size, val_le); 2719 2720 case MO_ATOM_IFALIGN_PAIR: 2721 /* 2722 * Since size > 8, both halves are misaligned, 2723 * and so neither is atomic. 2724 */ 2725 case MO_ATOM_IFALIGN: 2726 case MO_ATOM_NONE: 2727 stq_le_p(p->haddr, int128_getlo(val_le)); 2728 return store_bytes_leN(p->haddr + 8, p->size - 8, 2729 int128_gethi(val_le)); 2730 2731 default: 2732 g_assert_not_reached(); 2733 } 2734 } 2735 2736 static void do_st_1(CPUArchState *env, MMULookupPageData *p, uint8_t val, 2737 int mmu_idx, uintptr_t ra) 2738 { 2739 if (unlikely(p->flags & TLB_MMIO)) { 2740 io_writex(env, p->full, mmu_idx, val, p->addr, ra, MO_UB); 2741 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2742 /* nothing */ 2743 } else { 2744 *(uint8_t *)p->haddr = val; 2745 } 2746 } 2747 2748 static void do_st_2(CPUArchState *env, MMULookupPageData *p, uint16_t val, 2749 int mmu_idx, MemOp memop, uintptr_t ra) 2750 { 2751 if (unlikely(p->flags & TLB_MMIO)) { 2752 io_writex(env, p->full, mmu_idx, val, p->addr, ra, memop); 2753 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2754 /* nothing */ 2755 } else { 2756 /* Swap to host endian if necessary, then store. */ 2757 if (memop & MO_BSWAP) { 2758 val = bswap16(val); 2759 } 2760 store_atom_2(env, ra, p->haddr, memop, val); 2761 } 2762 } 2763 2764 static void do_st_4(CPUArchState *env, MMULookupPageData *p, uint32_t val, 2765 int mmu_idx, MemOp memop, uintptr_t ra) 2766 { 2767 if (unlikely(p->flags & TLB_MMIO)) { 2768 io_writex(env, p->full, mmu_idx, val, p->addr, ra, memop); 2769 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2770 /* nothing */ 2771 } else { 2772 /* Swap to host endian if necessary, then store. */ 2773 if (memop & MO_BSWAP) { 2774 val = bswap32(val); 2775 } 2776 store_atom_4(env, ra, p->haddr, memop, val); 2777 } 2778 } 2779 2780 static void do_st_8(CPUArchState *env, MMULookupPageData *p, uint64_t val, 2781 int mmu_idx, MemOp memop, uintptr_t ra) 2782 { 2783 if (unlikely(p->flags & TLB_MMIO)) { 2784 io_writex(env, p->full, mmu_idx, val, p->addr, ra, memop); 2785 } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) { 2786 /* nothing */ 2787 } else { 2788 /* Swap to host endian if necessary, then store. */ 2789 if (memop & MO_BSWAP) { 2790 val = bswap64(val); 2791 } 2792 store_atom_8(env, ra, p->haddr, memop, val); 2793 } 2794 } 2795 2796 void helper_stb_mmu(CPUArchState *env, uint64_t addr, uint32_t val, 2797 MemOpIdx oi, uintptr_t ra) 2798 { 2799 MMULookupLocals l; 2800 bool crosspage; 2801 2802 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_8); 2803 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE, &l); 2804 tcg_debug_assert(!crosspage); 2805 2806 do_st_1(env, &l.page[0], val, l.mmu_idx, ra); 2807 } 2808 2809 static void do_st2_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 2810 MemOpIdx oi, uintptr_t ra) 2811 { 2812 MMULookupLocals l; 2813 bool crosspage; 2814 uint8_t a, b; 2815 2816 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE, &l); 2817 if (likely(!crosspage)) { 2818 do_st_2(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2819 return; 2820 } 2821 2822 if ((l.memop & MO_BSWAP) == MO_LE) { 2823 a = val, b = val >> 8; 2824 } else { 2825 b = val, a = val >> 8; 2826 } 2827 do_st_1(env, &l.page[0], a, l.mmu_idx, ra); 2828 do_st_1(env, &l.page[1], b, l.mmu_idx, ra); 2829 } 2830 2831 void helper_stw_mmu(CPUArchState *env, uint64_t addr, uint32_t val, 2832 MemOpIdx oi, uintptr_t retaddr) 2833 { 2834 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_16); 2835 do_st2_mmu(env, addr, val, oi, retaddr); 2836 } 2837 2838 static void do_st4_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 2839 MemOpIdx oi, uintptr_t ra) 2840 { 2841 MMULookupLocals l; 2842 bool crosspage; 2843 2844 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE, &l); 2845 if (likely(!crosspage)) { 2846 do_st_4(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2847 return; 2848 } 2849 2850 /* Swap to little endian for simplicity, then store by bytes. */ 2851 if ((l.memop & MO_BSWAP) != MO_LE) { 2852 val = bswap32(val); 2853 } 2854 val = do_st_leN(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2855 (void) do_st_leN(env, &l.page[1], val, l.mmu_idx, l.memop, ra); 2856 } 2857 2858 void helper_stl_mmu(CPUArchState *env, uint64_t addr, uint32_t val, 2859 MemOpIdx oi, uintptr_t retaddr) 2860 { 2861 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_32); 2862 do_st4_mmu(env, addr, val, oi, retaddr); 2863 } 2864 2865 static void do_st8_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2866 MemOpIdx oi, uintptr_t ra) 2867 { 2868 MMULookupLocals l; 2869 bool crosspage; 2870 2871 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE, &l); 2872 if (likely(!crosspage)) { 2873 do_st_8(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2874 return; 2875 } 2876 2877 /* Swap to little endian for simplicity, then store by bytes. */ 2878 if ((l.memop & MO_BSWAP) != MO_LE) { 2879 val = bswap64(val); 2880 } 2881 val = do_st_leN(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2882 (void) do_st_leN(env, &l.page[1], val, l.mmu_idx, l.memop, ra); 2883 } 2884 2885 void helper_stq_mmu(CPUArchState *env, uint64_t addr, uint64_t val, 2886 MemOpIdx oi, uintptr_t retaddr) 2887 { 2888 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_64); 2889 do_st8_mmu(env, addr, val, oi, retaddr); 2890 } 2891 2892 static void do_st16_mmu(CPUArchState *env, target_ulong addr, Int128 val, 2893 MemOpIdx oi, uintptr_t ra) 2894 { 2895 MMULookupLocals l; 2896 bool crosspage; 2897 uint64_t a, b; 2898 int first; 2899 2900 crosspage = mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE, &l); 2901 if (likely(!crosspage)) { 2902 /* Swap to host endian if necessary, then store. */ 2903 if (l.memop & MO_BSWAP) { 2904 val = bswap128(val); 2905 } 2906 if (unlikely(l.page[0].flags & TLB_MMIO)) { 2907 QEMU_IOTHREAD_LOCK_GUARD(); 2908 if (HOST_BIG_ENDIAN) { 2909 b = int128_getlo(val), a = int128_gethi(val); 2910 } else { 2911 a = int128_getlo(val), b = int128_gethi(val); 2912 } 2913 io_writex(env, l.page[0].full, l.mmu_idx, a, addr, ra, MO_64); 2914 io_writex(env, l.page[0].full, l.mmu_idx, b, addr + 8, ra, MO_64); 2915 } else if (unlikely(l.page[0].flags & TLB_DISCARD_WRITE)) { 2916 /* nothing */ 2917 } else { 2918 store_atom_16(env, ra, l.page[0].haddr, l.memop, val); 2919 } 2920 return; 2921 } 2922 2923 first = l.page[0].size; 2924 if (first == 8) { 2925 MemOp mop8 = (l.memop & ~(MO_SIZE | MO_BSWAP)) | MO_64; 2926 2927 if (l.memop & MO_BSWAP) { 2928 val = bswap128(val); 2929 } 2930 if (HOST_BIG_ENDIAN) { 2931 b = int128_getlo(val), a = int128_gethi(val); 2932 } else { 2933 a = int128_getlo(val), b = int128_gethi(val); 2934 } 2935 do_st_8(env, &l.page[0], a, l.mmu_idx, mop8, ra); 2936 do_st_8(env, &l.page[1], b, l.mmu_idx, mop8, ra); 2937 return; 2938 } 2939 2940 if ((l.memop & MO_BSWAP) != MO_LE) { 2941 val = bswap128(val); 2942 } 2943 if (first < 8) { 2944 do_st_leN(env, &l.page[0], int128_getlo(val), l.mmu_idx, l.memop, ra); 2945 val = int128_urshift(val, first * 8); 2946 do_st16_leN(env, &l.page[1], val, l.mmu_idx, l.memop, ra); 2947 } else { 2948 b = do_st16_leN(env, &l.page[0], val, l.mmu_idx, l.memop, ra); 2949 do_st_leN(env, &l.page[1], b, l.mmu_idx, l.memop, ra); 2950 } 2951 } 2952 2953 void helper_st16_mmu(CPUArchState *env, uint64_t addr, Int128 val, 2954 MemOpIdx oi, uintptr_t retaddr) 2955 { 2956 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_128); 2957 do_st16_mmu(env, addr, val, oi, retaddr); 2958 } 2959 2960 void helper_st_i128(CPUArchState *env, uint64_t addr, Int128 val, MemOpIdx oi) 2961 { 2962 helper_st16_mmu(env, addr, val, oi, GETPC()); 2963 } 2964 2965 /* 2966 * Store Helpers for cpu_ldst.h 2967 */ 2968 2969 static void plugin_store_cb(CPUArchState *env, abi_ptr addr, MemOpIdx oi) 2970 { 2971 qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 2972 } 2973 2974 void cpu_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val, 2975 MemOpIdx oi, uintptr_t retaddr) 2976 { 2977 helper_stb_mmu(env, addr, val, oi, retaddr); 2978 plugin_store_cb(env, addr, oi); 2979 } 2980 2981 void cpu_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 2982 MemOpIdx oi, uintptr_t retaddr) 2983 { 2984 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_16); 2985 do_st2_mmu(env, addr, val, oi, retaddr); 2986 plugin_store_cb(env, addr, oi); 2987 } 2988 2989 void cpu_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 2990 MemOpIdx oi, uintptr_t retaddr) 2991 { 2992 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_32); 2993 do_st4_mmu(env, addr, val, oi, retaddr); 2994 plugin_store_cb(env, addr, oi); 2995 } 2996 2997 void cpu_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2998 MemOpIdx oi, uintptr_t retaddr) 2999 { 3000 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_64); 3001 do_st8_mmu(env, addr, val, oi, retaddr); 3002 plugin_store_cb(env, addr, oi); 3003 } 3004 3005 void cpu_st16_mmu(CPUArchState *env, target_ulong addr, Int128 val, 3006 MemOpIdx oi, uintptr_t retaddr) 3007 { 3008 tcg_debug_assert((get_memop(oi) & MO_SIZE) == MO_128); 3009 do_st16_mmu(env, addr, val, oi, retaddr); 3010 plugin_store_cb(env, addr, oi); 3011 } 3012 3013 #include "ldst_common.c.inc" 3014 3015 /* 3016 * First set of functions passes in OI and RETADDR. 3017 * This makes them callable from other helpers. 3018 */ 3019 3020 #define ATOMIC_NAME(X) \ 3021 glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu) 3022 3023 #define ATOMIC_MMU_CLEANUP 3024 3025 #include "atomic_common.c.inc" 3026 3027 #define DATA_SIZE 1 3028 #include "atomic_template.h" 3029 3030 #define DATA_SIZE 2 3031 #include "atomic_template.h" 3032 3033 #define DATA_SIZE 4 3034 #include "atomic_template.h" 3035 3036 #ifdef CONFIG_ATOMIC64 3037 #define DATA_SIZE 8 3038 #include "atomic_template.h" 3039 #endif 3040 3041 #if defined(CONFIG_ATOMIC128) || defined(CONFIG_CMPXCHG128) 3042 #define DATA_SIZE 16 3043 #include "atomic_template.h" 3044 #endif 3045 3046 /* Code access functions. */ 3047 3048 uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr) 3049 { 3050 MemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(env, true)); 3051 return do_ld1_mmu(env, addr, oi, 0, MMU_INST_FETCH); 3052 } 3053 3054 uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr) 3055 { 3056 MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(env, true)); 3057 return do_ld2_mmu(env, addr, oi, 0, MMU_INST_FETCH); 3058 } 3059 3060 uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr) 3061 { 3062 MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(env, true)); 3063 return do_ld4_mmu(env, addr, oi, 0, MMU_INST_FETCH); 3064 } 3065 3066 uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr) 3067 { 3068 MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(env, true)); 3069 return do_ld8_mmu(env, addr, oi, 0, MMU_INST_FETCH); 3070 } 3071 3072 uint8_t cpu_ldb_code_mmu(CPUArchState *env, abi_ptr addr, 3073 MemOpIdx oi, uintptr_t retaddr) 3074 { 3075 return do_ld1_mmu(env, addr, oi, retaddr, MMU_INST_FETCH); 3076 } 3077 3078 uint16_t cpu_ldw_code_mmu(CPUArchState *env, abi_ptr addr, 3079 MemOpIdx oi, uintptr_t retaddr) 3080 { 3081 return do_ld2_mmu(env, addr, oi, retaddr, MMU_INST_FETCH); 3082 } 3083 3084 uint32_t cpu_ldl_code_mmu(CPUArchState *env, abi_ptr addr, 3085 MemOpIdx oi, uintptr_t retaddr) 3086 { 3087 return do_ld4_mmu(env, addr, oi, retaddr, MMU_INST_FETCH); 3088 } 3089 3090 uint64_t cpu_ldq_code_mmu(CPUArchState *env, abi_ptr addr, 3091 MemOpIdx oi, uintptr_t retaddr) 3092 { 3093 return do_ld8_mmu(env, addr, oi, retaddr, MMU_INST_FETCH); 3094 } 3095