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