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