1d9bb58e5SYang Zhong /* 2d9bb58e5SYang Zhong * Common CPU TLB handling 3d9bb58e5SYang Zhong * 4d9bb58e5SYang Zhong * Copyright (c) 2003 Fabrice Bellard 5d9bb58e5SYang Zhong * 6d9bb58e5SYang Zhong * This library is free software; you can redistribute it and/or 7d9bb58e5SYang Zhong * modify it under the terms of the GNU Lesser General Public 8d9bb58e5SYang Zhong * License as published by the Free Software Foundation; either 9fb0343d5SThomas Huth * version 2.1 of the License, or (at your option) any later version. 10d9bb58e5SYang Zhong * 11d9bb58e5SYang Zhong * This library is distributed in the hope that it will be useful, 12d9bb58e5SYang Zhong * but WITHOUT ANY WARRANTY; without even the implied warranty of 13d9bb58e5SYang Zhong * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14d9bb58e5SYang Zhong * Lesser General Public License for more details. 15d9bb58e5SYang Zhong * 16d9bb58e5SYang Zhong * You should have received a copy of the GNU Lesser General Public 17d9bb58e5SYang Zhong * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18d9bb58e5SYang Zhong */ 19d9bb58e5SYang Zhong 20d9bb58e5SYang Zhong #include "qemu/osdep.h" 21d9bb58e5SYang Zhong #include "qemu/main-loop.h" 2278271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 23d9bb58e5SYang Zhong #include "exec/exec-all.h" 24d9bb58e5SYang Zhong #include "exec/memory.h" 25d9bb58e5SYang Zhong #include "exec/cpu_ldst.h" 26d9bb58e5SYang Zhong #include "exec/cputlb.h" 27d9bb58e5SYang Zhong #include "exec/memory-internal.h" 28d9bb58e5SYang Zhong #include "exec/ram_addr.h" 29d9bb58e5SYang Zhong #include "tcg/tcg.h" 30d9bb58e5SYang Zhong #include "qemu/error-report.h" 31d9bb58e5SYang Zhong #include "exec/log.h" 32d9bb58e5SYang Zhong #include "exec/helper-proto.h" 33d9bb58e5SYang Zhong #include "qemu/atomic.h" 34e6cd4bb5SRichard Henderson #include "qemu/atomic128.h" 353b9bd3f4SPaolo Bonzini #include "exec/translate-all.h" 36243af022SPaolo Bonzini #include "trace/trace-root.h" 37e5ceadffSPhilippe Mathieu-Daudé #include "tb-hash.h" 3865269192SPhilippe Mathieu-Daudé #include "internal.h" 39235537faSAlex Bennée #ifdef CONFIG_PLUGIN 40235537faSAlex Bennée #include "qemu/plugin-memory.h" 41235537faSAlex Bennée #endif 42d2ba8026SRichard Henderson #include "tcg/tcg-ldst.h" 43d9bb58e5SYang Zhong 44d9bb58e5SYang Zhong /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */ 45d9bb58e5SYang Zhong /* #define DEBUG_TLB */ 46d9bb58e5SYang Zhong /* #define DEBUG_TLB_LOG */ 47d9bb58e5SYang Zhong 48d9bb58e5SYang Zhong #ifdef DEBUG_TLB 49d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 1 50d9bb58e5SYang Zhong # ifdef DEBUG_TLB_LOG 51d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 1 52d9bb58e5SYang Zhong # else 53d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 0 54d9bb58e5SYang Zhong # endif 55d9bb58e5SYang Zhong #else 56d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 0 57d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 0 58d9bb58e5SYang Zhong #endif 59d9bb58e5SYang Zhong 60d9bb58e5SYang Zhong #define tlb_debug(fmt, ...) do { \ 61d9bb58e5SYang Zhong if (DEBUG_TLB_LOG_GATE) { \ 62d9bb58e5SYang Zhong qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \ 63d9bb58e5SYang Zhong ## __VA_ARGS__); \ 64d9bb58e5SYang Zhong } else if (DEBUG_TLB_GATE) { \ 65d9bb58e5SYang Zhong fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 66d9bb58e5SYang Zhong } \ 67d9bb58e5SYang Zhong } while (0) 68d9bb58e5SYang Zhong 69ea9025cbSEmilio G. Cota #define assert_cpu_is_self(cpu) do { \ 70d9bb58e5SYang Zhong if (DEBUG_TLB_GATE) { \ 71ea9025cbSEmilio G. Cota g_assert(!(cpu)->created || qemu_cpu_is_self(cpu)); \ 72d9bb58e5SYang Zhong } \ 73d9bb58e5SYang Zhong } while (0) 74d9bb58e5SYang Zhong 75d9bb58e5SYang Zhong /* run_on_cpu_data.target_ptr should always be big enough for a 76d9bb58e5SYang Zhong * target_ulong even on 32 bit builds */ 77d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(sizeof(target_ulong) > sizeof(run_on_cpu_data)); 78d9bb58e5SYang Zhong 79d9bb58e5SYang Zhong /* We currently can't handle more than 16 bits in the MMUIDX bitmask. 80d9bb58e5SYang Zhong */ 81d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16); 82d9bb58e5SYang Zhong #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1) 83d9bb58e5SYang Zhong 84722a1c1eSRichard Henderson static inline size_t tlb_n_entries(CPUTLBDescFast *fast) 857a1efe1bSRichard Henderson { 86722a1c1eSRichard Henderson return (fast->mask >> CPU_TLB_ENTRY_BITS) + 1; 877a1efe1bSRichard Henderson } 887a1efe1bSRichard Henderson 89722a1c1eSRichard Henderson static inline size_t sizeof_tlb(CPUTLBDescFast *fast) 9086e1eff8SEmilio G. Cota { 91722a1c1eSRichard Henderson return fast->mask + (1 << CPU_TLB_ENTRY_BITS); 9286e1eff8SEmilio G. Cota } 9386e1eff8SEmilio G. Cota 9479e42085SRichard Henderson static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns, 9586e1eff8SEmilio G. Cota size_t max_entries) 9686e1eff8SEmilio G. Cota { 9779e42085SRichard Henderson desc->window_begin_ns = ns; 9879e42085SRichard Henderson desc->window_max_entries = max_entries; 9986e1eff8SEmilio G. Cota } 10086e1eff8SEmilio G. Cota 1010f4abea8SRichard Henderson static void tb_jmp_cache_clear_page(CPUState *cpu, target_ulong page_addr) 1020f4abea8SRichard Henderson { 1030f4abea8SRichard Henderson unsigned int i, i0 = tb_jmp_cache_hash_page(page_addr); 1040f4abea8SRichard Henderson 1050f4abea8SRichard Henderson for (i = 0; i < TB_JMP_PAGE_SIZE; i++) { 1060f4abea8SRichard Henderson qatomic_set(&cpu->tb_jmp_cache[i0 + i], NULL); 1070f4abea8SRichard Henderson } 1080f4abea8SRichard Henderson } 1090f4abea8SRichard Henderson 1100f4abea8SRichard Henderson static void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr) 1110f4abea8SRichard Henderson { 1120f4abea8SRichard Henderson /* Discard jump cache entries for any tb which might potentially 1130f4abea8SRichard Henderson overlap the flushed page. */ 1140f4abea8SRichard Henderson tb_jmp_cache_clear_page(cpu, addr - TARGET_PAGE_SIZE); 1150f4abea8SRichard Henderson tb_jmp_cache_clear_page(cpu, addr); 1160f4abea8SRichard Henderson } 1170f4abea8SRichard Henderson 11886e1eff8SEmilio G. Cota /** 11986e1eff8SEmilio G. Cota * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary 12071ccd47bSRichard Henderson * @desc: The CPUTLBDesc portion of the TLB 12171ccd47bSRichard Henderson * @fast: The CPUTLBDescFast portion of the same TLB 12286e1eff8SEmilio G. Cota * 12386e1eff8SEmilio G. Cota * Called with tlb_lock_held. 12486e1eff8SEmilio G. Cota * 12586e1eff8SEmilio G. Cota * We have two main constraints when resizing a TLB: (1) we only resize it 12686e1eff8SEmilio G. Cota * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing 12786e1eff8SEmilio G. Cota * the array or unnecessarily flushing it), which means we do not control how 12886e1eff8SEmilio G. Cota * frequently the resizing can occur; (2) we don't have access to the guest's 12986e1eff8SEmilio G. Cota * future scheduling decisions, and therefore have to decide the magnitude of 13086e1eff8SEmilio G. Cota * the resize based on past observations. 13186e1eff8SEmilio G. Cota * 13286e1eff8SEmilio G. Cota * In general, a memory-hungry process can benefit greatly from an appropriately 13386e1eff8SEmilio G. Cota * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that 13486e1eff8SEmilio G. Cota * we just have to make the TLB as large as possible; while an oversized TLB 13586e1eff8SEmilio G. Cota * results in minimal TLB miss rates, it also takes longer to be flushed 13686e1eff8SEmilio G. Cota * (flushes can be _very_ frequent), and the reduced locality can also hurt 13786e1eff8SEmilio G. Cota * performance. 13886e1eff8SEmilio G. Cota * 13986e1eff8SEmilio G. Cota * To achieve near-optimal performance for all kinds of workloads, we: 14086e1eff8SEmilio G. Cota * 14186e1eff8SEmilio G. Cota * 1. Aggressively increase the size of the TLB when the use rate of the 14286e1eff8SEmilio G. Cota * TLB being flushed is high, since it is likely that in the near future this 14386e1eff8SEmilio G. Cota * memory-hungry process will execute again, and its memory hungriness will 14486e1eff8SEmilio G. Cota * probably be similar. 14586e1eff8SEmilio G. Cota * 14686e1eff8SEmilio G. Cota * 2. Slowly reduce the size of the TLB as the use rate declines over a 14786e1eff8SEmilio G. Cota * reasonably large time window. The rationale is that if in such a time window 14886e1eff8SEmilio G. Cota * we have not observed a high TLB use rate, it is likely that we won't observe 14986e1eff8SEmilio G. Cota * it in the near future. In that case, once a time window expires we downsize 15086e1eff8SEmilio G. Cota * the TLB to match the maximum use rate observed in the window. 15186e1eff8SEmilio G. Cota * 15286e1eff8SEmilio G. Cota * 3. Try to keep the maximum use rate in a time window in the 30-70% range, 15386e1eff8SEmilio G. Cota * since in that range performance is likely near-optimal. Recall that the TLB 15486e1eff8SEmilio G. Cota * is direct mapped, so we want the use rate to be low (or at least not too 15586e1eff8SEmilio G. Cota * high), since otherwise we are likely to have a significant amount of 15686e1eff8SEmilio G. Cota * conflict misses. 15786e1eff8SEmilio G. Cota */ 1583c3959f2SRichard Henderson static void tlb_mmu_resize_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast, 1593c3959f2SRichard Henderson int64_t now) 16086e1eff8SEmilio G. Cota { 16171ccd47bSRichard Henderson size_t old_size = tlb_n_entries(fast); 16286e1eff8SEmilio G. Cota size_t rate; 16386e1eff8SEmilio G. Cota size_t new_size = old_size; 16486e1eff8SEmilio G. Cota int64_t window_len_ms = 100; 16586e1eff8SEmilio G. Cota int64_t window_len_ns = window_len_ms * 1000 * 1000; 16679e42085SRichard Henderson bool window_expired = now > desc->window_begin_ns + window_len_ns; 16786e1eff8SEmilio G. Cota 16879e42085SRichard Henderson if (desc->n_used_entries > desc->window_max_entries) { 16979e42085SRichard Henderson desc->window_max_entries = desc->n_used_entries; 17086e1eff8SEmilio G. Cota } 17179e42085SRichard Henderson rate = desc->window_max_entries * 100 / old_size; 17286e1eff8SEmilio G. Cota 17386e1eff8SEmilio G. Cota if (rate > 70) { 17486e1eff8SEmilio G. Cota new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS); 17586e1eff8SEmilio G. Cota } else if (rate < 30 && window_expired) { 17679e42085SRichard Henderson size_t ceil = pow2ceil(desc->window_max_entries); 17779e42085SRichard Henderson size_t expected_rate = desc->window_max_entries * 100 / ceil; 17886e1eff8SEmilio G. Cota 17986e1eff8SEmilio G. Cota /* 18086e1eff8SEmilio G. Cota * Avoid undersizing when the max number of entries seen is just below 18186e1eff8SEmilio G. Cota * a pow2. For instance, if max_entries == 1025, the expected use rate 18286e1eff8SEmilio G. Cota * would be 1025/2048==50%. However, if max_entries == 1023, we'd get 18386e1eff8SEmilio G. Cota * 1023/1024==99.9% use rate, so we'd likely end up doubling the size 18486e1eff8SEmilio G. Cota * later. Thus, make sure that the expected use rate remains below 70%. 18586e1eff8SEmilio G. Cota * (and since we double the size, that means the lowest rate we'd 18686e1eff8SEmilio G. Cota * expect to get is 35%, which is still in the 30-70% range where 18786e1eff8SEmilio G. Cota * we consider that the size is appropriate.) 18886e1eff8SEmilio G. Cota */ 18986e1eff8SEmilio G. Cota if (expected_rate > 70) { 19086e1eff8SEmilio G. Cota ceil *= 2; 19186e1eff8SEmilio G. Cota } 19286e1eff8SEmilio G. Cota new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS); 19386e1eff8SEmilio G. Cota } 19486e1eff8SEmilio G. Cota 19586e1eff8SEmilio G. Cota if (new_size == old_size) { 19686e1eff8SEmilio G. Cota if (window_expired) { 19779e42085SRichard Henderson tlb_window_reset(desc, now, desc->n_used_entries); 19886e1eff8SEmilio G. Cota } 19986e1eff8SEmilio G. Cota return; 20086e1eff8SEmilio G. Cota } 20186e1eff8SEmilio G. Cota 20271ccd47bSRichard Henderson g_free(fast->table); 20371ccd47bSRichard Henderson g_free(desc->iotlb); 20486e1eff8SEmilio G. Cota 20579e42085SRichard Henderson tlb_window_reset(desc, now, 0); 20686e1eff8SEmilio G. Cota /* desc->n_used_entries is cleared by the caller */ 20771ccd47bSRichard Henderson fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS; 20871ccd47bSRichard Henderson fast->table = g_try_new(CPUTLBEntry, new_size); 20971ccd47bSRichard Henderson desc->iotlb = g_try_new(CPUIOTLBEntry, new_size); 21071ccd47bSRichard Henderson 21186e1eff8SEmilio G. Cota /* 21286e1eff8SEmilio G. Cota * If the allocations fail, try smaller sizes. We just freed some 21386e1eff8SEmilio G. Cota * memory, so going back to half of new_size has a good chance of working. 21486e1eff8SEmilio G. Cota * Increased memory pressure elsewhere in the system might cause the 21586e1eff8SEmilio G. Cota * allocations to fail though, so we progressively reduce the allocation 21686e1eff8SEmilio G. Cota * size, aborting if we cannot even allocate the smallest TLB we support. 21786e1eff8SEmilio G. Cota */ 21871ccd47bSRichard Henderson while (fast->table == NULL || desc->iotlb == NULL) { 21986e1eff8SEmilio G. Cota if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) { 22086e1eff8SEmilio G. Cota error_report("%s: %s", __func__, strerror(errno)); 22186e1eff8SEmilio G. Cota abort(); 22286e1eff8SEmilio G. Cota } 22386e1eff8SEmilio G. Cota new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS); 22471ccd47bSRichard Henderson fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS; 22586e1eff8SEmilio G. Cota 22671ccd47bSRichard Henderson g_free(fast->table); 22771ccd47bSRichard Henderson g_free(desc->iotlb); 22871ccd47bSRichard Henderson fast->table = g_try_new(CPUTLBEntry, new_size); 22971ccd47bSRichard Henderson desc->iotlb = g_try_new(CPUIOTLBEntry, new_size); 23086e1eff8SEmilio G. Cota } 23186e1eff8SEmilio G. Cota } 23286e1eff8SEmilio G. Cota 233bbf021b0SRichard Henderson static void tlb_mmu_flush_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast) 23486e1eff8SEmilio G. Cota { 2355c948e31SRichard Henderson desc->n_used_entries = 0; 2365c948e31SRichard Henderson desc->large_page_addr = -1; 2375c948e31SRichard Henderson desc->large_page_mask = -1; 2385c948e31SRichard Henderson desc->vindex = 0; 2395c948e31SRichard Henderson memset(fast->table, -1, sizeof_tlb(fast)); 2405c948e31SRichard Henderson memset(desc->vtable, -1, sizeof(desc->vtable)); 24186e1eff8SEmilio G. Cota } 24286e1eff8SEmilio G. Cota 2433c3959f2SRichard Henderson static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx, 2443c3959f2SRichard Henderson int64_t now) 245bbf021b0SRichard Henderson { 246bbf021b0SRichard Henderson CPUTLBDesc *desc = &env_tlb(env)->d[mmu_idx]; 247bbf021b0SRichard Henderson CPUTLBDescFast *fast = &env_tlb(env)->f[mmu_idx]; 248bbf021b0SRichard Henderson 2493c3959f2SRichard Henderson tlb_mmu_resize_locked(desc, fast, now); 250bbf021b0SRichard Henderson tlb_mmu_flush_locked(desc, fast); 251bbf021b0SRichard Henderson } 252bbf021b0SRichard Henderson 25356e89f76SRichard Henderson static void tlb_mmu_init(CPUTLBDesc *desc, CPUTLBDescFast *fast, int64_t now) 25456e89f76SRichard Henderson { 25556e89f76SRichard Henderson size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS; 25656e89f76SRichard Henderson 25756e89f76SRichard Henderson tlb_window_reset(desc, now, 0); 25856e89f76SRichard Henderson desc->n_used_entries = 0; 25956e89f76SRichard Henderson fast->mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS; 26056e89f76SRichard Henderson fast->table = g_new(CPUTLBEntry, n_entries); 26156e89f76SRichard Henderson desc->iotlb = g_new(CPUIOTLBEntry, n_entries); 2623c16304aSRichard Henderson tlb_mmu_flush_locked(desc, fast); 26356e89f76SRichard Henderson } 26456e89f76SRichard Henderson 26586e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_inc(CPUArchState *env, uintptr_t mmu_idx) 26686e1eff8SEmilio G. Cota { 267a40ec84eSRichard Henderson env_tlb(env)->d[mmu_idx].n_used_entries++; 26886e1eff8SEmilio G. Cota } 26986e1eff8SEmilio G. Cota 27086e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_dec(CPUArchState *env, uintptr_t mmu_idx) 27186e1eff8SEmilio G. Cota { 272a40ec84eSRichard Henderson env_tlb(env)->d[mmu_idx].n_used_entries--; 27386e1eff8SEmilio G. Cota } 27486e1eff8SEmilio G. Cota 2755005e253SEmilio G. Cota void tlb_init(CPUState *cpu) 2765005e253SEmilio G. Cota { 27771aec354SEmilio G. Cota CPUArchState *env = cpu->env_ptr; 27856e89f76SRichard Henderson int64_t now = get_clock_realtime(); 27956e89f76SRichard Henderson int i; 28071aec354SEmilio G. Cota 281a40ec84eSRichard Henderson qemu_spin_init(&env_tlb(env)->c.lock); 2823d1523ceSRichard Henderson 2833c16304aSRichard Henderson /* All tlbs are initialized flushed. */ 2843c16304aSRichard Henderson env_tlb(env)->c.dirty = 0; 28586e1eff8SEmilio G. Cota 28656e89f76SRichard Henderson for (i = 0; i < NB_MMU_MODES; i++) { 28756e89f76SRichard Henderson tlb_mmu_init(&env_tlb(env)->d[i], &env_tlb(env)->f[i], now); 28856e89f76SRichard Henderson } 2895005e253SEmilio G. Cota } 2905005e253SEmilio G. Cota 291816d9be5SEmilio G. Cota void tlb_destroy(CPUState *cpu) 292816d9be5SEmilio G. Cota { 293816d9be5SEmilio G. Cota CPUArchState *env = cpu->env_ptr; 294816d9be5SEmilio G. Cota int i; 295816d9be5SEmilio G. Cota 296816d9be5SEmilio G. Cota qemu_spin_destroy(&env_tlb(env)->c.lock); 297816d9be5SEmilio G. Cota for (i = 0; i < NB_MMU_MODES; i++) { 298816d9be5SEmilio G. Cota CPUTLBDesc *desc = &env_tlb(env)->d[i]; 299816d9be5SEmilio G. Cota CPUTLBDescFast *fast = &env_tlb(env)->f[i]; 300816d9be5SEmilio G. Cota 301816d9be5SEmilio G. Cota g_free(fast->table); 302816d9be5SEmilio G. Cota g_free(desc->iotlb); 303816d9be5SEmilio G. Cota } 304816d9be5SEmilio G. Cota } 305816d9be5SEmilio G. Cota 306d9bb58e5SYang Zhong /* flush_all_helper: run fn across all cpus 307d9bb58e5SYang Zhong * 308d9bb58e5SYang Zhong * If the wait flag is set then the src cpu's helper will be queued as 309d9bb58e5SYang Zhong * "safe" work and the loop exited creating a synchronisation point 310d9bb58e5SYang Zhong * where all queued work will be finished before execution starts 311d9bb58e5SYang Zhong * again. 312d9bb58e5SYang Zhong */ 313d9bb58e5SYang Zhong static void flush_all_helper(CPUState *src, run_on_cpu_func fn, 314d9bb58e5SYang Zhong run_on_cpu_data d) 315d9bb58e5SYang Zhong { 316d9bb58e5SYang Zhong CPUState *cpu; 317d9bb58e5SYang Zhong 318d9bb58e5SYang Zhong CPU_FOREACH(cpu) { 319d9bb58e5SYang Zhong if (cpu != src) { 320d9bb58e5SYang Zhong async_run_on_cpu(cpu, fn, d); 321d9bb58e5SYang Zhong } 322d9bb58e5SYang Zhong } 323d9bb58e5SYang Zhong } 324d9bb58e5SYang Zhong 325e09de0a2SRichard Henderson void tlb_flush_counts(size_t *pfull, size_t *ppart, size_t *pelide) 32683974cf4SEmilio G. Cota { 32783974cf4SEmilio G. Cota CPUState *cpu; 328e09de0a2SRichard Henderson size_t full = 0, part = 0, elide = 0; 32983974cf4SEmilio G. Cota 33083974cf4SEmilio G. Cota CPU_FOREACH(cpu) { 33183974cf4SEmilio G. Cota CPUArchState *env = cpu->env_ptr; 33283974cf4SEmilio G. Cota 333d73415a3SStefan Hajnoczi full += qatomic_read(&env_tlb(env)->c.full_flush_count); 334d73415a3SStefan Hajnoczi part += qatomic_read(&env_tlb(env)->c.part_flush_count); 335d73415a3SStefan Hajnoczi elide += qatomic_read(&env_tlb(env)->c.elide_flush_count); 33683974cf4SEmilio G. Cota } 337e09de0a2SRichard Henderson *pfull = full; 338e09de0a2SRichard Henderson *ppart = part; 339e09de0a2SRichard Henderson *pelide = elide; 34083974cf4SEmilio G. Cota } 341d9bb58e5SYang Zhong 342d9bb58e5SYang Zhong static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data) 343d9bb58e5SYang Zhong { 344d9bb58e5SYang Zhong CPUArchState *env = cpu->env_ptr; 3453d1523ceSRichard Henderson uint16_t asked = data.host_int; 3463d1523ceSRichard Henderson uint16_t all_dirty, work, to_clean; 3473c3959f2SRichard Henderson int64_t now = get_clock_realtime(); 348d9bb58e5SYang Zhong 349d9bb58e5SYang Zhong assert_cpu_is_self(cpu); 350d9bb58e5SYang Zhong 3513d1523ceSRichard Henderson tlb_debug("mmu_idx:0x%04" PRIx16 "\n", asked); 352d9bb58e5SYang Zhong 353a40ec84eSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 35460a2ad7dSRichard Henderson 355a40ec84eSRichard Henderson all_dirty = env_tlb(env)->c.dirty; 3563d1523ceSRichard Henderson to_clean = asked & all_dirty; 3573d1523ceSRichard Henderson all_dirty &= ~to_clean; 358a40ec84eSRichard Henderson env_tlb(env)->c.dirty = all_dirty; 3593d1523ceSRichard Henderson 3603d1523ceSRichard Henderson for (work = to_clean; work != 0; work &= work - 1) { 3613d1523ceSRichard Henderson int mmu_idx = ctz32(work); 3623c3959f2SRichard Henderson tlb_flush_one_mmuidx_locked(env, mmu_idx, now); 363d9bb58e5SYang Zhong } 3643d1523ceSRichard Henderson 365a40ec84eSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 366d9bb58e5SYang Zhong 367f3ced3c5SEmilio G. Cota cpu_tb_jmp_cache_clear(cpu); 36864f2674bSRichard Henderson 3693d1523ceSRichard Henderson if (to_clean == ALL_MMUIDX_BITS) { 370d73415a3SStefan Hajnoczi qatomic_set(&env_tlb(env)->c.full_flush_count, 371a40ec84eSRichard Henderson env_tlb(env)->c.full_flush_count + 1); 372e09de0a2SRichard Henderson } else { 373d73415a3SStefan Hajnoczi qatomic_set(&env_tlb(env)->c.part_flush_count, 374a40ec84eSRichard Henderson env_tlb(env)->c.part_flush_count + ctpop16(to_clean)); 3753d1523ceSRichard Henderson if (to_clean != asked) { 376d73415a3SStefan Hajnoczi qatomic_set(&env_tlb(env)->c.elide_flush_count, 377a40ec84eSRichard Henderson env_tlb(env)->c.elide_flush_count + 3783d1523ceSRichard Henderson ctpop16(asked & ~to_clean)); 3793d1523ceSRichard Henderson } 38064f2674bSRichard Henderson } 381d9bb58e5SYang Zhong } 382d9bb58e5SYang Zhong 383d9bb58e5SYang Zhong void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap) 384d9bb58e5SYang Zhong { 385d9bb58e5SYang Zhong tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap); 386d9bb58e5SYang Zhong 38764f2674bSRichard Henderson if (cpu->created && !qemu_cpu_is_self(cpu)) { 388d9bb58e5SYang Zhong async_run_on_cpu(cpu, tlb_flush_by_mmuidx_async_work, 389ab651105SRichard Henderson RUN_ON_CPU_HOST_INT(idxmap)); 390d9bb58e5SYang Zhong } else { 39160a2ad7dSRichard Henderson tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap)); 392d9bb58e5SYang Zhong } 393d9bb58e5SYang Zhong } 394d9bb58e5SYang Zhong 39564f2674bSRichard Henderson void tlb_flush(CPUState *cpu) 39664f2674bSRichard Henderson { 39764f2674bSRichard Henderson tlb_flush_by_mmuidx(cpu, ALL_MMUIDX_BITS); 39864f2674bSRichard Henderson } 39964f2674bSRichard Henderson 400d9bb58e5SYang Zhong void tlb_flush_by_mmuidx_all_cpus(CPUState *src_cpu, uint16_t idxmap) 401d9bb58e5SYang Zhong { 402d9bb58e5SYang Zhong const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work; 403d9bb58e5SYang Zhong 404d9bb58e5SYang Zhong tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap); 405d9bb58e5SYang Zhong 406d9bb58e5SYang Zhong flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 407d9bb58e5SYang Zhong fn(src_cpu, RUN_ON_CPU_HOST_INT(idxmap)); 408d9bb58e5SYang Zhong } 409d9bb58e5SYang Zhong 41064f2674bSRichard Henderson void tlb_flush_all_cpus(CPUState *src_cpu) 41164f2674bSRichard Henderson { 41264f2674bSRichard Henderson tlb_flush_by_mmuidx_all_cpus(src_cpu, ALL_MMUIDX_BITS); 41364f2674bSRichard Henderson } 41464f2674bSRichard Henderson 41564f2674bSRichard Henderson void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu, uint16_t idxmap) 416d9bb58e5SYang Zhong { 417d9bb58e5SYang Zhong const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work; 418d9bb58e5SYang Zhong 419d9bb58e5SYang Zhong tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap); 420d9bb58e5SYang Zhong 421d9bb58e5SYang Zhong flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 422d9bb58e5SYang Zhong async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap)); 423d9bb58e5SYang Zhong } 424d9bb58e5SYang Zhong 42564f2674bSRichard Henderson void tlb_flush_all_cpus_synced(CPUState *src_cpu) 42664f2674bSRichard Henderson { 42764f2674bSRichard Henderson tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS); 42864f2674bSRichard Henderson } 42964f2674bSRichard Henderson 4303ab6e68cSRichard Henderson static bool tlb_hit_page_mask_anyprot(CPUTLBEntry *tlb_entry, 4313ab6e68cSRichard Henderson target_ulong page, target_ulong mask) 4323ab6e68cSRichard Henderson { 4333ab6e68cSRichard Henderson page &= mask; 4343ab6e68cSRichard Henderson mask &= TARGET_PAGE_MASK | TLB_INVALID_MASK; 4353ab6e68cSRichard Henderson 4363ab6e68cSRichard Henderson return (page == (tlb_entry->addr_read & mask) || 4373ab6e68cSRichard Henderson page == (tlb_addr_write(tlb_entry) & mask) || 4383ab6e68cSRichard Henderson page == (tlb_entry->addr_code & mask)); 4393ab6e68cSRichard Henderson } 4403ab6e68cSRichard Henderson 44168fea038SRichard Henderson static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry, 44268fea038SRichard Henderson target_ulong page) 443d9bb58e5SYang Zhong { 4443ab6e68cSRichard Henderson return tlb_hit_page_mask_anyprot(tlb_entry, page, -1); 44568fea038SRichard Henderson } 44668fea038SRichard Henderson 4473cea94bbSEmilio G. Cota /** 4483cea94bbSEmilio G. Cota * tlb_entry_is_empty - return true if the entry is not in use 4493cea94bbSEmilio G. Cota * @te: pointer to CPUTLBEntry 4503cea94bbSEmilio G. Cota */ 4513cea94bbSEmilio G. Cota static inline bool tlb_entry_is_empty(const CPUTLBEntry *te) 4523cea94bbSEmilio G. Cota { 4533cea94bbSEmilio G. Cota return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1; 4543cea94bbSEmilio G. Cota } 4553cea94bbSEmilio G. Cota 45653d28455SRichard Henderson /* Called with tlb_c.lock held */ 4573ab6e68cSRichard Henderson static bool tlb_flush_entry_mask_locked(CPUTLBEntry *tlb_entry, 4583ab6e68cSRichard Henderson target_ulong page, 4593ab6e68cSRichard Henderson target_ulong mask) 46068fea038SRichard Henderson { 4613ab6e68cSRichard Henderson if (tlb_hit_page_mask_anyprot(tlb_entry, page, mask)) { 462d9bb58e5SYang Zhong memset(tlb_entry, -1, sizeof(*tlb_entry)); 46386e1eff8SEmilio G. Cota return true; 464d9bb58e5SYang Zhong } 46586e1eff8SEmilio G. Cota return false; 466d9bb58e5SYang Zhong } 467d9bb58e5SYang Zhong 4683ab6e68cSRichard Henderson static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry, 46968fea038SRichard Henderson target_ulong page) 47068fea038SRichard Henderson { 4713ab6e68cSRichard Henderson return tlb_flush_entry_mask_locked(tlb_entry, page, -1); 4723ab6e68cSRichard Henderson } 4733ab6e68cSRichard Henderson 4743ab6e68cSRichard Henderson /* Called with tlb_c.lock held */ 4753ab6e68cSRichard Henderson static void tlb_flush_vtlb_page_mask_locked(CPUArchState *env, int mmu_idx, 4763ab6e68cSRichard Henderson target_ulong page, 4773ab6e68cSRichard Henderson target_ulong mask) 4783ab6e68cSRichard Henderson { 479a40ec84eSRichard Henderson CPUTLBDesc *d = &env_tlb(env)->d[mmu_idx]; 48068fea038SRichard Henderson int k; 48171aec354SEmilio G. Cota 48229a0af61SRichard Henderson assert_cpu_is_self(env_cpu(env)); 48368fea038SRichard Henderson for (k = 0; k < CPU_VTLB_SIZE; k++) { 4843ab6e68cSRichard Henderson if (tlb_flush_entry_mask_locked(&d->vtable[k], page, mask)) { 48586e1eff8SEmilio G. Cota tlb_n_used_entries_dec(env, mmu_idx); 48686e1eff8SEmilio G. Cota } 48768fea038SRichard Henderson } 48868fea038SRichard Henderson } 48968fea038SRichard Henderson 4903ab6e68cSRichard Henderson static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx, 4913ab6e68cSRichard Henderson target_ulong page) 4923ab6e68cSRichard Henderson { 4933ab6e68cSRichard Henderson tlb_flush_vtlb_page_mask_locked(env, mmu_idx, page, -1); 4943ab6e68cSRichard Henderson } 4953ab6e68cSRichard Henderson 4961308e026SRichard Henderson static void tlb_flush_page_locked(CPUArchState *env, int midx, 4971308e026SRichard Henderson target_ulong page) 4981308e026SRichard Henderson { 499a40ec84eSRichard Henderson target_ulong lp_addr = env_tlb(env)->d[midx].large_page_addr; 500a40ec84eSRichard Henderson target_ulong lp_mask = env_tlb(env)->d[midx].large_page_mask; 5011308e026SRichard Henderson 5021308e026SRichard Henderson /* Check if we need to flush due to large pages. */ 5031308e026SRichard Henderson if ((page & lp_mask) == lp_addr) { 5041308e026SRichard Henderson tlb_debug("forcing full flush midx %d (" 5051308e026SRichard Henderson TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", 5061308e026SRichard Henderson midx, lp_addr, lp_mask); 5073c3959f2SRichard Henderson tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 5081308e026SRichard Henderson } else { 50986e1eff8SEmilio G. Cota if (tlb_flush_entry_locked(tlb_entry(env, midx, page), page)) { 51086e1eff8SEmilio G. Cota tlb_n_used_entries_dec(env, midx); 51186e1eff8SEmilio G. Cota } 5121308e026SRichard Henderson tlb_flush_vtlb_page_locked(env, midx, page); 5131308e026SRichard Henderson } 5141308e026SRichard Henderson } 5151308e026SRichard Henderson 5167b7d00e0SRichard Henderson /** 5177b7d00e0SRichard Henderson * tlb_flush_page_by_mmuidx_async_0: 5187b7d00e0SRichard Henderson * @cpu: cpu on which to flush 5197b7d00e0SRichard Henderson * @addr: page of virtual address to flush 5207b7d00e0SRichard Henderson * @idxmap: set of mmu_idx to flush 5217b7d00e0SRichard Henderson * 5227b7d00e0SRichard Henderson * Helper for tlb_flush_page_by_mmuidx and friends, flush one page 5237b7d00e0SRichard Henderson * at @addr from the tlbs indicated by @idxmap from @cpu. 524d9bb58e5SYang Zhong */ 5257b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_0(CPUState *cpu, 5267b7d00e0SRichard Henderson target_ulong addr, 5277b7d00e0SRichard Henderson uint16_t idxmap) 528d9bb58e5SYang Zhong { 529d9bb58e5SYang Zhong CPUArchState *env = cpu->env_ptr; 530d9bb58e5SYang Zhong int mmu_idx; 531d9bb58e5SYang Zhong 532d9bb58e5SYang Zhong assert_cpu_is_self(cpu); 533d9bb58e5SYang Zhong 5347b7d00e0SRichard Henderson tlb_debug("page addr:" TARGET_FMT_lx " mmu_map:0x%x\n", addr, idxmap); 535d9bb58e5SYang Zhong 536a40ec84eSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 537d9bb58e5SYang Zhong for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 5387b7d00e0SRichard Henderson if ((idxmap >> mmu_idx) & 1) { 5391308e026SRichard Henderson tlb_flush_page_locked(env, mmu_idx, addr); 540d9bb58e5SYang Zhong } 541d9bb58e5SYang Zhong } 542a40ec84eSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 543d9bb58e5SYang Zhong 544d9bb58e5SYang Zhong tb_flush_jmp_cache(cpu, addr); 545d9bb58e5SYang Zhong } 546d9bb58e5SYang Zhong 5477b7d00e0SRichard Henderson /** 5487b7d00e0SRichard Henderson * tlb_flush_page_by_mmuidx_async_1: 5497b7d00e0SRichard Henderson * @cpu: cpu on which to flush 5507b7d00e0SRichard Henderson * @data: encoded addr + idxmap 5517b7d00e0SRichard Henderson * 5527b7d00e0SRichard Henderson * Helper for tlb_flush_page_by_mmuidx and friends, called through 5537b7d00e0SRichard Henderson * async_run_on_cpu. The idxmap parameter is encoded in the page 5547b7d00e0SRichard Henderson * offset of the target_ptr field. This limits the set of mmu_idx 5557b7d00e0SRichard Henderson * that can be passed via this method. 5567b7d00e0SRichard Henderson */ 5577b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_1(CPUState *cpu, 5587b7d00e0SRichard Henderson run_on_cpu_data data) 5597b7d00e0SRichard Henderson { 5607b7d00e0SRichard Henderson target_ulong addr_and_idxmap = (target_ulong) data.target_ptr; 5617b7d00e0SRichard Henderson target_ulong addr = addr_and_idxmap & TARGET_PAGE_MASK; 5627b7d00e0SRichard Henderson uint16_t idxmap = addr_and_idxmap & ~TARGET_PAGE_MASK; 5637b7d00e0SRichard Henderson 5647b7d00e0SRichard Henderson tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap); 5657b7d00e0SRichard Henderson } 5667b7d00e0SRichard Henderson 5677b7d00e0SRichard Henderson typedef struct { 5687b7d00e0SRichard Henderson target_ulong addr; 5697b7d00e0SRichard Henderson uint16_t idxmap; 5707b7d00e0SRichard Henderson } TLBFlushPageByMMUIdxData; 5717b7d00e0SRichard Henderson 5727b7d00e0SRichard Henderson /** 5737b7d00e0SRichard Henderson * tlb_flush_page_by_mmuidx_async_2: 5747b7d00e0SRichard Henderson * @cpu: cpu on which to flush 5757b7d00e0SRichard Henderson * @data: allocated addr + idxmap 5767b7d00e0SRichard Henderson * 5777b7d00e0SRichard Henderson * Helper for tlb_flush_page_by_mmuidx and friends, called through 5787b7d00e0SRichard Henderson * async_run_on_cpu. The addr+idxmap parameters are stored in a 5797b7d00e0SRichard Henderson * TLBFlushPageByMMUIdxData structure that has been allocated 5807b7d00e0SRichard Henderson * specifically for this helper. Free the structure when done. 5817b7d00e0SRichard Henderson */ 5827b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_2(CPUState *cpu, 5837b7d00e0SRichard Henderson run_on_cpu_data data) 5847b7d00e0SRichard Henderson { 5857b7d00e0SRichard Henderson TLBFlushPageByMMUIdxData *d = data.host_ptr; 5867b7d00e0SRichard Henderson 5877b7d00e0SRichard Henderson tlb_flush_page_by_mmuidx_async_0(cpu, d->addr, d->idxmap); 5887b7d00e0SRichard Henderson g_free(d); 5897b7d00e0SRichard Henderson } 5907b7d00e0SRichard Henderson 591d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, uint16_t idxmap) 592d9bb58e5SYang Zhong { 593d9bb58e5SYang Zhong tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%" PRIx16 "\n", addr, idxmap); 594d9bb58e5SYang Zhong 595d9bb58e5SYang Zhong /* This should already be page aligned */ 5967b7d00e0SRichard Henderson addr &= TARGET_PAGE_MASK; 597d9bb58e5SYang Zhong 5987b7d00e0SRichard Henderson if (qemu_cpu_is_self(cpu)) { 5997b7d00e0SRichard Henderson tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap); 6007b7d00e0SRichard Henderson } else if (idxmap < TARGET_PAGE_SIZE) { 6017b7d00e0SRichard Henderson /* 6027b7d00e0SRichard Henderson * Most targets have only a few mmu_idx. In the case where 6037b7d00e0SRichard Henderson * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid 6047b7d00e0SRichard Henderson * allocating memory for this operation. 6057b7d00e0SRichard Henderson */ 6067b7d00e0SRichard Henderson async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_1, 6077b7d00e0SRichard Henderson RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 608d9bb58e5SYang Zhong } else { 6097b7d00e0SRichard Henderson TLBFlushPageByMMUIdxData *d = g_new(TLBFlushPageByMMUIdxData, 1); 6107b7d00e0SRichard Henderson 6117b7d00e0SRichard Henderson /* Otherwise allocate a structure, freed by the worker. */ 6127b7d00e0SRichard Henderson d->addr = addr; 6137b7d00e0SRichard Henderson d->idxmap = idxmap; 6147b7d00e0SRichard Henderson async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_2, 6157b7d00e0SRichard Henderson RUN_ON_CPU_HOST_PTR(d)); 616d9bb58e5SYang Zhong } 617d9bb58e5SYang Zhong } 618d9bb58e5SYang Zhong 619f8144c6cSRichard Henderson void tlb_flush_page(CPUState *cpu, target_ulong addr) 620f8144c6cSRichard Henderson { 621f8144c6cSRichard Henderson tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS); 622f8144c6cSRichard Henderson } 623f8144c6cSRichard Henderson 624d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, target_ulong addr, 625d9bb58e5SYang Zhong uint16_t idxmap) 626d9bb58e5SYang Zhong { 627d9bb58e5SYang Zhong tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap); 628d9bb58e5SYang Zhong 629d9bb58e5SYang Zhong /* This should already be page aligned */ 6307b7d00e0SRichard Henderson addr &= TARGET_PAGE_MASK; 631d9bb58e5SYang Zhong 6327b7d00e0SRichard Henderson /* 6337b7d00e0SRichard Henderson * Allocate memory to hold addr+idxmap only when needed. 6347b7d00e0SRichard Henderson * See tlb_flush_page_by_mmuidx for details. 6357b7d00e0SRichard Henderson */ 6367b7d00e0SRichard Henderson if (idxmap < TARGET_PAGE_SIZE) { 6377b7d00e0SRichard Henderson flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1, 6387b7d00e0SRichard Henderson RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 6397b7d00e0SRichard Henderson } else { 6407b7d00e0SRichard Henderson CPUState *dst_cpu; 6417b7d00e0SRichard Henderson 6427b7d00e0SRichard Henderson /* Allocate a separate data block for each destination cpu. */ 6437b7d00e0SRichard Henderson CPU_FOREACH(dst_cpu) { 6447b7d00e0SRichard Henderson if (dst_cpu != src_cpu) { 6457b7d00e0SRichard Henderson TLBFlushPageByMMUIdxData *d 6467b7d00e0SRichard Henderson = g_new(TLBFlushPageByMMUIdxData, 1); 6477b7d00e0SRichard Henderson 6487b7d00e0SRichard Henderson d->addr = addr; 6497b7d00e0SRichard Henderson d->idxmap = idxmap; 6507b7d00e0SRichard Henderson async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2, 6517b7d00e0SRichard Henderson RUN_ON_CPU_HOST_PTR(d)); 6527b7d00e0SRichard Henderson } 6537b7d00e0SRichard Henderson } 6547b7d00e0SRichard Henderson } 6557b7d00e0SRichard Henderson 6567b7d00e0SRichard Henderson tlb_flush_page_by_mmuidx_async_0(src_cpu, addr, idxmap); 657d9bb58e5SYang Zhong } 658d9bb58e5SYang Zhong 659f8144c6cSRichard Henderson void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr) 660f8144c6cSRichard Henderson { 661f8144c6cSRichard Henderson tlb_flush_page_by_mmuidx_all_cpus(src, addr, ALL_MMUIDX_BITS); 662f8144c6cSRichard Henderson } 663f8144c6cSRichard Henderson 664d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 665d9bb58e5SYang Zhong target_ulong addr, 666d9bb58e5SYang Zhong uint16_t idxmap) 667d9bb58e5SYang Zhong { 668d9bb58e5SYang Zhong tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap); 669d9bb58e5SYang Zhong 670d9bb58e5SYang Zhong /* This should already be page aligned */ 6717b7d00e0SRichard Henderson addr &= TARGET_PAGE_MASK; 672d9bb58e5SYang Zhong 6737b7d00e0SRichard Henderson /* 6747b7d00e0SRichard Henderson * Allocate memory to hold addr+idxmap only when needed. 6757b7d00e0SRichard Henderson * See tlb_flush_page_by_mmuidx for details. 6767b7d00e0SRichard Henderson */ 6777b7d00e0SRichard Henderson if (idxmap < TARGET_PAGE_SIZE) { 6787b7d00e0SRichard Henderson flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1, 6797b7d00e0SRichard Henderson RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 6807b7d00e0SRichard Henderson async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_1, 6817b7d00e0SRichard Henderson RUN_ON_CPU_TARGET_PTR(addr | idxmap)); 6827b7d00e0SRichard Henderson } else { 6837b7d00e0SRichard Henderson CPUState *dst_cpu; 6847b7d00e0SRichard Henderson TLBFlushPageByMMUIdxData *d; 6857b7d00e0SRichard Henderson 6867b7d00e0SRichard Henderson /* Allocate a separate data block for each destination cpu. */ 6877b7d00e0SRichard Henderson CPU_FOREACH(dst_cpu) { 6887b7d00e0SRichard Henderson if (dst_cpu != src_cpu) { 6897b7d00e0SRichard Henderson d = g_new(TLBFlushPageByMMUIdxData, 1); 6907b7d00e0SRichard Henderson d->addr = addr; 6917b7d00e0SRichard Henderson d->idxmap = idxmap; 6927b7d00e0SRichard Henderson async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2, 6937b7d00e0SRichard Henderson RUN_ON_CPU_HOST_PTR(d)); 6947b7d00e0SRichard Henderson } 6957b7d00e0SRichard Henderson } 6967b7d00e0SRichard Henderson 6977b7d00e0SRichard Henderson d = g_new(TLBFlushPageByMMUIdxData, 1); 6987b7d00e0SRichard Henderson d->addr = addr; 6997b7d00e0SRichard Henderson d->idxmap = idxmap; 7007b7d00e0SRichard Henderson async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_2, 7017b7d00e0SRichard Henderson RUN_ON_CPU_HOST_PTR(d)); 7027b7d00e0SRichard Henderson } 703d9bb58e5SYang Zhong } 704d9bb58e5SYang Zhong 705f8144c6cSRichard Henderson void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr) 706d9bb58e5SYang Zhong { 707f8144c6cSRichard Henderson tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS); 708d9bb58e5SYang Zhong } 709d9bb58e5SYang Zhong 7103c4ddec1SRichard Henderson static void tlb_flush_range_locked(CPUArchState *env, int midx, 7113c4ddec1SRichard Henderson target_ulong addr, target_ulong len, 7123c4ddec1SRichard Henderson unsigned bits) 7133ab6e68cSRichard Henderson { 7143ab6e68cSRichard Henderson CPUTLBDesc *d = &env_tlb(env)->d[midx]; 7153ab6e68cSRichard Henderson CPUTLBDescFast *f = &env_tlb(env)->f[midx]; 7163ab6e68cSRichard Henderson target_ulong mask = MAKE_64BIT_MASK(0, bits); 7173ab6e68cSRichard Henderson 7183ab6e68cSRichard Henderson /* 7193ab6e68cSRichard Henderson * If @bits is smaller than the tlb size, there may be multiple entries 7203ab6e68cSRichard Henderson * within the TLB; otherwise all addresses that match under @mask hit 7213ab6e68cSRichard Henderson * the same TLB entry. 7223ab6e68cSRichard Henderson * TODO: Perhaps allow bits to be a few bits less than the size. 7233ab6e68cSRichard Henderson * For now, just flush the entire TLB. 7243c4ddec1SRichard Henderson * 7253c4ddec1SRichard Henderson * If @len is larger than the tlb size, then it will take longer to 7263c4ddec1SRichard Henderson * test all of the entries in the TLB than it will to flush it all. 7273ab6e68cSRichard Henderson */ 7283c4ddec1SRichard Henderson if (mask < f->mask || len > f->mask) { 7293ab6e68cSRichard Henderson tlb_debug("forcing full flush midx %d (" 7303c4ddec1SRichard Henderson TARGET_FMT_lx "/" TARGET_FMT_lx "+" TARGET_FMT_lx ")\n", 7313c4ddec1SRichard Henderson midx, addr, mask, len); 7323ab6e68cSRichard Henderson tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 7333ab6e68cSRichard Henderson return; 7343ab6e68cSRichard Henderson } 7353ab6e68cSRichard Henderson 7363c4ddec1SRichard Henderson /* 7373c4ddec1SRichard Henderson * Check if we need to flush due to large pages. 7383c4ddec1SRichard Henderson * Because large_page_mask contains all 1's from the msb, 7393c4ddec1SRichard Henderson * we only need to test the end of the range. 7403c4ddec1SRichard Henderson */ 7413c4ddec1SRichard Henderson if (((addr + len - 1) & d->large_page_mask) == d->large_page_addr) { 7423ab6e68cSRichard Henderson tlb_debug("forcing full flush midx %d (" 7433ab6e68cSRichard Henderson TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", 7443ab6e68cSRichard Henderson midx, d->large_page_addr, d->large_page_mask); 7453ab6e68cSRichard Henderson tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime()); 7463ab6e68cSRichard Henderson return; 7473ab6e68cSRichard Henderson } 7483ab6e68cSRichard Henderson 7493c4ddec1SRichard Henderson for (target_ulong i = 0; i < len; i += TARGET_PAGE_SIZE) { 7503c4ddec1SRichard Henderson target_ulong page = addr + i; 7513c4ddec1SRichard Henderson CPUTLBEntry *entry = tlb_entry(env, midx, page); 7523c4ddec1SRichard Henderson 7533c4ddec1SRichard Henderson if (tlb_flush_entry_mask_locked(entry, page, mask)) { 7543ab6e68cSRichard Henderson tlb_n_used_entries_dec(env, midx); 7553ab6e68cSRichard Henderson } 7563ab6e68cSRichard Henderson tlb_flush_vtlb_page_mask_locked(env, midx, page, mask); 7573ab6e68cSRichard Henderson } 7583c4ddec1SRichard Henderson } 7593ab6e68cSRichard Henderson 7603ab6e68cSRichard Henderson typedef struct { 7613ab6e68cSRichard Henderson target_ulong addr; 7623c4ddec1SRichard Henderson target_ulong len; 7633ab6e68cSRichard Henderson uint16_t idxmap; 7643ab6e68cSRichard Henderson uint16_t bits; 7653960a59fSRichard Henderson } TLBFlushRangeData; 7663ab6e68cSRichard Henderson 7676be48e45SRichard Henderson static void tlb_flush_range_by_mmuidx_async_0(CPUState *cpu, 7683960a59fSRichard Henderson TLBFlushRangeData d) 7693ab6e68cSRichard Henderson { 7703ab6e68cSRichard Henderson CPUArchState *env = cpu->env_ptr; 7713ab6e68cSRichard Henderson int mmu_idx; 7723ab6e68cSRichard Henderson 7733ab6e68cSRichard Henderson assert_cpu_is_self(cpu); 7743ab6e68cSRichard Henderson 7753c4ddec1SRichard Henderson tlb_debug("range:" TARGET_FMT_lx "/%u+" TARGET_FMT_lx " mmu_map:0x%x\n", 7763c4ddec1SRichard Henderson d.addr, d.bits, d.len, d.idxmap); 7773ab6e68cSRichard Henderson 7783ab6e68cSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 7793ab6e68cSRichard Henderson for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 7803ab6e68cSRichard Henderson if ((d.idxmap >> mmu_idx) & 1) { 7813c4ddec1SRichard Henderson tlb_flush_range_locked(env, mmu_idx, d.addr, d.len, d.bits); 7823ab6e68cSRichard Henderson } 7833ab6e68cSRichard Henderson } 7843ab6e68cSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 7853ab6e68cSRichard Henderson 786cfc2a2d6SIdan Horowitz /* 787cfc2a2d6SIdan Horowitz * If the length is larger than the jump cache size, then it will take 788cfc2a2d6SIdan Horowitz * longer to clear each entry individually than it will to clear it all. 789cfc2a2d6SIdan Horowitz */ 790cfc2a2d6SIdan Horowitz if (d.len >= (TARGET_PAGE_SIZE * TB_JMP_CACHE_SIZE)) { 791cfc2a2d6SIdan Horowitz cpu_tb_jmp_cache_clear(cpu); 792cfc2a2d6SIdan Horowitz return; 793cfc2a2d6SIdan Horowitz } 794cfc2a2d6SIdan Horowitz 7953c4ddec1SRichard Henderson for (target_ulong i = 0; i < d.len; i += TARGET_PAGE_SIZE) { 7963c4ddec1SRichard Henderson tb_flush_jmp_cache(cpu, d.addr + i); 7973c4ddec1SRichard Henderson } 7983ab6e68cSRichard Henderson } 7993ab6e68cSRichard Henderson 800206a583dSRichard Henderson static void tlb_flush_range_by_mmuidx_async_1(CPUState *cpu, 8013ab6e68cSRichard Henderson run_on_cpu_data data) 8023ab6e68cSRichard Henderson { 8033960a59fSRichard Henderson TLBFlushRangeData *d = data.host_ptr; 8046be48e45SRichard Henderson tlb_flush_range_by_mmuidx_async_0(cpu, *d); 8053ab6e68cSRichard Henderson g_free(d); 8063ab6e68cSRichard Henderson } 8073ab6e68cSRichard Henderson 808e5b1921bSRichard Henderson void tlb_flush_range_by_mmuidx(CPUState *cpu, target_ulong addr, 809e5b1921bSRichard Henderson target_ulong len, uint16_t idxmap, 810e5b1921bSRichard Henderson unsigned bits) 8113ab6e68cSRichard Henderson { 8123960a59fSRichard Henderson TLBFlushRangeData d; 8133ab6e68cSRichard Henderson 814e5b1921bSRichard Henderson /* 815e5b1921bSRichard Henderson * If all bits are significant, and len is small, 816e5b1921bSRichard Henderson * this devolves to tlb_flush_page. 817e5b1921bSRichard Henderson */ 818e5b1921bSRichard Henderson if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 8193ab6e68cSRichard Henderson tlb_flush_page_by_mmuidx(cpu, addr, idxmap); 8203ab6e68cSRichard Henderson return; 8213ab6e68cSRichard Henderson } 8223ab6e68cSRichard Henderson /* If no page bits are significant, this devolves to tlb_flush. */ 8233ab6e68cSRichard Henderson if (bits < TARGET_PAGE_BITS) { 8243ab6e68cSRichard Henderson tlb_flush_by_mmuidx(cpu, idxmap); 8253ab6e68cSRichard Henderson return; 8263ab6e68cSRichard Henderson } 8273ab6e68cSRichard Henderson 8283ab6e68cSRichard Henderson /* This should already be page aligned */ 8293ab6e68cSRichard Henderson d.addr = addr & TARGET_PAGE_MASK; 830e5b1921bSRichard Henderson d.len = len; 8313ab6e68cSRichard Henderson d.idxmap = idxmap; 8323ab6e68cSRichard Henderson d.bits = bits; 8333ab6e68cSRichard Henderson 8343ab6e68cSRichard Henderson if (qemu_cpu_is_self(cpu)) { 8356be48e45SRichard Henderson tlb_flush_range_by_mmuidx_async_0(cpu, d); 8363ab6e68cSRichard Henderson } else { 8373ab6e68cSRichard Henderson /* Otherwise allocate a structure, freed by the worker. */ 8383960a59fSRichard Henderson TLBFlushRangeData *p = g_memdup(&d, sizeof(d)); 839206a583dSRichard Henderson async_run_on_cpu(cpu, tlb_flush_range_by_mmuidx_async_1, 8403ab6e68cSRichard Henderson RUN_ON_CPU_HOST_PTR(p)); 8413ab6e68cSRichard Henderson } 8423ab6e68cSRichard Henderson } 8433ab6e68cSRichard Henderson 844e5b1921bSRichard Henderson void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, target_ulong addr, 845e5b1921bSRichard Henderson uint16_t idxmap, unsigned bits) 846e5b1921bSRichard Henderson { 847e5b1921bSRichard Henderson tlb_flush_range_by_mmuidx(cpu, addr, TARGET_PAGE_SIZE, idxmap, bits); 848e5b1921bSRichard Henderson } 849e5b1921bSRichard Henderson 850600b819fSRichard Henderson void tlb_flush_range_by_mmuidx_all_cpus(CPUState *src_cpu, 851600b819fSRichard Henderson target_ulong addr, target_ulong len, 852600b819fSRichard Henderson uint16_t idxmap, unsigned bits) 8533ab6e68cSRichard Henderson { 8543960a59fSRichard Henderson TLBFlushRangeData d; 855d34e4d1aSRichard Henderson CPUState *dst_cpu; 8563ab6e68cSRichard Henderson 857600b819fSRichard Henderson /* 858600b819fSRichard Henderson * If all bits are significant, and len is small, 859600b819fSRichard Henderson * this devolves to tlb_flush_page. 860600b819fSRichard Henderson */ 861600b819fSRichard Henderson if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 8623ab6e68cSRichard Henderson tlb_flush_page_by_mmuidx_all_cpus(src_cpu, addr, idxmap); 8633ab6e68cSRichard Henderson return; 8643ab6e68cSRichard Henderson } 8653ab6e68cSRichard Henderson /* If no page bits are significant, this devolves to tlb_flush. */ 8663ab6e68cSRichard Henderson if (bits < TARGET_PAGE_BITS) { 8673ab6e68cSRichard Henderson tlb_flush_by_mmuidx_all_cpus(src_cpu, idxmap); 8683ab6e68cSRichard Henderson return; 8693ab6e68cSRichard Henderson } 8703ab6e68cSRichard Henderson 8713ab6e68cSRichard Henderson /* This should already be page aligned */ 8723ab6e68cSRichard Henderson d.addr = addr & TARGET_PAGE_MASK; 873600b819fSRichard Henderson d.len = len; 8743ab6e68cSRichard Henderson d.idxmap = idxmap; 8753ab6e68cSRichard Henderson d.bits = bits; 8763ab6e68cSRichard Henderson 8773ab6e68cSRichard Henderson /* Allocate a separate data block for each destination cpu. */ 8783ab6e68cSRichard Henderson CPU_FOREACH(dst_cpu) { 8793ab6e68cSRichard Henderson if (dst_cpu != src_cpu) { 8803960a59fSRichard Henderson TLBFlushRangeData *p = g_memdup(&d, sizeof(d)); 8813ab6e68cSRichard Henderson async_run_on_cpu(dst_cpu, 882206a583dSRichard Henderson tlb_flush_range_by_mmuidx_async_1, 8833ab6e68cSRichard Henderson RUN_ON_CPU_HOST_PTR(p)); 8843ab6e68cSRichard Henderson } 8853ab6e68cSRichard Henderson } 8863ab6e68cSRichard Henderson 8876be48e45SRichard Henderson tlb_flush_range_by_mmuidx_async_0(src_cpu, d); 8883ab6e68cSRichard Henderson } 8893ab6e68cSRichard Henderson 890600b819fSRichard Henderson void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState *src_cpu, 891600b819fSRichard Henderson target_ulong addr, 892600b819fSRichard Henderson uint16_t idxmap, unsigned bits) 893600b819fSRichard Henderson { 894600b819fSRichard Henderson tlb_flush_range_by_mmuidx_all_cpus(src_cpu, addr, TARGET_PAGE_SIZE, 895600b819fSRichard Henderson idxmap, bits); 896600b819fSRichard Henderson } 897600b819fSRichard Henderson 898c13b27d8SRichard Henderson void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 8993ab6e68cSRichard Henderson target_ulong addr, 900c13b27d8SRichard Henderson target_ulong len, 9013ab6e68cSRichard Henderson uint16_t idxmap, 9023ab6e68cSRichard Henderson unsigned bits) 9033ab6e68cSRichard Henderson { 904d34e4d1aSRichard Henderson TLBFlushRangeData d, *p; 905d34e4d1aSRichard Henderson CPUState *dst_cpu; 9063ab6e68cSRichard Henderson 907c13b27d8SRichard Henderson /* 908c13b27d8SRichard Henderson * If all bits are significant, and len is small, 909c13b27d8SRichard Henderson * this devolves to tlb_flush_page. 910c13b27d8SRichard Henderson */ 911c13b27d8SRichard Henderson if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) { 9123ab6e68cSRichard Henderson tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu, addr, idxmap); 9133ab6e68cSRichard Henderson return; 9143ab6e68cSRichard Henderson } 9153ab6e68cSRichard Henderson /* If no page bits are significant, this devolves to tlb_flush. */ 9163ab6e68cSRichard Henderson if (bits < TARGET_PAGE_BITS) { 9173ab6e68cSRichard Henderson tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, idxmap); 9183ab6e68cSRichard Henderson return; 9193ab6e68cSRichard Henderson } 9203ab6e68cSRichard Henderson 9213ab6e68cSRichard Henderson /* This should already be page aligned */ 9223ab6e68cSRichard Henderson d.addr = addr & TARGET_PAGE_MASK; 923c13b27d8SRichard Henderson d.len = len; 9243ab6e68cSRichard Henderson d.idxmap = idxmap; 9253ab6e68cSRichard Henderson d.bits = bits; 9263ab6e68cSRichard Henderson 9273ab6e68cSRichard Henderson /* Allocate a separate data block for each destination cpu. */ 9283ab6e68cSRichard Henderson CPU_FOREACH(dst_cpu) { 9293ab6e68cSRichard Henderson if (dst_cpu != src_cpu) { 9306d244788SRichard Henderson p = g_memdup(&d, sizeof(d)); 931206a583dSRichard Henderson async_run_on_cpu(dst_cpu, tlb_flush_range_by_mmuidx_async_1, 9323ab6e68cSRichard Henderson RUN_ON_CPU_HOST_PTR(p)); 9333ab6e68cSRichard Henderson } 9343ab6e68cSRichard Henderson } 9353ab6e68cSRichard Henderson 9366d244788SRichard Henderson p = g_memdup(&d, sizeof(d)); 937206a583dSRichard Henderson async_safe_run_on_cpu(src_cpu, tlb_flush_range_by_mmuidx_async_1, 9383ab6e68cSRichard Henderson RUN_ON_CPU_HOST_PTR(p)); 9393ab6e68cSRichard Henderson } 9403ab6e68cSRichard Henderson 941c13b27d8SRichard Henderson void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *src_cpu, 942c13b27d8SRichard Henderson target_ulong addr, 943c13b27d8SRichard Henderson uint16_t idxmap, 944c13b27d8SRichard Henderson unsigned bits) 945c13b27d8SRichard Henderson { 946c13b27d8SRichard Henderson tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu, addr, TARGET_PAGE_SIZE, 947c13b27d8SRichard Henderson idxmap, bits); 948c13b27d8SRichard Henderson } 949c13b27d8SRichard Henderson 950d9bb58e5SYang Zhong /* update the TLBs so that writes to code in the virtual page 'addr' 951d9bb58e5SYang Zhong can be detected */ 952d9bb58e5SYang Zhong void tlb_protect_code(ram_addr_t ram_addr) 953d9bb58e5SYang Zhong { 954d9bb58e5SYang Zhong cpu_physical_memory_test_and_clear_dirty(ram_addr, TARGET_PAGE_SIZE, 955d9bb58e5SYang Zhong DIRTY_MEMORY_CODE); 956d9bb58e5SYang Zhong } 957d9bb58e5SYang Zhong 958d9bb58e5SYang Zhong /* update the TLB so that writes in physical page 'phys_addr' are no longer 959d9bb58e5SYang Zhong tested for self modifying code */ 960d9bb58e5SYang Zhong void tlb_unprotect_code(ram_addr_t ram_addr) 961d9bb58e5SYang Zhong { 962d9bb58e5SYang Zhong cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE); 963d9bb58e5SYang Zhong } 964d9bb58e5SYang Zhong 965d9bb58e5SYang Zhong 966d9bb58e5SYang Zhong /* 967d9bb58e5SYang Zhong * Dirty write flag handling 968d9bb58e5SYang Zhong * 969d9bb58e5SYang Zhong * When the TCG code writes to a location it looks up the address in 970d9bb58e5SYang Zhong * the TLB and uses that data to compute the final address. If any of 971d9bb58e5SYang Zhong * the lower bits of the address are set then the slow path is forced. 972d9bb58e5SYang Zhong * There are a number of reasons to do this but for normal RAM the 973d9bb58e5SYang Zhong * most usual is detecting writes to code regions which may invalidate 974d9bb58e5SYang Zhong * generated code. 975d9bb58e5SYang Zhong * 97671aec354SEmilio G. Cota * Other vCPUs might be reading their TLBs during guest execution, so we update 977d73415a3SStefan Hajnoczi * te->addr_write with qatomic_set. We don't need to worry about this for 97871aec354SEmilio G. Cota * oversized guests as MTTCG is disabled for them. 979d9bb58e5SYang Zhong * 98053d28455SRichard Henderson * Called with tlb_c.lock held. 981d9bb58e5SYang Zhong */ 98271aec354SEmilio G. Cota static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry, 98371aec354SEmilio G. Cota uintptr_t start, uintptr_t length) 984d9bb58e5SYang Zhong { 985d9bb58e5SYang Zhong uintptr_t addr = tlb_entry->addr_write; 986d9bb58e5SYang Zhong 9877b0d792cSRichard Henderson if ((addr & (TLB_INVALID_MASK | TLB_MMIO | 9887b0d792cSRichard Henderson TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) { 989d9bb58e5SYang Zhong addr &= TARGET_PAGE_MASK; 990d9bb58e5SYang Zhong addr += tlb_entry->addend; 991d9bb58e5SYang Zhong if ((addr - start) < length) { 992d9bb58e5SYang Zhong #if TCG_OVERSIZED_GUEST 99371aec354SEmilio G. Cota tlb_entry->addr_write |= TLB_NOTDIRTY; 994d9bb58e5SYang Zhong #else 995d73415a3SStefan Hajnoczi qatomic_set(&tlb_entry->addr_write, 99671aec354SEmilio G. Cota tlb_entry->addr_write | TLB_NOTDIRTY); 997d9bb58e5SYang Zhong #endif 998d9bb58e5SYang Zhong } 99971aec354SEmilio G. Cota } 100071aec354SEmilio G. Cota } 100171aec354SEmilio G. Cota 100271aec354SEmilio G. Cota /* 100353d28455SRichard Henderson * Called with tlb_c.lock held. 100471aec354SEmilio G. Cota * Called only from the vCPU context, i.e. the TLB's owner thread. 100571aec354SEmilio G. Cota */ 100671aec354SEmilio G. Cota static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s) 100771aec354SEmilio G. Cota { 100871aec354SEmilio G. Cota *d = *s; 100971aec354SEmilio G. Cota } 1010d9bb58e5SYang Zhong 1011d9bb58e5SYang Zhong /* This is a cross vCPU call (i.e. another vCPU resetting the flags of 101271aec354SEmilio G. Cota * the target vCPU). 101353d28455SRichard Henderson * We must take tlb_c.lock to avoid racing with another vCPU update. The only 101471aec354SEmilio G. Cota * thing actually updated is the target TLB entry ->addr_write flags. 1015d9bb58e5SYang Zhong */ 1016d9bb58e5SYang Zhong void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length) 1017d9bb58e5SYang Zhong { 1018d9bb58e5SYang Zhong CPUArchState *env; 1019d9bb58e5SYang Zhong 1020d9bb58e5SYang Zhong int mmu_idx; 1021d9bb58e5SYang Zhong 1022d9bb58e5SYang Zhong env = cpu->env_ptr; 1023a40ec84eSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 1024d9bb58e5SYang Zhong for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1025d9bb58e5SYang Zhong unsigned int i; 1026722a1c1eSRichard Henderson unsigned int n = tlb_n_entries(&env_tlb(env)->f[mmu_idx]); 1027d9bb58e5SYang Zhong 102886e1eff8SEmilio G. Cota for (i = 0; i < n; i++) { 1029a40ec84eSRichard Henderson tlb_reset_dirty_range_locked(&env_tlb(env)->f[mmu_idx].table[i], 1030a40ec84eSRichard Henderson start1, length); 1031d9bb58e5SYang Zhong } 1032d9bb58e5SYang Zhong 1033d9bb58e5SYang Zhong for (i = 0; i < CPU_VTLB_SIZE; i++) { 1034a40ec84eSRichard Henderson tlb_reset_dirty_range_locked(&env_tlb(env)->d[mmu_idx].vtable[i], 1035a40ec84eSRichard Henderson start1, length); 1036d9bb58e5SYang Zhong } 1037d9bb58e5SYang Zhong } 1038a40ec84eSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 1039d9bb58e5SYang Zhong } 1040d9bb58e5SYang Zhong 104153d28455SRichard Henderson /* Called with tlb_c.lock held */ 104271aec354SEmilio G. Cota static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry, 104371aec354SEmilio G. Cota target_ulong vaddr) 1044d9bb58e5SYang Zhong { 1045d9bb58e5SYang Zhong if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) { 1046d9bb58e5SYang Zhong tlb_entry->addr_write = vaddr; 1047d9bb58e5SYang Zhong } 1048d9bb58e5SYang Zhong } 1049d9bb58e5SYang Zhong 1050d9bb58e5SYang Zhong /* update the TLB corresponding to virtual page vaddr 1051d9bb58e5SYang Zhong so that it is no longer dirty */ 1052d9bb58e5SYang Zhong void tlb_set_dirty(CPUState *cpu, target_ulong vaddr) 1053d9bb58e5SYang Zhong { 1054d9bb58e5SYang Zhong CPUArchState *env = cpu->env_ptr; 1055d9bb58e5SYang Zhong int mmu_idx; 1056d9bb58e5SYang Zhong 1057d9bb58e5SYang Zhong assert_cpu_is_self(cpu); 1058d9bb58e5SYang Zhong 1059d9bb58e5SYang Zhong vaddr &= TARGET_PAGE_MASK; 1060a40ec84eSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 1061d9bb58e5SYang Zhong for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1062383beda9SRichard Henderson tlb_set_dirty1_locked(tlb_entry(env, mmu_idx, vaddr), vaddr); 1063d9bb58e5SYang Zhong } 1064d9bb58e5SYang Zhong 1065d9bb58e5SYang Zhong for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { 1066d9bb58e5SYang Zhong int k; 1067d9bb58e5SYang Zhong for (k = 0; k < CPU_VTLB_SIZE; k++) { 1068a40ec84eSRichard Henderson tlb_set_dirty1_locked(&env_tlb(env)->d[mmu_idx].vtable[k], vaddr); 1069d9bb58e5SYang Zhong } 1070d9bb58e5SYang Zhong } 1071a40ec84eSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 1072d9bb58e5SYang Zhong } 1073d9bb58e5SYang Zhong 1074d9bb58e5SYang Zhong /* Our TLB does not support large pages, so remember the area covered by 1075d9bb58e5SYang Zhong large pages and trigger a full TLB flush if these are invalidated. */ 10761308e026SRichard Henderson static void tlb_add_large_page(CPUArchState *env, int mmu_idx, 10771308e026SRichard Henderson target_ulong vaddr, target_ulong size) 1078d9bb58e5SYang Zhong { 1079a40ec84eSRichard Henderson target_ulong lp_addr = env_tlb(env)->d[mmu_idx].large_page_addr; 10801308e026SRichard Henderson target_ulong lp_mask = ~(size - 1); 1081d9bb58e5SYang Zhong 10821308e026SRichard Henderson if (lp_addr == (target_ulong)-1) { 10831308e026SRichard Henderson /* No previous large page. */ 10841308e026SRichard Henderson lp_addr = vaddr; 10851308e026SRichard Henderson } else { 1086d9bb58e5SYang Zhong /* Extend the existing region to include the new page. 10871308e026SRichard Henderson This is a compromise between unnecessary flushes and 10881308e026SRichard Henderson the cost of maintaining a full variable size TLB. */ 1089a40ec84eSRichard Henderson lp_mask &= env_tlb(env)->d[mmu_idx].large_page_mask; 10901308e026SRichard Henderson while (((lp_addr ^ vaddr) & lp_mask) != 0) { 10911308e026SRichard Henderson lp_mask <<= 1; 1092d9bb58e5SYang Zhong } 10931308e026SRichard Henderson } 1094a40ec84eSRichard Henderson env_tlb(env)->d[mmu_idx].large_page_addr = lp_addr & lp_mask; 1095a40ec84eSRichard Henderson env_tlb(env)->d[mmu_idx].large_page_mask = lp_mask; 1096d9bb58e5SYang Zhong } 1097d9bb58e5SYang Zhong 1098d9bb58e5SYang Zhong /* Add a new TLB entry. At most one entry for a given virtual address 1099d9bb58e5SYang Zhong * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the 1100d9bb58e5SYang Zhong * supplied size is only used by tlb_flush_page. 1101d9bb58e5SYang Zhong * 1102d9bb58e5SYang Zhong * Called from TCG-generated code, which is under an RCU read-side 1103d9bb58e5SYang Zhong * critical section. 1104d9bb58e5SYang Zhong */ 1105d9bb58e5SYang Zhong void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, 1106d9bb58e5SYang Zhong hwaddr paddr, MemTxAttrs attrs, int prot, 1107d9bb58e5SYang Zhong int mmu_idx, target_ulong size) 1108d9bb58e5SYang Zhong { 1109d9bb58e5SYang Zhong CPUArchState *env = cpu->env_ptr; 1110a40ec84eSRichard Henderson CPUTLB *tlb = env_tlb(env); 1111a40ec84eSRichard Henderson CPUTLBDesc *desc = &tlb->d[mmu_idx]; 1112d9bb58e5SYang Zhong MemoryRegionSection *section; 1113d9bb58e5SYang Zhong unsigned int index; 1114d9bb58e5SYang Zhong target_ulong address; 11158f5db641SRichard Henderson target_ulong write_address; 1116d9bb58e5SYang Zhong uintptr_t addend; 111768fea038SRichard Henderson CPUTLBEntry *te, tn; 111855df6fcfSPeter Maydell hwaddr iotlb, xlat, sz, paddr_page; 111955df6fcfSPeter Maydell target_ulong vaddr_page; 1120d9bb58e5SYang Zhong int asidx = cpu_asidx_from_attrs(cpu, attrs); 112150b107c5SRichard Henderson int wp_flags; 11228f5db641SRichard Henderson bool is_ram, is_romd; 1123d9bb58e5SYang Zhong 1124d9bb58e5SYang Zhong assert_cpu_is_self(cpu); 112555df6fcfSPeter Maydell 11261308e026SRichard Henderson if (size <= TARGET_PAGE_SIZE) { 112755df6fcfSPeter Maydell sz = TARGET_PAGE_SIZE; 112855df6fcfSPeter Maydell } else { 11291308e026SRichard Henderson tlb_add_large_page(env, mmu_idx, vaddr, size); 1130d9bb58e5SYang Zhong sz = size; 113155df6fcfSPeter Maydell } 113255df6fcfSPeter Maydell vaddr_page = vaddr & TARGET_PAGE_MASK; 113355df6fcfSPeter Maydell paddr_page = paddr & TARGET_PAGE_MASK; 113455df6fcfSPeter Maydell 113555df6fcfSPeter Maydell section = address_space_translate_for_iotlb(cpu, asidx, paddr_page, 113655df6fcfSPeter Maydell &xlat, &sz, attrs, &prot); 1137d9bb58e5SYang Zhong assert(sz >= TARGET_PAGE_SIZE); 1138d9bb58e5SYang Zhong 1139d9bb58e5SYang Zhong tlb_debug("vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx 1140d9bb58e5SYang Zhong " prot=%x idx=%d\n", 1141d9bb58e5SYang Zhong vaddr, paddr, prot, mmu_idx); 1142d9bb58e5SYang Zhong 114355df6fcfSPeter Maydell address = vaddr_page; 114455df6fcfSPeter Maydell if (size < TARGET_PAGE_SIZE) { 114530d7e098SRichard Henderson /* Repeat the MMU check and TLB fill on every access. */ 114630d7e098SRichard Henderson address |= TLB_INVALID_MASK; 114755df6fcfSPeter Maydell } 1148a26fc6f5STony Nguyen if (attrs.byte_swap) { 11495b87b3e6SRichard Henderson address |= TLB_BSWAP; 1150a26fc6f5STony Nguyen } 11518f5db641SRichard Henderson 11528f5db641SRichard Henderson is_ram = memory_region_is_ram(section->mr); 11538f5db641SRichard Henderson is_romd = memory_region_is_romd(section->mr); 11548f5db641SRichard Henderson 11558f5db641SRichard Henderson if (is_ram || is_romd) { 11568f5db641SRichard Henderson /* RAM and ROMD both have associated host memory. */ 1157d9bb58e5SYang Zhong addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat; 11588f5db641SRichard Henderson } else { 11598f5db641SRichard Henderson /* I/O does not; force the host address to NULL. */ 11608f5db641SRichard Henderson addend = 0; 1161d9bb58e5SYang Zhong } 1162d9bb58e5SYang Zhong 11638f5db641SRichard Henderson write_address = address; 11648f5db641SRichard Henderson if (is_ram) { 11658f5db641SRichard Henderson iotlb = memory_region_get_ram_addr(section->mr) + xlat; 11668f5db641SRichard Henderson /* 11678f5db641SRichard Henderson * Computing is_clean is expensive; avoid all that unless 11688f5db641SRichard Henderson * the page is actually writable. 11698f5db641SRichard Henderson */ 11708f5db641SRichard Henderson if (prot & PAGE_WRITE) { 11718f5db641SRichard Henderson if (section->readonly) { 11728f5db641SRichard Henderson write_address |= TLB_DISCARD_WRITE; 11738f5db641SRichard Henderson } else if (cpu_physical_memory_is_clean(iotlb)) { 11748f5db641SRichard Henderson write_address |= TLB_NOTDIRTY; 11758f5db641SRichard Henderson } 11768f5db641SRichard Henderson } 11778f5db641SRichard Henderson } else { 11788f5db641SRichard Henderson /* I/O or ROMD */ 11798f5db641SRichard Henderson iotlb = memory_region_section_get_iotlb(cpu, section) + xlat; 11808f5db641SRichard Henderson /* 11818f5db641SRichard Henderson * Writes to romd devices must go through MMIO to enable write. 11828f5db641SRichard Henderson * Reads to romd devices go through the ram_ptr found above, 11838f5db641SRichard Henderson * but of course reads to I/O must go through MMIO. 11848f5db641SRichard Henderson */ 11858f5db641SRichard Henderson write_address |= TLB_MMIO; 11868f5db641SRichard Henderson if (!is_romd) { 11878f5db641SRichard Henderson address = write_address; 11888f5db641SRichard Henderson } 11898f5db641SRichard Henderson } 11908f5db641SRichard Henderson 119150b107c5SRichard Henderson wp_flags = cpu_watchpoint_address_matches(cpu, vaddr_page, 119250b107c5SRichard Henderson TARGET_PAGE_SIZE); 1193d9bb58e5SYang Zhong 1194383beda9SRichard Henderson index = tlb_index(env, mmu_idx, vaddr_page); 1195383beda9SRichard Henderson te = tlb_entry(env, mmu_idx, vaddr_page); 1196d9bb58e5SYang Zhong 119768fea038SRichard Henderson /* 119871aec354SEmilio G. Cota * Hold the TLB lock for the rest of the function. We could acquire/release 119971aec354SEmilio G. Cota * the lock several times in the function, but it is faster to amortize the 120071aec354SEmilio G. Cota * acquisition cost by acquiring it just once. Note that this leads to 120171aec354SEmilio G. Cota * a longer critical section, but this is not a concern since the TLB lock 120271aec354SEmilio G. Cota * is unlikely to be contended. 120371aec354SEmilio G. Cota */ 1204a40ec84eSRichard Henderson qemu_spin_lock(&tlb->c.lock); 120571aec354SEmilio G. Cota 12063d1523ceSRichard Henderson /* Note that the tlb is no longer clean. */ 1207a40ec84eSRichard Henderson tlb->c.dirty |= 1 << mmu_idx; 12083d1523ceSRichard Henderson 120971aec354SEmilio G. Cota /* Make sure there's no cached translation for the new page. */ 121071aec354SEmilio G. Cota tlb_flush_vtlb_page_locked(env, mmu_idx, vaddr_page); 121171aec354SEmilio G. Cota 121271aec354SEmilio G. Cota /* 121368fea038SRichard Henderson * Only evict the old entry to the victim tlb if it's for a 121468fea038SRichard Henderson * different page; otherwise just overwrite the stale data. 121568fea038SRichard Henderson */ 12163cea94bbSEmilio G. Cota if (!tlb_hit_page_anyprot(te, vaddr_page) && !tlb_entry_is_empty(te)) { 1217a40ec84eSRichard Henderson unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE; 1218a40ec84eSRichard Henderson CPUTLBEntry *tv = &desc->vtable[vidx]; 121968fea038SRichard Henderson 122068fea038SRichard Henderson /* Evict the old entry into the victim tlb. */ 122171aec354SEmilio G. Cota copy_tlb_helper_locked(tv, te); 1222a40ec84eSRichard Henderson desc->viotlb[vidx] = desc->iotlb[index]; 122386e1eff8SEmilio G. Cota tlb_n_used_entries_dec(env, mmu_idx); 122468fea038SRichard Henderson } 1225d9bb58e5SYang Zhong 1226d9bb58e5SYang Zhong /* refill the tlb */ 1227ace41090SPeter Maydell /* 1228ace41090SPeter Maydell * At this point iotlb contains a physical section number in the lower 1229ace41090SPeter Maydell * TARGET_PAGE_BITS, and either 12308f5db641SRichard Henderson * + the ram_addr_t of the page base of the target RAM (RAM) 12318f5db641SRichard Henderson * + the offset within section->mr of the page base (I/O, ROMD) 123255df6fcfSPeter Maydell * We subtract the vaddr_page (which is page aligned and thus won't 1233ace41090SPeter Maydell * disturb the low bits) to give an offset which can be added to the 1234ace41090SPeter Maydell * (non-page-aligned) vaddr of the eventual memory access to get 1235ace41090SPeter Maydell * the MemoryRegion offset for the access. Note that the vaddr we 1236ace41090SPeter Maydell * subtract here is that of the page base, and not the same as the 1237ace41090SPeter Maydell * vaddr we add back in io_readx()/io_writex()/get_page_addr_code(). 1238ace41090SPeter Maydell */ 1239a40ec84eSRichard Henderson desc->iotlb[index].addr = iotlb - vaddr_page; 1240a40ec84eSRichard Henderson desc->iotlb[index].attrs = attrs; 1241d9bb58e5SYang Zhong 1242d9bb58e5SYang Zhong /* Now calculate the new entry */ 124355df6fcfSPeter Maydell tn.addend = addend - vaddr_page; 1244d9bb58e5SYang Zhong if (prot & PAGE_READ) { 1245d9bb58e5SYang Zhong tn.addr_read = address; 124650b107c5SRichard Henderson if (wp_flags & BP_MEM_READ) { 124750b107c5SRichard Henderson tn.addr_read |= TLB_WATCHPOINT; 124850b107c5SRichard Henderson } 1249d9bb58e5SYang Zhong } else { 1250d9bb58e5SYang Zhong tn.addr_read = -1; 1251d9bb58e5SYang Zhong } 1252d9bb58e5SYang Zhong 1253d9bb58e5SYang Zhong if (prot & PAGE_EXEC) { 12548f5db641SRichard Henderson tn.addr_code = address; 1255d9bb58e5SYang Zhong } else { 1256d9bb58e5SYang Zhong tn.addr_code = -1; 1257d9bb58e5SYang Zhong } 1258d9bb58e5SYang Zhong 1259d9bb58e5SYang Zhong tn.addr_write = -1; 1260d9bb58e5SYang Zhong if (prot & PAGE_WRITE) { 12618f5db641SRichard Henderson tn.addr_write = write_address; 1262f52bfb12SDavid Hildenbrand if (prot & PAGE_WRITE_INV) { 1263f52bfb12SDavid Hildenbrand tn.addr_write |= TLB_INVALID_MASK; 1264f52bfb12SDavid Hildenbrand } 126550b107c5SRichard Henderson if (wp_flags & BP_MEM_WRITE) { 126650b107c5SRichard Henderson tn.addr_write |= TLB_WATCHPOINT; 126750b107c5SRichard Henderson } 1268d9bb58e5SYang Zhong } 1269d9bb58e5SYang Zhong 127071aec354SEmilio G. Cota copy_tlb_helper_locked(te, &tn); 127186e1eff8SEmilio G. Cota tlb_n_used_entries_inc(env, mmu_idx); 1272a40ec84eSRichard Henderson qemu_spin_unlock(&tlb->c.lock); 1273d9bb58e5SYang Zhong } 1274d9bb58e5SYang Zhong 1275d9bb58e5SYang Zhong /* Add a new TLB entry, but without specifying the memory 1276d9bb58e5SYang Zhong * transaction attributes to be used. 1277d9bb58e5SYang Zhong */ 1278d9bb58e5SYang Zhong void tlb_set_page(CPUState *cpu, target_ulong vaddr, 1279d9bb58e5SYang Zhong hwaddr paddr, int prot, 1280d9bb58e5SYang Zhong int mmu_idx, target_ulong size) 1281d9bb58e5SYang Zhong { 1282d9bb58e5SYang Zhong tlb_set_page_with_attrs(cpu, vaddr, paddr, MEMTXATTRS_UNSPECIFIED, 1283d9bb58e5SYang Zhong prot, mmu_idx, size); 1284d9bb58e5SYang Zhong } 1285d9bb58e5SYang Zhong 1286d9bb58e5SYang Zhong static inline ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr) 1287d9bb58e5SYang Zhong { 1288d9bb58e5SYang Zhong ram_addr_t ram_addr; 1289d9bb58e5SYang Zhong 1290d9bb58e5SYang Zhong ram_addr = qemu_ram_addr_from_host(ptr); 1291d9bb58e5SYang Zhong if (ram_addr == RAM_ADDR_INVALID) { 1292d9bb58e5SYang Zhong error_report("Bad ram pointer %p", ptr); 1293d9bb58e5SYang Zhong abort(); 1294d9bb58e5SYang Zhong } 1295d9bb58e5SYang Zhong return ram_addr; 1296d9bb58e5SYang Zhong } 1297d9bb58e5SYang Zhong 1298c319dc13SRichard Henderson /* 1299c319dc13SRichard Henderson * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the 1300c319dc13SRichard Henderson * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must 1301c319dc13SRichard Henderson * be discarded and looked up again (e.g. via tlb_entry()). 1302c319dc13SRichard Henderson */ 1303c319dc13SRichard Henderson static void tlb_fill(CPUState *cpu, target_ulong addr, int size, 1304c319dc13SRichard Henderson MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 1305c319dc13SRichard Henderson { 1306c319dc13SRichard Henderson CPUClass *cc = CPU_GET_CLASS(cpu); 1307c319dc13SRichard Henderson bool ok; 1308c319dc13SRichard Henderson 1309c319dc13SRichard Henderson /* 1310c319dc13SRichard Henderson * This is not a probe, so only valid return is success; failure 1311c319dc13SRichard Henderson * should result in exception + longjmp to the cpu loop. 1312c319dc13SRichard Henderson */ 131378271684SClaudio Fontana ok = cc->tcg_ops->tlb_fill(cpu, addr, size, 1314e124536fSEduardo Habkost access_type, mmu_idx, false, retaddr); 1315c319dc13SRichard Henderson assert(ok); 1316c319dc13SRichard Henderson } 1317c319dc13SRichard Henderson 131878271684SClaudio Fontana static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, 131978271684SClaudio Fontana MMUAccessType access_type, 132078271684SClaudio Fontana int mmu_idx, uintptr_t retaddr) 132178271684SClaudio Fontana { 132278271684SClaudio Fontana CPUClass *cc = CPU_GET_CLASS(cpu); 132378271684SClaudio Fontana 132478271684SClaudio Fontana cc->tcg_ops->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr); 132578271684SClaudio Fontana } 132678271684SClaudio Fontana 132778271684SClaudio Fontana static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr, 132878271684SClaudio Fontana vaddr addr, unsigned size, 132978271684SClaudio Fontana MMUAccessType access_type, 133078271684SClaudio Fontana int mmu_idx, MemTxAttrs attrs, 133178271684SClaudio Fontana MemTxResult response, 133278271684SClaudio Fontana uintptr_t retaddr) 133378271684SClaudio Fontana { 133478271684SClaudio Fontana CPUClass *cc = CPU_GET_CLASS(cpu); 133578271684SClaudio Fontana 133678271684SClaudio Fontana if (!cpu->ignore_memory_transaction_failures && 133778271684SClaudio Fontana cc->tcg_ops->do_transaction_failed) { 133878271684SClaudio Fontana cc->tcg_ops->do_transaction_failed(cpu, physaddr, addr, size, 133978271684SClaudio Fontana access_type, mmu_idx, attrs, 134078271684SClaudio Fontana response, retaddr); 134178271684SClaudio Fontana } 134278271684SClaudio Fontana } 134378271684SClaudio Fontana 1344d9bb58e5SYang Zhong static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry, 1345f1be3696SRichard Henderson int mmu_idx, target_ulong addr, uintptr_t retaddr, 1346be5c4787STony Nguyen MMUAccessType access_type, MemOp op) 1347d9bb58e5SYang Zhong { 134829a0af61SRichard Henderson CPUState *cpu = env_cpu(env); 13492d54f194SPeter Maydell hwaddr mr_offset; 13502d54f194SPeter Maydell MemoryRegionSection *section; 13512d54f194SPeter Maydell MemoryRegion *mr; 1352d9bb58e5SYang Zhong uint64_t val; 1353d9bb58e5SYang Zhong bool locked = false; 135404e3aabdSPeter Maydell MemTxResult r; 1355d9bb58e5SYang Zhong 13562d54f194SPeter Maydell section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs); 13572d54f194SPeter Maydell mr = section->mr; 13582d54f194SPeter Maydell mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr; 1359d9bb58e5SYang Zhong cpu->mem_io_pc = retaddr; 136008565552SRichard Henderson if (!cpu->can_do_io) { 1361d9bb58e5SYang Zhong cpu_io_recompile(cpu, retaddr); 1362d9bb58e5SYang Zhong } 1363d9bb58e5SYang Zhong 136441744954SPhilippe Mathieu-Daudé if (!qemu_mutex_iothread_locked()) { 1365d9bb58e5SYang Zhong qemu_mutex_lock_iothread(); 1366d9bb58e5SYang Zhong locked = true; 1367d9bb58e5SYang Zhong } 1368be5c4787STony Nguyen r = memory_region_dispatch_read(mr, mr_offset, &val, op, iotlbentry->attrs); 136904e3aabdSPeter Maydell if (r != MEMTX_OK) { 13702d54f194SPeter Maydell hwaddr physaddr = mr_offset + 13712d54f194SPeter Maydell section->offset_within_address_space - 13722d54f194SPeter Maydell section->offset_within_region; 13732d54f194SPeter Maydell 1374be5c4787STony Nguyen cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), access_type, 137504e3aabdSPeter Maydell mmu_idx, iotlbentry->attrs, r, retaddr); 137604e3aabdSPeter Maydell } 1377d9bb58e5SYang Zhong if (locked) { 1378d9bb58e5SYang Zhong qemu_mutex_unlock_iothread(); 1379d9bb58e5SYang Zhong } 1380d9bb58e5SYang Zhong 1381d9bb58e5SYang Zhong return val; 1382d9bb58e5SYang Zhong } 1383d9bb58e5SYang Zhong 13842f3a57eeSAlex Bennée /* 13852f3a57eeSAlex Bennée * Save a potentially trashed IOTLB entry for later lookup by plugin. 1386570ef309SAlex Bennée * This is read by tlb_plugin_lookup if the iotlb entry doesn't match 1387570ef309SAlex Bennée * because of the side effect of io_writex changing memory layout. 13882f3a57eeSAlex Bennée */ 13892f3a57eeSAlex Bennée static void save_iotlb_data(CPUState *cs, hwaddr addr, 13902f3a57eeSAlex Bennée MemoryRegionSection *section, hwaddr mr_offset) 13912f3a57eeSAlex Bennée { 13922f3a57eeSAlex Bennée #ifdef CONFIG_PLUGIN 13932f3a57eeSAlex Bennée SavedIOTLB *saved = &cs->saved_iotlb; 13942f3a57eeSAlex Bennée saved->addr = addr; 13952f3a57eeSAlex Bennée saved->section = section; 13962f3a57eeSAlex Bennée saved->mr_offset = mr_offset; 13972f3a57eeSAlex Bennée #endif 13982f3a57eeSAlex Bennée } 13992f3a57eeSAlex Bennée 1400d9bb58e5SYang Zhong static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry, 1401f1be3696SRichard Henderson int mmu_idx, uint64_t val, target_ulong addr, 1402be5c4787STony Nguyen uintptr_t retaddr, MemOp op) 1403d9bb58e5SYang Zhong { 140429a0af61SRichard Henderson CPUState *cpu = env_cpu(env); 14052d54f194SPeter Maydell hwaddr mr_offset; 14062d54f194SPeter Maydell MemoryRegionSection *section; 14072d54f194SPeter Maydell MemoryRegion *mr; 1408d9bb58e5SYang Zhong bool locked = false; 140904e3aabdSPeter Maydell MemTxResult r; 1410d9bb58e5SYang Zhong 14112d54f194SPeter Maydell section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs); 14122d54f194SPeter Maydell mr = section->mr; 14132d54f194SPeter Maydell mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr; 141408565552SRichard Henderson if (!cpu->can_do_io) { 1415d9bb58e5SYang Zhong cpu_io_recompile(cpu, retaddr); 1416d9bb58e5SYang Zhong } 1417d9bb58e5SYang Zhong cpu->mem_io_pc = retaddr; 1418d9bb58e5SYang Zhong 14192f3a57eeSAlex Bennée /* 14202f3a57eeSAlex Bennée * The memory_region_dispatch may trigger a flush/resize 14212f3a57eeSAlex Bennée * so for plugins we save the iotlb_data just in case. 14222f3a57eeSAlex Bennée */ 14232f3a57eeSAlex Bennée save_iotlb_data(cpu, iotlbentry->addr, section, mr_offset); 14242f3a57eeSAlex Bennée 142541744954SPhilippe Mathieu-Daudé if (!qemu_mutex_iothread_locked()) { 1426d9bb58e5SYang Zhong qemu_mutex_lock_iothread(); 1427d9bb58e5SYang Zhong locked = true; 1428d9bb58e5SYang Zhong } 1429be5c4787STony Nguyen r = memory_region_dispatch_write(mr, mr_offset, val, op, iotlbentry->attrs); 143004e3aabdSPeter Maydell if (r != MEMTX_OK) { 14312d54f194SPeter Maydell hwaddr physaddr = mr_offset + 14322d54f194SPeter Maydell section->offset_within_address_space - 14332d54f194SPeter Maydell section->offset_within_region; 14342d54f194SPeter Maydell 1435be5c4787STony Nguyen cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), 1436be5c4787STony Nguyen MMU_DATA_STORE, mmu_idx, iotlbentry->attrs, r, 1437be5c4787STony Nguyen retaddr); 143804e3aabdSPeter Maydell } 1439d9bb58e5SYang Zhong if (locked) { 1440d9bb58e5SYang Zhong qemu_mutex_unlock_iothread(); 1441d9bb58e5SYang Zhong } 1442d9bb58e5SYang Zhong } 1443d9bb58e5SYang Zhong 14444811e909SRichard Henderson static inline target_ulong tlb_read_ofs(CPUTLBEntry *entry, size_t ofs) 14454811e909SRichard Henderson { 14464811e909SRichard Henderson #if TCG_OVERSIZED_GUEST 14474811e909SRichard Henderson return *(target_ulong *)((uintptr_t)entry + ofs); 14484811e909SRichard Henderson #else 1449d73415a3SStefan Hajnoczi /* ofs might correspond to .addr_write, so use qatomic_read */ 1450d73415a3SStefan Hajnoczi return qatomic_read((target_ulong *)((uintptr_t)entry + ofs)); 14514811e909SRichard Henderson #endif 14524811e909SRichard Henderson } 14534811e909SRichard Henderson 1454d9bb58e5SYang Zhong /* Return true if ADDR is present in the victim tlb, and has been copied 1455d9bb58e5SYang Zhong back to the main tlb. */ 1456d9bb58e5SYang Zhong static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index, 1457d9bb58e5SYang Zhong size_t elt_ofs, target_ulong page) 1458d9bb58e5SYang Zhong { 1459d9bb58e5SYang Zhong size_t vidx; 146071aec354SEmilio G. Cota 146129a0af61SRichard Henderson assert_cpu_is_self(env_cpu(env)); 1462d9bb58e5SYang Zhong for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) { 1463a40ec84eSRichard Henderson CPUTLBEntry *vtlb = &env_tlb(env)->d[mmu_idx].vtable[vidx]; 1464a40ec84eSRichard Henderson target_ulong cmp; 1465a40ec84eSRichard Henderson 1466d73415a3SStefan Hajnoczi /* elt_ofs might correspond to .addr_write, so use qatomic_read */ 1467a40ec84eSRichard Henderson #if TCG_OVERSIZED_GUEST 1468a40ec84eSRichard Henderson cmp = *(target_ulong *)((uintptr_t)vtlb + elt_ofs); 1469a40ec84eSRichard Henderson #else 1470d73415a3SStefan Hajnoczi cmp = qatomic_read((target_ulong *)((uintptr_t)vtlb + elt_ofs)); 1471a40ec84eSRichard Henderson #endif 1472d9bb58e5SYang Zhong 1473d9bb58e5SYang Zhong if (cmp == page) { 1474d9bb58e5SYang Zhong /* Found entry in victim tlb, swap tlb and iotlb. */ 1475a40ec84eSRichard Henderson CPUTLBEntry tmptlb, *tlb = &env_tlb(env)->f[mmu_idx].table[index]; 1476d9bb58e5SYang Zhong 1477a40ec84eSRichard Henderson qemu_spin_lock(&env_tlb(env)->c.lock); 147871aec354SEmilio G. Cota copy_tlb_helper_locked(&tmptlb, tlb); 147971aec354SEmilio G. Cota copy_tlb_helper_locked(tlb, vtlb); 148071aec354SEmilio G. Cota copy_tlb_helper_locked(vtlb, &tmptlb); 1481a40ec84eSRichard Henderson qemu_spin_unlock(&env_tlb(env)->c.lock); 1482d9bb58e5SYang Zhong 1483a40ec84eSRichard Henderson CPUIOTLBEntry tmpio, *io = &env_tlb(env)->d[mmu_idx].iotlb[index]; 1484a40ec84eSRichard Henderson CPUIOTLBEntry *vio = &env_tlb(env)->d[mmu_idx].viotlb[vidx]; 1485d9bb58e5SYang Zhong tmpio = *io; *io = *vio; *vio = tmpio; 1486d9bb58e5SYang Zhong return true; 1487d9bb58e5SYang Zhong } 1488d9bb58e5SYang Zhong } 1489d9bb58e5SYang Zhong return false; 1490d9bb58e5SYang Zhong } 1491d9bb58e5SYang Zhong 1492d9bb58e5SYang Zhong /* Macro to call the above, with local variables from the use context. */ 1493d9bb58e5SYang Zhong #define VICTIM_TLB_HIT(TY, ADDR) \ 1494d9bb58e5SYang Zhong victim_tlb_hit(env, mmu_idx, index, offsetof(CPUTLBEntry, TY), \ 1495d9bb58e5SYang Zhong (ADDR) & TARGET_PAGE_MASK) 1496d9bb58e5SYang Zhong 149730d7e098SRichard Henderson /* 149830d7e098SRichard Henderson * Return a ram_addr_t for the virtual address for execution. 149930d7e098SRichard Henderson * 150030d7e098SRichard Henderson * Return -1 if we can't translate and execute from an entire page 150130d7e098SRichard Henderson * of RAM. This will force us to execute by loading and translating 150230d7e098SRichard Henderson * one insn at a time, without caching. 150330d7e098SRichard Henderson * 150430d7e098SRichard Henderson * NOTE: This function will trigger an exception if the page is 150530d7e098SRichard Henderson * not executable. 1506f2553f04SKONRAD Frederic */ 15074b2190daSEmilio G. Cota tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr, 15084b2190daSEmilio G. Cota void **hostp) 1509f2553f04SKONRAD Frederic { 1510383beda9SRichard Henderson uintptr_t mmu_idx = cpu_mmu_index(env, true); 1511383beda9SRichard Henderson uintptr_t index = tlb_index(env, mmu_idx, addr); 1512383beda9SRichard Henderson CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr); 1513f2553f04SKONRAD Frederic void *p; 1514f2553f04SKONRAD Frederic 1515383beda9SRichard Henderson if (unlikely(!tlb_hit(entry->addr_code, addr))) { 1516b493ccf1SPeter Maydell if (!VICTIM_TLB_HIT(addr_code, addr)) { 151729a0af61SRichard Henderson tlb_fill(env_cpu(env), addr, 0, MMU_INST_FETCH, mmu_idx, 0); 15186d967cb8SEmilio G. Cota index = tlb_index(env, mmu_idx, addr); 15196d967cb8SEmilio G. Cota entry = tlb_entry(env, mmu_idx, addr); 152030d7e098SRichard Henderson 152130d7e098SRichard Henderson if (unlikely(entry->addr_code & TLB_INVALID_MASK)) { 152230d7e098SRichard Henderson /* 152330d7e098SRichard Henderson * The MMU protection covers a smaller range than a target 152430d7e098SRichard Henderson * page, so we must redo the MMU check for every insn. 152530d7e098SRichard Henderson */ 152630d7e098SRichard Henderson return -1; 152730d7e098SRichard Henderson } 152871b9a453SKONRAD Frederic } 1529383beda9SRichard Henderson assert(tlb_hit(entry->addr_code, addr)); 1530f2553f04SKONRAD Frederic } 153155df6fcfSPeter Maydell 153230d7e098SRichard Henderson if (unlikely(entry->addr_code & TLB_MMIO)) { 153330d7e098SRichard Henderson /* The region is not backed by RAM. */ 15344b2190daSEmilio G. Cota if (hostp) { 15354b2190daSEmilio G. Cota *hostp = NULL; 15364b2190daSEmilio G. Cota } 153720cb6ae4SPeter Maydell return -1; 153855df6fcfSPeter Maydell } 153955df6fcfSPeter Maydell 1540383beda9SRichard Henderson p = (void *)((uintptr_t)addr + entry->addend); 15414b2190daSEmilio G. Cota if (hostp) { 15424b2190daSEmilio G. Cota *hostp = p; 15434b2190daSEmilio G. Cota } 1544f2553f04SKONRAD Frederic return qemu_ram_addr_from_host_nofail(p); 1545f2553f04SKONRAD Frederic } 1546f2553f04SKONRAD Frederic 15474b2190daSEmilio G. Cota tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr) 15484b2190daSEmilio G. Cota { 15494b2190daSEmilio G. Cota return get_page_addr_code_hostp(env, addr, NULL); 15504b2190daSEmilio G. Cota } 15514b2190daSEmilio G. Cota 1552707526adSRichard Henderson static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, 1553707526adSRichard Henderson CPUIOTLBEntry *iotlbentry, uintptr_t retaddr) 1554707526adSRichard Henderson { 1555707526adSRichard Henderson ram_addr_t ram_addr = mem_vaddr + iotlbentry->addr; 1556707526adSRichard Henderson 1557707526adSRichard Henderson trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size); 1558707526adSRichard Henderson 1559707526adSRichard Henderson if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) { 1560707526adSRichard Henderson struct page_collection *pages 1561707526adSRichard Henderson = page_collection_lock(ram_addr, ram_addr + size); 15625a7c27bbSRichard Henderson tb_invalidate_phys_page_fast(pages, ram_addr, size, retaddr); 1563707526adSRichard Henderson page_collection_unlock(pages); 1564707526adSRichard Henderson } 1565707526adSRichard Henderson 1566707526adSRichard Henderson /* 1567707526adSRichard Henderson * Set both VGA and migration bits for simplicity and to remove 1568707526adSRichard Henderson * the notdirty callback faster. 1569707526adSRichard Henderson */ 1570707526adSRichard Henderson cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE); 1571707526adSRichard Henderson 1572707526adSRichard Henderson /* We remove the notdirty callback only if the code has been flushed. */ 1573707526adSRichard Henderson if (!cpu_physical_memory_is_clean(ram_addr)) { 1574707526adSRichard Henderson trace_memory_notdirty_set_dirty(mem_vaddr); 1575707526adSRichard Henderson tlb_set_dirty(cpu, mem_vaddr); 1576707526adSRichard Henderson } 1577707526adSRichard Henderson } 1578707526adSRichard Henderson 1579069cfe77SRichard Henderson static int probe_access_internal(CPUArchState *env, target_ulong addr, 1580069cfe77SRichard Henderson int fault_size, MMUAccessType access_type, 1581069cfe77SRichard Henderson int mmu_idx, bool nonfault, 1582069cfe77SRichard Henderson void **phost, uintptr_t retaddr) 1583d9bb58e5SYang Zhong { 1584383beda9SRichard Henderson uintptr_t index = tlb_index(env, mmu_idx, addr); 1585383beda9SRichard Henderson CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr); 1586069cfe77SRichard Henderson target_ulong tlb_addr, page_addr; 1587c25c283dSDavid Hildenbrand size_t elt_ofs; 1588069cfe77SRichard Henderson int flags; 1589ca86cf32SDavid Hildenbrand 1590c25c283dSDavid Hildenbrand switch (access_type) { 1591c25c283dSDavid Hildenbrand case MMU_DATA_LOAD: 1592c25c283dSDavid Hildenbrand elt_ofs = offsetof(CPUTLBEntry, addr_read); 1593c25c283dSDavid Hildenbrand break; 1594c25c283dSDavid Hildenbrand case MMU_DATA_STORE: 1595c25c283dSDavid Hildenbrand elt_ofs = offsetof(CPUTLBEntry, addr_write); 1596c25c283dSDavid Hildenbrand break; 1597c25c283dSDavid Hildenbrand case MMU_INST_FETCH: 1598c25c283dSDavid Hildenbrand elt_ofs = offsetof(CPUTLBEntry, addr_code); 1599c25c283dSDavid Hildenbrand break; 1600c25c283dSDavid Hildenbrand default: 1601c25c283dSDavid Hildenbrand g_assert_not_reached(); 1602c25c283dSDavid Hildenbrand } 1603c25c283dSDavid Hildenbrand tlb_addr = tlb_read_ofs(entry, elt_ofs); 1604c25c283dSDavid Hildenbrand 1605069cfe77SRichard Henderson page_addr = addr & TARGET_PAGE_MASK; 1606069cfe77SRichard Henderson if (!tlb_hit_page(tlb_addr, page_addr)) { 1607069cfe77SRichard Henderson if (!victim_tlb_hit(env, mmu_idx, index, elt_ofs, page_addr)) { 1608069cfe77SRichard Henderson CPUState *cs = env_cpu(env); 1609069cfe77SRichard Henderson CPUClass *cc = CPU_GET_CLASS(cs); 1610069cfe77SRichard Henderson 161178271684SClaudio Fontana if (!cc->tcg_ops->tlb_fill(cs, addr, fault_size, access_type, 1612069cfe77SRichard Henderson mmu_idx, nonfault, retaddr)) { 1613069cfe77SRichard Henderson /* Non-faulting page table read failed. */ 1614069cfe77SRichard Henderson *phost = NULL; 1615069cfe77SRichard Henderson return TLB_INVALID_MASK; 1616069cfe77SRichard Henderson } 1617069cfe77SRichard Henderson 161803a98189SDavid Hildenbrand /* TLB resize via tlb_fill may have moved the entry. */ 161903a98189SDavid Hildenbrand entry = tlb_entry(env, mmu_idx, addr); 1620d9bb58e5SYang Zhong } 1621c25c283dSDavid Hildenbrand tlb_addr = tlb_read_ofs(entry, elt_ofs); 162203a98189SDavid Hildenbrand } 1623069cfe77SRichard Henderson flags = tlb_addr & TLB_FLAGS_MASK; 162403a98189SDavid Hildenbrand 1625069cfe77SRichard Henderson /* Fold all "mmio-like" bits into TLB_MMIO. This is not RAM. */ 1626069cfe77SRichard Henderson if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY))) { 1627069cfe77SRichard Henderson *phost = NULL; 1628069cfe77SRichard Henderson return TLB_MMIO; 1629fef39ccdSDavid Hildenbrand } 1630fef39ccdSDavid Hildenbrand 1631069cfe77SRichard Henderson /* Everything else is RAM. */ 1632069cfe77SRichard Henderson *phost = (void *)((uintptr_t)addr + entry->addend); 1633069cfe77SRichard Henderson return flags; 1634069cfe77SRichard Henderson } 1635069cfe77SRichard Henderson 1636069cfe77SRichard Henderson int probe_access_flags(CPUArchState *env, target_ulong addr, 1637069cfe77SRichard Henderson MMUAccessType access_type, int mmu_idx, 1638069cfe77SRichard Henderson bool nonfault, void **phost, uintptr_t retaddr) 1639069cfe77SRichard Henderson { 1640069cfe77SRichard Henderson int flags; 1641069cfe77SRichard Henderson 1642069cfe77SRichard Henderson flags = probe_access_internal(env, addr, 0, access_type, mmu_idx, 1643069cfe77SRichard Henderson nonfault, phost, retaddr); 1644069cfe77SRichard Henderson 1645069cfe77SRichard Henderson /* Handle clean RAM pages. */ 1646069cfe77SRichard Henderson if (unlikely(flags & TLB_NOTDIRTY)) { 1647069cfe77SRichard Henderson uintptr_t index = tlb_index(env, mmu_idx, addr); 164873bc0bd4SRichard Henderson CPUIOTLBEntry *iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index]; 164973bc0bd4SRichard Henderson 1650069cfe77SRichard Henderson notdirty_write(env_cpu(env), addr, 1, iotlbentry, retaddr); 1651069cfe77SRichard Henderson flags &= ~TLB_NOTDIRTY; 1652069cfe77SRichard Henderson } 1653069cfe77SRichard Henderson 1654069cfe77SRichard Henderson return flags; 1655069cfe77SRichard Henderson } 1656069cfe77SRichard Henderson 1657069cfe77SRichard Henderson void *probe_access(CPUArchState *env, target_ulong addr, int size, 1658069cfe77SRichard Henderson MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 1659069cfe77SRichard Henderson { 1660069cfe77SRichard Henderson void *host; 1661069cfe77SRichard Henderson int flags; 1662069cfe77SRichard Henderson 1663069cfe77SRichard Henderson g_assert(-(addr | TARGET_PAGE_MASK) >= size); 1664069cfe77SRichard Henderson 1665069cfe77SRichard Henderson flags = probe_access_internal(env, addr, size, access_type, mmu_idx, 1666069cfe77SRichard Henderson false, &host, retaddr); 1667069cfe77SRichard Henderson 1668069cfe77SRichard Henderson /* Per the interface, size == 0 merely faults the access. */ 1669069cfe77SRichard Henderson if (size == 0) { 167073bc0bd4SRichard Henderson return NULL; 167173bc0bd4SRichard Henderson } 167273bc0bd4SRichard Henderson 1673069cfe77SRichard Henderson if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) { 1674069cfe77SRichard Henderson uintptr_t index = tlb_index(env, mmu_idx, addr); 1675069cfe77SRichard Henderson CPUIOTLBEntry *iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index]; 1676069cfe77SRichard Henderson 167703a98189SDavid Hildenbrand /* Handle watchpoints. */ 1678069cfe77SRichard Henderson if (flags & TLB_WATCHPOINT) { 1679069cfe77SRichard Henderson int wp_access = (access_type == MMU_DATA_STORE 1680069cfe77SRichard Henderson ? BP_MEM_WRITE : BP_MEM_READ); 168103a98189SDavid Hildenbrand cpu_check_watchpoint(env_cpu(env), addr, size, 168273bc0bd4SRichard Henderson iotlbentry->attrs, wp_access, retaddr); 1683d9bb58e5SYang Zhong } 1684fef39ccdSDavid Hildenbrand 168573bc0bd4SRichard Henderson /* Handle clean RAM pages. */ 1686069cfe77SRichard Henderson if (flags & TLB_NOTDIRTY) { 1687069cfe77SRichard Henderson notdirty_write(env_cpu(env), addr, 1, iotlbentry, retaddr); 168873bc0bd4SRichard Henderson } 1689fef39ccdSDavid Hildenbrand } 1690fef39ccdSDavid Hildenbrand 1691069cfe77SRichard Henderson return host; 1692d9bb58e5SYang Zhong } 1693d9bb58e5SYang Zhong 16944811e909SRichard Henderson void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr, 16954811e909SRichard Henderson MMUAccessType access_type, int mmu_idx) 16964811e909SRichard Henderson { 1697069cfe77SRichard Henderson void *host; 1698069cfe77SRichard Henderson int flags; 16994811e909SRichard Henderson 1700069cfe77SRichard Henderson flags = probe_access_internal(env, addr, 0, access_type, 1701069cfe77SRichard Henderson mmu_idx, true, &host, 0); 1702069cfe77SRichard Henderson 1703069cfe77SRichard Henderson /* No combination of flags are expected by the caller. */ 1704069cfe77SRichard Henderson return flags ? NULL : host; 17054811e909SRichard Henderson } 17064811e909SRichard Henderson 1707235537faSAlex Bennée #ifdef CONFIG_PLUGIN 1708235537faSAlex Bennée /* 1709235537faSAlex Bennée * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure. 1710235537faSAlex Bennée * This should be a hot path as we will have just looked this path up 1711235537faSAlex Bennée * in the softmmu lookup code (or helper). We don't handle re-fills or 1712235537faSAlex Bennée * checking the victim table. This is purely informational. 1713235537faSAlex Bennée * 17142f3a57eeSAlex Bennée * This almost never fails as the memory access being instrumented 17152f3a57eeSAlex Bennée * should have just filled the TLB. The one corner case is io_writex 17162f3a57eeSAlex Bennée * which can cause TLB flushes and potential resizing of the TLBs 1717570ef309SAlex Bennée * losing the information we need. In those cases we need to recover 1718570ef309SAlex Bennée * data from a copy of the iotlbentry. As long as this always occurs 1719570ef309SAlex Bennée * from the same thread (which a mem callback will be) this is safe. 1720235537faSAlex Bennée */ 1721235537faSAlex Bennée 1722235537faSAlex Bennée bool tlb_plugin_lookup(CPUState *cpu, target_ulong addr, int mmu_idx, 1723235537faSAlex Bennée bool is_store, struct qemu_plugin_hwaddr *data) 1724235537faSAlex Bennée { 1725235537faSAlex Bennée CPUArchState *env = cpu->env_ptr; 1726235537faSAlex Bennée CPUTLBEntry *tlbe = tlb_entry(env, mmu_idx, addr); 1727235537faSAlex Bennée uintptr_t index = tlb_index(env, mmu_idx, addr); 1728235537faSAlex Bennée target_ulong tlb_addr = is_store ? tlb_addr_write(tlbe) : tlbe->addr_read; 1729235537faSAlex Bennée 1730235537faSAlex Bennée if (likely(tlb_hit(tlb_addr, addr))) { 1731235537faSAlex Bennée /* We must have an iotlb entry for MMIO */ 1732235537faSAlex Bennée if (tlb_addr & TLB_MMIO) { 1733235537faSAlex Bennée CPUIOTLBEntry *iotlbentry; 1734235537faSAlex Bennée iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index]; 1735235537faSAlex Bennée data->is_io = true; 1736235537faSAlex Bennée data->v.io.section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs); 1737235537faSAlex Bennée data->v.io.offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr; 1738235537faSAlex Bennée } else { 1739235537faSAlex Bennée data->is_io = false; 17402d932039SAlex Bennée data->v.ram.hostaddr = (void *)((uintptr_t)addr + tlbe->addend); 1741235537faSAlex Bennée } 1742235537faSAlex Bennée return true; 17432f3a57eeSAlex Bennée } else { 17442f3a57eeSAlex Bennée SavedIOTLB *saved = &cpu->saved_iotlb; 17452f3a57eeSAlex Bennée data->is_io = true; 17462f3a57eeSAlex Bennée data->v.io.section = saved->section; 17472f3a57eeSAlex Bennée data->v.io.offset = saved->mr_offset; 17482f3a57eeSAlex Bennée return true; 1749235537faSAlex Bennée } 1750235537faSAlex Bennée } 1751235537faSAlex Bennée 1752235537faSAlex Bennée #endif 1753235537faSAlex Bennée 175408dff435SRichard Henderson /* 175508dff435SRichard Henderson * Probe for an atomic operation. Do not allow unaligned operations, 175608dff435SRichard Henderson * or io operations to proceed. Return the host address. 175708dff435SRichard Henderson * 175808dff435SRichard Henderson * @prot may be PAGE_READ, PAGE_WRITE, or PAGE_READ|PAGE_WRITE. 175908dff435SRichard Henderson */ 1760d9bb58e5SYang Zhong static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr, 17619002ffcbSRichard Henderson MemOpIdx oi, int size, int prot, 176208dff435SRichard Henderson uintptr_t retaddr) 1763d9bb58e5SYang Zhong { 1764*b826044fSRichard Henderson uintptr_t mmu_idx = get_mmuidx(oi); 176514776ab5STony Nguyen MemOp mop = get_memop(oi); 1766d9bb58e5SYang Zhong int a_bits = get_alignment_bits(mop); 176708dff435SRichard Henderson uintptr_t index; 176808dff435SRichard Henderson CPUTLBEntry *tlbe; 176908dff435SRichard Henderson target_ulong tlb_addr; 177034d49937SPeter Maydell void *hostaddr; 1771d9bb58e5SYang Zhong 1772*b826044fSRichard Henderson tcg_debug_assert(mmu_idx < NB_MMU_MODES); 1773*b826044fSRichard Henderson 1774d9bb58e5SYang Zhong /* Adjust the given return address. */ 1775d9bb58e5SYang Zhong retaddr -= GETPC_ADJ; 1776d9bb58e5SYang Zhong 1777d9bb58e5SYang Zhong /* Enforce guest required alignment. */ 1778d9bb58e5SYang Zhong if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) { 1779d9bb58e5SYang Zhong /* ??? Maybe indicate atomic op to cpu_unaligned_access */ 178029a0af61SRichard Henderson cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE, 1781d9bb58e5SYang Zhong mmu_idx, retaddr); 1782d9bb58e5SYang Zhong } 1783d9bb58e5SYang Zhong 1784d9bb58e5SYang Zhong /* Enforce qemu required alignment. */ 178508dff435SRichard Henderson if (unlikely(addr & (size - 1))) { 1786d9bb58e5SYang Zhong /* We get here if guest alignment was not requested, 1787d9bb58e5SYang Zhong or was not enforced by cpu_unaligned_access above. 1788d9bb58e5SYang Zhong We might widen the access and emulate, but for now 1789d9bb58e5SYang Zhong mark an exception and exit the cpu loop. */ 1790d9bb58e5SYang Zhong goto stop_the_world; 1791d9bb58e5SYang Zhong } 1792d9bb58e5SYang Zhong 179308dff435SRichard Henderson index = tlb_index(env, mmu_idx, addr); 179408dff435SRichard Henderson tlbe = tlb_entry(env, mmu_idx, addr); 179508dff435SRichard Henderson 1796d9bb58e5SYang Zhong /* Check TLB entry and enforce page permissions. */ 179708dff435SRichard Henderson if (prot & PAGE_WRITE) { 179808dff435SRichard Henderson tlb_addr = tlb_addr_write(tlbe); 1799334692bcSPeter Maydell if (!tlb_hit(tlb_addr, addr)) { 1800d9bb58e5SYang Zhong if (!VICTIM_TLB_HIT(addr_write, addr)) { 180108dff435SRichard Henderson tlb_fill(env_cpu(env), addr, size, 180208dff435SRichard Henderson MMU_DATA_STORE, mmu_idx, retaddr); 18036d967cb8SEmilio G. Cota index = tlb_index(env, mmu_idx, addr); 18046d967cb8SEmilio G. Cota tlbe = tlb_entry(env, mmu_idx, addr); 1805d9bb58e5SYang Zhong } 1806403f290cSEmilio G. Cota tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK; 1807d9bb58e5SYang Zhong } 1808d9bb58e5SYang Zhong 180908dff435SRichard Henderson /* Let the guest notice RMW on a write-only page. */ 181008dff435SRichard Henderson if ((prot & PAGE_READ) && 181108dff435SRichard Henderson unlikely(tlbe->addr_read != (tlb_addr & ~TLB_NOTDIRTY))) { 181208dff435SRichard Henderson tlb_fill(env_cpu(env), addr, size, 181308dff435SRichard Henderson MMU_DATA_LOAD, mmu_idx, retaddr); 181408dff435SRichard Henderson /* 181508dff435SRichard Henderson * Since we don't support reads and writes to different addresses, 181608dff435SRichard Henderson * and we do have the proper page loaded for write, this shouldn't 181708dff435SRichard Henderson * ever return. But just in case, handle via stop-the-world. 181808dff435SRichard Henderson */ 181908dff435SRichard Henderson goto stop_the_world; 182008dff435SRichard Henderson } 182108dff435SRichard Henderson } else /* if (prot & PAGE_READ) */ { 182208dff435SRichard Henderson tlb_addr = tlbe->addr_read; 182308dff435SRichard Henderson if (!tlb_hit(tlb_addr, addr)) { 182408dff435SRichard Henderson if (!VICTIM_TLB_HIT(addr_write, addr)) { 182508dff435SRichard Henderson tlb_fill(env_cpu(env), addr, size, 182608dff435SRichard Henderson MMU_DATA_LOAD, mmu_idx, retaddr); 182708dff435SRichard Henderson index = tlb_index(env, mmu_idx, addr); 182808dff435SRichard Henderson tlbe = tlb_entry(env, mmu_idx, addr); 182908dff435SRichard Henderson } 183008dff435SRichard Henderson tlb_addr = tlbe->addr_read & ~TLB_INVALID_MASK; 183108dff435SRichard Henderson } 183208dff435SRichard Henderson } 183308dff435SRichard Henderson 183455df6fcfSPeter Maydell /* Notice an IO access or a needs-MMU-lookup access */ 183530d7e098SRichard Henderson if (unlikely(tlb_addr & TLB_MMIO)) { 1836d9bb58e5SYang Zhong /* There's really nothing that can be done to 1837d9bb58e5SYang Zhong support this apart from stop-the-world. */ 1838d9bb58e5SYang Zhong goto stop_the_world; 1839d9bb58e5SYang Zhong } 1840d9bb58e5SYang Zhong 184134d49937SPeter Maydell hostaddr = (void *)((uintptr_t)addr + tlbe->addend); 184234d49937SPeter Maydell 184334d49937SPeter Maydell if (unlikely(tlb_addr & TLB_NOTDIRTY)) { 184408dff435SRichard Henderson notdirty_write(env_cpu(env), addr, size, 1845707526adSRichard Henderson &env_tlb(env)->d[mmu_idx].iotlb[index], retaddr); 184634d49937SPeter Maydell } 184734d49937SPeter Maydell 184834d49937SPeter Maydell return hostaddr; 1849d9bb58e5SYang Zhong 1850d9bb58e5SYang Zhong stop_the_world: 185129a0af61SRichard Henderson cpu_loop_exit_atomic(env_cpu(env), retaddr); 1852d9bb58e5SYang Zhong } 1853d9bb58e5SYang Zhong 1854eed56642SAlex Bennée /* 1855f83bcecbSRichard Henderson * Verify that we have passed the correct MemOp to the correct function. 1856f83bcecbSRichard Henderson * 1857f83bcecbSRichard Henderson * In the case of the helper_*_mmu functions, we will have done this by 1858f83bcecbSRichard Henderson * using the MemOp to look up the helper during code generation. 1859f83bcecbSRichard Henderson * 1860f83bcecbSRichard Henderson * In the case of the cpu_*_mmu functions, this is up to the caller. 1861f83bcecbSRichard Henderson * We could present one function to target code, and dispatch based on 1862f83bcecbSRichard Henderson * the MemOp, but so far we have worked hard to avoid an indirect function 1863f83bcecbSRichard Henderson * call along the memory path. 1864f83bcecbSRichard Henderson */ 1865f83bcecbSRichard Henderson static void validate_memop(MemOpIdx oi, MemOp expected) 1866f83bcecbSRichard Henderson { 1867f83bcecbSRichard Henderson #ifdef CONFIG_DEBUG_TCG 1868f83bcecbSRichard Henderson MemOp have = get_memop(oi) & (MO_SIZE | MO_BSWAP); 1869f83bcecbSRichard Henderson assert(have == expected); 1870f83bcecbSRichard Henderson #endif 1871f83bcecbSRichard Henderson } 1872f83bcecbSRichard Henderson 1873f83bcecbSRichard Henderson /* 1874eed56642SAlex Bennée * Load Helpers 1875eed56642SAlex Bennée * 1876eed56642SAlex Bennée * We support two different access types. SOFTMMU_CODE_ACCESS is 1877eed56642SAlex Bennée * specifically for reading instructions from system memory. It is 1878eed56642SAlex Bennée * called by the translation loop and in some helpers where the code 1879eed56642SAlex Bennée * is disassembled. It shouldn't be called directly by guest code. 1880eed56642SAlex Bennée */ 1881d9bb58e5SYang Zhong 18822dd92606SRichard Henderson typedef uint64_t FullLoadHelper(CPUArchState *env, target_ulong addr, 18839002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr); 18842dd92606SRichard Henderson 1885c6b716cdSRichard Henderson static inline uint64_t QEMU_ALWAYS_INLINE 188680d9d1c6SRichard Henderson load_memop(const void *haddr, MemOp op) 188780d9d1c6SRichard Henderson { 188880d9d1c6SRichard Henderson switch (op) { 188980d9d1c6SRichard Henderson case MO_UB: 189080d9d1c6SRichard Henderson return ldub_p(haddr); 189180d9d1c6SRichard Henderson case MO_BEUW: 189280d9d1c6SRichard Henderson return lduw_be_p(haddr); 189380d9d1c6SRichard Henderson case MO_LEUW: 189480d9d1c6SRichard Henderson return lduw_le_p(haddr); 189580d9d1c6SRichard Henderson case MO_BEUL: 189680d9d1c6SRichard Henderson return (uint32_t)ldl_be_p(haddr); 189780d9d1c6SRichard Henderson case MO_LEUL: 189880d9d1c6SRichard Henderson return (uint32_t)ldl_le_p(haddr); 1899fc313c64SFrédéric Pétrot case MO_BEUQ: 190080d9d1c6SRichard Henderson return ldq_be_p(haddr); 1901fc313c64SFrédéric Pétrot case MO_LEUQ: 190280d9d1c6SRichard Henderson return ldq_le_p(haddr); 190380d9d1c6SRichard Henderson default: 190480d9d1c6SRichard Henderson qemu_build_not_reached(); 190580d9d1c6SRichard Henderson } 190680d9d1c6SRichard Henderson } 190780d9d1c6SRichard Henderson 190880d9d1c6SRichard Henderson static inline uint64_t QEMU_ALWAYS_INLINE 19099002ffcbSRichard Henderson load_helper(CPUArchState *env, target_ulong addr, MemOpIdx oi, 1910be5c4787STony Nguyen uintptr_t retaddr, MemOp op, bool code_read, 19112dd92606SRichard Henderson FullLoadHelper *full_load) 1912eed56642SAlex Bennée { 1913eed56642SAlex Bennée const size_t tlb_off = code_read ? 1914eed56642SAlex Bennée offsetof(CPUTLBEntry, addr_code) : offsetof(CPUTLBEntry, addr_read); 1915f1be3696SRichard Henderson const MMUAccessType access_type = 1916f1be3696SRichard Henderson code_read ? MMU_INST_FETCH : MMU_DATA_LOAD; 1917*b826044fSRichard Henderson const unsigned a_bits = get_alignment_bits(get_memop(oi)); 1918*b826044fSRichard Henderson const size_t size = memop_size(op); 1919*b826044fSRichard Henderson uintptr_t mmu_idx = get_mmuidx(oi); 1920*b826044fSRichard Henderson uintptr_t index; 1921*b826044fSRichard Henderson CPUTLBEntry *entry; 1922*b826044fSRichard Henderson target_ulong tlb_addr; 1923eed56642SAlex Bennée void *haddr; 1924eed56642SAlex Bennée uint64_t res; 1925*b826044fSRichard Henderson 1926*b826044fSRichard Henderson tcg_debug_assert(mmu_idx < NB_MMU_MODES); 1927d9bb58e5SYang Zhong 1928eed56642SAlex Bennée /* Handle CPU specific unaligned behaviour */ 1929eed56642SAlex Bennée if (addr & ((1 << a_bits) - 1)) { 193029a0af61SRichard Henderson cpu_unaligned_access(env_cpu(env), addr, access_type, 1931eed56642SAlex Bennée mmu_idx, retaddr); 1932eed56642SAlex Bennée } 1933eed56642SAlex Bennée 1934*b826044fSRichard Henderson index = tlb_index(env, mmu_idx, addr); 1935*b826044fSRichard Henderson entry = tlb_entry(env, mmu_idx, addr); 1936*b826044fSRichard Henderson tlb_addr = code_read ? entry->addr_code : entry->addr_read; 1937*b826044fSRichard Henderson 1938eed56642SAlex Bennée /* If the TLB entry is for a different page, reload and try again. */ 1939eed56642SAlex Bennée if (!tlb_hit(tlb_addr, addr)) { 1940eed56642SAlex Bennée if (!victim_tlb_hit(env, mmu_idx, index, tlb_off, 1941eed56642SAlex Bennée addr & TARGET_PAGE_MASK)) { 194229a0af61SRichard Henderson tlb_fill(env_cpu(env), addr, size, 1943f1be3696SRichard Henderson access_type, mmu_idx, retaddr); 1944eed56642SAlex Bennée index = tlb_index(env, mmu_idx, addr); 1945eed56642SAlex Bennée entry = tlb_entry(env, mmu_idx, addr); 1946eed56642SAlex Bennée } 1947eed56642SAlex Bennée tlb_addr = code_read ? entry->addr_code : entry->addr_read; 194830d7e098SRichard Henderson tlb_addr &= ~TLB_INVALID_MASK; 1949eed56642SAlex Bennée } 1950eed56642SAlex Bennée 195150b107c5SRichard Henderson /* Handle anything that isn't just a straight memory access. */ 1952eed56642SAlex Bennée if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) { 195350b107c5SRichard Henderson CPUIOTLBEntry *iotlbentry; 19545b87b3e6SRichard Henderson bool need_swap; 195550b107c5SRichard Henderson 195650b107c5SRichard Henderson /* For anything that is unaligned, recurse through full_load. */ 1957eed56642SAlex Bennée if ((addr & (size - 1)) != 0) { 1958eed56642SAlex Bennée goto do_unaligned_access; 1959eed56642SAlex Bennée } 196050b107c5SRichard Henderson 196150b107c5SRichard Henderson iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index]; 196250b107c5SRichard Henderson 196350b107c5SRichard Henderson /* Handle watchpoints. */ 196450b107c5SRichard Henderson if (unlikely(tlb_addr & TLB_WATCHPOINT)) { 196550b107c5SRichard Henderson /* On watchpoint hit, this will longjmp out. */ 196650b107c5SRichard Henderson cpu_check_watchpoint(env_cpu(env), addr, size, 196750b107c5SRichard Henderson iotlbentry->attrs, BP_MEM_READ, retaddr); 19685b87b3e6SRichard Henderson } 196950b107c5SRichard Henderson 19705b87b3e6SRichard Henderson need_swap = size > 1 && (tlb_addr & TLB_BSWAP); 197150b107c5SRichard Henderson 197250b107c5SRichard Henderson /* Handle I/O access. */ 19735b87b3e6SRichard Henderson if (likely(tlb_addr & TLB_MMIO)) { 19745b87b3e6SRichard Henderson return io_readx(env, iotlbentry, mmu_idx, addr, retaddr, 19755b87b3e6SRichard Henderson access_type, op ^ (need_swap * MO_BSWAP)); 19765b87b3e6SRichard Henderson } 19775b87b3e6SRichard Henderson 19785b87b3e6SRichard Henderson haddr = (void *)((uintptr_t)addr + entry->addend); 19795b87b3e6SRichard Henderson 19805b87b3e6SRichard Henderson /* 19815b87b3e6SRichard Henderson * Keep these two load_memop separate to ensure that the compiler 19825b87b3e6SRichard Henderson * is able to fold the entire function to a single instruction. 19835b87b3e6SRichard Henderson * There is a build-time assert inside to remind you of this. ;-) 19845b87b3e6SRichard Henderson */ 19855b87b3e6SRichard Henderson if (unlikely(need_swap)) { 19865b87b3e6SRichard Henderson return load_memop(haddr, op ^ MO_BSWAP); 19875b87b3e6SRichard Henderson } 19885b87b3e6SRichard Henderson return load_memop(haddr, op); 1989eed56642SAlex Bennée } 1990eed56642SAlex Bennée 1991eed56642SAlex Bennée /* Handle slow unaligned access (it spans two pages or IO). */ 1992eed56642SAlex Bennée if (size > 1 1993eed56642SAlex Bennée && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1 1994eed56642SAlex Bennée >= TARGET_PAGE_SIZE)) { 1995eed56642SAlex Bennée target_ulong addr1, addr2; 19968c79b288SAlex Bennée uint64_t r1, r2; 1997eed56642SAlex Bennée unsigned shift; 1998eed56642SAlex Bennée do_unaligned_access: 1999ab7a2009SAlex Bennée addr1 = addr & ~((target_ulong)size - 1); 2000eed56642SAlex Bennée addr2 = addr1 + size; 20012dd92606SRichard Henderson r1 = full_load(env, addr1, oi, retaddr); 20022dd92606SRichard Henderson r2 = full_load(env, addr2, oi, retaddr); 2003eed56642SAlex Bennée shift = (addr & (size - 1)) * 8; 2004eed56642SAlex Bennée 2005be5c4787STony Nguyen if (memop_big_endian(op)) { 2006eed56642SAlex Bennée /* Big-endian combine. */ 2007eed56642SAlex Bennée res = (r1 << shift) | (r2 >> ((size * 8) - shift)); 2008eed56642SAlex Bennée } else { 2009eed56642SAlex Bennée /* Little-endian combine. */ 2010eed56642SAlex Bennée res = (r1 >> shift) | (r2 << ((size * 8) - shift)); 2011eed56642SAlex Bennée } 2012eed56642SAlex Bennée return res & MAKE_64BIT_MASK(0, size * 8); 2013eed56642SAlex Bennée } 2014eed56642SAlex Bennée 2015eed56642SAlex Bennée haddr = (void *)((uintptr_t)addr + entry->addend); 201680d9d1c6SRichard Henderson return load_memop(haddr, op); 2017eed56642SAlex Bennée } 2018eed56642SAlex Bennée 2019eed56642SAlex Bennée /* 2020eed56642SAlex Bennée * For the benefit of TCG generated code, we want to avoid the 2021eed56642SAlex Bennée * complication of ABI-specific return type promotion and always 2022eed56642SAlex Bennée * return a value extended to the register size of the host. This is 2023eed56642SAlex Bennée * tcg_target_long, except in the case of a 32-bit host and 64-bit 2024eed56642SAlex Bennée * data, and for that we always have uint64_t. 2025eed56642SAlex Bennée * 2026eed56642SAlex Bennée * We don't bother with this widened value for SOFTMMU_CODE_ACCESS. 2027eed56642SAlex Bennée */ 2028eed56642SAlex Bennée 20292dd92606SRichard Henderson static uint64_t full_ldub_mmu(CPUArchState *env, target_ulong addr, 20309002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 20312dd92606SRichard Henderson { 2032f83bcecbSRichard Henderson validate_memop(oi, MO_UB); 2033be5c4787STony Nguyen return load_helper(env, addr, oi, retaddr, MO_UB, false, full_ldub_mmu); 20342dd92606SRichard Henderson } 20352dd92606SRichard Henderson 2036fc1bc777SRichard Henderson tcg_target_ulong helper_ret_ldub_mmu(CPUArchState *env, target_ulong addr, 20379002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2038eed56642SAlex Bennée { 20392dd92606SRichard Henderson return full_ldub_mmu(env, addr, oi, retaddr); 20402dd92606SRichard Henderson } 20412dd92606SRichard Henderson 20422dd92606SRichard Henderson static uint64_t full_le_lduw_mmu(CPUArchState *env, target_ulong addr, 20439002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 20442dd92606SRichard Henderson { 2045f83bcecbSRichard Henderson validate_memop(oi, MO_LEUW); 2046be5c4787STony Nguyen return load_helper(env, addr, oi, retaddr, MO_LEUW, false, 20472dd92606SRichard Henderson full_le_lduw_mmu); 2048eed56642SAlex Bennée } 2049eed56642SAlex Bennée 2050fc1bc777SRichard Henderson tcg_target_ulong helper_le_lduw_mmu(CPUArchState *env, target_ulong addr, 20519002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2052eed56642SAlex Bennée { 20532dd92606SRichard Henderson return full_le_lduw_mmu(env, addr, oi, retaddr); 20542dd92606SRichard Henderson } 20552dd92606SRichard Henderson 20562dd92606SRichard Henderson static uint64_t full_be_lduw_mmu(CPUArchState *env, target_ulong addr, 20579002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 20582dd92606SRichard Henderson { 2059f83bcecbSRichard Henderson validate_memop(oi, MO_BEUW); 2060be5c4787STony Nguyen return load_helper(env, addr, oi, retaddr, MO_BEUW, false, 20612dd92606SRichard Henderson full_be_lduw_mmu); 2062eed56642SAlex Bennée } 2063eed56642SAlex Bennée 2064fc1bc777SRichard Henderson tcg_target_ulong helper_be_lduw_mmu(CPUArchState *env, target_ulong addr, 20659002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2066eed56642SAlex Bennée { 20672dd92606SRichard Henderson return full_be_lduw_mmu(env, addr, oi, retaddr); 20682dd92606SRichard Henderson } 20692dd92606SRichard Henderson 20702dd92606SRichard Henderson static uint64_t full_le_ldul_mmu(CPUArchState *env, target_ulong addr, 20719002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 20722dd92606SRichard Henderson { 2073f83bcecbSRichard Henderson validate_memop(oi, MO_LEUL); 2074be5c4787STony Nguyen return load_helper(env, addr, oi, retaddr, MO_LEUL, false, 20752dd92606SRichard Henderson full_le_ldul_mmu); 2076eed56642SAlex Bennée } 2077eed56642SAlex Bennée 2078fc1bc777SRichard Henderson tcg_target_ulong helper_le_ldul_mmu(CPUArchState *env, target_ulong addr, 20799002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2080eed56642SAlex Bennée { 20812dd92606SRichard Henderson return full_le_ldul_mmu(env, addr, oi, retaddr); 20822dd92606SRichard Henderson } 20832dd92606SRichard Henderson 20842dd92606SRichard Henderson static uint64_t full_be_ldul_mmu(CPUArchState *env, target_ulong addr, 20859002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 20862dd92606SRichard Henderson { 2087f83bcecbSRichard Henderson validate_memop(oi, MO_BEUL); 2088be5c4787STony Nguyen return load_helper(env, addr, oi, retaddr, MO_BEUL, false, 20892dd92606SRichard Henderson full_be_ldul_mmu); 2090eed56642SAlex Bennée } 2091eed56642SAlex Bennée 2092fc1bc777SRichard Henderson tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr, 20939002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2094eed56642SAlex Bennée { 20952dd92606SRichard Henderson return full_be_ldul_mmu(env, addr, oi, retaddr); 2096eed56642SAlex Bennée } 2097eed56642SAlex Bennée 2098fc1bc777SRichard Henderson uint64_t helper_le_ldq_mmu(CPUArchState *env, target_ulong addr, 20999002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2100eed56642SAlex Bennée { 2101fc313c64SFrédéric Pétrot validate_memop(oi, MO_LEUQ); 2102fc313c64SFrédéric Pétrot return load_helper(env, addr, oi, retaddr, MO_LEUQ, false, 21032dd92606SRichard Henderson helper_le_ldq_mmu); 2104eed56642SAlex Bennée } 2105eed56642SAlex Bennée 2106fc1bc777SRichard Henderson uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr, 21079002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2108eed56642SAlex Bennée { 2109fc313c64SFrédéric Pétrot validate_memop(oi, MO_BEUQ); 2110fc313c64SFrédéric Pétrot return load_helper(env, addr, oi, retaddr, MO_BEUQ, false, 21112dd92606SRichard Henderson helper_be_ldq_mmu); 2112eed56642SAlex Bennée } 2113eed56642SAlex Bennée 2114eed56642SAlex Bennée /* 2115eed56642SAlex Bennée * Provide signed versions of the load routines as well. We can of course 2116eed56642SAlex Bennée * avoid this for 64-bit data, or for 32-bit data on 32-bit host. 2117eed56642SAlex Bennée */ 2118eed56642SAlex Bennée 2119eed56642SAlex Bennée 2120eed56642SAlex Bennée tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr, 21219002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2122eed56642SAlex Bennée { 2123eed56642SAlex Bennée return (int8_t)helper_ret_ldub_mmu(env, addr, oi, retaddr); 2124eed56642SAlex Bennée } 2125eed56642SAlex Bennée 2126eed56642SAlex Bennée tcg_target_ulong helper_le_ldsw_mmu(CPUArchState *env, target_ulong addr, 21279002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2128eed56642SAlex Bennée { 2129eed56642SAlex Bennée return (int16_t)helper_le_lduw_mmu(env, addr, oi, retaddr); 2130eed56642SAlex Bennée } 2131eed56642SAlex Bennée 2132eed56642SAlex Bennée tcg_target_ulong helper_be_ldsw_mmu(CPUArchState *env, target_ulong addr, 21339002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2134eed56642SAlex Bennée { 2135eed56642SAlex Bennée return (int16_t)helper_be_lduw_mmu(env, addr, oi, retaddr); 2136eed56642SAlex Bennée } 2137eed56642SAlex Bennée 2138eed56642SAlex Bennée tcg_target_ulong helper_le_ldsl_mmu(CPUArchState *env, target_ulong addr, 21399002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2140eed56642SAlex Bennée { 2141eed56642SAlex Bennée return (int32_t)helper_le_ldul_mmu(env, addr, oi, retaddr); 2142eed56642SAlex Bennée } 2143eed56642SAlex Bennée 2144eed56642SAlex Bennée tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr, 21459002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2146eed56642SAlex Bennée { 2147eed56642SAlex Bennée return (int32_t)helper_be_ldul_mmu(env, addr, oi, retaddr); 2148eed56642SAlex Bennée } 2149eed56642SAlex Bennée 2150eed56642SAlex Bennée /* 2151d03f1408SRichard Henderson * Load helpers for cpu_ldst.h. 2152d03f1408SRichard Henderson */ 2153d03f1408SRichard Henderson 2154d03f1408SRichard Henderson static inline uint64_t cpu_load_helper(CPUArchState *env, abi_ptr addr, 2155f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr, 2156f83bcecbSRichard Henderson FullLoadHelper *full_load) 2157d03f1408SRichard Henderson { 2158d03f1408SRichard Henderson uint64_t ret; 2159d03f1408SRichard Henderson 2160d03f1408SRichard Henderson ret = full_load(env, addr, oi, retaddr); 216137aff087SRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 2162d03f1408SRichard Henderson return ret; 2163d03f1408SRichard Henderson } 2164d03f1408SRichard Henderson 2165f83bcecbSRichard Henderson uint8_t cpu_ldb_mmu(CPUArchState *env, abi_ptr addr, MemOpIdx oi, uintptr_t ra) 2166d03f1408SRichard Henderson { 2167f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, full_ldub_mmu); 2168d03f1408SRichard Henderson } 2169d03f1408SRichard Henderson 2170f83bcecbSRichard Henderson uint16_t cpu_ldw_be_mmu(CPUArchState *env, abi_ptr addr, 2171f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2172d03f1408SRichard Henderson { 2173f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, full_be_lduw_mmu); 2174d03f1408SRichard Henderson } 2175d03f1408SRichard Henderson 2176f83bcecbSRichard Henderson uint32_t cpu_ldl_be_mmu(CPUArchState *env, abi_ptr addr, 2177f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2178d03f1408SRichard Henderson { 2179f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, full_be_ldul_mmu); 2180d03f1408SRichard Henderson } 2181d03f1408SRichard Henderson 2182f83bcecbSRichard Henderson uint64_t cpu_ldq_be_mmu(CPUArchState *env, abi_ptr addr, 2183f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2184d03f1408SRichard Henderson { 218546697cb9SRichard Henderson return cpu_load_helper(env, addr, oi, ra, helper_be_ldq_mmu); 2186d03f1408SRichard Henderson } 2187d03f1408SRichard Henderson 2188f83bcecbSRichard Henderson uint16_t cpu_ldw_le_mmu(CPUArchState *env, abi_ptr addr, 2189f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2190d03f1408SRichard Henderson { 2191f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, full_le_lduw_mmu); 2192d03f1408SRichard Henderson } 2193d03f1408SRichard Henderson 2194f83bcecbSRichard Henderson uint32_t cpu_ldl_le_mmu(CPUArchState *env, abi_ptr addr, 2195f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2196d03f1408SRichard Henderson { 2197f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, full_le_ldul_mmu); 2198b9e60257SRichard Henderson } 2199b9e60257SRichard Henderson 2200f83bcecbSRichard Henderson uint64_t cpu_ldq_le_mmu(CPUArchState *env, abi_ptr addr, 2201f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 2202b9e60257SRichard Henderson { 2203f83bcecbSRichard Henderson return cpu_load_helper(env, addr, oi, ra, helper_le_ldq_mmu); 2204cfe04a4bSRichard Henderson } 2205cfe04a4bSRichard Henderson 2206d03f1408SRichard Henderson /* 2207eed56642SAlex Bennée * Store Helpers 2208eed56642SAlex Bennée */ 2209eed56642SAlex Bennée 2210c6b716cdSRichard Henderson static inline void QEMU_ALWAYS_INLINE 221180d9d1c6SRichard Henderson store_memop(void *haddr, uint64_t val, MemOp op) 221280d9d1c6SRichard Henderson { 221380d9d1c6SRichard Henderson switch (op) { 221480d9d1c6SRichard Henderson case MO_UB: 221580d9d1c6SRichard Henderson stb_p(haddr, val); 221680d9d1c6SRichard Henderson break; 221780d9d1c6SRichard Henderson case MO_BEUW: 221880d9d1c6SRichard Henderson stw_be_p(haddr, val); 221980d9d1c6SRichard Henderson break; 222080d9d1c6SRichard Henderson case MO_LEUW: 222180d9d1c6SRichard Henderson stw_le_p(haddr, val); 222280d9d1c6SRichard Henderson break; 222380d9d1c6SRichard Henderson case MO_BEUL: 222480d9d1c6SRichard Henderson stl_be_p(haddr, val); 222580d9d1c6SRichard Henderson break; 222680d9d1c6SRichard Henderson case MO_LEUL: 222780d9d1c6SRichard Henderson stl_le_p(haddr, val); 222880d9d1c6SRichard Henderson break; 2229fc313c64SFrédéric Pétrot case MO_BEUQ: 223080d9d1c6SRichard Henderson stq_be_p(haddr, val); 223180d9d1c6SRichard Henderson break; 2232fc313c64SFrédéric Pétrot case MO_LEUQ: 223380d9d1c6SRichard Henderson stq_le_p(haddr, val); 223480d9d1c6SRichard Henderson break; 223580d9d1c6SRichard Henderson default: 223680d9d1c6SRichard Henderson qemu_build_not_reached(); 223780d9d1c6SRichard Henderson } 223880d9d1c6SRichard Henderson } 223980d9d1c6SRichard Henderson 2240f83bcecbSRichard Henderson static void full_stb_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2241f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr); 2242f83bcecbSRichard Henderson 22436b8b622eSRichard Henderson static void __attribute__((noinline)) 22446b8b622eSRichard Henderson store_helper_unaligned(CPUArchState *env, target_ulong addr, uint64_t val, 22456b8b622eSRichard Henderson uintptr_t retaddr, size_t size, uintptr_t mmu_idx, 22466b8b622eSRichard Henderson bool big_endian) 22476b8b622eSRichard Henderson { 22486b8b622eSRichard Henderson const size_t tlb_off = offsetof(CPUTLBEntry, addr_write); 22496b8b622eSRichard Henderson uintptr_t index, index2; 22506b8b622eSRichard Henderson CPUTLBEntry *entry, *entry2; 22516b8b622eSRichard Henderson target_ulong page2, tlb_addr, tlb_addr2; 22529002ffcbSRichard Henderson MemOpIdx oi; 22536b8b622eSRichard Henderson size_t size2; 22546b8b622eSRichard Henderson int i; 22556b8b622eSRichard Henderson 22566b8b622eSRichard Henderson /* 22576b8b622eSRichard Henderson * Ensure the second page is in the TLB. Note that the first page 22586b8b622eSRichard Henderson * is already guaranteed to be filled, and that the second page 22596b8b622eSRichard Henderson * cannot evict the first. 22606b8b622eSRichard Henderson */ 22616b8b622eSRichard Henderson page2 = (addr + size) & TARGET_PAGE_MASK; 22626b8b622eSRichard Henderson size2 = (addr + size) & ~TARGET_PAGE_MASK; 22636b8b622eSRichard Henderson index2 = tlb_index(env, mmu_idx, page2); 22646b8b622eSRichard Henderson entry2 = tlb_entry(env, mmu_idx, page2); 22656b8b622eSRichard Henderson 22666b8b622eSRichard Henderson tlb_addr2 = tlb_addr_write(entry2); 22676b8b622eSRichard Henderson if (!tlb_hit_page(tlb_addr2, page2)) { 22686b8b622eSRichard Henderson if (!victim_tlb_hit(env, mmu_idx, index2, tlb_off, page2)) { 22696b8b622eSRichard Henderson tlb_fill(env_cpu(env), page2, size2, MMU_DATA_STORE, 22706b8b622eSRichard Henderson mmu_idx, retaddr); 22716b8b622eSRichard Henderson index2 = tlb_index(env, mmu_idx, page2); 22726b8b622eSRichard Henderson entry2 = tlb_entry(env, mmu_idx, page2); 22736b8b622eSRichard Henderson } 22746b8b622eSRichard Henderson tlb_addr2 = tlb_addr_write(entry2); 22756b8b622eSRichard Henderson } 22766b8b622eSRichard Henderson 22776b8b622eSRichard Henderson index = tlb_index(env, mmu_idx, addr); 22786b8b622eSRichard Henderson entry = tlb_entry(env, mmu_idx, addr); 22796b8b622eSRichard Henderson tlb_addr = tlb_addr_write(entry); 22806b8b622eSRichard Henderson 22816b8b622eSRichard Henderson /* 22826b8b622eSRichard Henderson * Handle watchpoints. Since this may trap, all checks 22836b8b622eSRichard Henderson * must happen before any store. 22846b8b622eSRichard Henderson */ 22856b8b622eSRichard Henderson if (unlikely(tlb_addr & TLB_WATCHPOINT)) { 22866b8b622eSRichard Henderson cpu_check_watchpoint(env_cpu(env), addr, size - size2, 22876b8b622eSRichard Henderson env_tlb(env)->d[mmu_idx].iotlb[index].attrs, 22886b8b622eSRichard Henderson BP_MEM_WRITE, retaddr); 22896b8b622eSRichard Henderson } 22906b8b622eSRichard Henderson if (unlikely(tlb_addr2 & TLB_WATCHPOINT)) { 22916b8b622eSRichard Henderson cpu_check_watchpoint(env_cpu(env), page2, size2, 22926b8b622eSRichard Henderson env_tlb(env)->d[mmu_idx].iotlb[index2].attrs, 22936b8b622eSRichard Henderson BP_MEM_WRITE, retaddr); 22946b8b622eSRichard Henderson } 22956b8b622eSRichard Henderson 22966b8b622eSRichard Henderson /* 22976b8b622eSRichard Henderson * XXX: not efficient, but simple. 22986b8b622eSRichard Henderson * This loop must go in the forward direction to avoid issues 22996b8b622eSRichard Henderson * with self-modifying code in Windows 64-bit. 23006b8b622eSRichard Henderson */ 23016b8b622eSRichard Henderson oi = make_memop_idx(MO_UB, mmu_idx); 23026b8b622eSRichard Henderson if (big_endian) { 23036b8b622eSRichard Henderson for (i = 0; i < size; ++i) { 23046b8b622eSRichard Henderson /* Big-endian extract. */ 23056b8b622eSRichard Henderson uint8_t val8 = val >> (((size - 1) * 8) - (i * 8)); 2306f83bcecbSRichard Henderson full_stb_mmu(env, addr + i, val8, oi, retaddr); 23076b8b622eSRichard Henderson } 23086b8b622eSRichard Henderson } else { 23096b8b622eSRichard Henderson for (i = 0; i < size; ++i) { 23106b8b622eSRichard Henderson /* Little-endian extract. */ 23116b8b622eSRichard Henderson uint8_t val8 = val >> (i * 8); 2312f83bcecbSRichard Henderson full_stb_mmu(env, addr + i, val8, oi, retaddr); 23136b8b622eSRichard Henderson } 23146b8b622eSRichard Henderson } 23156b8b622eSRichard Henderson } 23166b8b622eSRichard Henderson 231780d9d1c6SRichard Henderson static inline void QEMU_ALWAYS_INLINE 23184601f8d1SRichard Henderson store_helper(CPUArchState *env, target_ulong addr, uint64_t val, 23199002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr, MemOp op) 2320eed56642SAlex Bennée { 2321eed56642SAlex Bennée const size_t tlb_off = offsetof(CPUTLBEntry, addr_write); 2322*b826044fSRichard Henderson const unsigned a_bits = get_alignment_bits(get_memop(oi)); 2323*b826044fSRichard Henderson const size_t size = memop_size(op); 2324*b826044fSRichard Henderson uintptr_t mmu_idx = get_mmuidx(oi); 2325*b826044fSRichard Henderson uintptr_t index; 2326*b826044fSRichard Henderson CPUTLBEntry *entry; 2327*b826044fSRichard Henderson target_ulong tlb_addr; 2328eed56642SAlex Bennée void *haddr; 2329*b826044fSRichard Henderson 2330*b826044fSRichard Henderson tcg_debug_assert(mmu_idx < NB_MMU_MODES); 2331eed56642SAlex Bennée 2332eed56642SAlex Bennée /* Handle CPU specific unaligned behaviour */ 2333eed56642SAlex Bennée if (addr & ((1 << a_bits) - 1)) { 233429a0af61SRichard Henderson cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE, 2335eed56642SAlex Bennée mmu_idx, retaddr); 2336eed56642SAlex Bennée } 2337eed56642SAlex Bennée 2338*b826044fSRichard Henderson index = tlb_index(env, mmu_idx, addr); 2339*b826044fSRichard Henderson entry = tlb_entry(env, mmu_idx, addr); 2340*b826044fSRichard Henderson tlb_addr = tlb_addr_write(entry); 2341*b826044fSRichard Henderson 2342eed56642SAlex Bennée /* If the TLB entry is for a different page, reload and try again. */ 2343eed56642SAlex Bennée if (!tlb_hit(tlb_addr, addr)) { 2344eed56642SAlex Bennée if (!victim_tlb_hit(env, mmu_idx, index, tlb_off, 2345eed56642SAlex Bennée addr & TARGET_PAGE_MASK)) { 234629a0af61SRichard Henderson tlb_fill(env_cpu(env), addr, size, MMU_DATA_STORE, 2347eed56642SAlex Bennée mmu_idx, retaddr); 2348eed56642SAlex Bennée index = tlb_index(env, mmu_idx, addr); 2349eed56642SAlex Bennée entry = tlb_entry(env, mmu_idx, addr); 2350eed56642SAlex Bennée } 2351eed56642SAlex Bennée tlb_addr = tlb_addr_write(entry) & ~TLB_INVALID_MASK; 2352eed56642SAlex Bennée } 2353eed56642SAlex Bennée 235450b107c5SRichard Henderson /* Handle anything that isn't just a straight memory access. */ 2355eed56642SAlex Bennée if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) { 235650b107c5SRichard Henderson CPUIOTLBEntry *iotlbentry; 23575b87b3e6SRichard Henderson bool need_swap; 235850b107c5SRichard Henderson 235950b107c5SRichard Henderson /* For anything that is unaligned, recurse through byte stores. */ 2360eed56642SAlex Bennée if ((addr & (size - 1)) != 0) { 2361eed56642SAlex Bennée goto do_unaligned_access; 2362eed56642SAlex Bennée } 236350b107c5SRichard Henderson 236450b107c5SRichard Henderson iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index]; 236550b107c5SRichard Henderson 236650b107c5SRichard Henderson /* Handle watchpoints. */ 236750b107c5SRichard Henderson if (unlikely(tlb_addr & TLB_WATCHPOINT)) { 236850b107c5SRichard Henderson /* On watchpoint hit, this will longjmp out. */ 236950b107c5SRichard Henderson cpu_check_watchpoint(env_cpu(env), addr, size, 237050b107c5SRichard Henderson iotlbentry->attrs, BP_MEM_WRITE, retaddr); 23715b87b3e6SRichard Henderson } 237250b107c5SRichard Henderson 23735b87b3e6SRichard Henderson need_swap = size > 1 && (tlb_addr & TLB_BSWAP); 237450b107c5SRichard Henderson 237550b107c5SRichard Henderson /* Handle I/O access. */ 237608565552SRichard Henderson if (tlb_addr & TLB_MMIO) { 23775b87b3e6SRichard Henderson io_writex(env, iotlbentry, mmu_idx, val, addr, retaddr, 23785b87b3e6SRichard Henderson op ^ (need_swap * MO_BSWAP)); 23795b87b3e6SRichard Henderson return; 23805b87b3e6SRichard Henderson } 23815b87b3e6SRichard Henderson 23827b0d792cSRichard Henderson /* Ignore writes to ROM. */ 23837b0d792cSRichard Henderson if (unlikely(tlb_addr & TLB_DISCARD_WRITE)) { 23847b0d792cSRichard Henderson return; 23857b0d792cSRichard Henderson } 23867b0d792cSRichard Henderson 238708565552SRichard Henderson /* Handle clean RAM pages. */ 238808565552SRichard Henderson if (tlb_addr & TLB_NOTDIRTY) { 2389707526adSRichard Henderson notdirty_write(env_cpu(env), addr, size, iotlbentry, retaddr); 239008565552SRichard Henderson } 239108565552SRichard Henderson 2392707526adSRichard Henderson haddr = (void *)((uintptr_t)addr + entry->addend); 239308565552SRichard Henderson 23945b87b3e6SRichard Henderson /* 23955b87b3e6SRichard Henderson * Keep these two store_memop separate to ensure that the compiler 23965b87b3e6SRichard Henderson * is able to fold the entire function to a single instruction. 23975b87b3e6SRichard Henderson * There is a build-time assert inside to remind you of this. ;-) 23985b87b3e6SRichard Henderson */ 23995b87b3e6SRichard Henderson if (unlikely(need_swap)) { 24005b87b3e6SRichard Henderson store_memop(haddr, val, op ^ MO_BSWAP); 24015b87b3e6SRichard Henderson } else { 24025b87b3e6SRichard Henderson store_memop(haddr, val, op); 24035b87b3e6SRichard Henderson } 2404eed56642SAlex Bennée return; 2405eed56642SAlex Bennée } 2406eed56642SAlex Bennée 2407eed56642SAlex Bennée /* Handle slow unaligned access (it spans two pages or IO). */ 2408eed56642SAlex Bennée if (size > 1 2409eed56642SAlex Bennée && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1 2410eed56642SAlex Bennée >= TARGET_PAGE_SIZE)) { 2411eed56642SAlex Bennée do_unaligned_access: 24126b8b622eSRichard Henderson store_helper_unaligned(env, addr, val, retaddr, size, 24136b8b622eSRichard Henderson mmu_idx, memop_big_endian(op)); 2414eed56642SAlex Bennée return; 2415eed56642SAlex Bennée } 2416eed56642SAlex Bennée 2417eed56642SAlex Bennée haddr = (void *)((uintptr_t)addr + entry->addend); 241880d9d1c6SRichard Henderson store_memop(haddr, val, op); 2419eed56642SAlex Bennée } 2420eed56642SAlex Bennée 2421f83bcecbSRichard Henderson static void __attribute__((noinline)) 2422f83bcecbSRichard Henderson full_stb_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 24239002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2424eed56642SAlex Bennée { 2425f83bcecbSRichard Henderson validate_memop(oi, MO_UB); 2426be5c4787STony Nguyen store_helper(env, addr, val, oi, retaddr, MO_UB); 2427eed56642SAlex Bennée } 2428eed56642SAlex Bennée 2429f83bcecbSRichard Henderson void helper_ret_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val, 2430f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2431f83bcecbSRichard Henderson { 2432f83bcecbSRichard Henderson full_stb_mmu(env, addr, val, oi, retaddr); 2433f83bcecbSRichard Henderson } 2434f83bcecbSRichard Henderson 2435f83bcecbSRichard Henderson static void full_le_stw_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2436f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2437f83bcecbSRichard Henderson { 2438f83bcecbSRichard Henderson validate_memop(oi, MO_LEUW); 2439f83bcecbSRichard Henderson store_helper(env, addr, val, oi, retaddr, MO_LEUW); 2440f83bcecbSRichard Henderson } 2441f83bcecbSRichard Henderson 2442fc1bc777SRichard Henderson void helper_le_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 24439002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2444eed56642SAlex Bennée { 2445f83bcecbSRichard Henderson full_le_stw_mmu(env, addr, val, oi, retaddr); 2446f83bcecbSRichard Henderson } 2447f83bcecbSRichard Henderson 2448f83bcecbSRichard Henderson static void full_be_stw_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2449f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2450f83bcecbSRichard Henderson { 2451f83bcecbSRichard Henderson validate_memop(oi, MO_BEUW); 2452f83bcecbSRichard Henderson store_helper(env, addr, val, oi, retaddr, MO_BEUW); 2453eed56642SAlex Bennée } 2454eed56642SAlex Bennée 2455fc1bc777SRichard Henderson void helper_be_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 24569002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2457eed56642SAlex Bennée { 2458f83bcecbSRichard Henderson full_be_stw_mmu(env, addr, val, oi, retaddr); 2459f83bcecbSRichard Henderson } 2460f83bcecbSRichard Henderson 2461f83bcecbSRichard Henderson static void full_le_stl_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2462f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2463f83bcecbSRichard Henderson { 2464f83bcecbSRichard Henderson validate_memop(oi, MO_LEUL); 2465f83bcecbSRichard Henderson store_helper(env, addr, val, oi, retaddr, MO_LEUL); 2466eed56642SAlex Bennée } 2467eed56642SAlex Bennée 2468fc1bc777SRichard Henderson void helper_le_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 24699002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2470eed56642SAlex Bennée { 2471f83bcecbSRichard Henderson full_le_stl_mmu(env, addr, val, oi, retaddr); 2472f83bcecbSRichard Henderson } 2473f83bcecbSRichard Henderson 2474f83bcecbSRichard Henderson static void full_be_stl_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2475f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2476f83bcecbSRichard Henderson { 2477f83bcecbSRichard Henderson validate_memop(oi, MO_BEUL); 2478f83bcecbSRichard Henderson store_helper(env, addr, val, oi, retaddr, MO_BEUL); 2479eed56642SAlex Bennée } 2480eed56642SAlex Bennée 2481fc1bc777SRichard Henderson void helper_be_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 24829002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2483eed56642SAlex Bennée { 2484f83bcecbSRichard Henderson full_be_stl_mmu(env, addr, val, oi, retaddr); 2485eed56642SAlex Bennée } 2486eed56642SAlex Bennée 2487fc1bc777SRichard Henderson void helper_le_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 24889002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2489eed56642SAlex Bennée { 2490fc313c64SFrédéric Pétrot validate_memop(oi, MO_LEUQ); 2491fc313c64SFrédéric Pétrot store_helper(env, addr, val, oi, retaddr, MO_LEUQ); 2492eed56642SAlex Bennée } 2493eed56642SAlex Bennée 2494fc1bc777SRichard Henderson void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 24959002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2496eed56642SAlex Bennée { 2497fc313c64SFrédéric Pétrot validate_memop(oi, MO_BEUQ); 2498fc313c64SFrédéric Pétrot store_helper(env, addr, val, oi, retaddr, MO_BEUQ); 2499eed56642SAlex Bennée } 2500d9bb58e5SYang Zhong 2501d03f1408SRichard Henderson /* 2502d03f1408SRichard Henderson * Store Helpers for cpu_ldst.h 2503d03f1408SRichard Henderson */ 2504d03f1408SRichard Henderson 2505f83bcecbSRichard Henderson typedef void FullStoreHelper(CPUArchState *env, target_ulong addr, 2506f83bcecbSRichard Henderson uint64_t val, MemOpIdx oi, uintptr_t retaddr); 2507f83bcecbSRichard Henderson 2508f83bcecbSRichard Henderson static inline void cpu_store_helper(CPUArchState *env, target_ulong addr, 2509f83bcecbSRichard Henderson uint64_t val, MemOpIdx oi, uintptr_t ra, 2510f83bcecbSRichard Henderson FullStoreHelper *full_store) 2511d03f1408SRichard Henderson { 2512f83bcecbSRichard Henderson full_store(env, addr, val, oi, ra); 251337aff087SRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 2514d03f1408SRichard Henderson } 2515d03f1408SRichard Henderson 2516f83bcecbSRichard Henderson void cpu_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val, 2517f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2518d03f1408SRichard Henderson { 2519f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, full_stb_mmu); 2520d03f1408SRichard Henderson } 2521d03f1408SRichard Henderson 2522f83bcecbSRichard Henderson void cpu_stw_be_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 2523f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2524d03f1408SRichard Henderson { 2525f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, full_be_stw_mmu); 2526d03f1408SRichard Henderson } 2527d03f1408SRichard Henderson 2528f83bcecbSRichard Henderson void cpu_stl_be_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 2529f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2530d03f1408SRichard Henderson { 2531f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, full_be_stl_mmu); 2532d03f1408SRichard Henderson } 2533d03f1408SRichard Henderson 2534f83bcecbSRichard Henderson void cpu_stq_be_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2535f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2536d03f1408SRichard Henderson { 2537f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, helper_be_stq_mmu); 2538b9e60257SRichard Henderson } 2539b9e60257SRichard Henderson 2540f83bcecbSRichard Henderson void cpu_stw_le_mmu(CPUArchState *env, target_ulong addr, uint16_t val, 2541f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2542b9e60257SRichard Henderson { 2543f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, full_le_stw_mmu); 2544b9e60257SRichard Henderson } 2545b9e60257SRichard Henderson 2546f83bcecbSRichard Henderson void cpu_stl_le_mmu(CPUArchState *env, target_ulong addr, uint32_t val, 2547f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2548b9e60257SRichard Henderson { 2549f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, full_le_stl_mmu); 2550b9e60257SRichard Henderson } 2551b9e60257SRichard Henderson 2552f83bcecbSRichard Henderson void cpu_stq_le_mmu(CPUArchState *env, target_ulong addr, uint64_t val, 2553f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2554b9e60257SRichard Henderson { 2555f83bcecbSRichard Henderson cpu_store_helper(env, addr, val, oi, retaddr, helper_le_stq_mmu); 2556d03f1408SRichard Henderson } 2557d03f1408SRichard Henderson 2558f83bcecbSRichard Henderson #include "ldst_common.c.inc" 2559cfe04a4bSRichard Henderson 2560be9568b4SRichard Henderson /* 2561be9568b4SRichard Henderson * First set of functions passes in OI and RETADDR. 2562be9568b4SRichard Henderson * This makes them callable from other helpers. 2563be9568b4SRichard Henderson */ 2564d9bb58e5SYang Zhong 2565d9bb58e5SYang Zhong #define ATOMIC_NAME(X) \ 2566be9568b4SRichard Henderson glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu) 2567a754f7f3SRichard Henderson 2568707526adSRichard Henderson #define ATOMIC_MMU_CLEANUP 2569d9bb58e5SYang Zhong 2570139c1837SPaolo Bonzini #include "atomic_common.c.inc" 2571d9bb58e5SYang Zhong 2572d9bb58e5SYang Zhong #define DATA_SIZE 1 2573d9bb58e5SYang Zhong #include "atomic_template.h" 2574d9bb58e5SYang Zhong 2575d9bb58e5SYang Zhong #define DATA_SIZE 2 2576d9bb58e5SYang Zhong #include "atomic_template.h" 2577d9bb58e5SYang Zhong 2578d9bb58e5SYang Zhong #define DATA_SIZE 4 2579d9bb58e5SYang Zhong #include "atomic_template.h" 2580d9bb58e5SYang Zhong 2581d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64 2582d9bb58e5SYang Zhong #define DATA_SIZE 8 2583d9bb58e5SYang Zhong #include "atomic_template.h" 2584d9bb58e5SYang Zhong #endif 2585d9bb58e5SYang Zhong 2586e6cd4bb5SRichard Henderson #if HAVE_CMPXCHG128 || HAVE_ATOMIC128 2587d9bb58e5SYang Zhong #define DATA_SIZE 16 2588d9bb58e5SYang Zhong #include "atomic_template.h" 2589d9bb58e5SYang Zhong #endif 2590d9bb58e5SYang Zhong 2591d9bb58e5SYang Zhong /* Code access functions. */ 2592d9bb58e5SYang Zhong 2593fc4120a3SRichard Henderson static uint64_t full_ldub_code(CPUArchState *env, target_ulong addr, 25949002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 25952dd92606SRichard Henderson { 2596fc4120a3SRichard Henderson return load_helper(env, addr, oi, retaddr, MO_8, true, full_ldub_code); 25972dd92606SRichard Henderson } 25982dd92606SRichard Henderson 2599fc4120a3SRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr) 2600eed56642SAlex Bennée { 26019002ffcbSRichard Henderson MemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(env, true)); 2602fc4120a3SRichard Henderson return full_ldub_code(env, addr, oi, 0); 26032dd92606SRichard Henderson } 26042dd92606SRichard Henderson 2605fc4120a3SRichard Henderson static uint64_t full_lduw_code(CPUArchState *env, target_ulong addr, 26069002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 26074cef72d0SAlex Bennée { 2608fc4120a3SRichard Henderson return load_helper(env, addr, oi, retaddr, MO_TEUW, true, full_lduw_code); 26094cef72d0SAlex Bennée } 26104cef72d0SAlex Bennée 2611fc4120a3SRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr) 26122dd92606SRichard Henderson { 26139002ffcbSRichard Henderson MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(env, true)); 2614fc4120a3SRichard Henderson return full_lduw_code(env, addr, oi, 0); 2615eed56642SAlex Bennée } 2616d9bb58e5SYang Zhong 2617fc4120a3SRichard Henderson static uint64_t full_ldl_code(CPUArchState *env, target_ulong addr, 26189002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 2619eed56642SAlex Bennée { 2620fc4120a3SRichard Henderson return load_helper(env, addr, oi, retaddr, MO_TEUL, true, full_ldl_code); 26212dd92606SRichard Henderson } 26222dd92606SRichard Henderson 2623fc4120a3SRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr) 26244cef72d0SAlex Bennée { 26259002ffcbSRichard Henderson MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(env, true)); 2626fc4120a3SRichard Henderson return full_ldl_code(env, addr, oi, 0); 26274cef72d0SAlex Bennée } 26284cef72d0SAlex Bennée 2629fc4120a3SRichard Henderson static uint64_t full_ldq_code(CPUArchState *env, target_ulong addr, 26309002ffcbSRichard Henderson MemOpIdx oi, uintptr_t retaddr) 26312dd92606SRichard Henderson { 2632fc313c64SFrédéric Pétrot return load_helper(env, addr, oi, retaddr, MO_TEUQ, true, full_ldq_code); 2633eed56642SAlex Bennée } 2634d9bb58e5SYang Zhong 2635fc4120a3SRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr) 2636eed56642SAlex Bennée { 2637fc313c64SFrédéric Pétrot MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(env, true)); 2638fc4120a3SRichard Henderson return full_ldq_code(env, addr, oi, 0); 2639eed56642SAlex Bennée } 2640