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