xref: /openbmc/qemu/accel/tcg/cputlb.c (revision a26fc6f5152b47f1d7ed928f9c9d462d01ff1624)
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"
22d9bb58e5SYang Zhong #include "cpu.h"
23d9bb58e5SYang Zhong #include "exec/exec-all.h"
24d9bb58e5SYang Zhong #include "exec/memory.h"
25d9bb58e5SYang Zhong #include "exec/address-spaces.h"
26d9bb58e5SYang Zhong #include "exec/cpu_ldst.h"
27d9bb58e5SYang Zhong #include "exec/cputlb.h"
28d9bb58e5SYang Zhong #include "exec/memory-internal.h"
29d9bb58e5SYang Zhong #include "exec/ram_addr.h"
30d9bb58e5SYang Zhong #include "tcg/tcg.h"
31d9bb58e5SYang Zhong #include "qemu/error-report.h"
32d9bb58e5SYang Zhong #include "exec/log.h"
33d9bb58e5SYang Zhong #include "exec/helper-proto.h"
34d9bb58e5SYang Zhong #include "qemu/atomic.h"
35e6cd4bb5SRichard Henderson #include "qemu/atomic128.h"
36d9bb58e5SYang Zhong 
37d9bb58e5SYang Zhong /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
38d9bb58e5SYang Zhong /* #define DEBUG_TLB */
39d9bb58e5SYang Zhong /* #define DEBUG_TLB_LOG */
40d9bb58e5SYang Zhong 
41d9bb58e5SYang Zhong #ifdef DEBUG_TLB
42d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 1
43d9bb58e5SYang Zhong # ifdef DEBUG_TLB_LOG
44d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 1
45d9bb58e5SYang Zhong # else
46d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 0
47d9bb58e5SYang Zhong # endif
48d9bb58e5SYang Zhong #else
49d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 0
50d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 0
51d9bb58e5SYang Zhong #endif
52d9bb58e5SYang Zhong 
53d9bb58e5SYang Zhong #define tlb_debug(fmt, ...) do { \
54d9bb58e5SYang Zhong     if (DEBUG_TLB_LOG_GATE) { \
55d9bb58e5SYang Zhong         qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
56d9bb58e5SYang Zhong                       ## __VA_ARGS__); \
57d9bb58e5SYang Zhong     } else if (DEBUG_TLB_GATE) { \
58d9bb58e5SYang Zhong         fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
59d9bb58e5SYang Zhong     } \
60d9bb58e5SYang Zhong } while (0)
61d9bb58e5SYang Zhong 
62ea9025cbSEmilio G. Cota #define assert_cpu_is_self(cpu) do {                              \
63d9bb58e5SYang Zhong         if (DEBUG_TLB_GATE) {                                     \
64ea9025cbSEmilio G. Cota             g_assert(!(cpu)->created || qemu_cpu_is_self(cpu));   \
65d9bb58e5SYang Zhong         }                                                         \
66d9bb58e5SYang Zhong     } while (0)
67d9bb58e5SYang Zhong 
68d9bb58e5SYang Zhong /* run_on_cpu_data.target_ptr should always be big enough for a
69d9bb58e5SYang Zhong  * target_ulong even on 32 bit builds */
70d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(sizeof(target_ulong) > sizeof(run_on_cpu_data));
71d9bb58e5SYang Zhong 
72d9bb58e5SYang Zhong /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
73d9bb58e5SYang Zhong  */
74d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16);
75d9bb58e5SYang Zhong #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
76d9bb58e5SYang Zhong 
7786e1eff8SEmilio G. Cota static inline size_t sizeof_tlb(CPUArchState *env, uintptr_t mmu_idx)
7886e1eff8SEmilio G. Cota {
79a40ec84eSRichard Henderson     return env_tlb(env)->f[mmu_idx].mask + (1 << CPU_TLB_ENTRY_BITS);
8086e1eff8SEmilio G. Cota }
8186e1eff8SEmilio G. Cota 
8279e42085SRichard Henderson static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns,
8386e1eff8SEmilio G. Cota                              size_t max_entries)
8486e1eff8SEmilio G. Cota {
8579e42085SRichard Henderson     desc->window_begin_ns = ns;
8679e42085SRichard Henderson     desc->window_max_entries = max_entries;
8786e1eff8SEmilio G. Cota }
8886e1eff8SEmilio G. Cota 
8986e1eff8SEmilio G. Cota static void tlb_dyn_init(CPUArchState *env)
9086e1eff8SEmilio G. Cota {
9186e1eff8SEmilio G. Cota     int i;
9286e1eff8SEmilio G. Cota 
9386e1eff8SEmilio G. Cota     for (i = 0; i < NB_MMU_MODES; i++) {
94a40ec84eSRichard Henderson         CPUTLBDesc *desc = &env_tlb(env)->d[i];
9586e1eff8SEmilio G. Cota         size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS;
9686e1eff8SEmilio G. Cota 
9779e42085SRichard Henderson         tlb_window_reset(desc, get_clock_realtime(), 0);
9886e1eff8SEmilio G. Cota         desc->n_used_entries = 0;
99a40ec84eSRichard Henderson         env_tlb(env)->f[i].mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS;
100a40ec84eSRichard Henderson         env_tlb(env)->f[i].table = g_new(CPUTLBEntry, n_entries);
101a40ec84eSRichard Henderson         env_tlb(env)->d[i].iotlb = g_new(CPUIOTLBEntry, n_entries);
10286e1eff8SEmilio G. Cota     }
10386e1eff8SEmilio G. Cota }
10486e1eff8SEmilio G. Cota 
10586e1eff8SEmilio G. Cota /**
10686e1eff8SEmilio G. Cota  * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
10786e1eff8SEmilio G. Cota  * @env: CPU that owns the TLB
10886e1eff8SEmilio G. Cota  * @mmu_idx: MMU index of the TLB
10986e1eff8SEmilio G. Cota  *
11086e1eff8SEmilio G. Cota  * Called with tlb_lock_held.
11186e1eff8SEmilio G. Cota  *
11286e1eff8SEmilio G. Cota  * We have two main constraints when resizing a TLB: (1) we only resize it
11386e1eff8SEmilio G. Cota  * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
11486e1eff8SEmilio G. Cota  * the array or unnecessarily flushing it), which means we do not control how
11586e1eff8SEmilio G. Cota  * frequently the resizing can occur; (2) we don't have access to the guest's
11686e1eff8SEmilio G. Cota  * future scheduling decisions, and therefore have to decide the magnitude of
11786e1eff8SEmilio G. Cota  * the resize based on past observations.
11886e1eff8SEmilio G. Cota  *
11986e1eff8SEmilio G. Cota  * In general, a memory-hungry process can benefit greatly from an appropriately
12086e1eff8SEmilio G. Cota  * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
12186e1eff8SEmilio G. Cota  * we just have to make the TLB as large as possible; while an oversized TLB
12286e1eff8SEmilio G. Cota  * results in minimal TLB miss rates, it also takes longer to be flushed
12386e1eff8SEmilio G. Cota  * (flushes can be _very_ frequent), and the reduced locality can also hurt
12486e1eff8SEmilio G. Cota  * performance.
12586e1eff8SEmilio G. Cota  *
12686e1eff8SEmilio G. Cota  * To achieve near-optimal performance for all kinds of workloads, we:
12786e1eff8SEmilio G. Cota  *
12886e1eff8SEmilio G. Cota  * 1. Aggressively increase the size of the TLB when the use rate of the
12986e1eff8SEmilio G. Cota  * TLB being flushed is high, since it is likely that in the near future this
13086e1eff8SEmilio G. Cota  * memory-hungry process will execute again, and its memory hungriness will
13186e1eff8SEmilio G. Cota  * probably be similar.
13286e1eff8SEmilio G. Cota  *
13386e1eff8SEmilio G. Cota  * 2. Slowly reduce the size of the TLB as the use rate declines over a
13486e1eff8SEmilio G. Cota  * reasonably large time window. The rationale is that if in such a time window
13586e1eff8SEmilio G. Cota  * we have not observed a high TLB use rate, it is likely that we won't observe
13686e1eff8SEmilio G. Cota  * it in the near future. In that case, once a time window expires we downsize
13786e1eff8SEmilio G. Cota  * the TLB to match the maximum use rate observed in the window.
13886e1eff8SEmilio G. Cota  *
13986e1eff8SEmilio G. Cota  * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
14086e1eff8SEmilio G. Cota  * since in that range performance is likely near-optimal. Recall that the TLB
14186e1eff8SEmilio G. Cota  * is direct mapped, so we want the use rate to be low (or at least not too
14286e1eff8SEmilio G. Cota  * high), since otherwise we are likely to have a significant amount of
14386e1eff8SEmilio G. Cota  * conflict misses.
14486e1eff8SEmilio G. Cota  */
14586e1eff8SEmilio G. Cota static void tlb_mmu_resize_locked(CPUArchState *env, int mmu_idx)
14686e1eff8SEmilio G. Cota {
147a40ec84eSRichard Henderson     CPUTLBDesc *desc = &env_tlb(env)->d[mmu_idx];
14886e1eff8SEmilio G. Cota     size_t old_size = tlb_n_entries(env, mmu_idx);
14986e1eff8SEmilio G. Cota     size_t rate;
15086e1eff8SEmilio G. Cota     size_t new_size = old_size;
15186e1eff8SEmilio G. Cota     int64_t now = get_clock_realtime();
15286e1eff8SEmilio G. Cota     int64_t window_len_ms = 100;
15386e1eff8SEmilio G. Cota     int64_t window_len_ns = window_len_ms * 1000 * 1000;
15479e42085SRichard Henderson     bool window_expired = now > desc->window_begin_ns + window_len_ns;
15586e1eff8SEmilio G. Cota 
15679e42085SRichard Henderson     if (desc->n_used_entries > desc->window_max_entries) {
15779e42085SRichard Henderson         desc->window_max_entries = desc->n_used_entries;
15886e1eff8SEmilio G. Cota     }
15979e42085SRichard Henderson     rate = desc->window_max_entries * 100 / old_size;
16086e1eff8SEmilio G. Cota 
16186e1eff8SEmilio G. Cota     if (rate > 70) {
16286e1eff8SEmilio G. Cota         new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS);
16386e1eff8SEmilio G. Cota     } else if (rate < 30 && window_expired) {
16479e42085SRichard Henderson         size_t ceil = pow2ceil(desc->window_max_entries);
16579e42085SRichard Henderson         size_t expected_rate = desc->window_max_entries * 100 / ceil;
16686e1eff8SEmilio G. Cota 
16786e1eff8SEmilio G. Cota         /*
16886e1eff8SEmilio G. Cota          * Avoid undersizing when the max number of entries seen is just below
16986e1eff8SEmilio G. Cota          * a pow2. For instance, if max_entries == 1025, the expected use rate
17086e1eff8SEmilio G. Cota          * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
17186e1eff8SEmilio G. Cota          * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
17286e1eff8SEmilio G. Cota          * later. Thus, make sure that the expected use rate remains below 70%.
17386e1eff8SEmilio G. Cota          * (and since we double the size, that means the lowest rate we'd
17486e1eff8SEmilio G. Cota          * expect to get is 35%, which is still in the 30-70% range where
17586e1eff8SEmilio G. Cota          * we consider that the size is appropriate.)
17686e1eff8SEmilio G. Cota          */
17786e1eff8SEmilio G. Cota         if (expected_rate > 70) {
17886e1eff8SEmilio G. Cota             ceil *= 2;
17986e1eff8SEmilio G. Cota         }
18086e1eff8SEmilio G. Cota         new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS);
18186e1eff8SEmilio G. Cota     }
18286e1eff8SEmilio G. Cota 
18386e1eff8SEmilio G. Cota     if (new_size == old_size) {
18486e1eff8SEmilio G. Cota         if (window_expired) {
18579e42085SRichard Henderson             tlb_window_reset(desc, now, desc->n_used_entries);
18686e1eff8SEmilio G. Cota         }
18786e1eff8SEmilio G. Cota         return;
18886e1eff8SEmilio G. Cota     }
18986e1eff8SEmilio G. Cota 
190a40ec84eSRichard Henderson     g_free(env_tlb(env)->f[mmu_idx].table);
191a40ec84eSRichard Henderson     g_free(env_tlb(env)->d[mmu_idx].iotlb);
19286e1eff8SEmilio G. Cota 
19379e42085SRichard Henderson     tlb_window_reset(desc, now, 0);
19486e1eff8SEmilio G. Cota     /* desc->n_used_entries is cleared by the caller */
195a40ec84eSRichard Henderson     env_tlb(env)->f[mmu_idx].mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
196a40ec84eSRichard Henderson     env_tlb(env)->f[mmu_idx].table = g_try_new(CPUTLBEntry, new_size);
197a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].iotlb = g_try_new(CPUIOTLBEntry, new_size);
19886e1eff8SEmilio G. Cota     /*
19986e1eff8SEmilio G. Cota      * If the allocations fail, try smaller sizes. We just freed some
20086e1eff8SEmilio G. Cota      * memory, so going back to half of new_size has a good chance of working.
20186e1eff8SEmilio G. Cota      * Increased memory pressure elsewhere in the system might cause the
20286e1eff8SEmilio G. Cota      * allocations to fail though, so we progressively reduce the allocation
20386e1eff8SEmilio G. Cota      * size, aborting if we cannot even allocate the smallest TLB we support.
20486e1eff8SEmilio G. Cota      */
205a40ec84eSRichard Henderson     while (env_tlb(env)->f[mmu_idx].table == NULL ||
206a40ec84eSRichard Henderson            env_tlb(env)->d[mmu_idx].iotlb == NULL) {
20786e1eff8SEmilio G. Cota         if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) {
20886e1eff8SEmilio G. Cota             error_report("%s: %s", __func__, strerror(errno));
20986e1eff8SEmilio G. Cota             abort();
21086e1eff8SEmilio G. Cota         }
21186e1eff8SEmilio G. Cota         new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS);
212a40ec84eSRichard Henderson         env_tlb(env)->f[mmu_idx].mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
21386e1eff8SEmilio G. Cota 
214a40ec84eSRichard Henderson         g_free(env_tlb(env)->f[mmu_idx].table);
215a40ec84eSRichard Henderson         g_free(env_tlb(env)->d[mmu_idx].iotlb);
216a40ec84eSRichard Henderson         env_tlb(env)->f[mmu_idx].table = g_try_new(CPUTLBEntry, new_size);
217a40ec84eSRichard Henderson         env_tlb(env)->d[mmu_idx].iotlb = g_try_new(CPUIOTLBEntry, new_size);
21886e1eff8SEmilio G. Cota     }
21986e1eff8SEmilio G. Cota }
22086e1eff8SEmilio G. Cota 
22186e1eff8SEmilio G. Cota static inline void tlb_table_flush_by_mmuidx(CPUArchState *env, int mmu_idx)
22286e1eff8SEmilio G. Cota {
22386e1eff8SEmilio G. Cota     tlb_mmu_resize_locked(env, mmu_idx);
224a40ec84eSRichard Henderson     memset(env_tlb(env)->f[mmu_idx].table, -1, sizeof_tlb(env, mmu_idx));
225a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].n_used_entries = 0;
22686e1eff8SEmilio G. Cota }
22786e1eff8SEmilio G. Cota 
22886e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_inc(CPUArchState *env, uintptr_t mmu_idx)
22986e1eff8SEmilio G. Cota {
230a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].n_used_entries++;
23186e1eff8SEmilio G. Cota }
23286e1eff8SEmilio G. Cota 
23386e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_dec(CPUArchState *env, uintptr_t mmu_idx)
23486e1eff8SEmilio G. Cota {
235a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].n_used_entries--;
23686e1eff8SEmilio G. Cota }
23786e1eff8SEmilio G. Cota 
2385005e253SEmilio G. Cota void tlb_init(CPUState *cpu)
2395005e253SEmilio G. Cota {
24071aec354SEmilio G. Cota     CPUArchState *env = cpu->env_ptr;
24171aec354SEmilio G. Cota 
242a40ec84eSRichard Henderson     qemu_spin_init(&env_tlb(env)->c.lock);
2433d1523ceSRichard Henderson 
2443d1523ceSRichard Henderson     /* Ensure that cpu_reset performs a full flush.  */
245a40ec84eSRichard Henderson     env_tlb(env)->c.dirty = ALL_MMUIDX_BITS;
24686e1eff8SEmilio G. Cota 
24786e1eff8SEmilio G. Cota     tlb_dyn_init(env);
2485005e253SEmilio G. Cota }
2495005e253SEmilio G. Cota 
250d9bb58e5SYang Zhong /* flush_all_helper: run fn across all cpus
251d9bb58e5SYang Zhong  *
252d9bb58e5SYang Zhong  * If the wait flag is set then the src cpu's helper will be queued as
253d9bb58e5SYang Zhong  * "safe" work and the loop exited creating a synchronisation point
254d9bb58e5SYang Zhong  * where all queued work will be finished before execution starts
255d9bb58e5SYang Zhong  * again.
256d9bb58e5SYang Zhong  */
257d9bb58e5SYang Zhong static void flush_all_helper(CPUState *src, run_on_cpu_func fn,
258d9bb58e5SYang Zhong                              run_on_cpu_data d)
259d9bb58e5SYang Zhong {
260d9bb58e5SYang Zhong     CPUState *cpu;
261d9bb58e5SYang Zhong 
262d9bb58e5SYang Zhong     CPU_FOREACH(cpu) {
263d9bb58e5SYang Zhong         if (cpu != src) {
264d9bb58e5SYang Zhong             async_run_on_cpu(cpu, fn, d);
265d9bb58e5SYang Zhong         }
266d9bb58e5SYang Zhong     }
267d9bb58e5SYang Zhong }
268d9bb58e5SYang Zhong 
269e09de0a2SRichard Henderson void tlb_flush_counts(size_t *pfull, size_t *ppart, size_t *pelide)
27083974cf4SEmilio G. Cota {
27183974cf4SEmilio G. Cota     CPUState *cpu;
272e09de0a2SRichard Henderson     size_t full = 0, part = 0, elide = 0;
27383974cf4SEmilio G. Cota 
27483974cf4SEmilio G. Cota     CPU_FOREACH(cpu) {
27583974cf4SEmilio G. Cota         CPUArchState *env = cpu->env_ptr;
27683974cf4SEmilio G. Cota 
277a40ec84eSRichard Henderson         full += atomic_read(&env_tlb(env)->c.full_flush_count);
278a40ec84eSRichard Henderson         part += atomic_read(&env_tlb(env)->c.part_flush_count);
279a40ec84eSRichard Henderson         elide += atomic_read(&env_tlb(env)->c.elide_flush_count);
28083974cf4SEmilio G. Cota     }
281e09de0a2SRichard Henderson     *pfull = full;
282e09de0a2SRichard Henderson     *ppart = part;
283e09de0a2SRichard Henderson     *pelide = elide;
28483974cf4SEmilio G. Cota }
285d9bb58e5SYang Zhong 
2861308e026SRichard Henderson static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx)
2871308e026SRichard Henderson {
28886e1eff8SEmilio G. Cota     tlb_table_flush_by_mmuidx(env, mmu_idx);
289a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_addr = -1;
290a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_mask = -1;
291a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].vindex = 0;
292a40ec84eSRichard Henderson     memset(env_tlb(env)->d[mmu_idx].vtable, -1,
293a40ec84eSRichard Henderson            sizeof(env_tlb(env)->d[0].vtable));
2941308e026SRichard Henderson }
2951308e026SRichard Henderson 
296d9bb58e5SYang Zhong static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)
297d9bb58e5SYang Zhong {
298d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
2993d1523ceSRichard Henderson     uint16_t asked = data.host_int;
3003d1523ceSRichard Henderson     uint16_t all_dirty, work, to_clean;
301d9bb58e5SYang Zhong 
302d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
303d9bb58e5SYang Zhong 
3043d1523ceSRichard Henderson     tlb_debug("mmu_idx:0x%04" PRIx16 "\n", asked);
305d9bb58e5SYang Zhong 
306a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
30760a2ad7dSRichard Henderson 
308a40ec84eSRichard Henderson     all_dirty = env_tlb(env)->c.dirty;
3093d1523ceSRichard Henderson     to_clean = asked & all_dirty;
3103d1523ceSRichard Henderson     all_dirty &= ~to_clean;
311a40ec84eSRichard Henderson     env_tlb(env)->c.dirty = all_dirty;
3123d1523ceSRichard Henderson 
3133d1523ceSRichard Henderson     for (work = to_clean; work != 0; work &= work - 1) {
3143d1523ceSRichard Henderson         int mmu_idx = ctz32(work);
3151308e026SRichard Henderson         tlb_flush_one_mmuidx_locked(env, mmu_idx);
316d9bb58e5SYang Zhong     }
3173d1523ceSRichard Henderson 
318a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
319d9bb58e5SYang Zhong 
320f3ced3c5SEmilio G. Cota     cpu_tb_jmp_cache_clear(cpu);
32164f2674bSRichard Henderson 
3223d1523ceSRichard Henderson     if (to_clean == ALL_MMUIDX_BITS) {
323a40ec84eSRichard Henderson         atomic_set(&env_tlb(env)->c.full_flush_count,
324a40ec84eSRichard Henderson                    env_tlb(env)->c.full_flush_count + 1);
325e09de0a2SRichard Henderson     } else {
326a40ec84eSRichard Henderson         atomic_set(&env_tlb(env)->c.part_flush_count,
327a40ec84eSRichard Henderson                    env_tlb(env)->c.part_flush_count + ctpop16(to_clean));
3283d1523ceSRichard Henderson         if (to_clean != asked) {
329a40ec84eSRichard Henderson             atomic_set(&env_tlb(env)->c.elide_flush_count,
330a40ec84eSRichard Henderson                        env_tlb(env)->c.elide_flush_count +
3313d1523ceSRichard Henderson                        ctpop16(asked & ~to_clean));
3323d1523ceSRichard Henderson         }
33364f2674bSRichard Henderson     }
334d9bb58e5SYang Zhong }
335d9bb58e5SYang Zhong 
336d9bb58e5SYang Zhong void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
337d9bb58e5SYang Zhong {
338d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap);
339d9bb58e5SYang Zhong 
34064f2674bSRichard Henderson     if (cpu->created && !qemu_cpu_is_self(cpu)) {
341d9bb58e5SYang Zhong         async_run_on_cpu(cpu, tlb_flush_by_mmuidx_async_work,
342ab651105SRichard Henderson                          RUN_ON_CPU_HOST_INT(idxmap));
343d9bb58e5SYang Zhong     } else {
34460a2ad7dSRichard Henderson         tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap));
345d9bb58e5SYang Zhong     }
346d9bb58e5SYang Zhong }
347d9bb58e5SYang Zhong 
34864f2674bSRichard Henderson void tlb_flush(CPUState *cpu)
34964f2674bSRichard Henderson {
35064f2674bSRichard Henderson     tlb_flush_by_mmuidx(cpu, ALL_MMUIDX_BITS);
35164f2674bSRichard Henderson }
35264f2674bSRichard Henderson 
353d9bb58e5SYang Zhong void tlb_flush_by_mmuidx_all_cpus(CPUState *src_cpu, uint16_t idxmap)
354d9bb58e5SYang Zhong {
355d9bb58e5SYang Zhong     const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
356d9bb58e5SYang Zhong 
357d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap);
358d9bb58e5SYang Zhong 
359d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
360d9bb58e5SYang Zhong     fn(src_cpu, RUN_ON_CPU_HOST_INT(idxmap));
361d9bb58e5SYang Zhong }
362d9bb58e5SYang Zhong 
36364f2674bSRichard Henderson void tlb_flush_all_cpus(CPUState *src_cpu)
36464f2674bSRichard Henderson {
36564f2674bSRichard Henderson     tlb_flush_by_mmuidx_all_cpus(src_cpu, ALL_MMUIDX_BITS);
36664f2674bSRichard Henderson }
36764f2674bSRichard Henderson 
36864f2674bSRichard Henderson void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu, uint16_t idxmap)
369d9bb58e5SYang Zhong {
370d9bb58e5SYang Zhong     const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
371d9bb58e5SYang Zhong 
372d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap);
373d9bb58e5SYang Zhong 
374d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
375d9bb58e5SYang Zhong     async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
376d9bb58e5SYang Zhong }
377d9bb58e5SYang Zhong 
37864f2674bSRichard Henderson void tlb_flush_all_cpus_synced(CPUState *src_cpu)
37964f2674bSRichard Henderson {
38064f2674bSRichard Henderson     tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS);
38164f2674bSRichard Henderson }
38264f2674bSRichard Henderson 
38368fea038SRichard Henderson static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry,
38468fea038SRichard Henderson                                         target_ulong page)
385d9bb58e5SYang Zhong {
38668fea038SRichard Henderson     return tlb_hit_page(tlb_entry->addr_read, page) ||
387403f290cSEmilio G. Cota            tlb_hit_page(tlb_addr_write(tlb_entry), page) ||
38868fea038SRichard Henderson            tlb_hit_page(tlb_entry->addr_code, page);
38968fea038SRichard Henderson }
39068fea038SRichard Henderson 
3913cea94bbSEmilio G. Cota /**
3923cea94bbSEmilio G. Cota  * tlb_entry_is_empty - return true if the entry is not in use
3933cea94bbSEmilio G. Cota  * @te: pointer to CPUTLBEntry
3943cea94bbSEmilio G. Cota  */
3953cea94bbSEmilio G. Cota static inline bool tlb_entry_is_empty(const CPUTLBEntry *te)
3963cea94bbSEmilio G. Cota {
3973cea94bbSEmilio G. Cota     return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1;
3983cea94bbSEmilio G. Cota }
3993cea94bbSEmilio G. Cota 
40053d28455SRichard Henderson /* Called with tlb_c.lock held */
40186e1eff8SEmilio G. Cota static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry,
40271aec354SEmilio G. Cota                                           target_ulong page)
40368fea038SRichard Henderson {
40468fea038SRichard Henderson     if (tlb_hit_page_anyprot(tlb_entry, page)) {
405d9bb58e5SYang Zhong         memset(tlb_entry, -1, sizeof(*tlb_entry));
40686e1eff8SEmilio G. Cota         return true;
407d9bb58e5SYang Zhong     }
40886e1eff8SEmilio G. Cota     return false;
409d9bb58e5SYang Zhong }
410d9bb58e5SYang Zhong 
41153d28455SRichard Henderson /* Called with tlb_c.lock held */
41271aec354SEmilio G. Cota static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx,
41368fea038SRichard Henderson                                               target_ulong page)
41468fea038SRichard Henderson {
415a40ec84eSRichard Henderson     CPUTLBDesc *d = &env_tlb(env)->d[mmu_idx];
41668fea038SRichard Henderson     int k;
41771aec354SEmilio G. Cota 
41829a0af61SRichard Henderson     assert_cpu_is_self(env_cpu(env));
41968fea038SRichard Henderson     for (k = 0; k < CPU_VTLB_SIZE; k++) {
420a40ec84eSRichard Henderson         if (tlb_flush_entry_locked(&d->vtable[k], page)) {
42186e1eff8SEmilio G. Cota             tlb_n_used_entries_dec(env, mmu_idx);
42286e1eff8SEmilio G. Cota         }
42368fea038SRichard Henderson     }
42468fea038SRichard Henderson }
42568fea038SRichard Henderson 
4261308e026SRichard Henderson static void tlb_flush_page_locked(CPUArchState *env, int midx,
4271308e026SRichard Henderson                                   target_ulong page)
4281308e026SRichard Henderson {
429a40ec84eSRichard Henderson     target_ulong lp_addr = env_tlb(env)->d[midx].large_page_addr;
430a40ec84eSRichard Henderson     target_ulong lp_mask = env_tlb(env)->d[midx].large_page_mask;
4311308e026SRichard Henderson 
4321308e026SRichard Henderson     /* Check if we need to flush due to large pages.  */
4331308e026SRichard Henderson     if ((page & lp_mask) == lp_addr) {
4341308e026SRichard Henderson         tlb_debug("forcing full flush midx %d ("
4351308e026SRichard Henderson                   TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
4361308e026SRichard Henderson                   midx, lp_addr, lp_mask);
4371308e026SRichard Henderson         tlb_flush_one_mmuidx_locked(env, midx);
4381308e026SRichard Henderson     } else {
43986e1eff8SEmilio G. Cota         if (tlb_flush_entry_locked(tlb_entry(env, midx, page), page)) {
44086e1eff8SEmilio G. Cota             tlb_n_used_entries_dec(env, midx);
44186e1eff8SEmilio G. Cota         }
4421308e026SRichard Henderson         tlb_flush_vtlb_page_locked(env, midx, page);
4431308e026SRichard Henderson     }
4441308e026SRichard Henderson }
4451308e026SRichard Henderson 
446d9bb58e5SYang Zhong /* As we are going to hijack the bottom bits of the page address for a
447d9bb58e5SYang Zhong  * mmuidx bit mask we need to fail to build if we can't do that
448d9bb58e5SYang Zhong  */
449d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(NB_MMU_MODES > TARGET_PAGE_BITS_MIN);
450d9bb58e5SYang Zhong 
451d9bb58e5SYang Zhong static void tlb_flush_page_by_mmuidx_async_work(CPUState *cpu,
452d9bb58e5SYang Zhong                                                 run_on_cpu_data data)
453d9bb58e5SYang Zhong {
454d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
455d9bb58e5SYang Zhong     target_ulong addr_and_mmuidx = (target_ulong) data.target_ptr;
456d9bb58e5SYang Zhong     target_ulong addr = addr_and_mmuidx & TARGET_PAGE_MASK;
457d9bb58e5SYang Zhong     unsigned long mmu_idx_bitmap = addr_and_mmuidx & ALL_MMUIDX_BITS;
458d9bb58e5SYang Zhong     int mmu_idx;
459d9bb58e5SYang Zhong 
460d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
461d9bb58e5SYang Zhong 
4621308e026SRichard Henderson     tlb_debug("page addr:" TARGET_FMT_lx " mmu_map:0x%lx\n",
463383beda9SRichard Henderson               addr, mmu_idx_bitmap);
464d9bb58e5SYang Zhong 
465a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
466d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
467d9bb58e5SYang Zhong         if (test_bit(mmu_idx, &mmu_idx_bitmap)) {
4681308e026SRichard Henderson             tlb_flush_page_locked(env, mmu_idx, addr);
469d9bb58e5SYang Zhong         }
470d9bb58e5SYang Zhong     }
471a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
472d9bb58e5SYang Zhong 
473d9bb58e5SYang Zhong     tb_flush_jmp_cache(cpu, addr);
474d9bb58e5SYang Zhong }
475d9bb58e5SYang Zhong 
476d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, uint16_t idxmap)
477d9bb58e5SYang Zhong {
478d9bb58e5SYang Zhong     target_ulong addr_and_mmu_idx;
479d9bb58e5SYang Zhong 
480d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%" PRIx16 "\n", addr, idxmap);
481d9bb58e5SYang Zhong 
482d9bb58e5SYang Zhong     /* This should already be page aligned */
483d9bb58e5SYang Zhong     addr_and_mmu_idx = addr & TARGET_PAGE_MASK;
484d9bb58e5SYang Zhong     addr_and_mmu_idx |= idxmap;
485d9bb58e5SYang Zhong 
486d9bb58e5SYang Zhong     if (!qemu_cpu_is_self(cpu)) {
4871308e026SRichard Henderson         async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_work,
488d9bb58e5SYang Zhong                          RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
489d9bb58e5SYang Zhong     } else {
4901308e026SRichard Henderson         tlb_flush_page_by_mmuidx_async_work(
491d9bb58e5SYang Zhong             cpu, RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
492d9bb58e5SYang Zhong     }
493d9bb58e5SYang Zhong }
494d9bb58e5SYang Zhong 
495f8144c6cSRichard Henderson void tlb_flush_page(CPUState *cpu, target_ulong addr)
496f8144c6cSRichard Henderson {
497f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS);
498f8144c6cSRichard Henderson }
499f8144c6cSRichard Henderson 
500d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, target_ulong addr,
501d9bb58e5SYang Zhong                                        uint16_t idxmap)
502d9bb58e5SYang Zhong {
5031308e026SRichard Henderson     const run_on_cpu_func fn = tlb_flush_page_by_mmuidx_async_work;
504d9bb58e5SYang Zhong     target_ulong addr_and_mmu_idx;
505d9bb58e5SYang Zhong 
506d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap);
507d9bb58e5SYang Zhong 
508d9bb58e5SYang Zhong     /* This should already be page aligned */
509d9bb58e5SYang Zhong     addr_and_mmu_idx = addr & TARGET_PAGE_MASK;
510d9bb58e5SYang Zhong     addr_and_mmu_idx |= idxmap;
511d9bb58e5SYang Zhong 
512d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
513d9bb58e5SYang Zhong     fn(src_cpu, RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
514d9bb58e5SYang Zhong }
515d9bb58e5SYang Zhong 
516f8144c6cSRichard Henderson void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr)
517f8144c6cSRichard Henderson {
518f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus(src, addr, ALL_MMUIDX_BITS);
519f8144c6cSRichard Henderson }
520f8144c6cSRichard Henderson 
521d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
522d9bb58e5SYang Zhong                                               target_ulong addr,
523d9bb58e5SYang Zhong                                               uint16_t idxmap)
524d9bb58e5SYang Zhong {
5251308e026SRichard Henderson     const run_on_cpu_func fn = tlb_flush_page_by_mmuidx_async_work;
526d9bb58e5SYang Zhong     target_ulong addr_and_mmu_idx;
527d9bb58e5SYang Zhong 
528d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap);
529d9bb58e5SYang Zhong 
530d9bb58e5SYang Zhong     /* This should already be page aligned */
531d9bb58e5SYang Zhong     addr_and_mmu_idx = addr & TARGET_PAGE_MASK;
532d9bb58e5SYang Zhong     addr_and_mmu_idx |= idxmap;
533d9bb58e5SYang Zhong 
534d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
535d9bb58e5SYang Zhong     async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_TARGET_PTR(addr_and_mmu_idx));
536d9bb58e5SYang Zhong }
537d9bb58e5SYang Zhong 
538f8144c6cSRichard Henderson void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr)
539d9bb58e5SYang Zhong {
540f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS);
541d9bb58e5SYang Zhong }
542d9bb58e5SYang Zhong 
543d9bb58e5SYang Zhong /* update the TLBs so that writes to code in the virtual page 'addr'
544d9bb58e5SYang Zhong    can be detected */
545d9bb58e5SYang Zhong void tlb_protect_code(ram_addr_t ram_addr)
546d9bb58e5SYang Zhong {
547d9bb58e5SYang Zhong     cpu_physical_memory_test_and_clear_dirty(ram_addr, TARGET_PAGE_SIZE,
548d9bb58e5SYang Zhong                                              DIRTY_MEMORY_CODE);
549d9bb58e5SYang Zhong }
550d9bb58e5SYang Zhong 
551d9bb58e5SYang Zhong /* update the TLB so that writes in physical page 'phys_addr' are no longer
552d9bb58e5SYang Zhong    tested for self modifying code */
553d9bb58e5SYang Zhong void tlb_unprotect_code(ram_addr_t ram_addr)
554d9bb58e5SYang Zhong {
555d9bb58e5SYang Zhong     cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
556d9bb58e5SYang Zhong }
557d9bb58e5SYang Zhong 
558d9bb58e5SYang Zhong 
559d9bb58e5SYang Zhong /*
560d9bb58e5SYang Zhong  * Dirty write flag handling
561d9bb58e5SYang Zhong  *
562d9bb58e5SYang Zhong  * When the TCG code writes to a location it looks up the address in
563d9bb58e5SYang Zhong  * the TLB and uses that data to compute the final address. If any of
564d9bb58e5SYang Zhong  * the lower bits of the address are set then the slow path is forced.
565d9bb58e5SYang Zhong  * There are a number of reasons to do this but for normal RAM the
566d9bb58e5SYang Zhong  * most usual is detecting writes to code regions which may invalidate
567d9bb58e5SYang Zhong  * generated code.
568d9bb58e5SYang Zhong  *
56971aec354SEmilio G. Cota  * Other vCPUs might be reading their TLBs during guest execution, so we update
57071aec354SEmilio G. Cota  * te->addr_write with atomic_set. We don't need to worry about this for
57171aec354SEmilio G. Cota  * oversized guests as MTTCG is disabled for them.
572d9bb58e5SYang Zhong  *
57353d28455SRichard Henderson  * Called with tlb_c.lock held.
574d9bb58e5SYang Zhong  */
57571aec354SEmilio G. Cota static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry,
57671aec354SEmilio G. Cota                                          uintptr_t start, uintptr_t length)
577d9bb58e5SYang Zhong {
578d9bb58e5SYang Zhong     uintptr_t addr = tlb_entry->addr_write;
579d9bb58e5SYang Zhong 
580d9bb58e5SYang Zhong     if ((addr & (TLB_INVALID_MASK | TLB_MMIO | TLB_NOTDIRTY)) == 0) {
581d9bb58e5SYang Zhong         addr &= TARGET_PAGE_MASK;
582d9bb58e5SYang Zhong         addr += tlb_entry->addend;
583d9bb58e5SYang Zhong         if ((addr - start) < length) {
584d9bb58e5SYang Zhong #if TCG_OVERSIZED_GUEST
58571aec354SEmilio G. Cota             tlb_entry->addr_write |= TLB_NOTDIRTY;
586d9bb58e5SYang Zhong #else
58771aec354SEmilio G. Cota             atomic_set(&tlb_entry->addr_write,
58871aec354SEmilio G. Cota                        tlb_entry->addr_write | TLB_NOTDIRTY);
589d9bb58e5SYang Zhong #endif
590d9bb58e5SYang Zhong         }
59171aec354SEmilio G. Cota     }
59271aec354SEmilio G. Cota }
59371aec354SEmilio G. Cota 
59471aec354SEmilio G. Cota /*
59553d28455SRichard Henderson  * Called with tlb_c.lock held.
59671aec354SEmilio G. Cota  * Called only from the vCPU context, i.e. the TLB's owner thread.
59771aec354SEmilio G. Cota  */
59871aec354SEmilio G. Cota static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s)
59971aec354SEmilio G. Cota {
60071aec354SEmilio G. Cota     *d = *s;
60171aec354SEmilio G. Cota }
602d9bb58e5SYang Zhong 
603d9bb58e5SYang Zhong /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
60471aec354SEmilio G. Cota  * the target vCPU).
60553d28455SRichard Henderson  * We must take tlb_c.lock to avoid racing with another vCPU update. The only
60671aec354SEmilio G. Cota  * thing actually updated is the target TLB entry ->addr_write flags.
607d9bb58e5SYang Zhong  */
608d9bb58e5SYang Zhong void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length)
609d9bb58e5SYang Zhong {
610d9bb58e5SYang Zhong     CPUArchState *env;
611d9bb58e5SYang Zhong 
612d9bb58e5SYang Zhong     int mmu_idx;
613d9bb58e5SYang Zhong 
614d9bb58e5SYang Zhong     env = cpu->env_ptr;
615a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
616d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
617d9bb58e5SYang Zhong         unsigned int i;
61886e1eff8SEmilio G. Cota         unsigned int n = tlb_n_entries(env, mmu_idx);
619d9bb58e5SYang Zhong 
62086e1eff8SEmilio G. Cota         for (i = 0; i < n; i++) {
621a40ec84eSRichard Henderson             tlb_reset_dirty_range_locked(&env_tlb(env)->f[mmu_idx].table[i],
622a40ec84eSRichard Henderson                                          start1, length);
623d9bb58e5SYang Zhong         }
624d9bb58e5SYang Zhong 
625d9bb58e5SYang Zhong         for (i = 0; i < CPU_VTLB_SIZE; i++) {
626a40ec84eSRichard Henderson             tlb_reset_dirty_range_locked(&env_tlb(env)->d[mmu_idx].vtable[i],
627a40ec84eSRichard Henderson                                          start1, length);
628d9bb58e5SYang Zhong         }
629d9bb58e5SYang Zhong     }
630a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
631d9bb58e5SYang Zhong }
632d9bb58e5SYang Zhong 
63353d28455SRichard Henderson /* Called with tlb_c.lock held */
63471aec354SEmilio G. Cota static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry,
63571aec354SEmilio G. Cota                                          target_ulong vaddr)
636d9bb58e5SYang Zhong {
637d9bb58e5SYang Zhong     if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) {
638d9bb58e5SYang Zhong         tlb_entry->addr_write = vaddr;
639d9bb58e5SYang Zhong     }
640d9bb58e5SYang Zhong }
641d9bb58e5SYang Zhong 
642d9bb58e5SYang Zhong /* update the TLB corresponding to virtual page vaddr
643d9bb58e5SYang Zhong    so that it is no longer dirty */
644d9bb58e5SYang Zhong void tlb_set_dirty(CPUState *cpu, target_ulong vaddr)
645d9bb58e5SYang Zhong {
646d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
647d9bb58e5SYang Zhong     int mmu_idx;
648d9bb58e5SYang Zhong 
649d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
650d9bb58e5SYang Zhong 
651d9bb58e5SYang Zhong     vaddr &= TARGET_PAGE_MASK;
652a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
653d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
654383beda9SRichard Henderson         tlb_set_dirty1_locked(tlb_entry(env, mmu_idx, vaddr), vaddr);
655d9bb58e5SYang Zhong     }
656d9bb58e5SYang Zhong 
657d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
658d9bb58e5SYang Zhong         int k;
659d9bb58e5SYang Zhong         for (k = 0; k < CPU_VTLB_SIZE; k++) {
660a40ec84eSRichard Henderson             tlb_set_dirty1_locked(&env_tlb(env)->d[mmu_idx].vtable[k], vaddr);
661d9bb58e5SYang Zhong         }
662d9bb58e5SYang Zhong     }
663a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
664d9bb58e5SYang Zhong }
665d9bb58e5SYang Zhong 
666d9bb58e5SYang Zhong /* Our TLB does not support large pages, so remember the area covered by
667d9bb58e5SYang Zhong    large pages and trigger a full TLB flush if these are invalidated.  */
6681308e026SRichard Henderson static void tlb_add_large_page(CPUArchState *env, int mmu_idx,
6691308e026SRichard Henderson                                target_ulong vaddr, target_ulong size)
670d9bb58e5SYang Zhong {
671a40ec84eSRichard Henderson     target_ulong lp_addr = env_tlb(env)->d[mmu_idx].large_page_addr;
6721308e026SRichard Henderson     target_ulong lp_mask = ~(size - 1);
673d9bb58e5SYang Zhong 
6741308e026SRichard Henderson     if (lp_addr == (target_ulong)-1) {
6751308e026SRichard Henderson         /* No previous large page.  */
6761308e026SRichard Henderson         lp_addr = vaddr;
6771308e026SRichard Henderson     } else {
678d9bb58e5SYang Zhong         /* Extend the existing region to include the new page.
6791308e026SRichard Henderson            This is a compromise between unnecessary flushes and
6801308e026SRichard Henderson            the cost of maintaining a full variable size TLB.  */
681a40ec84eSRichard Henderson         lp_mask &= env_tlb(env)->d[mmu_idx].large_page_mask;
6821308e026SRichard Henderson         while (((lp_addr ^ vaddr) & lp_mask) != 0) {
6831308e026SRichard Henderson             lp_mask <<= 1;
684d9bb58e5SYang Zhong         }
6851308e026SRichard Henderson     }
686a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_addr = lp_addr & lp_mask;
687a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_mask = lp_mask;
688d9bb58e5SYang Zhong }
689d9bb58e5SYang Zhong 
690d9bb58e5SYang Zhong /* Add a new TLB entry. At most one entry for a given virtual address
691d9bb58e5SYang Zhong  * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
692d9bb58e5SYang Zhong  * supplied size is only used by tlb_flush_page.
693d9bb58e5SYang Zhong  *
694d9bb58e5SYang Zhong  * Called from TCG-generated code, which is under an RCU read-side
695d9bb58e5SYang Zhong  * critical section.
696d9bb58e5SYang Zhong  */
697d9bb58e5SYang Zhong void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
698d9bb58e5SYang Zhong                              hwaddr paddr, MemTxAttrs attrs, int prot,
699d9bb58e5SYang Zhong                              int mmu_idx, target_ulong size)
700d9bb58e5SYang Zhong {
701d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
702a40ec84eSRichard Henderson     CPUTLB *tlb = env_tlb(env);
703a40ec84eSRichard Henderson     CPUTLBDesc *desc = &tlb->d[mmu_idx];
704d9bb58e5SYang Zhong     MemoryRegionSection *section;
705d9bb58e5SYang Zhong     unsigned int index;
706d9bb58e5SYang Zhong     target_ulong address;
707d9bb58e5SYang Zhong     target_ulong code_address;
708d9bb58e5SYang Zhong     uintptr_t addend;
70968fea038SRichard Henderson     CPUTLBEntry *te, tn;
71055df6fcfSPeter Maydell     hwaddr iotlb, xlat, sz, paddr_page;
71155df6fcfSPeter Maydell     target_ulong vaddr_page;
712d9bb58e5SYang Zhong     int asidx = cpu_asidx_from_attrs(cpu, attrs);
713d9bb58e5SYang Zhong 
714d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
71555df6fcfSPeter Maydell 
7161308e026SRichard Henderson     if (size <= TARGET_PAGE_SIZE) {
71755df6fcfSPeter Maydell         sz = TARGET_PAGE_SIZE;
71855df6fcfSPeter Maydell     } else {
7191308e026SRichard Henderson         tlb_add_large_page(env, mmu_idx, vaddr, size);
720d9bb58e5SYang Zhong         sz = size;
72155df6fcfSPeter Maydell     }
72255df6fcfSPeter Maydell     vaddr_page = vaddr & TARGET_PAGE_MASK;
72355df6fcfSPeter Maydell     paddr_page = paddr & TARGET_PAGE_MASK;
72455df6fcfSPeter Maydell 
72555df6fcfSPeter Maydell     section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
72655df6fcfSPeter Maydell                                                 &xlat, &sz, attrs, &prot);
727d9bb58e5SYang Zhong     assert(sz >= TARGET_PAGE_SIZE);
728d9bb58e5SYang Zhong 
729d9bb58e5SYang Zhong     tlb_debug("vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
730d9bb58e5SYang Zhong               " prot=%x idx=%d\n",
731d9bb58e5SYang Zhong               vaddr, paddr, prot, mmu_idx);
732d9bb58e5SYang Zhong 
73355df6fcfSPeter Maydell     address = vaddr_page;
73455df6fcfSPeter Maydell     if (size < TARGET_PAGE_SIZE) {
73555df6fcfSPeter Maydell         /*
73655df6fcfSPeter Maydell          * Slow-path the TLB entries; we will repeat the MMU check and TLB
73755df6fcfSPeter Maydell          * fill on every access.
73855df6fcfSPeter Maydell          */
73955df6fcfSPeter Maydell         address |= TLB_RECHECK;
74055df6fcfSPeter Maydell     }
741*a26fc6f5STony Nguyen     if (attrs.byte_swap) {
742*a26fc6f5STony Nguyen         /* Force the access through the I/O slow path.  */
743*a26fc6f5STony Nguyen         address |= TLB_MMIO;
744*a26fc6f5STony Nguyen     }
74555df6fcfSPeter Maydell     if (!memory_region_is_ram(section->mr) &&
74655df6fcfSPeter Maydell         !memory_region_is_romd(section->mr)) {
747d9bb58e5SYang Zhong         /* IO memory case */
748d9bb58e5SYang Zhong         address |= TLB_MMIO;
749d9bb58e5SYang Zhong         addend = 0;
750d9bb58e5SYang Zhong     } else {
751d9bb58e5SYang Zhong         /* TLB_MMIO for rom/romd handled below */
752d9bb58e5SYang Zhong         addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
753d9bb58e5SYang Zhong     }
754d9bb58e5SYang Zhong 
755d9bb58e5SYang Zhong     code_address = address;
75655df6fcfSPeter Maydell     iotlb = memory_region_section_get_iotlb(cpu, section, vaddr_page,
75755df6fcfSPeter Maydell                                             paddr_page, xlat, prot, &address);
758d9bb58e5SYang Zhong 
759383beda9SRichard Henderson     index = tlb_index(env, mmu_idx, vaddr_page);
760383beda9SRichard Henderson     te = tlb_entry(env, mmu_idx, vaddr_page);
761d9bb58e5SYang Zhong 
76268fea038SRichard Henderson     /*
76371aec354SEmilio G. Cota      * Hold the TLB lock for the rest of the function. We could acquire/release
76471aec354SEmilio G. Cota      * the lock several times in the function, but it is faster to amortize the
76571aec354SEmilio G. Cota      * acquisition cost by acquiring it just once. Note that this leads to
76671aec354SEmilio G. Cota      * a longer critical section, but this is not a concern since the TLB lock
76771aec354SEmilio G. Cota      * is unlikely to be contended.
76871aec354SEmilio G. Cota      */
769a40ec84eSRichard Henderson     qemu_spin_lock(&tlb->c.lock);
77071aec354SEmilio G. Cota 
7713d1523ceSRichard Henderson     /* Note that the tlb is no longer clean.  */
772a40ec84eSRichard Henderson     tlb->c.dirty |= 1 << mmu_idx;
7733d1523ceSRichard Henderson 
77471aec354SEmilio G. Cota     /* Make sure there's no cached translation for the new page.  */
77571aec354SEmilio G. Cota     tlb_flush_vtlb_page_locked(env, mmu_idx, vaddr_page);
77671aec354SEmilio G. Cota 
77771aec354SEmilio G. Cota     /*
77868fea038SRichard Henderson      * Only evict the old entry to the victim tlb if it's for a
77968fea038SRichard Henderson      * different page; otherwise just overwrite the stale data.
78068fea038SRichard Henderson      */
7813cea94bbSEmilio G. Cota     if (!tlb_hit_page_anyprot(te, vaddr_page) && !tlb_entry_is_empty(te)) {
782a40ec84eSRichard Henderson         unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE;
783a40ec84eSRichard Henderson         CPUTLBEntry *tv = &desc->vtable[vidx];
78468fea038SRichard Henderson 
78568fea038SRichard Henderson         /* Evict the old entry into the victim tlb.  */
78671aec354SEmilio G. Cota         copy_tlb_helper_locked(tv, te);
787a40ec84eSRichard Henderson         desc->viotlb[vidx] = desc->iotlb[index];
78886e1eff8SEmilio G. Cota         tlb_n_used_entries_dec(env, mmu_idx);
78968fea038SRichard Henderson     }
790d9bb58e5SYang Zhong 
791d9bb58e5SYang Zhong     /* refill the tlb */
792ace41090SPeter Maydell     /*
793ace41090SPeter Maydell      * At this point iotlb contains a physical section number in the lower
794ace41090SPeter Maydell      * TARGET_PAGE_BITS, and either
795ace41090SPeter Maydell      *  + the ram_addr_t of the page base of the target RAM (if NOTDIRTY or ROM)
796ace41090SPeter Maydell      *  + the offset within section->mr of the page base (otherwise)
79755df6fcfSPeter Maydell      * We subtract the vaddr_page (which is page aligned and thus won't
798ace41090SPeter Maydell      * disturb the low bits) to give an offset which can be added to the
799ace41090SPeter Maydell      * (non-page-aligned) vaddr of the eventual memory access to get
800ace41090SPeter Maydell      * the MemoryRegion offset for the access. Note that the vaddr we
801ace41090SPeter Maydell      * subtract here is that of the page base, and not the same as the
802ace41090SPeter Maydell      * vaddr we add back in io_readx()/io_writex()/get_page_addr_code().
803ace41090SPeter Maydell      */
804a40ec84eSRichard Henderson     desc->iotlb[index].addr = iotlb - vaddr_page;
805a40ec84eSRichard Henderson     desc->iotlb[index].attrs = attrs;
806d9bb58e5SYang Zhong 
807d9bb58e5SYang Zhong     /* Now calculate the new entry */
80855df6fcfSPeter Maydell     tn.addend = addend - vaddr_page;
809d9bb58e5SYang Zhong     if (prot & PAGE_READ) {
810d9bb58e5SYang Zhong         tn.addr_read = address;
811d9bb58e5SYang Zhong     } else {
812d9bb58e5SYang Zhong         tn.addr_read = -1;
813d9bb58e5SYang Zhong     }
814d9bb58e5SYang Zhong 
815d9bb58e5SYang Zhong     if (prot & PAGE_EXEC) {
816d9bb58e5SYang Zhong         tn.addr_code = code_address;
817d9bb58e5SYang Zhong     } else {
818d9bb58e5SYang Zhong         tn.addr_code = -1;
819d9bb58e5SYang Zhong     }
820d9bb58e5SYang Zhong 
821d9bb58e5SYang Zhong     tn.addr_write = -1;
822d9bb58e5SYang Zhong     if (prot & PAGE_WRITE) {
823d9bb58e5SYang Zhong         if ((memory_region_is_ram(section->mr) && section->readonly)
824d9bb58e5SYang Zhong             || memory_region_is_romd(section->mr)) {
825d9bb58e5SYang Zhong             /* Write access calls the I/O callback.  */
826d9bb58e5SYang Zhong             tn.addr_write = address | TLB_MMIO;
827d9bb58e5SYang Zhong         } else if (memory_region_is_ram(section->mr)
828d9bb58e5SYang Zhong                    && cpu_physical_memory_is_clean(
829d9bb58e5SYang Zhong                        memory_region_get_ram_addr(section->mr) + xlat)) {
830d9bb58e5SYang Zhong             tn.addr_write = address | TLB_NOTDIRTY;
831d9bb58e5SYang Zhong         } else {
832d9bb58e5SYang Zhong             tn.addr_write = address;
833d9bb58e5SYang Zhong         }
834f52bfb12SDavid Hildenbrand         if (prot & PAGE_WRITE_INV) {
835f52bfb12SDavid Hildenbrand             tn.addr_write |= TLB_INVALID_MASK;
836f52bfb12SDavid Hildenbrand         }
837d9bb58e5SYang Zhong     }
838d9bb58e5SYang Zhong 
83971aec354SEmilio G. Cota     copy_tlb_helper_locked(te, &tn);
84086e1eff8SEmilio G. Cota     tlb_n_used_entries_inc(env, mmu_idx);
841a40ec84eSRichard Henderson     qemu_spin_unlock(&tlb->c.lock);
842d9bb58e5SYang Zhong }
843d9bb58e5SYang Zhong 
844d9bb58e5SYang Zhong /* Add a new TLB entry, but without specifying the memory
845d9bb58e5SYang Zhong  * transaction attributes to be used.
846d9bb58e5SYang Zhong  */
847d9bb58e5SYang Zhong void tlb_set_page(CPUState *cpu, target_ulong vaddr,
848d9bb58e5SYang Zhong                   hwaddr paddr, int prot,
849d9bb58e5SYang Zhong                   int mmu_idx, target_ulong size)
850d9bb58e5SYang Zhong {
851d9bb58e5SYang Zhong     tlb_set_page_with_attrs(cpu, vaddr, paddr, MEMTXATTRS_UNSPECIFIED,
852d9bb58e5SYang Zhong                             prot, mmu_idx, size);
853d9bb58e5SYang Zhong }
854d9bb58e5SYang Zhong 
855d9bb58e5SYang Zhong static inline ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
856d9bb58e5SYang Zhong {
857d9bb58e5SYang Zhong     ram_addr_t ram_addr;
858d9bb58e5SYang Zhong 
859d9bb58e5SYang Zhong     ram_addr = qemu_ram_addr_from_host(ptr);
860d9bb58e5SYang Zhong     if (ram_addr == RAM_ADDR_INVALID) {
861d9bb58e5SYang Zhong         error_report("Bad ram pointer %p", ptr);
862d9bb58e5SYang Zhong         abort();
863d9bb58e5SYang Zhong     }
864d9bb58e5SYang Zhong     return ram_addr;
865d9bb58e5SYang Zhong }
866d9bb58e5SYang Zhong 
867c319dc13SRichard Henderson /*
868c319dc13SRichard Henderson  * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
869c319dc13SRichard Henderson  * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
870c319dc13SRichard Henderson  * be discarded and looked up again (e.g. via tlb_entry()).
871c319dc13SRichard Henderson  */
872c319dc13SRichard Henderson static void tlb_fill(CPUState *cpu, target_ulong addr, int size,
873c319dc13SRichard Henderson                      MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
874c319dc13SRichard Henderson {
875c319dc13SRichard Henderson     CPUClass *cc = CPU_GET_CLASS(cpu);
876c319dc13SRichard Henderson     bool ok;
877c319dc13SRichard Henderson 
878c319dc13SRichard Henderson     /*
879c319dc13SRichard Henderson      * This is not a probe, so only valid return is success; failure
880c319dc13SRichard Henderson      * should result in exception + longjmp to the cpu loop.
881c319dc13SRichard Henderson      */
882c319dc13SRichard Henderson     ok = cc->tlb_fill(cpu, addr, size, access_type, mmu_idx, false, retaddr);
883c319dc13SRichard Henderson     assert(ok);
884c319dc13SRichard Henderson }
885c319dc13SRichard Henderson 
886d9bb58e5SYang Zhong static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
887f1be3696SRichard Henderson                          int mmu_idx, target_ulong addr, uintptr_t retaddr,
888be5c4787STony Nguyen                          MMUAccessType access_type, MemOp op)
889d9bb58e5SYang Zhong {
89029a0af61SRichard Henderson     CPUState *cpu = env_cpu(env);
8912d54f194SPeter Maydell     hwaddr mr_offset;
8922d54f194SPeter Maydell     MemoryRegionSection *section;
8932d54f194SPeter Maydell     MemoryRegion *mr;
894d9bb58e5SYang Zhong     uint64_t val;
895d9bb58e5SYang Zhong     bool locked = false;
89604e3aabdSPeter Maydell     MemTxResult r;
897d9bb58e5SYang Zhong 
898*a26fc6f5STony Nguyen     if (iotlbentry->attrs.byte_swap) {
899*a26fc6f5STony Nguyen         op ^= MO_BSWAP;
900*a26fc6f5STony Nguyen     }
901*a26fc6f5STony Nguyen 
9022d54f194SPeter Maydell     section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
9032d54f194SPeter Maydell     mr = section->mr;
9042d54f194SPeter Maydell     mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
905d9bb58e5SYang Zhong     cpu->mem_io_pc = retaddr;
906d9bb58e5SYang Zhong     if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
907d9bb58e5SYang Zhong         cpu_io_recompile(cpu, retaddr);
908d9bb58e5SYang Zhong     }
909d9bb58e5SYang Zhong 
910d9bb58e5SYang Zhong     cpu->mem_io_vaddr = addr;
911dbea78a4SPeter Maydell     cpu->mem_io_access_type = access_type;
912d9bb58e5SYang Zhong 
9138b812533SAlex Bennée     if (mr->global_locking && !qemu_mutex_iothread_locked()) {
914d9bb58e5SYang Zhong         qemu_mutex_lock_iothread();
915d9bb58e5SYang Zhong         locked = true;
916d9bb58e5SYang Zhong     }
917be5c4787STony Nguyen     r = memory_region_dispatch_read(mr, mr_offset, &val, op, iotlbentry->attrs);
91804e3aabdSPeter Maydell     if (r != MEMTX_OK) {
9192d54f194SPeter Maydell         hwaddr physaddr = mr_offset +
9202d54f194SPeter Maydell             section->offset_within_address_space -
9212d54f194SPeter Maydell             section->offset_within_region;
9222d54f194SPeter Maydell 
923be5c4787STony Nguyen         cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), access_type,
92404e3aabdSPeter Maydell                                mmu_idx, iotlbentry->attrs, r, retaddr);
92504e3aabdSPeter Maydell     }
926d9bb58e5SYang Zhong     if (locked) {
927d9bb58e5SYang Zhong         qemu_mutex_unlock_iothread();
928d9bb58e5SYang Zhong     }
929d9bb58e5SYang Zhong 
930d9bb58e5SYang Zhong     return val;
931d9bb58e5SYang Zhong }
932d9bb58e5SYang Zhong 
933d9bb58e5SYang Zhong static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
934f1be3696SRichard Henderson                       int mmu_idx, uint64_t val, target_ulong addr,
935be5c4787STony Nguyen                       uintptr_t retaddr, MemOp op)
936d9bb58e5SYang Zhong {
93729a0af61SRichard Henderson     CPUState *cpu = env_cpu(env);
9382d54f194SPeter Maydell     hwaddr mr_offset;
9392d54f194SPeter Maydell     MemoryRegionSection *section;
9402d54f194SPeter Maydell     MemoryRegion *mr;
941d9bb58e5SYang Zhong     bool locked = false;
94204e3aabdSPeter Maydell     MemTxResult r;
943d9bb58e5SYang Zhong 
944*a26fc6f5STony Nguyen     if (iotlbentry->attrs.byte_swap) {
945*a26fc6f5STony Nguyen         op ^= MO_BSWAP;
946*a26fc6f5STony Nguyen     }
947*a26fc6f5STony Nguyen 
9482d54f194SPeter Maydell     section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
9492d54f194SPeter Maydell     mr = section->mr;
9502d54f194SPeter Maydell     mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
951d9bb58e5SYang Zhong     if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
952d9bb58e5SYang Zhong         cpu_io_recompile(cpu, retaddr);
953d9bb58e5SYang Zhong     }
954d9bb58e5SYang Zhong     cpu->mem_io_vaddr = addr;
955d9bb58e5SYang Zhong     cpu->mem_io_pc = retaddr;
956d9bb58e5SYang Zhong 
9578b812533SAlex Bennée     if (mr->global_locking && !qemu_mutex_iothread_locked()) {
958d9bb58e5SYang Zhong         qemu_mutex_lock_iothread();
959d9bb58e5SYang Zhong         locked = true;
960d9bb58e5SYang Zhong     }
961be5c4787STony Nguyen     r = memory_region_dispatch_write(mr, mr_offset, val, op, iotlbentry->attrs);
96204e3aabdSPeter Maydell     if (r != MEMTX_OK) {
9632d54f194SPeter Maydell         hwaddr physaddr = mr_offset +
9642d54f194SPeter Maydell             section->offset_within_address_space -
9652d54f194SPeter Maydell             section->offset_within_region;
9662d54f194SPeter Maydell 
967be5c4787STony Nguyen         cpu_transaction_failed(cpu, physaddr, addr, memop_size(op),
968be5c4787STony Nguyen                                MMU_DATA_STORE, mmu_idx, iotlbentry->attrs, r,
969be5c4787STony Nguyen                                retaddr);
97004e3aabdSPeter Maydell     }
971d9bb58e5SYang Zhong     if (locked) {
972d9bb58e5SYang Zhong         qemu_mutex_unlock_iothread();
973d9bb58e5SYang Zhong     }
974d9bb58e5SYang Zhong }
975d9bb58e5SYang Zhong 
9764811e909SRichard Henderson static inline target_ulong tlb_read_ofs(CPUTLBEntry *entry, size_t ofs)
9774811e909SRichard Henderson {
9784811e909SRichard Henderson #if TCG_OVERSIZED_GUEST
9794811e909SRichard Henderson     return *(target_ulong *)((uintptr_t)entry + ofs);
9804811e909SRichard Henderson #else
9814811e909SRichard Henderson     /* ofs might correspond to .addr_write, so use atomic_read */
9824811e909SRichard Henderson     return atomic_read((target_ulong *)((uintptr_t)entry + ofs));
9834811e909SRichard Henderson #endif
9844811e909SRichard Henderson }
9854811e909SRichard Henderson 
986d9bb58e5SYang Zhong /* Return true if ADDR is present in the victim tlb, and has been copied
987d9bb58e5SYang Zhong    back to the main tlb.  */
988d9bb58e5SYang Zhong static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index,
989d9bb58e5SYang Zhong                            size_t elt_ofs, target_ulong page)
990d9bb58e5SYang Zhong {
991d9bb58e5SYang Zhong     size_t vidx;
99271aec354SEmilio G. Cota 
99329a0af61SRichard Henderson     assert_cpu_is_self(env_cpu(env));
994d9bb58e5SYang Zhong     for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) {
995a40ec84eSRichard Henderson         CPUTLBEntry *vtlb = &env_tlb(env)->d[mmu_idx].vtable[vidx];
996a40ec84eSRichard Henderson         target_ulong cmp;
997a40ec84eSRichard Henderson 
998a40ec84eSRichard Henderson         /* elt_ofs might correspond to .addr_write, so use atomic_read */
999a40ec84eSRichard Henderson #if TCG_OVERSIZED_GUEST
1000a40ec84eSRichard Henderson         cmp = *(target_ulong *)((uintptr_t)vtlb + elt_ofs);
1001a40ec84eSRichard Henderson #else
1002a40ec84eSRichard Henderson         cmp = atomic_read((target_ulong *)((uintptr_t)vtlb + elt_ofs));
1003a40ec84eSRichard Henderson #endif
1004d9bb58e5SYang Zhong 
1005d9bb58e5SYang Zhong         if (cmp == page) {
1006d9bb58e5SYang Zhong             /* Found entry in victim tlb, swap tlb and iotlb.  */
1007a40ec84eSRichard Henderson             CPUTLBEntry tmptlb, *tlb = &env_tlb(env)->f[mmu_idx].table[index];
1008d9bb58e5SYang Zhong 
1009a40ec84eSRichard Henderson             qemu_spin_lock(&env_tlb(env)->c.lock);
101071aec354SEmilio G. Cota             copy_tlb_helper_locked(&tmptlb, tlb);
101171aec354SEmilio G. Cota             copy_tlb_helper_locked(tlb, vtlb);
101271aec354SEmilio G. Cota             copy_tlb_helper_locked(vtlb, &tmptlb);
1013a40ec84eSRichard Henderson             qemu_spin_unlock(&env_tlb(env)->c.lock);
1014d9bb58e5SYang Zhong 
1015a40ec84eSRichard Henderson             CPUIOTLBEntry tmpio, *io = &env_tlb(env)->d[mmu_idx].iotlb[index];
1016a40ec84eSRichard Henderson             CPUIOTLBEntry *vio = &env_tlb(env)->d[mmu_idx].viotlb[vidx];
1017d9bb58e5SYang Zhong             tmpio = *io; *io = *vio; *vio = tmpio;
1018d9bb58e5SYang Zhong             return true;
1019d9bb58e5SYang Zhong         }
1020d9bb58e5SYang Zhong     }
1021d9bb58e5SYang Zhong     return false;
1022d9bb58e5SYang Zhong }
1023d9bb58e5SYang Zhong 
1024d9bb58e5SYang Zhong /* Macro to call the above, with local variables from the use context.  */
1025d9bb58e5SYang Zhong #define VICTIM_TLB_HIT(TY, ADDR) \
1026d9bb58e5SYang Zhong   victim_tlb_hit(env, mmu_idx, index, offsetof(CPUTLBEntry, TY), \
1027d9bb58e5SYang Zhong                  (ADDR) & TARGET_PAGE_MASK)
1028d9bb58e5SYang Zhong 
1029f2553f04SKONRAD Frederic /* NOTE: this function can trigger an exception */
1030f2553f04SKONRAD Frederic /* NOTE2: the returned address is not exactly the physical address: it
1031f2553f04SKONRAD Frederic  * is actually a ram_addr_t (in system mode; the user mode emulation
1032f2553f04SKONRAD Frederic  * version of this function returns a guest virtual address).
1033f2553f04SKONRAD Frederic  */
1034f2553f04SKONRAD Frederic tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)
1035f2553f04SKONRAD Frederic {
1036383beda9SRichard Henderson     uintptr_t mmu_idx = cpu_mmu_index(env, true);
1037383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1038383beda9SRichard Henderson     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1039f2553f04SKONRAD Frederic     void *p;
1040f2553f04SKONRAD Frederic 
1041383beda9SRichard Henderson     if (unlikely(!tlb_hit(entry->addr_code, addr))) {
1042b493ccf1SPeter Maydell         if (!VICTIM_TLB_HIT(addr_code, addr)) {
104329a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, 0, MMU_INST_FETCH, mmu_idx, 0);
10446d967cb8SEmilio G. Cota             index = tlb_index(env, mmu_idx, addr);
10456d967cb8SEmilio G. Cota             entry = tlb_entry(env, mmu_idx, addr);
104671b9a453SKONRAD Frederic         }
1047383beda9SRichard Henderson         assert(tlb_hit(entry->addr_code, addr));
1048f2553f04SKONRAD Frederic     }
104955df6fcfSPeter Maydell 
1050383beda9SRichard Henderson     if (unlikely(entry->addr_code & (TLB_RECHECK | TLB_MMIO))) {
105155df6fcfSPeter Maydell         /*
105255a7cb14SPeter Maydell          * Return -1 if we can't translate and execute from an entire
105355a7cb14SPeter Maydell          * page of RAM here, which will cause us to execute by loading
105455a7cb14SPeter Maydell          * and translating one insn at a time, without caching:
105555a7cb14SPeter Maydell          *  - TLB_RECHECK: means the MMU protection covers a smaller range
105655a7cb14SPeter Maydell          *    than a target page, so we must redo the MMU check every insn
105755a7cb14SPeter Maydell          *  - TLB_MMIO: region is not backed by RAM
105855df6fcfSPeter Maydell          */
105920cb6ae4SPeter Maydell         return -1;
106055df6fcfSPeter Maydell     }
106155df6fcfSPeter Maydell 
1062383beda9SRichard Henderson     p = (void *)((uintptr_t)addr + entry->addend);
1063f2553f04SKONRAD Frederic     return qemu_ram_addr_from_host_nofail(p);
1064f2553f04SKONRAD Frederic }
1065f2553f04SKONRAD Frederic 
1066d9bb58e5SYang Zhong /* Probe for whether the specified guest write access is permitted.
1067d9bb58e5SYang Zhong  * If it is not permitted then an exception will be taken in the same
1068d9bb58e5SYang Zhong  * way as if this were a real write access (and we will not return).
1069d9bb58e5SYang Zhong  * Otherwise the function will return, and there will be a valid
1070d9bb58e5SYang Zhong  * entry in the TLB for this access.
1071d9bb58e5SYang Zhong  */
107298670d47SLaurent Vivier void probe_write(CPUArchState *env, target_ulong addr, int size, int mmu_idx,
1073d9bb58e5SYang Zhong                  uintptr_t retaddr)
1074d9bb58e5SYang Zhong {
1075383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1076383beda9SRichard Henderson     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1077d9bb58e5SYang Zhong 
1078403f290cSEmilio G. Cota     if (!tlb_hit(tlb_addr_write(entry), addr)) {
1079d9bb58e5SYang Zhong         /* TLB entry is for a different page */
1080d9bb58e5SYang Zhong         if (!VICTIM_TLB_HIT(addr_write, addr)) {
108129a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size, MMU_DATA_STORE,
108298670d47SLaurent Vivier                      mmu_idx, retaddr);
1083d9bb58e5SYang Zhong         }
1084d9bb58e5SYang Zhong     }
1085d9bb58e5SYang Zhong }
1086d9bb58e5SYang Zhong 
10874811e909SRichard Henderson void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
10884811e909SRichard Henderson                         MMUAccessType access_type, int mmu_idx)
10894811e909SRichard Henderson {
10904811e909SRichard Henderson     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
10914811e909SRichard Henderson     uintptr_t tlb_addr, page;
10924811e909SRichard Henderson     size_t elt_ofs;
10934811e909SRichard Henderson 
10944811e909SRichard Henderson     switch (access_type) {
10954811e909SRichard Henderson     case MMU_DATA_LOAD:
10964811e909SRichard Henderson         elt_ofs = offsetof(CPUTLBEntry, addr_read);
10974811e909SRichard Henderson         break;
10984811e909SRichard Henderson     case MMU_DATA_STORE:
10994811e909SRichard Henderson         elt_ofs = offsetof(CPUTLBEntry, addr_write);
11004811e909SRichard Henderson         break;
11014811e909SRichard Henderson     case MMU_INST_FETCH:
11024811e909SRichard Henderson         elt_ofs = offsetof(CPUTLBEntry, addr_code);
11034811e909SRichard Henderson         break;
11044811e909SRichard Henderson     default:
11054811e909SRichard Henderson         g_assert_not_reached();
11064811e909SRichard Henderson     }
11074811e909SRichard Henderson 
11084811e909SRichard Henderson     page = addr & TARGET_PAGE_MASK;
11094811e909SRichard Henderson     tlb_addr = tlb_read_ofs(entry, elt_ofs);
11104811e909SRichard Henderson 
11114811e909SRichard Henderson     if (!tlb_hit_page(tlb_addr, page)) {
11124811e909SRichard Henderson         uintptr_t index = tlb_index(env, mmu_idx, addr);
11134811e909SRichard Henderson 
11144811e909SRichard Henderson         if (!victim_tlb_hit(env, mmu_idx, index, elt_ofs, page)) {
111529a0af61SRichard Henderson             CPUState *cs = env_cpu(env);
11164811e909SRichard Henderson             CPUClass *cc = CPU_GET_CLASS(cs);
11174811e909SRichard Henderson 
11184811e909SRichard Henderson             if (!cc->tlb_fill(cs, addr, 0, access_type, mmu_idx, true, 0)) {
11194811e909SRichard Henderson                 /* Non-faulting page table read failed.  */
11204811e909SRichard Henderson                 return NULL;
11214811e909SRichard Henderson             }
11224811e909SRichard Henderson 
11234811e909SRichard Henderson             /* TLB resize via tlb_fill may have moved the entry.  */
11244811e909SRichard Henderson             entry = tlb_entry(env, mmu_idx, addr);
11254811e909SRichard Henderson         }
11264811e909SRichard Henderson         tlb_addr = tlb_read_ofs(entry, elt_ofs);
11274811e909SRichard Henderson     }
11284811e909SRichard Henderson 
11294811e909SRichard Henderson     if (tlb_addr & ~TARGET_PAGE_MASK) {
11304811e909SRichard Henderson         /* IO access */
11314811e909SRichard Henderson         return NULL;
11324811e909SRichard Henderson     }
11334811e909SRichard Henderson 
11344811e909SRichard Henderson     return (void *)((uintptr_t)addr + entry->addend);
11354811e909SRichard Henderson }
11364811e909SRichard Henderson 
1137d9bb58e5SYang Zhong /* Probe for a read-modify-write atomic operation.  Do not allow unaligned
1138d9bb58e5SYang Zhong  * operations, or io operations to proceed.  Return the host address.  */
1139d9bb58e5SYang Zhong static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
114034d49937SPeter Maydell                                TCGMemOpIdx oi, uintptr_t retaddr,
114134d49937SPeter Maydell                                NotDirtyInfo *ndi)
1142d9bb58e5SYang Zhong {
1143d9bb58e5SYang Zhong     size_t mmu_idx = get_mmuidx(oi);
1144383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1145383beda9SRichard Henderson     CPUTLBEntry *tlbe = tlb_entry(env, mmu_idx, addr);
1146403f290cSEmilio G. Cota     target_ulong tlb_addr = tlb_addr_write(tlbe);
114714776ab5STony Nguyen     MemOp mop = get_memop(oi);
1148d9bb58e5SYang Zhong     int a_bits = get_alignment_bits(mop);
1149d9bb58e5SYang Zhong     int s_bits = mop & MO_SIZE;
115034d49937SPeter Maydell     void *hostaddr;
1151d9bb58e5SYang Zhong 
1152d9bb58e5SYang Zhong     /* Adjust the given return address.  */
1153d9bb58e5SYang Zhong     retaddr -= GETPC_ADJ;
1154d9bb58e5SYang Zhong 
1155d9bb58e5SYang Zhong     /* Enforce guest required alignment.  */
1156d9bb58e5SYang Zhong     if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) {
1157d9bb58e5SYang Zhong         /* ??? Maybe indicate atomic op to cpu_unaligned_access */
115829a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE,
1159d9bb58e5SYang Zhong                              mmu_idx, retaddr);
1160d9bb58e5SYang Zhong     }
1161d9bb58e5SYang Zhong 
1162d9bb58e5SYang Zhong     /* Enforce qemu required alignment.  */
1163d9bb58e5SYang Zhong     if (unlikely(addr & ((1 << s_bits) - 1))) {
1164d9bb58e5SYang Zhong         /* We get here if guest alignment was not requested,
1165d9bb58e5SYang Zhong            or was not enforced by cpu_unaligned_access above.
1166d9bb58e5SYang Zhong            We might widen the access and emulate, but for now
1167d9bb58e5SYang Zhong            mark an exception and exit the cpu loop.  */
1168d9bb58e5SYang Zhong         goto stop_the_world;
1169d9bb58e5SYang Zhong     }
1170d9bb58e5SYang Zhong 
1171d9bb58e5SYang Zhong     /* Check TLB entry and enforce page permissions.  */
1172334692bcSPeter Maydell     if (!tlb_hit(tlb_addr, addr)) {
1173d9bb58e5SYang Zhong         if (!VICTIM_TLB_HIT(addr_write, addr)) {
117429a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, 1 << s_bits, MMU_DATA_STORE,
117598670d47SLaurent Vivier                      mmu_idx, retaddr);
11766d967cb8SEmilio G. Cota             index = tlb_index(env, mmu_idx, addr);
11776d967cb8SEmilio G. Cota             tlbe = tlb_entry(env, mmu_idx, addr);
1178d9bb58e5SYang Zhong         }
1179403f290cSEmilio G. Cota         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
1180d9bb58e5SYang Zhong     }
1181d9bb58e5SYang Zhong 
118255df6fcfSPeter Maydell     /* Notice an IO access or a needs-MMU-lookup access */
118355df6fcfSPeter Maydell     if (unlikely(tlb_addr & (TLB_MMIO | TLB_RECHECK))) {
1184d9bb58e5SYang Zhong         /* There's really nothing that can be done to
1185d9bb58e5SYang Zhong            support this apart from stop-the-world.  */
1186d9bb58e5SYang Zhong         goto stop_the_world;
1187d9bb58e5SYang Zhong     }
1188d9bb58e5SYang Zhong 
1189d9bb58e5SYang Zhong     /* Let the guest notice RMW on a write-only page.  */
119034d49937SPeter Maydell     if (unlikely(tlbe->addr_read != (tlb_addr & ~TLB_NOTDIRTY))) {
119129a0af61SRichard Henderson         tlb_fill(env_cpu(env), addr, 1 << s_bits, MMU_DATA_LOAD,
119298670d47SLaurent Vivier                  mmu_idx, retaddr);
1193d9bb58e5SYang Zhong         /* Since we don't support reads and writes to different addresses,
1194d9bb58e5SYang Zhong            and we do have the proper page loaded for write, this shouldn't
1195d9bb58e5SYang Zhong            ever return.  But just in case, handle via stop-the-world.  */
1196d9bb58e5SYang Zhong         goto stop_the_world;
1197d9bb58e5SYang Zhong     }
1198d9bb58e5SYang Zhong 
119934d49937SPeter Maydell     hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
120034d49937SPeter Maydell 
120134d49937SPeter Maydell     ndi->active = false;
120234d49937SPeter Maydell     if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
120334d49937SPeter Maydell         ndi->active = true;
120429a0af61SRichard Henderson         memory_notdirty_write_prepare(ndi, env_cpu(env), addr,
120534d49937SPeter Maydell                                       qemu_ram_addr_from_host_nofail(hostaddr),
120634d49937SPeter Maydell                                       1 << s_bits);
120734d49937SPeter Maydell     }
120834d49937SPeter Maydell 
120934d49937SPeter Maydell     return hostaddr;
1210d9bb58e5SYang Zhong 
1211d9bb58e5SYang Zhong  stop_the_world:
121229a0af61SRichard Henderson     cpu_loop_exit_atomic(env_cpu(env), retaddr);
1213d9bb58e5SYang Zhong }
1214d9bb58e5SYang Zhong 
1215eed56642SAlex Bennée /*
1216eed56642SAlex Bennée  * Load Helpers
1217eed56642SAlex Bennée  *
1218eed56642SAlex Bennée  * We support two different access types. SOFTMMU_CODE_ACCESS is
1219eed56642SAlex Bennée  * specifically for reading instructions from system memory. It is
1220eed56642SAlex Bennée  * called by the translation loop and in some helpers where the code
1221eed56642SAlex Bennée  * is disassembled. It shouldn't be called directly by guest code.
1222eed56642SAlex Bennée  */
1223d9bb58e5SYang Zhong 
12242dd92606SRichard Henderson typedef uint64_t FullLoadHelper(CPUArchState *env, target_ulong addr,
12252dd92606SRichard Henderson                                 TCGMemOpIdx oi, uintptr_t retaddr);
12262dd92606SRichard Henderson 
12272dd92606SRichard Henderson static inline uint64_t __attribute__((always_inline))
12282dd92606SRichard Henderson load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
1229be5c4787STony Nguyen             uintptr_t retaddr, MemOp op, bool code_read,
12302dd92606SRichard Henderson             FullLoadHelper *full_load)
1231eed56642SAlex Bennée {
1232eed56642SAlex Bennée     uintptr_t mmu_idx = get_mmuidx(oi);
1233eed56642SAlex Bennée     uintptr_t index = tlb_index(env, mmu_idx, addr);
1234eed56642SAlex Bennée     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1235eed56642SAlex Bennée     target_ulong tlb_addr = code_read ? entry->addr_code : entry->addr_read;
1236eed56642SAlex Bennée     const size_t tlb_off = code_read ?
1237eed56642SAlex Bennée         offsetof(CPUTLBEntry, addr_code) : offsetof(CPUTLBEntry, addr_read);
1238f1be3696SRichard Henderson     const MMUAccessType access_type =
1239f1be3696SRichard Henderson         code_read ? MMU_INST_FETCH : MMU_DATA_LOAD;
1240eed56642SAlex Bennée     unsigned a_bits = get_alignment_bits(get_memop(oi));
1241eed56642SAlex Bennée     void *haddr;
1242eed56642SAlex Bennée     uint64_t res;
1243be5c4787STony Nguyen     size_t size = memop_size(op);
1244d9bb58e5SYang Zhong 
1245eed56642SAlex Bennée     /* Handle CPU specific unaligned behaviour */
1246eed56642SAlex Bennée     if (addr & ((1 << a_bits) - 1)) {
124729a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, access_type,
1248eed56642SAlex Bennée                              mmu_idx, retaddr);
1249eed56642SAlex Bennée     }
1250eed56642SAlex Bennée 
1251eed56642SAlex Bennée     /* If the TLB entry is for a different page, reload and try again.  */
1252eed56642SAlex Bennée     if (!tlb_hit(tlb_addr, addr)) {
1253eed56642SAlex Bennée         if (!victim_tlb_hit(env, mmu_idx, index, tlb_off,
1254eed56642SAlex Bennée                             addr & TARGET_PAGE_MASK)) {
125529a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size,
1256f1be3696SRichard Henderson                      access_type, mmu_idx, retaddr);
1257eed56642SAlex Bennée             index = tlb_index(env, mmu_idx, addr);
1258eed56642SAlex Bennée             entry = tlb_entry(env, mmu_idx, addr);
1259eed56642SAlex Bennée         }
1260eed56642SAlex Bennée         tlb_addr = code_read ? entry->addr_code : entry->addr_read;
1261eed56642SAlex Bennée     }
1262eed56642SAlex Bennée 
1263eed56642SAlex Bennée     /* Handle an IO access.  */
1264eed56642SAlex Bennée     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
1265eed56642SAlex Bennée         if ((addr & (size - 1)) != 0) {
1266eed56642SAlex Bennée             goto do_unaligned_access;
1267eed56642SAlex Bennée         }
1268eed56642SAlex Bennée 
1269f1be3696SRichard Henderson         if (tlb_addr & TLB_RECHECK) {
1270f1be3696SRichard Henderson             /*
1271f1be3696SRichard Henderson              * This is a TLB_RECHECK access, where the MMU protection
1272f1be3696SRichard Henderson              * covers a smaller range than a target page, and we must
1273f1be3696SRichard Henderson              * repeat the MMU check here. This tlb_fill() call might
1274f1be3696SRichard Henderson              * longjump out if this access should cause a guest exception.
1275f1be3696SRichard Henderson              */
127629a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size,
1277f1be3696SRichard Henderson                      access_type, mmu_idx, retaddr);
1278f1be3696SRichard Henderson             index = tlb_index(env, mmu_idx, addr);
1279f1be3696SRichard Henderson             entry = tlb_entry(env, mmu_idx, addr);
1280f1be3696SRichard Henderson 
1281f1be3696SRichard Henderson             tlb_addr = code_read ? entry->addr_code : entry->addr_read;
1282f1be3696SRichard Henderson             tlb_addr &= ~TLB_RECHECK;
1283f1be3696SRichard Henderson             if (!(tlb_addr & ~TARGET_PAGE_MASK)) {
1284f1be3696SRichard Henderson                 /* RAM access */
1285f1be3696SRichard Henderson                 goto do_aligned_access;
1286f1be3696SRichard Henderson             }
1287f1be3696SRichard Henderson         }
1288f1be3696SRichard Henderson 
12899bf825bfSTony Nguyen         return io_readx(env, &env_tlb(env)->d[mmu_idx].iotlb[index],
1290be5c4787STony Nguyen                         mmu_idx, addr, retaddr, access_type, op);
1291eed56642SAlex Bennée     }
1292eed56642SAlex Bennée 
1293eed56642SAlex Bennée     /* Handle slow unaligned access (it spans two pages or IO).  */
1294eed56642SAlex Bennée     if (size > 1
1295eed56642SAlex Bennée         && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1
1296eed56642SAlex Bennée                     >= TARGET_PAGE_SIZE)) {
1297eed56642SAlex Bennée         target_ulong addr1, addr2;
12988c79b288SAlex Bennée         uint64_t r1, r2;
1299eed56642SAlex Bennée         unsigned shift;
1300eed56642SAlex Bennée     do_unaligned_access:
1301ab7a2009SAlex Bennée         addr1 = addr & ~((target_ulong)size - 1);
1302eed56642SAlex Bennée         addr2 = addr1 + size;
13032dd92606SRichard Henderson         r1 = full_load(env, addr1, oi, retaddr);
13042dd92606SRichard Henderson         r2 = full_load(env, addr2, oi, retaddr);
1305eed56642SAlex Bennée         shift = (addr & (size - 1)) * 8;
1306eed56642SAlex Bennée 
1307be5c4787STony Nguyen         if (memop_big_endian(op)) {
1308eed56642SAlex Bennée             /* Big-endian combine.  */
1309eed56642SAlex Bennée             res = (r1 << shift) | (r2 >> ((size * 8) - shift));
1310eed56642SAlex Bennée         } else {
1311eed56642SAlex Bennée             /* Little-endian combine.  */
1312eed56642SAlex Bennée             res = (r1 >> shift) | (r2 << ((size * 8) - shift));
1313eed56642SAlex Bennée         }
1314eed56642SAlex Bennée         return res & MAKE_64BIT_MASK(0, size * 8);
1315eed56642SAlex Bennée     }
1316eed56642SAlex Bennée 
1317f1be3696SRichard Henderson  do_aligned_access:
1318eed56642SAlex Bennée     haddr = (void *)((uintptr_t)addr + entry->addend);
1319be5c4787STony Nguyen     switch (op) {
1320be5c4787STony Nguyen     case MO_UB:
1321eed56642SAlex Bennée         res = ldub_p(haddr);
1322eed56642SAlex Bennée         break;
1323be5c4787STony Nguyen     case MO_BEUW:
1324eed56642SAlex Bennée         res = lduw_be_p(haddr);
1325be5c4787STony Nguyen         break;
1326be5c4787STony Nguyen     case MO_LEUW:
1327eed56642SAlex Bennée         res = lduw_le_p(haddr);
1328eed56642SAlex Bennée         break;
1329be5c4787STony Nguyen     case MO_BEUL:
1330eed56642SAlex Bennée         res = (uint32_t)ldl_be_p(haddr);
1331eed56642SAlex Bennée         break;
1332be5c4787STony Nguyen     case MO_LEUL:
1333be5c4787STony Nguyen         res = (uint32_t)ldl_le_p(haddr);
1334be5c4787STony Nguyen         break;
1335be5c4787STony Nguyen     case MO_BEQ:
1336eed56642SAlex Bennée         res = ldq_be_p(haddr);
1337be5c4787STony Nguyen         break;
1338be5c4787STony Nguyen     case MO_LEQ:
1339eed56642SAlex Bennée         res = ldq_le_p(haddr);
1340eed56642SAlex Bennée         break;
1341eed56642SAlex Bennée     default:
1342eed56642SAlex Bennée         g_assert_not_reached();
1343eed56642SAlex Bennée     }
1344eed56642SAlex Bennée 
1345eed56642SAlex Bennée     return res;
1346eed56642SAlex Bennée }
1347eed56642SAlex Bennée 
1348eed56642SAlex Bennée /*
1349eed56642SAlex Bennée  * For the benefit of TCG generated code, we want to avoid the
1350eed56642SAlex Bennée  * complication of ABI-specific return type promotion and always
1351eed56642SAlex Bennée  * return a value extended to the register size of the host. This is
1352eed56642SAlex Bennée  * tcg_target_long, except in the case of a 32-bit host and 64-bit
1353eed56642SAlex Bennée  * data, and for that we always have uint64_t.
1354eed56642SAlex Bennée  *
1355eed56642SAlex Bennée  * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1356eed56642SAlex Bennée  */
1357eed56642SAlex Bennée 
13582dd92606SRichard Henderson static uint64_t full_ldub_mmu(CPUArchState *env, target_ulong addr,
13592dd92606SRichard Henderson                               TCGMemOpIdx oi, uintptr_t retaddr)
13602dd92606SRichard Henderson {
1361be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_UB, false, full_ldub_mmu);
13622dd92606SRichard Henderson }
13632dd92606SRichard Henderson 
1364fc1bc777SRichard Henderson tcg_target_ulong helper_ret_ldub_mmu(CPUArchState *env, target_ulong addr,
1365fc1bc777SRichard Henderson                                      TCGMemOpIdx oi, uintptr_t retaddr)
1366eed56642SAlex Bennée {
13672dd92606SRichard Henderson     return full_ldub_mmu(env, addr, oi, retaddr);
13682dd92606SRichard Henderson }
13692dd92606SRichard Henderson 
13702dd92606SRichard Henderson static uint64_t full_le_lduw_mmu(CPUArchState *env, target_ulong addr,
13712dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
13722dd92606SRichard Henderson {
1373be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUW, false,
13742dd92606SRichard Henderson                        full_le_lduw_mmu);
1375eed56642SAlex Bennée }
1376eed56642SAlex Bennée 
1377fc1bc777SRichard Henderson tcg_target_ulong helper_le_lduw_mmu(CPUArchState *env, target_ulong addr,
1378fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1379eed56642SAlex Bennée {
13802dd92606SRichard Henderson     return full_le_lduw_mmu(env, addr, oi, retaddr);
13812dd92606SRichard Henderson }
13822dd92606SRichard Henderson 
13832dd92606SRichard Henderson static uint64_t full_be_lduw_mmu(CPUArchState *env, target_ulong addr,
13842dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
13852dd92606SRichard Henderson {
1386be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUW, false,
13872dd92606SRichard Henderson                        full_be_lduw_mmu);
1388eed56642SAlex Bennée }
1389eed56642SAlex Bennée 
1390fc1bc777SRichard Henderson tcg_target_ulong helper_be_lduw_mmu(CPUArchState *env, target_ulong addr,
1391fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1392eed56642SAlex Bennée {
13932dd92606SRichard Henderson     return full_be_lduw_mmu(env, addr, oi, retaddr);
13942dd92606SRichard Henderson }
13952dd92606SRichard Henderson 
13962dd92606SRichard Henderson static uint64_t full_le_ldul_mmu(CPUArchState *env, target_ulong addr,
13972dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
13982dd92606SRichard Henderson {
1399be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUL, false,
14002dd92606SRichard Henderson                        full_le_ldul_mmu);
1401eed56642SAlex Bennée }
1402eed56642SAlex Bennée 
1403fc1bc777SRichard Henderson tcg_target_ulong helper_le_ldul_mmu(CPUArchState *env, target_ulong addr,
1404fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1405eed56642SAlex Bennée {
14062dd92606SRichard Henderson     return full_le_ldul_mmu(env, addr, oi, retaddr);
14072dd92606SRichard Henderson }
14082dd92606SRichard Henderson 
14092dd92606SRichard Henderson static uint64_t full_be_ldul_mmu(CPUArchState *env, target_ulong addr,
14102dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
14112dd92606SRichard Henderson {
1412be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUL, false,
14132dd92606SRichard Henderson                        full_be_ldul_mmu);
1414eed56642SAlex Bennée }
1415eed56642SAlex Bennée 
1416fc1bc777SRichard Henderson tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr,
1417fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1418eed56642SAlex Bennée {
14192dd92606SRichard Henderson     return full_be_ldul_mmu(env, addr, oi, retaddr);
1420eed56642SAlex Bennée }
1421eed56642SAlex Bennée 
1422fc1bc777SRichard Henderson uint64_t helper_le_ldq_mmu(CPUArchState *env, target_ulong addr,
1423fc1bc777SRichard Henderson                            TCGMemOpIdx oi, uintptr_t retaddr)
1424eed56642SAlex Bennée {
1425be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEQ, false,
14262dd92606SRichard Henderson                        helper_le_ldq_mmu);
1427eed56642SAlex Bennée }
1428eed56642SAlex Bennée 
1429fc1bc777SRichard Henderson uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr,
1430fc1bc777SRichard Henderson                            TCGMemOpIdx oi, uintptr_t retaddr)
1431eed56642SAlex Bennée {
1432be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEQ, false,
14332dd92606SRichard Henderson                        helper_be_ldq_mmu);
1434eed56642SAlex Bennée }
1435eed56642SAlex Bennée 
1436eed56642SAlex Bennée /*
1437eed56642SAlex Bennée  * Provide signed versions of the load routines as well.  We can of course
1438eed56642SAlex Bennée  * avoid this for 64-bit data, or for 32-bit data on 32-bit host.
1439eed56642SAlex Bennée  */
1440eed56642SAlex Bennée 
1441eed56642SAlex Bennée 
1442eed56642SAlex Bennée tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr,
1443eed56642SAlex Bennée                                      TCGMemOpIdx oi, uintptr_t retaddr)
1444eed56642SAlex Bennée {
1445eed56642SAlex Bennée     return (int8_t)helper_ret_ldub_mmu(env, addr, oi, retaddr);
1446eed56642SAlex Bennée }
1447eed56642SAlex Bennée 
1448eed56642SAlex Bennée tcg_target_ulong helper_le_ldsw_mmu(CPUArchState *env, target_ulong addr,
1449eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1450eed56642SAlex Bennée {
1451eed56642SAlex Bennée     return (int16_t)helper_le_lduw_mmu(env, addr, oi, retaddr);
1452eed56642SAlex Bennée }
1453eed56642SAlex Bennée 
1454eed56642SAlex Bennée tcg_target_ulong helper_be_ldsw_mmu(CPUArchState *env, target_ulong addr,
1455eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1456eed56642SAlex Bennée {
1457eed56642SAlex Bennée     return (int16_t)helper_be_lduw_mmu(env, addr, oi, retaddr);
1458eed56642SAlex Bennée }
1459eed56642SAlex Bennée 
1460eed56642SAlex Bennée tcg_target_ulong helper_le_ldsl_mmu(CPUArchState *env, target_ulong addr,
1461eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1462eed56642SAlex Bennée {
1463eed56642SAlex Bennée     return (int32_t)helper_le_ldul_mmu(env, addr, oi, retaddr);
1464eed56642SAlex Bennée }
1465eed56642SAlex Bennée 
1466eed56642SAlex Bennée tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr,
1467eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1468eed56642SAlex Bennée {
1469eed56642SAlex Bennée     return (int32_t)helper_be_ldul_mmu(env, addr, oi, retaddr);
1470eed56642SAlex Bennée }
1471eed56642SAlex Bennée 
1472eed56642SAlex Bennée /*
1473eed56642SAlex Bennée  * Store Helpers
1474eed56642SAlex Bennée  */
1475eed56642SAlex Bennée 
14764601f8d1SRichard Henderson static inline void __attribute__((always_inline))
14774601f8d1SRichard Henderson store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
1478be5c4787STony Nguyen              TCGMemOpIdx oi, uintptr_t retaddr, MemOp op)
1479eed56642SAlex Bennée {
1480eed56642SAlex Bennée     uintptr_t mmu_idx = get_mmuidx(oi);
1481eed56642SAlex Bennée     uintptr_t index = tlb_index(env, mmu_idx, addr);
1482eed56642SAlex Bennée     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1483eed56642SAlex Bennée     target_ulong tlb_addr = tlb_addr_write(entry);
1484eed56642SAlex Bennée     const size_t tlb_off = offsetof(CPUTLBEntry, addr_write);
1485eed56642SAlex Bennée     unsigned a_bits = get_alignment_bits(get_memop(oi));
1486eed56642SAlex Bennée     void *haddr;
1487be5c4787STony Nguyen     size_t size = memop_size(op);
1488eed56642SAlex Bennée 
1489eed56642SAlex Bennée     /* Handle CPU specific unaligned behaviour */
1490eed56642SAlex Bennée     if (addr & ((1 << a_bits) - 1)) {
149129a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE,
1492eed56642SAlex Bennée                              mmu_idx, retaddr);
1493eed56642SAlex Bennée     }
1494eed56642SAlex Bennée 
1495eed56642SAlex Bennée     /* If the TLB entry is for a different page, reload and try again.  */
1496eed56642SAlex Bennée     if (!tlb_hit(tlb_addr, addr)) {
1497eed56642SAlex Bennée         if (!victim_tlb_hit(env, mmu_idx, index, tlb_off,
1498eed56642SAlex Bennée             addr & TARGET_PAGE_MASK)) {
149929a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size, MMU_DATA_STORE,
1500eed56642SAlex Bennée                      mmu_idx, retaddr);
1501eed56642SAlex Bennée             index = tlb_index(env, mmu_idx, addr);
1502eed56642SAlex Bennée             entry = tlb_entry(env, mmu_idx, addr);
1503eed56642SAlex Bennée         }
1504eed56642SAlex Bennée         tlb_addr = tlb_addr_write(entry) & ~TLB_INVALID_MASK;
1505eed56642SAlex Bennée     }
1506eed56642SAlex Bennée 
1507eed56642SAlex Bennée     /* Handle an IO access.  */
1508eed56642SAlex Bennée     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
1509eed56642SAlex Bennée         if ((addr & (size - 1)) != 0) {
1510eed56642SAlex Bennée             goto do_unaligned_access;
1511eed56642SAlex Bennée         }
1512eed56642SAlex Bennée 
1513f1be3696SRichard Henderson         if (tlb_addr & TLB_RECHECK) {
1514f1be3696SRichard Henderson             /*
1515f1be3696SRichard Henderson              * This is a TLB_RECHECK access, where the MMU protection
1516f1be3696SRichard Henderson              * covers a smaller range than a target page, and we must
1517f1be3696SRichard Henderson              * repeat the MMU check here. This tlb_fill() call might
1518f1be3696SRichard Henderson              * longjump out if this access should cause a guest exception.
1519f1be3696SRichard Henderson              */
152029a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size, MMU_DATA_STORE,
1521f1be3696SRichard Henderson                      mmu_idx, retaddr);
1522f1be3696SRichard Henderson             index = tlb_index(env, mmu_idx, addr);
1523f1be3696SRichard Henderson             entry = tlb_entry(env, mmu_idx, addr);
1524f1be3696SRichard Henderson 
1525f1be3696SRichard Henderson             tlb_addr = tlb_addr_write(entry);
1526f1be3696SRichard Henderson             tlb_addr &= ~TLB_RECHECK;
1527f1be3696SRichard Henderson             if (!(tlb_addr & ~TARGET_PAGE_MASK)) {
1528f1be3696SRichard Henderson                 /* RAM access */
1529f1be3696SRichard Henderson                 goto do_aligned_access;
1530f1be3696SRichard Henderson             }
1531f1be3696SRichard Henderson         }
1532f1be3696SRichard Henderson 
1533a40ec84eSRichard Henderson         io_writex(env, &env_tlb(env)->d[mmu_idx].iotlb[index], mmu_idx,
15349bf825bfSTony Nguyen                   val, addr, retaddr, op);
1535eed56642SAlex Bennée         return;
1536eed56642SAlex Bennée     }
1537eed56642SAlex Bennée 
1538eed56642SAlex Bennée     /* Handle slow unaligned access (it spans two pages or IO).  */
1539eed56642SAlex Bennée     if (size > 1
1540eed56642SAlex Bennée         && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1
1541eed56642SAlex Bennée                      >= TARGET_PAGE_SIZE)) {
1542eed56642SAlex Bennée         int i;
1543eed56642SAlex Bennée         uintptr_t index2;
1544eed56642SAlex Bennée         CPUTLBEntry *entry2;
1545eed56642SAlex Bennée         target_ulong page2, tlb_addr2;
1546eed56642SAlex Bennée     do_unaligned_access:
1547eed56642SAlex Bennée         /*
1548eed56642SAlex Bennée          * Ensure the second page is in the TLB.  Note that the first page
1549eed56642SAlex Bennée          * is already guaranteed to be filled, and that the second page
1550eed56642SAlex Bennée          * cannot evict the first.
1551eed56642SAlex Bennée          */
1552eed56642SAlex Bennée         page2 = (addr + size) & TARGET_PAGE_MASK;
1553eed56642SAlex Bennée         index2 = tlb_index(env, mmu_idx, page2);
1554eed56642SAlex Bennée         entry2 = tlb_entry(env, mmu_idx, page2);
1555eed56642SAlex Bennée         tlb_addr2 = tlb_addr_write(entry2);
1556eed56642SAlex Bennée         if (!tlb_hit_page(tlb_addr2, page2)
1557eed56642SAlex Bennée             && !victim_tlb_hit(env, mmu_idx, index2, tlb_off,
1558eed56642SAlex Bennée                                page2 & TARGET_PAGE_MASK)) {
155929a0af61SRichard Henderson             tlb_fill(env_cpu(env), page2, size, MMU_DATA_STORE,
1560eed56642SAlex Bennée                      mmu_idx, retaddr);
1561eed56642SAlex Bennée         }
1562eed56642SAlex Bennée 
1563eed56642SAlex Bennée         /*
1564eed56642SAlex Bennée          * XXX: not efficient, but simple.
1565eed56642SAlex Bennée          * This loop must go in the forward direction to avoid issues
1566eed56642SAlex Bennée          * with self-modifying code in Windows 64-bit.
1567eed56642SAlex Bennée          */
1568eed56642SAlex Bennée         for (i = 0; i < size; ++i) {
1569eed56642SAlex Bennée             uint8_t val8;
1570be5c4787STony Nguyen             if (memop_big_endian(op)) {
1571eed56642SAlex Bennée                 /* Big-endian extract.  */
1572eed56642SAlex Bennée                 val8 = val >> (((size - 1) * 8) - (i * 8));
1573eed56642SAlex Bennée             } else {
1574eed56642SAlex Bennée                 /* Little-endian extract.  */
1575eed56642SAlex Bennée                 val8 = val >> (i * 8);
1576eed56642SAlex Bennée             }
15774601f8d1SRichard Henderson             helper_ret_stb_mmu(env, addr + i, val8, oi, retaddr);
1578eed56642SAlex Bennée         }
1579eed56642SAlex Bennée         return;
1580eed56642SAlex Bennée     }
1581eed56642SAlex Bennée 
1582f1be3696SRichard Henderson  do_aligned_access:
1583eed56642SAlex Bennée     haddr = (void *)((uintptr_t)addr + entry->addend);
1584be5c4787STony Nguyen     switch (op) {
1585be5c4787STony Nguyen     case MO_UB:
1586eed56642SAlex Bennée         stb_p(haddr, val);
1587eed56642SAlex Bennée         break;
1588be5c4787STony Nguyen     case MO_BEUW:
1589eed56642SAlex Bennée         stw_be_p(haddr, val);
1590be5c4787STony Nguyen         break;
1591be5c4787STony Nguyen     case MO_LEUW:
1592eed56642SAlex Bennée         stw_le_p(haddr, val);
1593eed56642SAlex Bennée         break;
1594be5c4787STony Nguyen     case MO_BEUL:
1595eed56642SAlex Bennée         stl_be_p(haddr, val);
1596eed56642SAlex Bennée         break;
1597be5c4787STony Nguyen     case MO_LEUL:
1598be5c4787STony Nguyen         stl_le_p(haddr, val);
1599be5c4787STony Nguyen         break;
1600be5c4787STony Nguyen     case MO_BEQ:
1601eed56642SAlex Bennée         stq_be_p(haddr, val);
1602be5c4787STony Nguyen         break;
1603be5c4787STony Nguyen     case MO_LEQ:
1604eed56642SAlex Bennée         stq_le_p(haddr, val);
1605eed56642SAlex Bennée         break;
1606eed56642SAlex Bennée     default:
1607eed56642SAlex Bennée         g_assert_not_reached();
1608eed56642SAlex Bennée         break;
1609eed56642SAlex Bennée     }
1610eed56642SAlex Bennée }
1611eed56642SAlex Bennée 
1612fc1bc777SRichard Henderson void helper_ret_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val,
1613eed56642SAlex Bennée                         TCGMemOpIdx oi, uintptr_t retaddr)
1614eed56642SAlex Bennée {
1615be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_UB);
1616eed56642SAlex Bennée }
1617eed56642SAlex Bennée 
1618fc1bc777SRichard Henderson void helper_le_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
1619eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1620eed56642SAlex Bennée {
1621be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEUW);
1622eed56642SAlex Bennée }
1623eed56642SAlex Bennée 
1624fc1bc777SRichard Henderson void helper_be_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
1625eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1626eed56642SAlex Bennée {
1627be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEUW);
1628eed56642SAlex Bennée }
1629eed56642SAlex Bennée 
1630fc1bc777SRichard Henderson void helper_le_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
1631eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1632eed56642SAlex Bennée {
1633be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEUL);
1634eed56642SAlex Bennée }
1635eed56642SAlex Bennée 
1636fc1bc777SRichard Henderson void helper_be_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
1637eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1638eed56642SAlex Bennée {
1639be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEUL);
1640eed56642SAlex Bennée }
1641eed56642SAlex Bennée 
1642fc1bc777SRichard Henderson void helper_le_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
1643eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1644eed56642SAlex Bennée {
1645be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEQ);
1646eed56642SAlex Bennée }
1647eed56642SAlex Bennée 
1648fc1bc777SRichard Henderson void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
1649eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
1650eed56642SAlex Bennée {
1651be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEQ);
1652eed56642SAlex Bennée }
1653d9bb58e5SYang Zhong 
1654d9bb58e5SYang Zhong /* First set of helpers allows passing in of OI and RETADDR.  This makes
1655d9bb58e5SYang Zhong    them callable from other helpers.  */
1656d9bb58e5SYang Zhong 
1657d9bb58e5SYang Zhong #define EXTRA_ARGS     , TCGMemOpIdx oi, uintptr_t retaddr
1658d9bb58e5SYang Zhong #define ATOMIC_NAME(X) \
1659d9bb58e5SYang Zhong     HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
166034d49937SPeter Maydell #define ATOMIC_MMU_DECLS NotDirtyInfo ndi
166134d49937SPeter Maydell #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, retaddr, &ndi)
166234d49937SPeter Maydell #define ATOMIC_MMU_CLEANUP                              \
166334d49937SPeter Maydell     do {                                                \
166434d49937SPeter Maydell         if (unlikely(ndi.active)) {                     \
166534d49937SPeter Maydell             memory_notdirty_write_complete(&ndi);       \
166634d49937SPeter Maydell         }                                               \
166734d49937SPeter Maydell     } while (0)
1668d9bb58e5SYang Zhong 
1669d9bb58e5SYang Zhong #define DATA_SIZE 1
1670d9bb58e5SYang Zhong #include "atomic_template.h"
1671d9bb58e5SYang Zhong 
1672d9bb58e5SYang Zhong #define DATA_SIZE 2
1673d9bb58e5SYang Zhong #include "atomic_template.h"
1674d9bb58e5SYang Zhong 
1675d9bb58e5SYang Zhong #define DATA_SIZE 4
1676d9bb58e5SYang Zhong #include "atomic_template.h"
1677d9bb58e5SYang Zhong 
1678d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
1679d9bb58e5SYang Zhong #define DATA_SIZE 8
1680d9bb58e5SYang Zhong #include "atomic_template.h"
1681d9bb58e5SYang Zhong #endif
1682d9bb58e5SYang Zhong 
1683e6cd4bb5SRichard Henderson #if HAVE_CMPXCHG128 || HAVE_ATOMIC128
1684d9bb58e5SYang Zhong #define DATA_SIZE 16
1685d9bb58e5SYang Zhong #include "atomic_template.h"
1686d9bb58e5SYang Zhong #endif
1687d9bb58e5SYang Zhong 
1688d9bb58e5SYang Zhong /* Second set of helpers are directly callable from TCG as helpers.  */
1689d9bb58e5SYang Zhong 
1690d9bb58e5SYang Zhong #undef EXTRA_ARGS
1691d9bb58e5SYang Zhong #undef ATOMIC_NAME
1692d9bb58e5SYang Zhong #undef ATOMIC_MMU_LOOKUP
1693d9bb58e5SYang Zhong #define EXTRA_ARGS         , TCGMemOpIdx oi
1694d9bb58e5SYang Zhong #define ATOMIC_NAME(X)     HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
169534d49937SPeter Maydell #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, oi, GETPC(), &ndi)
1696d9bb58e5SYang Zhong 
1697d9bb58e5SYang Zhong #define DATA_SIZE 1
1698d9bb58e5SYang Zhong #include "atomic_template.h"
1699d9bb58e5SYang Zhong 
1700d9bb58e5SYang Zhong #define DATA_SIZE 2
1701d9bb58e5SYang Zhong #include "atomic_template.h"
1702d9bb58e5SYang Zhong 
1703d9bb58e5SYang Zhong #define DATA_SIZE 4
1704d9bb58e5SYang Zhong #include "atomic_template.h"
1705d9bb58e5SYang Zhong 
1706d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
1707d9bb58e5SYang Zhong #define DATA_SIZE 8
1708d9bb58e5SYang Zhong #include "atomic_template.h"
1709d9bb58e5SYang Zhong #endif
1710d9bb58e5SYang Zhong 
1711d9bb58e5SYang Zhong /* Code access functions.  */
1712d9bb58e5SYang Zhong 
17132dd92606SRichard Henderson static uint64_t full_ldub_cmmu(CPUArchState *env, target_ulong addr,
17142dd92606SRichard Henderson                                TCGMemOpIdx oi, uintptr_t retaddr)
17152dd92606SRichard Henderson {
1716be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_8, true, full_ldub_cmmu);
17172dd92606SRichard Henderson }
17182dd92606SRichard Henderson 
1719fc1bc777SRichard Henderson uint8_t helper_ret_ldb_cmmu(CPUArchState *env, target_ulong addr,
1720fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1721eed56642SAlex Bennée {
17222dd92606SRichard Henderson     return full_ldub_cmmu(env, addr, oi, retaddr);
17232dd92606SRichard Henderson }
17242dd92606SRichard Henderson 
17252dd92606SRichard Henderson static uint64_t full_le_lduw_cmmu(CPUArchState *env, target_ulong addr,
17262dd92606SRichard Henderson                                   TCGMemOpIdx oi, uintptr_t retaddr)
17272dd92606SRichard Henderson {
1728be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUW, true,
17292dd92606SRichard Henderson                        full_le_lduw_cmmu);
1730eed56642SAlex Bennée }
1731d9bb58e5SYang Zhong 
1732fc1bc777SRichard Henderson uint16_t helper_le_ldw_cmmu(CPUArchState *env, target_ulong addr,
1733fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1734eed56642SAlex Bennée {
17352dd92606SRichard Henderson     return full_le_lduw_cmmu(env, addr, oi, retaddr);
17362dd92606SRichard Henderson }
17372dd92606SRichard Henderson 
17382dd92606SRichard Henderson static uint64_t full_be_lduw_cmmu(CPUArchState *env, target_ulong addr,
17392dd92606SRichard Henderson                                   TCGMemOpIdx oi, uintptr_t retaddr)
17402dd92606SRichard Henderson {
1741be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUW, true,
17422dd92606SRichard Henderson                        full_be_lduw_cmmu);
1743eed56642SAlex Bennée }
1744d9bb58e5SYang Zhong 
1745fc1bc777SRichard Henderson uint16_t helper_be_ldw_cmmu(CPUArchState *env, target_ulong addr,
1746fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1747eed56642SAlex Bennée {
17482dd92606SRichard Henderson     return full_be_lduw_cmmu(env, addr, oi, retaddr);
17492dd92606SRichard Henderson }
17502dd92606SRichard Henderson 
17512dd92606SRichard Henderson static uint64_t full_le_ldul_cmmu(CPUArchState *env, target_ulong addr,
17522dd92606SRichard Henderson                                   TCGMemOpIdx oi, uintptr_t retaddr)
17532dd92606SRichard Henderson {
1754be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUL, true,
17552dd92606SRichard Henderson                        full_le_ldul_cmmu);
1756eed56642SAlex Bennée }
1757d9bb58e5SYang Zhong 
1758fc1bc777SRichard Henderson uint32_t helper_le_ldl_cmmu(CPUArchState *env, target_ulong addr,
1759fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1760eed56642SAlex Bennée {
17612dd92606SRichard Henderson     return full_le_ldul_cmmu(env, addr, oi, retaddr);
17622dd92606SRichard Henderson }
17632dd92606SRichard Henderson 
17642dd92606SRichard Henderson static uint64_t full_be_ldul_cmmu(CPUArchState *env, target_ulong addr,
17652dd92606SRichard Henderson                                   TCGMemOpIdx oi, uintptr_t retaddr)
17662dd92606SRichard Henderson {
1767be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUL, true,
17682dd92606SRichard Henderson                        full_be_ldul_cmmu);
1769eed56642SAlex Bennée }
1770d9bb58e5SYang Zhong 
1771fc1bc777SRichard Henderson uint32_t helper_be_ldl_cmmu(CPUArchState *env, target_ulong addr,
1772fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1773eed56642SAlex Bennée {
17742dd92606SRichard Henderson     return full_be_ldul_cmmu(env, addr, oi, retaddr);
1775eed56642SAlex Bennée }
1776eed56642SAlex Bennée 
1777fc1bc777SRichard Henderson uint64_t helper_le_ldq_cmmu(CPUArchState *env, target_ulong addr,
1778fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1779eed56642SAlex Bennée {
1780be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEQ, true,
17812dd92606SRichard Henderson                        helper_le_ldq_cmmu);
1782eed56642SAlex Bennée }
1783eed56642SAlex Bennée 
1784fc1bc777SRichard Henderson uint64_t helper_be_ldq_cmmu(CPUArchState *env, target_ulong addr,
1785fc1bc777SRichard Henderson                             TCGMemOpIdx oi, uintptr_t retaddr)
1786eed56642SAlex Bennée {
1787be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEQ, true,
17882dd92606SRichard Henderson                        helper_be_ldq_cmmu);
1789eed56642SAlex Bennée }
1790