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