xref: /openbmc/qemu/accel/tcg/cputlb.c (revision 2f3a57ee47df970d19fa5b324d44aab857d43517)
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"
36707526adSRichard Henderson #include "translate-all.h"
37d03f1408SRichard Henderson #include "trace-root.h"
38d03f1408SRichard Henderson #include "trace/mem.h"
39235537faSAlex Bennée #ifdef CONFIG_PLUGIN
40235537faSAlex Bennée #include "qemu/plugin-memory.h"
41235537faSAlex Bennée #endif
42d9bb58e5SYang Zhong 
43d9bb58e5SYang Zhong /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
44d9bb58e5SYang Zhong /* #define DEBUG_TLB */
45d9bb58e5SYang Zhong /* #define DEBUG_TLB_LOG */
46d9bb58e5SYang Zhong 
47d9bb58e5SYang Zhong #ifdef DEBUG_TLB
48d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 1
49d9bb58e5SYang Zhong # ifdef DEBUG_TLB_LOG
50d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 1
51d9bb58e5SYang Zhong # else
52d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 0
53d9bb58e5SYang Zhong # endif
54d9bb58e5SYang Zhong #else
55d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 0
56d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 0
57d9bb58e5SYang Zhong #endif
58d9bb58e5SYang Zhong 
59d9bb58e5SYang Zhong #define tlb_debug(fmt, ...) do { \
60d9bb58e5SYang Zhong     if (DEBUG_TLB_LOG_GATE) { \
61d9bb58e5SYang Zhong         qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
62d9bb58e5SYang Zhong                       ## __VA_ARGS__); \
63d9bb58e5SYang Zhong     } else if (DEBUG_TLB_GATE) { \
64d9bb58e5SYang Zhong         fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
65d9bb58e5SYang Zhong     } \
66d9bb58e5SYang Zhong } while (0)
67d9bb58e5SYang Zhong 
68ea9025cbSEmilio G. Cota #define assert_cpu_is_self(cpu) do {                              \
69d9bb58e5SYang Zhong         if (DEBUG_TLB_GATE) {                                     \
70ea9025cbSEmilio G. Cota             g_assert(!(cpu)->created || qemu_cpu_is_self(cpu));   \
71d9bb58e5SYang Zhong         }                                                         \
72d9bb58e5SYang Zhong     } while (0)
73d9bb58e5SYang Zhong 
74d9bb58e5SYang Zhong /* run_on_cpu_data.target_ptr should always be big enough for a
75d9bb58e5SYang Zhong  * target_ulong even on 32 bit builds */
76d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(sizeof(target_ulong) > sizeof(run_on_cpu_data));
77d9bb58e5SYang Zhong 
78d9bb58e5SYang Zhong /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
79d9bb58e5SYang Zhong  */
80d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16);
81d9bb58e5SYang Zhong #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
82d9bb58e5SYang Zhong 
83722a1c1eSRichard Henderson static inline size_t tlb_n_entries(CPUTLBDescFast *fast)
847a1efe1bSRichard Henderson {
85722a1c1eSRichard Henderson     return (fast->mask >> CPU_TLB_ENTRY_BITS) + 1;
867a1efe1bSRichard Henderson }
877a1efe1bSRichard Henderson 
88722a1c1eSRichard Henderson static inline size_t sizeof_tlb(CPUTLBDescFast *fast)
8986e1eff8SEmilio G. Cota {
90722a1c1eSRichard Henderson     return fast->mask + (1 << CPU_TLB_ENTRY_BITS);
9186e1eff8SEmilio G. Cota }
9286e1eff8SEmilio G. Cota 
9379e42085SRichard Henderson static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns,
9486e1eff8SEmilio G. Cota                              size_t max_entries)
9586e1eff8SEmilio G. Cota {
9679e42085SRichard Henderson     desc->window_begin_ns = ns;
9779e42085SRichard Henderson     desc->window_max_entries = max_entries;
9886e1eff8SEmilio G. Cota }
9986e1eff8SEmilio G. Cota 
10086e1eff8SEmilio G. Cota /**
10186e1eff8SEmilio G. Cota  * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
10271ccd47bSRichard Henderson  * @desc: The CPUTLBDesc portion of the TLB
10371ccd47bSRichard Henderson  * @fast: The CPUTLBDescFast portion of the same TLB
10486e1eff8SEmilio G. Cota  *
10586e1eff8SEmilio G. Cota  * Called with tlb_lock_held.
10686e1eff8SEmilio G. Cota  *
10786e1eff8SEmilio G. Cota  * We have two main constraints when resizing a TLB: (1) we only resize it
10886e1eff8SEmilio G. Cota  * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
10986e1eff8SEmilio G. Cota  * the array or unnecessarily flushing it), which means we do not control how
11086e1eff8SEmilio G. Cota  * frequently the resizing can occur; (2) we don't have access to the guest's
11186e1eff8SEmilio G. Cota  * future scheduling decisions, and therefore have to decide the magnitude of
11286e1eff8SEmilio G. Cota  * the resize based on past observations.
11386e1eff8SEmilio G. Cota  *
11486e1eff8SEmilio G. Cota  * In general, a memory-hungry process can benefit greatly from an appropriately
11586e1eff8SEmilio G. Cota  * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
11686e1eff8SEmilio G. Cota  * we just have to make the TLB as large as possible; while an oversized TLB
11786e1eff8SEmilio G. Cota  * results in minimal TLB miss rates, it also takes longer to be flushed
11886e1eff8SEmilio G. Cota  * (flushes can be _very_ frequent), and the reduced locality can also hurt
11986e1eff8SEmilio G. Cota  * performance.
12086e1eff8SEmilio G. Cota  *
12186e1eff8SEmilio G. Cota  * To achieve near-optimal performance for all kinds of workloads, we:
12286e1eff8SEmilio G. Cota  *
12386e1eff8SEmilio G. Cota  * 1. Aggressively increase the size of the TLB when the use rate of the
12486e1eff8SEmilio G. Cota  * TLB being flushed is high, since it is likely that in the near future this
12586e1eff8SEmilio G. Cota  * memory-hungry process will execute again, and its memory hungriness will
12686e1eff8SEmilio G. Cota  * probably be similar.
12786e1eff8SEmilio G. Cota  *
12886e1eff8SEmilio G. Cota  * 2. Slowly reduce the size of the TLB as the use rate declines over a
12986e1eff8SEmilio G. Cota  * reasonably large time window. The rationale is that if in such a time window
13086e1eff8SEmilio G. Cota  * we have not observed a high TLB use rate, it is likely that we won't observe
13186e1eff8SEmilio G. Cota  * it in the near future. In that case, once a time window expires we downsize
13286e1eff8SEmilio G. Cota  * the TLB to match the maximum use rate observed in the window.
13386e1eff8SEmilio G. Cota  *
13486e1eff8SEmilio G. Cota  * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
13586e1eff8SEmilio G. Cota  * since in that range performance is likely near-optimal. Recall that the TLB
13686e1eff8SEmilio G. Cota  * is direct mapped, so we want the use rate to be low (or at least not too
13786e1eff8SEmilio G. Cota  * high), since otherwise we are likely to have a significant amount of
13886e1eff8SEmilio G. Cota  * conflict misses.
13986e1eff8SEmilio G. Cota  */
1403c3959f2SRichard Henderson static void tlb_mmu_resize_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast,
1413c3959f2SRichard Henderson                                   int64_t now)
14286e1eff8SEmilio G. Cota {
14371ccd47bSRichard Henderson     size_t old_size = tlb_n_entries(fast);
14486e1eff8SEmilio G. Cota     size_t rate;
14586e1eff8SEmilio G. Cota     size_t new_size = old_size;
14686e1eff8SEmilio G. Cota     int64_t window_len_ms = 100;
14786e1eff8SEmilio G. Cota     int64_t window_len_ns = window_len_ms * 1000 * 1000;
14879e42085SRichard Henderson     bool window_expired = now > desc->window_begin_ns + window_len_ns;
14986e1eff8SEmilio G. Cota 
15079e42085SRichard Henderson     if (desc->n_used_entries > desc->window_max_entries) {
15179e42085SRichard Henderson         desc->window_max_entries = desc->n_used_entries;
15286e1eff8SEmilio G. Cota     }
15379e42085SRichard Henderson     rate = desc->window_max_entries * 100 / old_size;
15486e1eff8SEmilio G. Cota 
15586e1eff8SEmilio G. Cota     if (rate > 70) {
15686e1eff8SEmilio G. Cota         new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS);
15786e1eff8SEmilio G. Cota     } else if (rate < 30 && window_expired) {
15879e42085SRichard Henderson         size_t ceil = pow2ceil(desc->window_max_entries);
15979e42085SRichard Henderson         size_t expected_rate = desc->window_max_entries * 100 / ceil;
16086e1eff8SEmilio G. Cota 
16186e1eff8SEmilio G. Cota         /*
16286e1eff8SEmilio G. Cota          * Avoid undersizing when the max number of entries seen is just below
16386e1eff8SEmilio G. Cota          * a pow2. For instance, if max_entries == 1025, the expected use rate
16486e1eff8SEmilio G. Cota          * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
16586e1eff8SEmilio G. Cota          * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
16686e1eff8SEmilio G. Cota          * later. Thus, make sure that the expected use rate remains below 70%.
16786e1eff8SEmilio G. Cota          * (and since we double the size, that means the lowest rate we'd
16886e1eff8SEmilio G. Cota          * expect to get is 35%, which is still in the 30-70% range where
16986e1eff8SEmilio G. Cota          * we consider that the size is appropriate.)
17086e1eff8SEmilio G. Cota          */
17186e1eff8SEmilio G. Cota         if (expected_rate > 70) {
17286e1eff8SEmilio G. Cota             ceil *= 2;
17386e1eff8SEmilio G. Cota         }
17486e1eff8SEmilio G. Cota         new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS);
17586e1eff8SEmilio G. Cota     }
17686e1eff8SEmilio G. Cota 
17786e1eff8SEmilio G. Cota     if (new_size == old_size) {
17886e1eff8SEmilio G. Cota         if (window_expired) {
17979e42085SRichard Henderson             tlb_window_reset(desc, now, desc->n_used_entries);
18086e1eff8SEmilio G. Cota         }
18186e1eff8SEmilio G. Cota         return;
18286e1eff8SEmilio G. Cota     }
18386e1eff8SEmilio G. Cota 
18471ccd47bSRichard Henderson     g_free(fast->table);
18571ccd47bSRichard Henderson     g_free(desc->iotlb);
18686e1eff8SEmilio G. Cota 
18779e42085SRichard Henderson     tlb_window_reset(desc, now, 0);
18886e1eff8SEmilio G. Cota     /* desc->n_used_entries is cleared by the caller */
18971ccd47bSRichard Henderson     fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
19071ccd47bSRichard Henderson     fast->table = g_try_new(CPUTLBEntry, new_size);
19171ccd47bSRichard Henderson     desc->iotlb = g_try_new(CPUIOTLBEntry, new_size);
19271ccd47bSRichard Henderson 
19386e1eff8SEmilio G. Cota     /*
19486e1eff8SEmilio G. Cota      * If the allocations fail, try smaller sizes. We just freed some
19586e1eff8SEmilio G. Cota      * memory, so going back to half of new_size has a good chance of working.
19686e1eff8SEmilio G. Cota      * Increased memory pressure elsewhere in the system might cause the
19786e1eff8SEmilio G. Cota      * allocations to fail though, so we progressively reduce the allocation
19886e1eff8SEmilio G. Cota      * size, aborting if we cannot even allocate the smallest TLB we support.
19986e1eff8SEmilio G. Cota      */
20071ccd47bSRichard Henderson     while (fast->table == NULL || desc->iotlb == NULL) {
20186e1eff8SEmilio G. Cota         if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) {
20286e1eff8SEmilio G. Cota             error_report("%s: %s", __func__, strerror(errno));
20386e1eff8SEmilio G. Cota             abort();
20486e1eff8SEmilio G. Cota         }
20586e1eff8SEmilio G. Cota         new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS);
20671ccd47bSRichard Henderson         fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
20786e1eff8SEmilio G. Cota 
20871ccd47bSRichard Henderson         g_free(fast->table);
20971ccd47bSRichard Henderson         g_free(desc->iotlb);
21071ccd47bSRichard Henderson         fast->table = g_try_new(CPUTLBEntry, new_size);
21171ccd47bSRichard Henderson         desc->iotlb = g_try_new(CPUIOTLBEntry, new_size);
21286e1eff8SEmilio G. Cota     }
21386e1eff8SEmilio G. Cota }
21486e1eff8SEmilio G. Cota 
215bbf021b0SRichard Henderson static void tlb_mmu_flush_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast)
21686e1eff8SEmilio G. Cota {
2175c948e31SRichard Henderson     desc->n_used_entries = 0;
2185c948e31SRichard Henderson     desc->large_page_addr = -1;
2195c948e31SRichard Henderson     desc->large_page_mask = -1;
2205c948e31SRichard Henderson     desc->vindex = 0;
2215c948e31SRichard Henderson     memset(fast->table, -1, sizeof_tlb(fast));
2225c948e31SRichard Henderson     memset(desc->vtable, -1, sizeof(desc->vtable));
22386e1eff8SEmilio G. Cota }
22486e1eff8SEmilio G. Cota 
2253c3959f2SRichard Henderson static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx,
2263c3959f2SRichard Henderson                                         int64_t now)
227bbf021b0SRichard Henderson {
228bbf021b0SRichard Henderson     CPUTLBDesc *desc = &env_tlb(env)->d[mmu_idx];
229bbf021b0SRichard Henderson     CPUTLBDescFast *fast = &env_tlb(env)->f[mmu_idx];
230bbf021b0SRichard Henderson 
2313c3959f2SRichard Henderson     tlb_mmu_resize_locked(desc, fast, now);
232bbf021b0SRichard Henderson     tlb_mmu_flush_locked(desc, fast);
233bbf021b0SRichard Henderson }
234bbf021b0SRichard Henderson 
23556e89f76SRichard Henderson static void tlb_mmu_init(CPUTLBDesc *desc, CPUTLBDescFast *fast, int64_t now)
23656e89f76SRichard Henderson {
23756e89f76SRichard Henderson     size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS;
23856e89f76SRichard Henderson 
23956e89f76SRichard Henderson     tlb_window_reset(desc, now, 0);
24056e89f76SRichard Henderson     desc->n_used_entries = 0;
24156e89f76SRichard Henderson     fast->mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS;
24256e89f76SRichard Henderson     fast->table = g_new(CPUTLBEntry, n_entries);
24356e89f76SRichard Henderson     desc->iotlb = g_new(CPUIOTLBEntry, n_entries);
2443c16304aSRichard Henderson     tlb_mmu_flush_locked(desc, fast);
24556e89f76SRichard Henderson }
24656e89f76SRichard Henderson 
24786e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_inc(CPUArchState *env, uintptr_t mmu_idx)
24886e1eff8SEmilio G. Cota {
249a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].n_used_entries++;
25086e1eff8SEmilio G. Cota }
25186e1eff8SEmilio G. Cota 
25286e1eff8SEmilio G. Cota static inline void tlb_n_used_entries_dec(CPUArchState *env, uintptr_t mmu_idx)
25386e1eff8SEmilio G. Cota {
254a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].n_used_entries--;
25586e1eff8SEmilio G. Cota }
25686e1eff8SEmilio G. Cota 
2575005e253SEmilio G. Cota void tlb_init(CPUState *cpu)
2585005e253SEmilio G. Cota {
25971aec354SEmilio G. Cota     CPUArchState *env = cpu->env_ptr;
26056e89f76SRichard Henderson     int64_t now = get_clock_realtime();
26156e89f76SRichard Henderson     int i;
26271aec354SEmilio G. Cota 
263a40ec84eSRichard Henderson     qemu_spin_init(&env_tlb(env)->c.lock);
2643d1523ceSRichard Henderson 
2653c16304aSRichard Henderson     /* All tlbs are initialized flushed. */
2663c16304aSRichard Henderson     env_tlb(env)->c.dirty = 0;
26786e1eff8SEmilio G. Cota 
26856e89f76SRichard Henderson     for (i = 0; i < NB_MMU_MODES; i++) {
26956e89f76SRichard Henderson         tlb_mmu_init(&env_tlb(env)->d[i], &env_tlb(env)->f[i], now);
27056e89f76SRichard Henderson     }
2715005e253SEmilio G. Cota }
2725005e253SEmilio G. Cota 
273816d9be5SEmilio G. Cota void tlb_destroy(CPUState *cpu)
274816d9be5SEmilio G. Cota {
275816d9be5SEmilio G. Cota     CPUArchState *env = cpu->env_ptr;
276816d9be5SEmilio G. Cota     int i;
277816d9be5SEmilio G. Cota 
278816d9be5SEmilio G. Cota     qemu_spin_destroy(&env_tlb(env)->c.lock);
279816d9be5SEmilio G. Cota     for (i = 0; i < NB_MMU_MODES; i++) {
280816d9be5SEmilio G. Cota         CPUTLBDesc *desc = &env_tlb(env)->d[i];
281816d9be5SEmilio G. Cota         CPUTLBDescFast *fast = &env_tlb(env)->f[i];
282816d9be5SEmilio G. Cota 
283816d9be5SEmilio G. Cota         g_free(fast->table);
284816d9be5SEmilio G. Cota         g_free(desc->iotlb);
285816d9be5SEmilio G. Cota     }
286816d9be5SEmilio G. Cota }
287816d9be5SEmilio G. Cota 
288d9bb58e5SYang Zhong /* flush_all_helper: run fn across all cpus
289d9bb58e5SYang Zhong  *
290d9bb58e5SYang Zhong  * If the wait flag is set then the src cpu's helper will be queued as
291d9bb58e5SYang Zhong  * "safe" work and the loop exited creating a synchronisation point
292d9bb58e5SYang Zhong  * where all queued work will be finished before execution starts
293d9bb58e5SYang Zhong  * again.
294d9bb58e5SYang Zhong  */
295d9bb58e5SYang Zhong static void flush_all_helper(CPUState *src, run_on_cpu_func fn,
296d9bb58e5SYang Zhong                              run_on_cpu_data d)
297d9bb58e5SYang Zhong {
298d9bb58e5SYang Zhong     CPUState *cpu;
299d9bb58e5SYang Zhong 
300d9bb58e5SYang Zhong     CPU_FOREACH(cpu) {
301d9bb58e5SYang Zhong         if (cpu != src) {
302d9bb58e5SYang Zhong             async_run_on_cpu(cpu, fn, d);
303d9bb58e5SYang Zhong         }
304d9bb58e5SYang Zhong     }
305d9bb58e5SYang Zhong }
306d9bb58e5SYang Zhong 
307e09de0a2SRichard Henderson void tlb_flush_counts(size_t *pfull, size_t *ppart, size_t *pelide)
30883974cf4SEmilio G. Cota {
30983974cf4SEmilio G. Cota     CPUState *cpu;
310e09de0a2SRichard Henderson     size_t full = 0, part = 0, elide = 0;
31183974cf4SEmilio G. Cota 
31283974cf4SEmilio G. Cota     CPU_FOREACH(cpu) {
31383974cf4SEmilio G. Cota         CPUArchState *env = cpu->env_ptr;
31483974cf4SEmilio G. Cota 
315a40ec84eSRichard Henderson         full += atomic_read(&env_tlb(env)->c.full_flush_count);
316a40ec84eSRichard Henderson         part += atomic_read(&env_tlb(env)->c.part_flush_count);
317a40ec84eSRichard Henderson         elide += atomic_read(&env_tlb(env)->c.elide_flush_count);
31883974cf4SEmilio G. Cota     }
319e09de0a2SRichard Henderson     *pfull = full;
320e09de0a2SRichard Henderson     *ppart = part;
321e09de0a2SRichard Henderson     *pelide = elide;
32283974cf4SEmilio G. Cota }
323d9bb58e5SYang Zhong 
324d9bb58e5SYang Zhong static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)
325d9bb58e5SYang Zhong {
326d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
3273d1523ceSRichard Henderson     uint16_t asked = data.host_int;
3283d1523ceSRichard Henderson     uint16_t all_dirty, work, to_clean;
3293c3959f2SRichard Henderson     int64_t now = get_clock_realtime();
330d9bb58e5SYang Zhong 
331d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
332d9bb58e5SYang Zhong 
3333d1523ceSRichard Henderson     tlb_debug("mmu_idx:0x%04" PRIx16 "\n", asked);
334d9bb58e5SYang Zhong 
335a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
33660a2ad7dSRichard Henderson 
337a40ec84eSRichard Henderson     all_dirty = env_tlb(env)->c.dirty;
3383d1523ceSRichard Henderson     to_clean = asked & all_dirty;
3393d1523ceSRichard Henderson     all_dirty &= ~to_clean;
340a40ec84eSRichard Henderson     env_tlb(env)->c.dirty = all_dirty;
3413d1523ceSRichard Henderson 
3423d1523ceSRichard Henderson     for (work = to_clean; work != 0; work &= work - 1) {
3433d1523ceSRichard Henderson         int mmu_idx = ctz32(work);
3443c3959f2SRichard Henderson         tlb_flush_one_mmuidx_locked(env, mmu_idx, now);
345d9bb58e5SYang Zhong     }
3463d1523ceSRichard Henderson 
347a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
348d9bb58e5SYang Zhong 
349f3ced3c5SEmilio G. Cota     cpu_tb_jmp_cache_clear(cpu);
35064f2674bSRichard Henderson 
3513d1523ceSRichard Henderson     if (to_clean == ALL_MMUIDX_BITS) {
352a40ec84eSRichard Henderson         atomic_set(&env_tlb(env)->c.full_flush_count,
353a40ec84eSRichard Henderson                    env_tlb(env)->c.full_flush_count + 1);
354e09de0a2SRichard Henderson     } else {
355a40ec84eSRichard Henderson         atomic_set(&env_tlb(env)->c.part_flush_count,
356a40ec84eSRichard Henderson                    env_tlb(env)->c.part_flush_count + ctpop16(to_clean));
3573d1523ceSRichard Henderson         if (to_clean != asked) {
358a40ec84eSRichard Henderson             atomic_set(&env_tlb(env)->c.elide_flush_count,
359a40ec84eSRichard Henderson                        env_tlb(env)->c.elide_flush_count +
3603d1523ceSRichard Henderson                        ctpop16(asked & ~to_clean));
3613d1523ceSRichard Henderson         }
36264f2674bSRichard Henderson     }
363d9bb58e5SYang Zhong }
364d9bb58e5SYang Zhong 
365d9bb58e5SYang Zhong void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
366d9bb58e5SYang Zhong {
367d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap);
368d9bb58e5SYang Zhong 
36964f2674bSRichard Henderson     if (cpu->created && !qemu_cpu_is_self(cpu)) {
370d9bb58e5SYang Zhong         async_run_on_cpu(cpu, tlb_flush_by_mmuidx_async_work,
371ab651105SRichard Henderson                          RUN_ON_CPU_HOST_INT(idxmap));
372d9bb58e5SYang Zhong     } else {
37360a2ad7dSRichard Henderson         tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap));
374d9bb58e5SYang Zhong     }
375d9bb58e5SYang Zhong }
376d9bb58e5SYang Zhong 
37764f2674bSRichard Henderson void tlb_flush(CPUState *cpu)
37864f2674bSRichard Henderson {
37964f2674bSRichard Henderson     tlb_flush_by_mmuidx(cpu, ALL_MMUIDX_BITS);
38064f2674bSRichard Henderson }
38164f2674bSRichard Henderson 
382d9bb58e5SYang Zhong void tlb_flush_by_mmuidx_all_cpus(CPUState *src_cpu, uint16_t idxmap)
383d9bb58e5SYang Zhong {
384d9bb58e5SYang Zhong     const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
385d9bb58e5SYang Zhong 
386d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap);
387d9bb58e5SYang Zhong 
388d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
389d9bb58e5SYang Zhong     fn(src_cpu, RUN_ON_CPU_HOST_INT(idxmap));
390d9bb58e5SYang Zhong }
391d9bb58e5SYang Zhong 
39264f2674bSRichard Henderson void tlb_flush_all_cpus(CPUState *src_cpu)
39364f2674bSRichard Henderson {
39464f2674bSRichard Henderson     tlb_flush_by_mmuidx_all_cpus(src_cpu, ALL_MMUIDX_BITS);
39564f2674bSRichard Henderson }
39664f2674bSRichard Henderson 
39764f2674bSRichard Henderson void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu, uint16_t idxmap)
398d9bb58e5SYang Zhong {
399d9bb58e5SYang Zhong     const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
400d9bb58e5SYang Zhong 
401d9bb58e5SYang Zhong     tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap);
402d9bb58e5SYang Zhong 
403d9bb58e5SYang Zhong     flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
404d9bb58e5SYang Zhong     async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
405d9bb58e5SYang Zhong }
406d9bb58e5SYang Zhong 
40764f2674bSRichard Henderson void tlb_flush_all_cpus_synced(CPUState *src_cpu)
40864f2674bSRichard Henderson {
40964f2674bSRichard Henderson     tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS);
41064f2674bSRichard Henderson }
41164f2674bSRichard Henderson 
41268fea038SRichard Henderson static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry,
41368fea038SRichard Henderson                                         target_ulong page)
414d9bb58e5SYang Zhong {
41568fea038SRichard Henderson     return tlb_hit_page(tlb_entry->addr_read, page) ||
416403f290cSEmilio G. Cota            tlb_hit_page(tlb_addr_write(tlb_entry), page) ||
41768fea038SRichard Henderson            tlb_hit_page(tlb_entry->addr_code, page);
41868fea038SRichard Henderson }
41968fea038SRichard Henderson 
4203cea94bbSEmilio G. Cota /**
4213cea94bbSEmilio G. Cota  * tlb_entry_is_empty - return true if the entry is not in use
4223cea94bbSEmilio G. Cota  * @te: pointer to CPUTLBEntry
4233cea94bbSEmilio G. Cota  */
4243cea94bbSEmilio G. Cota static inline bool tlb_entry_is_empty(const CPUTLBEntry *te)
4253cea94bbSEmilio G. Cota {
4263cea94bbSEmilio G. Cota     return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1;
4273cea94bbSEmilio G. Cota }
4283cea94bbSEmilio G. Cota 
42953d28455SRichard Henderson /* Called with tlb_c.lock held */
43086e1eff8SEmilio G. Cota static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry,
43171aec354SEmilio G. Cota                                           target_ulong page)
43268fea038SRichard Henderson {
43368fea038SRichard Henderson     if (tlb_hit_page_anyprot(tlb_entry, page)) {
434d9bb58e5SYang Zhong         memset(tlb_entry, -1, sizeof(*tlb_entry));
43586e1eff8SEmilio G. Cota         return true;
436d9bb58e5SYang Zhong     }
43786e1eff8SEmilio G. Cota     return false;
438d9bb58e5SYang Zhong }
439d9bb58e5SYang Zhong 
44053d28455SRichard Henderson /* Called with tlb_c.lock held */
44171aec354SEmilio G. Cota static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx,
44268fea038SRichard Henderson                                               target_ulong page)
44368fea038SRichard Henderson {
444a40ec84eSRichard Henderson     CPUTLBDesc *d = &env_tlb(env)->d[mmu_idx];
44568fea038SRichard Henderson     int k;
44671aec354SEmilio G. Cota 
44729a0af61SRichard Henderson     assert_cpu_is_self(env_cpu(env));
44868fea038SRichard Henderson     for (k = 0; k < CPU_VTLB_SIZE; k++) {
449a40ec84eSRichard Henderson         if (tlb_flush_entry_locked(&d->vtable[k], page)) {
45086e1eff8SEmilio G. Cota             tlb_n_used_entries_dec(env, mmu_idx);
45186e1eff8SEmilio G. Cota         }
45268fea038SRichard Henderson     }
45368fea038SRichard Henderson }
45468fea038SRichard Henderson 
4551308e026SRichard Henderson static void tlb_flush_page_locked(CPUArchState *env, int midx,
4561308e026SRichard Henderson                                   target_ulong page)
4571308e026SRichard Henderson {
458a40ec84eSRichard Henderson     target_ulong lp_addr = env_tlb(env)->d[midx].large_page_addr;
459a40ec84eSRichard Henderson     target_ulong lp_mask = env_tlb(env)->d[midx].large_page_mask;
4601308e026SRichard Henderson 
4611308e026SRichard Henderson     /* Check if we need to flush due to large pages.  */
4621308e026SRichard Henderson     if ((page & lp_mask) == lp_addr) {
4631308e026SRichard Henderson         tlb_debug("forcing full flush midx %d ("
4641308e026SRichard Henderson                   TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
4651308e026SRichard Henderson                   midx, lp_addr, lp_mask);
4663c3959f2SRichard Henderson         tlb_flush_one_mmuidx_locked(env, midx, get_clock_realtime());
4671308e026SRichard Henderson     } else {
46886e1eff8SEmilio G. Cota         if (tlb_flush_entry_locked(tlb_entry(env, midx, page), page)) {
46986e1eff8SEmilio G. Cota             tlb_n_used_entries_dec(env, midx);
47086e1eff8SEmilio G. Cota         }
4711308e026SRichard Henderson         tlb_flush_vtlb_page_locked(env, midx, page);
4721308e026SRichard Henderson     }
4731308e026SRichard Henderson }
4741308e026SRichard Henderson 
4757b7d00e0SRichard Henderson /**
4767b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_0:
4777b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
4787b7d00e0SRichard Henderson  * @addr: page of virtual address to flush
4797b7d00e0SRichard Henderson  * @idxmap: set of mmu_idx to flush
4807b7d00e0SRichard Henderson  *
4817b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
4827b7d00e0SRichard Henderson  * at @addr from the tlbs indicated by @idxmap from @cpu.
483d9bb58e5SYang Zhong  */
4847b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_0(CPUState *cpu,
4857b7d00e0SRichard Henderson                                              target_ulong addr,
4867b7d00e0SRichard Henderson                                              uint16_t idxmap)
487d9bb58e5SYang Zhong {
488d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
489d9bb58e5SYang Zhong     int mmu_idx;
490d9bb58e5SYang Zhong 
491d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
492d9bb58e5SYang Zhong 
4937b7d00e0SRichard Henderson     tlb_debug("page addr:" TARGET_FMT_lx " mmu_map:0x%x\n", addr, idxmap);
494d9bb58e5SYang Zhong 
495a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
496d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
4977b7d00e0SRichard Henderson         if ((idxmap >> mmu_idx) & 1) {
4981308e026SRichard Henderson             tlb_flush_page_locked(env, mmu_idx, addr);
499d9bb58e5SYang Zhong         }
500d9bb58e5SYang Zhong     }
501a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
502d9bb58e5SYang Zhong 
503d9bb58e5SYang Zhong     tb_flush_jmp_cache(cpu, addr);
504d9bb58e5SYang Zhong }
505d9bb58e5SYang Zhong 
5067b7d00e0SRichard Henderson /**
5077b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_1:
5087b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
5097b7d00e0SRichard Henderson  * @data: encoded addr + idxmap
5107b7d00e0SRichard Henderson  *
5117b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, called through
5127b7d00e0SRichard Henderson  * async_run_on_cpu.  The idxmap parameter is encoded in the page
5137b7d00e0SRichard Henderson  * offset of the target_ptr field.  This limits the set of mmu_idx
5147b7d00e0SRichard Henderson  * that can be passed via this method.
5157b7d00e0SRichard Henderson  */
5167b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_1(CPUState *cpu,
5177b7d00e0SRichard Henderson                                              run_on_cpu_data data)
5187b7d00e0SRichard Henderson {
5197b7d00e0SRichard Henderson     target_ulong addr_and_idxmap = (target_ulong) data.target_ptr;
5207b7d00e0SRichard Henderson     target_ulong addr = addr_and_idxmap & TARGET_PAGE_MASK;
5217b7d00e0SRichard Henderson     uint16_t idxmap = addr_and_idxmap & ~TARGET_PAGE_MASK;
5227b7d00e0SRichard Henderson 
5237b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
5247b7d00e0SRichard Henderson }
5257b7d00e0SRichard Henderson 
5267b7d00e0SRichard Henderson typedef struct {
5277b7d00e0SRichard Henderson     target_ulong addr;
5287b7d00e0SRichard Henderson     uint16_t idxmap;
5297b7d00e0SRichard Henderson } TLBFlushPageByMMUIdxData;
5307b7d00e0SRichard Henderson 
5317b7d00e0SRichard Henderson /**
5327b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_2:
5337b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
5347b7d00e0SRichard Henderson  * @data: allocated addr + idxmap
5357b7d00e0SRichard Henderson  *
5367b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, called through
5377b7d00e0SRichard Henderson  * async_run_on_cpu.  The addr+idxmap parameters are stored in a
5387b7d00e0SRichard Henderson  * TLBFlushPageByMMUIdxData structure that has been allocated
5397b7d00e0SRichard Henderson  * specifically for this helper.  Free the structure when done.
5407b7d00e0SRichard Henderson  */
5417b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_2(CPUState *cpu,
5427b7d00e0SRichard Henderson                                              run_on_cpu_data data)
5437b7d00e0SRichard Henderson {
5447b7d00e0SRichard Henderson     TLBFlushPageByMMUIdxData *d = data.host_ptr;
5457b7d00e0SRichard Henderson 
5467b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(cpu, d->addr, d->idxmap);
5477b7d00e0SRichard Henderson     g_free(d);
5487b7d00e0SRichard Henderson }
5497b7d00e0SRichard Henderson 
550d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, uint16_t idxmap)
551d9bb58e5SYang Zhong {
552d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%" PRIx16 "\n", addr, idxmap);
553d9bb58e5SYang Zhong 
554d9bb58e5SYang Zhong     /* This should already be page aligned */
5557b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
556d9bb58e5SYang Zhong 
5577b7d00e0SRichard Henderson     if (qemu_cpu_is_self(cpu)) {
5587b7d00e0SRichard Henderson         tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
5597b7d00e0SRichard Henderson     } else if (idxmap < TARGET_PAGE_SIZE) {
5607b7d00e0SRichard Henderson         /*
5617b7d00e0SRichard Henderson          * Most targets have only a few mmu_idx.  In the case where
5627b7d00e0SRichard Henderson          * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid
5637b7d00e0SRichard Henderson          * allocating memory for this operation.
5647b7d00e0SRichard Henderson          */
5657b7d00e0SRichard Henderson         async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_1,
5667b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
567d9bb58e5SYang Zhong     } else {
5687b7d00e0SRichard Henderson         TLBFlushPageByMMUIdxData *d = g_new(TLBFlushPageByMMUIdxData, 1);
5697b7d00e0SRichard Henderson 
5707b7d00e0SRichard Henderson         /* Otherwise allocate a structure, freed by the worker.  */
5717b7d00e0SRichard Henderson         d->addr = addr;
5727b7d00e0SRichard Henderson         d->idxmap = idxmap;
5737b7d00e0SRichard Henderson         async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_2,
5747b7d00e0SRichard Henderson                          RUN_ON_CPU_HOST_PTR(d));
575d9bb58e5SYang Zhong     }
576d9bb58e5SYang Zhong }
577d9bb58e5SYang Zhong 
578f8144c6cSRichard Henderson void tlb_flush_page(CPUState *cpu, target_ulong addr)
579f8144c6cSRichard Henderson {
580f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS);
581f8144c6cSRichard Henderson }
582f8144c6cSRichard Henderson 
583d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, target_ulong addr,
584d9bb58e5SYang Zhong                                        uint16_t idxmap)
585d9bb58e5SYang Zhong {
586d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap);
587d9bb58e5SYang Zhong 
588d9bb58e5SYang Zhong     /* This should already be page aligned */
5897b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
590d9bb58e5SYang Zhong 
5917b7d00e0SRichard Henderson     /*
5927b7d00e0SRichard Henderson      * Allocate memory to hold addr+idxmap only when needed.
5937b7d00e0SRichard Henderson      * See tlb_flush_page_by_mmuidx for details.
5947b7d00e0SRichard Henderson      */
5957b7d00e0SRichard Henderson     if (idxmap < TARGET_PAGE_SIZE) {
5967b7d00e0SRichard Henderson         flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1,
5977b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
5987b7d00e0SRichard Henderson     } else {
5997b7d00e0SRichard Henderson         CPUState *dst_cpu;
6007b7d00e0SRichard Henderson 
6017b7d00e0SRichard Henderson         /* Allocate a separate data block for each destination cpu.  */
6027b7d00e0SRichard Henderson         CPU_FOREACH(dst_cpu) {
6037b7d00e0SRichard Henderson             if (dst_cpu != src_cpu) {
6047b7d00e0SRichard Henderson                 TLBFlushPageByMMUIdxData *d
6057b7d00e0SRichard Henderson                     = g_new(TLBFlushPageByMMUIdxData, 1);
6067b7d00e0SRichard Henderson 
6077b7d00e0SRichard Henderson                 d->addr = addr;
6087b7d00e0SRichard Henderson                 d->idxmap = idxmap;
6097b7d00e0SRichard Henderson                 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2,
6107b7d00e0SRichard Henderson                                  RUN_ON_CPU_HOST_PTR(d));
6117b7d00e0SRichard Henderson             }
6127b7d00e0SRichard Henderson         }
6137b7d00e0SRichard Henderson     }
6147b7d00e0SRichard Henderson 
6157b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(src_cpu, addr, idxmap);
616d9bb58e5SYang Zhong }
617d9bb58e5SYang Zhong 
618f8144c6cSRichard Henderson void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr)
619f8144c6cSRichard Henderson {
620f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus(src, addr, ALL_MMUIDX_BITS);
621f8144c6cSRichard Henderson }
622f8144c6cSRichard Henderson 
623d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
624d9bb58e5SYang Zhong                                               target_ulong addr,
625d9bb58e5SYang Zhong                                               uint16_t idxmap)
626d9bb58e5SYang Zhong {
627d9bb58e5SYang Zhong     tlb_debug("addr: "TARGET_FMT_lx" mmu_idx:%"PRIx16"\n", addr, idxmap);
628d9bb58e5SYang Zhong 
629d9bb58e5SYang Zhong     /* This should already be page aligned */
6307b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
631d9bb58e5SYang Zhong 
6327b7d00e0SRichard Henderson     /*
6337b7d00e0SRichard Henderson      * Allocate memory to hold addr+idxmap only when needed.
6347b7d00e0SRichard Henderson      * See tlb_flush_page_by_mmuidx for details.
6357b7d00e0SRichard Henderson      */
6367b7d00e0SRichard Henderson     if (idxmap < TARGET_PAGE_SIZE) {
6377b7d00e0SRichard Henderson         flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1,
6387b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
6397b7d00e0SRichard Henderson         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_1,
6407b7d00e0SRichard Henderson                               RUN_ON_CPU_TARGET_PTR(addr | idxmap));
6417b7d00e0SRichard Henderson     } else {
6427b7d00e0SRichard Henderson         CPUState *dst_cpu;
6437b7d00e0SRichard Henderson         TLBFlushPageByMMUIdxData *d;
6447b7d00e0SRichard Henderson 
6457b7d00e0SRichard Henderson         /* Allocate a separate data block for each destination cpu.  */
6467b7d00e0SRichard Henderson         CPU_FOREACH(dst_cpu) {
6477b7d00e0SRichard Henderson             if (dst_cpu != src_cpu) {
6487b7d00e0SRichard Henderson                 d = g_new(TLBFlushPageByMMUIdxData, 1);
6497b7d00e0SRichard Henderson                 d->addr = addr;
6507b7d00e0SRichard Henderson                 d->idxmap = idxmap;
6517b7d00e0SRichard Henderson                 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2,
6527b7d00e0SRichard Henderson                                  RUN_ON_CPU_HOST_PTR(d));
6537b7d00e0SRichard Henderson             }
6547b7d00e0SRichard Henderson         }
6557b7d00e0SRichard Henderson 
6567b7d00e0SRichard Henderson         d = g_new(TLBFlushPageByMMUIdxData, 1);
6577b7d00e0SRichard Henderson         d->addr = addr;
6587b7d00e0SRichard Henderson         d->idxmap = idxmap;
6597b7d00e0SRichard Henderson         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_2,
6607b7d00e0SRichard Henderson                               RUN_ON_CPU_HOST_PTR(d));
6617b7d00e0SRichard Henderson     }
662d9bb58e5SYang Zhong }
663d9bb58e5SYang Zhong 
664f8144c6cSRichard Henderson void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr)
665d9bb58e5SYang Zhong {
666f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS);
667d9bb58e5SYang Zhong }
668d9bb58e5SYang Zhong 
669d9bb58e5SYang Zhong /* update the TLBs so that writes to code in the virtual page 'addr'
670d9bb58e5SYang Zhong    can be detected */
671d9bb58e5SYang Zhong void tlb_protect_code(ram_addr_t ram_addr)
672d9bb58e5SYang Zhong {
673d9bb58e5SYang Zhong     cpu_physical_memory_test_and_clear_dirty(ram_addr, TARGET_PAGE_SIZE,
674d9bb58e5SYang Zhong                                              DIRTY_MEMORY_CODE);
675d9bb58e5SYang Zhong }
676d9bb58e5SYang Zhong 
677d9bb58e5SYang Zhong /* update the TLB so that writes in physical page 'phys_addr' are no longer
678d9bb58e5SYang Zhong    tested for self modifying code */
679d9bb58e5SYang Zhong void tlb_unprotect_code(ram_addr_t ram_addr)
680d9bb58e5SYang Zhong {
681d9bb58e5SYang Zhong     cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
682d9bb58e5SYang Zhong }
683d9bb58e5SYang Zhong 
684d9bb58e5SYang Zhong 
685d9bb58e5SYang Zhong /*
686d9bb58e5SYang Zhong  * Dirty write flag handling
687d9bb58e5SYang Zhong  *
688d9bb58e5SYang Zhong  * When the TCG code writes to a location it looks up the address in
689d9bb58e5SYang Zhong  * the TLB and uses that data to compute the final address. If any of
690d9bb58e5SYang Zhong  * the lower bits of the address are set then the slow path is forced.
691d9bb58e5SYang Zhong  * There are a number of reasons to do this but for normal RAM the
692d9bb58e5SYang Zhong  * most usual is detecting writes to code regions which may invalidate
693d9bb58e5SYang Zhong  * generated code.
694d9bb58e5SYang Zhong  *
69571aec354SEmilio G. Cota  * Other vCPUs might be reading their TLBs during guest execution, so we update
69671aec354SEmilio G. Cota  * te->addr_write with atomic_set. We don't need to worry about this for
69771aec354SEmilio G. Cota  * oversized guests as MTTCG is disabled for them.
698d9bb58e5SYang Zhong  *
69953d28455SRichard Henderson  * Called with tlb_c.lock held.
700d9bb58e5SYang Zhong  */
70171aec354SEmilio G. Cota static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry,
70271aec354SEmilio G. Cota                                          uintptr_t start, uintptr_t length)
703d9bb58e5SYang Zhong {
704d9bb58e5SYang Zhong     uintptr_t addr = tlb_entry->addr_write;
705d9bb58e5SYang Zhong 
7067b0d792cSRichard Henderson     if ((addr & (TLB_INVALID_MASK | TLB_MMIO |
7077b0d792cSRichard Henderson                  TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) {
708d9bb58e5SYang Zhong         addr &= TARGET_PAGE_MASK;
709d9bb58e5SYang Zhong         addr += tlb_entry->addend;
710d9bb58e5SYang Zhong         if ((addr - start) < length) {
711d9bb58e5SYang Zhong #if TCG_OVERSIZED_GUEST
71271aec354SEmilio G. Cota             tlb_entry->addr_write |= TLB_NOTDIRTY;
713d9bb58e5SYang Zhong #else
71471aec354SEmilio G. Cota             atomic_set(&tlb_entry->addr_write,
71571aec354SEmilio G. Cota                        tlb_entry->addr_write | TLB_NOTDIRTY);
716d9bb58e5SYang Zhong #endif
717d9bb58e5SYang Zhong         }
71871aec354SEmilio G. Cota     }
71971aec354SEmilio G. Cota }
72071aec354SEmilio G. Cota 
72171aec354SEmilio G. Cota /*
72253d28455SRichard Henderson  * Called with tlb_c.lock held.
72371aec354SEmilio G. Cota  * Called only from the vCPU context, i.e. the TLB's owner thread.
72471aec354SEmilio G. Cota  */
72571aec354SEmilio G. Cota static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s)
72671aec354SEmilio G. Cota {
72771aec354SEmilio G. Cota     *d = *s;
72871aec354SEmilio G. Cota }
729d9bb58e5SYang Zhong 
730d9bb58e5SYang Zhong /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
73171aec354SEmilio G. Cota  * the target vCPU).
73253d28455SRichard Henderson  * We must take tlb_c.lock to avoid racing with another vCPU update. The only
73371aec354SEmilio G. Cota  * thing actually updated is the target TLB entry ->addr_write flags.
734d9bb58e5SYang Zhong  */
735d9bb58e5SYang Zhong void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length)
736d9bb58e5SYang Zhong {
737d9bb58e5SYang Zhong     CPUArchState *env;
738d9bb58e5SYang Zhong 
739d9bb58e5SYang Zhong     int mmu_idx;
740d9bb58e5SYang Zhong 
741d9bb58e5SYang Zhong     env = cpu->env_ptr;
742a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
743d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
744d9bb58e5SYang Zhong         unsigned int i;
745722a1c1eSRichard Henderson         unsigned int n = tlb_n_entries(&env_tlb(env)->f[mmu_idx]);
746d9bb58e5SYang Zhong 
74786e1eff8SEmilio G. Cota         for (i = 0; i < n; i++) {
748a40ec84eSRichard Henderson             tlb_reset_dirty_range_locked(&env_tlb(env)->f[mmu_idx].table[i],
749a40ec84eSRichard Henderson                                          start1, length);
750d9bb58e5SYang Zhong         }
751d9bb58e5SYang Zhong 
752d9bb58e5SYang Zhong         for (i = 0; i < CPU_VTLB_SIZE; i++) {
753a40ec84eSRichard Henderson             tlb_reset_dirty_range_locked(&env_tlb(env)->d[mmu_idx].vtable[i],
754a40ec84eSRichard Henderson                                          start1, length);
755d9bb58e5SYang Zhong         }
756d9bb58e5SYang Zhong     }
757a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
758d9bb58e5SYang Zhong }
759d9bb58e5SYang Zhong 
76053d28455SRichard Henderson /* Called with tlb_c.lock held */
76171aec354SEmilio G. Cota static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry,
76271aec354SEmilio G. Cota                                          target_ulong vaddr)
763d9bb58e5SYang Zhong {
764d9bb58e5SYang Zhong     if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) {
765d9bb58e5SYang Zhong         tlb_entry->addr_write = vaddr;
766d9bb58e5SYang Zhong     }
767d9bb58e5SYang Zhong }
768d9bb58e5SYang Zhong 
769d9bb58e5SYang Zhong /* update the TLB corresponding to virtual page vaddr
770d9bb58e5SYang Zhong    so that it is no longer dirty */
771d9bb58e5SYang Zhong void tlb_set_dirty(CPUState *cpu, target_ulong vaddr)
772d9bb58e5SYang Zhong {
773d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
774d9bb58e5SYang Zhong     int mmu_idx;
775d9bb58e5SYang Zhong 
776d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
777d9bb58e5SYang Zhong 
778d9bb58e5SYang Zhong     vaddr &= TARGET_PAGE_MASK;
779a40ec84eSRichard Henderson     qemu_spin_lock(&env_tlb(env)->c.lock);
780d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
781383beda9SRichard Henderson         tlb_set_dirty1_locked(tlb_entry(env, mmu_idx, vaddr), vaddr);
782d9bb58e5SYang Zhong     }
783d9bb58e5SYang Zhong 
784d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
785d9bb58e5SYang Zhong         int k;
786d9bb58e5SYang Zhong         for (k = 0; k < CPU_VTLB_SIZE; k++) {
787a40ec84eSRichard Henderson             tlb_set_dirty1_locked(&env_tlb(env)->d[mmu_idx].vtable[k], vaddr);
788d9bb58e5SYang Zhong         }
789d9bb58e5SYang Zhong     }
790a40ec84eSRichard Henderson     qemu_spin_unlock(&env_tlb(env)->c.lock);
791d9bb58e5SYang Zhong }
792d9bb58e5SYang Zhong 
793d9bb58e5SYang Zhong /* Our TLB does not support large pages, so remember the area covered by
794d9bb58e5SYang Zhong    large pages and trigger a full TLB flush if these are invalidated.  */
7951308e026SRichard Henderson static void tlb_add_large_page(CPUArchState *env, int mmu_idx,
7961308e026SRichard Henderson                                target_ulong vaddr, target_ulong size)
797d9bb58e5SYang Zhong {
798a40ec84eSRichard Henderson     target_ulong lp_addr = env_tlb(env)->d[mmu_idx].large_page_addr;
7991308e026SRichard Henderson     target_ulong lp_mask = ~(size - 1);
800d9bb58e5SYang Zhong 
8011308e026SRichard Henderson     if (lp_addr == (target_ulong)-1) {
8021308e026SRichard Henderson         /* No previous large page.  */
8031308e026SRichard Henderson         lp_addr = vaddr;
8041308e026SRichard Henderson     } else {
805d9bb58e5SYang Zhong         /* Extend the existing region to include the new page.
8061308e026SRichard Henderson            This is a compromise between unnecessary flushes and
8071308e026SRichard Henderson            the cost of maintaining a full variable size TLB.  */
808a40ec84eSRichard Henderson         lp_mask &= env_tlb(env)->d[mmu_idx].large_page_mask;
8091308e026SRichard Henderson         while (((lp_addr ^ vaddr) & lp_mask) != 0) {
8101308e026SRichard Henderson             lp_mask <<= 1;
811d9bb58e5SYang Zhong         }
8121308e026SRichard Henderson     }
813a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_addr = lp_addr & lp_mask;
814a40ec84eSRichard Henderson     env_tlb(env)->d[mmu_idx].large_page_mask = lp_mask;
815d9bb58e5SYang Zhong }
816d9bb58e5SYang Zhong 
817d9bb58e5SYang Zhong /* Add a new TLB entry. At most one entry for a given virtual address
818d9bb58e5SYang Zhong  * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
819d9bb58e5SYang Zhong  * supplied size is only used by tlb_flush_page.
820d9bb58e5SYang Zhong  *
821d9bb58e5SYang Zhong  * Called from TCG-generated code, which is under an RCU read-side
822d9bb58e5SYang Zhong  * critical section.
823d9bb58e5SYang Zhong  */
824d9bb58e5SYang Zhong void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
825d9bb58e5SYang Zhong                              hwaddr paddr, MemTxAttrs attrs, int prot,
826d9bb58e5SYang Zhong                              int mmu_idx, target_ulong size)
827d9bb58e5SYang Zhong {
828d9bb58e5SYang Zhong     CPUArchState *env = cpu->env_ptr;
829a40ec84eSRichard Henderson     CPUTLB *tlb = env_tlb(env);
830a40ec84eSRichard Henderson     CPUTLBDesc *desc = &tlb->d[mmu_idx];
831d9bb58e5SYang Zhong     MemoryRegionSection *section;
832d9bb58e5SYang Zhong     unsigned int index;
833d9bb58e5SYang Zhong     target_ulong address;
8348f5db641SRichard Henderson     target_ulong write_address;
835d9bb58e5SYang Zhong     uintptr_t addend;
83668fea038SRichard Henderson     CPUTLBEntry *te, tn;
83755df6fcfSPeter Maydell     hwaddr iotlb, xlat, sz, paddr_page;
83855df6fcfSPeter Maydell     target_ulong vaddr_page;
839d9bb58e5SYang Zhong     int asidx = cpu_asidx_from_attrs(cpu, attrs);
84050b107c5SRichard Henderson     int wp_flags;
8418f5db641SRichard Henderson     bool is_ram, is_romd;
842d9bb58e5SYang Zhong 
843d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
84455df6fcfSPeter Maydell 
8451308e026SRichard Henderson     if (size <= TARGET_PAGE_SIZE) {
84655df6fcfSPeter Maydell         sz = TARGET_PAGE_SIZE;
84755df6fcfSPeter Maydell     } else {
8481308e026SRichard Henderson         tlb_add_large_page(env, mmu_idx, vaddr, size);
849d9bb58e5SYang Zhong         sz = size;
85055df6fcfSPeter Maydell     }
85155df6fcfSPeter Maydell     vaddr_page = vaddr & TARGET_PAGE_MASK;
85255df6fcfSPeter Maydell     paddr_page = paddr & TARGET_PAGE_MASK;
85355df6fcfSPeter Maydell 
85455df6fcfSPeter Maydell     section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
85555df6fcfSPeter Maydell                                                 &xlat, &sz, attrs, &prot);
856d9bb58e5SYang Zhong     assert(sz >= TARGET_PAGE_SIZE);
857d9bb58e5SYang Zhong 
858d9bb58e5SYang Zhong     tlb_debug("vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
859d9bb58e5SYang Zhong               " prot=%x idx=%d\n",
860d9bb58e5SYang Zhong               vaddr, paddr, prot, mmu_idx);
861d9bb58e5SYang Zhong 
86255df6fcfSPeter Maydell     address = vaddr_page;
86355df6fcfSPeter Maydell     if (size < TARGET_PAGE_SIZE) {
86430d7e098SRichard Henderson         /* Repeat the MMU check and TLB fill on every access.  */
86530d7e098SRichard Henderson         address |= TLB_INVALID_MASK;
86655df6fcfSPeter Maydell     }
867a26fc6f5STony Nguyen     if (attrs.byte_swap) {
8685b87b3e6SRichard Henderson         address |= TLB_BSWAP;
869a26fc6f5STony Nguyen     }
8708f5db641SRichard Henderson 
8718f5db641SRichard Henderson     is_ram = memory_region_is_ram(section->mr);
8728f5db641SRichard Henderson     is_romd = memory_region_is_romd(section->mr);
8738f5db641SRichard Henderson 
8748f5db641SRichard Henderson     if (is_ram || is_romd) {
8758f5db641SRichard Henderson         /* RAM and ROMD both have associated host memory. */
876d9bb58e5SYang Zhong         addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
8778f5db641SRichard Henderson     } else {
8788f5db641SRichard Henderson         /* I/O does not; force the host address to NULL. */
8798f5db641SRichard Henderson         addend = 0;
880d9bb58e5SYang Zhong     }
881d9bb58e5SYang Zhong 
8828f5db641SRichard Henderson     write_address = address;
8838f5db641SRichard Henderson     if (is_ram) {
8848f5db641SRichard Henderson         iotlb = memory_region_get_ram_addr(section->mr) + xlat;
8858f5db641SRichard Henderson         /*
8868f5db641SRichard Henderson          * Computing is_clean is expensive; avoid all that unless
8878f5db641SRichard Henderson          * the page is actually writable.
8888f5db641SRichard Henderson          */
8898f5db641SRichard Henderson         if (prot & PAGE_WRITE) {
8908f5db641SRichard Henderson             if (section->readonly) {
8918f5db641SRichard Henderson                 write_address |= TLB_DISCARD_WRITE;
8928f5db641SRichard Henderson             } else if (cpu_physical_memory_is_clean(iotlb)) {
8938f5db641SRichard Henderson                 write_address |= TLB_NOTDIRTY;
8948f5db641SRichard Henderson             }
8958f5db641SRichard Henderson         }
8968f5db641SRichard Henderson     } else {
8978f5db641SRichard Henderson         /* I/O or ROMD */
8988f5db641SRichard Henderson         iotlb = memory_region_section_get_iotlb(cpu, section) + xlat;
8998f5db641SRichard Henderson         /*
9008f5db641SRichard Henderson          * Writes to romd devices must go through MMIO to enable write.
9018f5db641SRichard Henderson          * Reads to romd devices go through the ram_ptr found above,
9028f5db641SRichard Henderson          * but of course reads to I/O must go through MMIO.
9038f5db641SRichard Henderson          */
9048f5db641SRichard Henderson         write_address |= TLB_MMIO;
9058f5db641SRichard Henderson         if (!is_romd) {
9068f5db641SRichard Henderson             address = write_address;
9078f5db641SRichard Henderson         }
9088f5db641SRichard Henderson     }
9098f5db641SRichard Henderson 
91050b107c5SRichard Henderson     wp_flags = cpu_watchpoint_address_matches(cpu, vaddr_page,
91150b107c5SRichard Henderson                                               TARGET_PAGE_SIZE);
912d9bb58e5SYang Zhong 
913383beda9SRichard Henderson     index = tlb_index(env, mmu_idx, vaddr_page);
914383beda9SRichard Henderson     te = tlb_entry(env, mmu_idx, vaddr_page);
915d9bb58e5SYang Zhong 
91668fea038SRichard Henderson     /*
91771aec354SEmilio G. Cota      * Hold the TLB lock for the rest of the function. We could acquire/release
91871aec354SEmilio G. Cota      * the lock several times in the function, but it is faster to amortize the
91971aec354SEmilio G. Cota      * acquisition cost by acquiring it just once. Note that this leads to
92071aec354SEmilio G. Cota      * a longer critical section, but this is not a concern since the TLB lock
92171aec354SEmilio G. Cota      * is unlikely to be contended.
92271aec354SEmilio G. Cota      */
923a40ec84eSRichard Henderson     qemu_spin_lock(&tlb->c.lock);
92471aec354SEmilio G. Cota 
9253d1523ceSRichard Henderson     /* Note that the tlb is no longer clean.  */
926a40ec84eSRichard Henderson     tlb->c.dirty |= 1 << mmu_idx;
9273d1523ceSRichard Henderson 
92871aec354SEmilio G. Cota     /* Make sure there's no cached translation for the new page.  */
92971aec354SEmilio G. Cota     tlb_flush_vtlb_page_locked(env, mmu_idx, vaddr_page);
93071aec354SEmilio G. Cota 
93171aec354SEmilio G. Cota     /*
93268fea038SRichard Henderson      * Only evict the old entry to the victim tlb if it's for a
93368fea038SRichard Henderson      * different page; otherwise just overwrite the stale data.
93468fea038SRichard Henderson      */
9353cea94bbSEmilio G. Cota     if (!tlb_hit_page_anyprot(te, vaddr_page) && !tlb_entry_is_empty(te)) {
936a40ec84eSRichard Henderson         unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE;
937a40ec84eSRichard Henderson         CPUTLBEntry *tv = &desc->vtable[vidx];
93868fea038SRichard Henderson 
93968fea038SRichard Henderson         /* Evict the old entry into the victim tlb.  */
94071aec354SEmilio G. Cota         copy_tlb_helper_locked(tv, te);
941a40ec84eSRichard Henderson         desc->viotlb[vidx] = desc->iotlb[index];
94286e1eff8SEmilio G. Cota         tlb_n_used_entries_dec(env, mmu_idx);
94368fea038SRichard Henderson     }
944d9bb58e5SYang Zhong 
945d9bb58e5SYang Zhong     /* refill the tlb */
946ace41090SPeter Maydell     /*
947ace41090SPeter Maydell      * At this point iotlb contains a physical section number in the lower
948ace41090SPeter Maydell      * TARGET_PAGE_BITS, and either
9498f5db641SRichard Henderson      *  + the ram_addr_t of the page base of the target RAM (RAM)
9508f5db641SRichard Henderson      *  + the offset within section->mr of the page base (I/O, ROMD)
95155df6fcfSPeter Maydell      * We subtract the vaddr_page (which is page aligned and thus won't
952ace41090SPeter Maydell      * disturb the low bits) to give an offset which can be added to the
953ace41090SPeter Maydell      * (non-page-aligned) vaddr of the eventual memory access to get
954ace41090SPeter Maydell      * the MemoryRegion offset for the access. Note that the vaddr we
955ace41090SPeter Maydell      * subtract here is that of the page base, and not the same as the
956ace41090SPeter Maydell      * vaddr we add back in io_readx()/io_writex()/get_page_addr_code().
957ace41090SPeter Maydell      */
958a40ec84eSRichard Henderson     desc->iotlb[index].addr = iotlb - vaddr_page;
959a40ec84eSRichard Henderson     desc->iotlb[index].attrs = attrs;
960d9bb58e5SYang Zhong 
961d9bb58e5SYang Zhong     /* Now calculate the new entry */
96255df6fcfSPeter Maydell     tn.addend = addend - vaddr_page;
963d9bb58e5SYang Zhong     if (prot & PAGE_READ) {
964d9bb58e5SYang Zhong         tn.addr_read = address;
96550b107c5SRichard Henderson         if (wp_flags & BP_MEM_READ) {
96650b107c5SRichard Henderson             tn.addr_read |= TLB_WATCHPOINT;
96750b107c5SRichard Henderson         }
968d9bb58e5SYang Zhong     } else {
969d9bb58e5SYang Zhong         tn.addr_read = -1;
970d9bb58e5SYang Zhong     }
971d9bb58e5SYang Zhong 
972d9bb58e5SYang Zhong     if (prot & PAGE_EXEC) {
9738f5db641SRichard Henderson         tn.addr_code = address;
974d9bb58e5SYang Zhong     } else {
975d9bb58e5SYang Zhong         tn.addr_code = -1;
976d9bb58e5SYang Zhong     }
977d9bb58e5SYang Zhong 
978d9bb58e5SYang Zhong     tn.addr_write = -1;
979d9bb58e5SYang Zhong     if (prot & PAGE_WRITE) {
9808f5db641SRichard Henderson         tn.addr_write = write_address;
981f52bfb12SDavid Hildenbrand         if (prot & PAGE_WRITE_INV) {
982f52bfb12SDavid Hildenbrand             tn.addr_write |= TLB_INVALID_MASK;
983f52bfb12SDavid Hildenbrand         }
98450b107c5SRichard Henderson         if (wp_flags & BP_MEM_WRITE) {
98550b107c5SRichard Henderson             tn.addr_write |= TLB_WATCHPOINT;
98650b107c5SRichard Henderson         }
987d9bb58e5SYang Zhong     }
988d9bb58e5SYang Zhong 
98971aec354SEmilio G. Cota     copy_tlb_helper_locked(te, &tn);
99086e1eff8SEmilio G. Cota     tlb_n_used_entries_inc(env, mmu_idx);
991a40ec84eSRichard Henderson     qemu_spin_unlock(&tlb->c.lock);
992d9bb58e5SYang Zhong }
993d9bb58e5SYang Zhong 
994d9bb58e5SYang Zhong /* Add a new TLB entry, but without specifying the memory
995d9bb58e5SYang Zhong  * transaction attributes to be used.
996d9bb58e5SYang Zhong  */
997d9bb58e5SYang Zhong void tlb_set_page(CPUState *cpu, target_ulong vaddr,
998d9bb58e5SYang Zhong                   hwaddr paddr, int prot,
999d9bb58e5SYang Zhong                   int mmu_idx, target_ulong size)
1000d9bb58e5SYang Zhong {
1001d9bb58e5SYang Zhong     tlb_set_page_with_attrs(cpu, vaddr, paddr, MEMTXATTRS_UNSPECIFIED,
1002d9bb58e5SYang Zhong                             prot, mmu_idx, size);
1003d9bb58e5SYang Zhong }
1004d9bb58e5SYang Zhong 
1005d9bb58e5SYang Zhong static inline ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
1006d9bb58e5SYang Zhong {
1007d9bb58e5SYang Zhong     ram_addr_t ram_addr;
1008d9bb58e5SYang Zhong 
1009d9bb58e5SYang Zhong     ram_addr = qemu_ram_addr_from_host(ptr);
1010d9bb58e5SYang Zhong     if (ram_addr == RAM_ADDR_INVALID) {
1011d9bb58e5SYang Zhong         error_report("Bad ram pointer %p", ptr);
1012d9bb58e5SYang Zhong         abort();
1013d9bb58e5SYang Zhong     }
1014d9bb58e5SYang Zhong     return ram_addr;
1015d9bb58e5SYang Zhong }
1016d9bb58e5SYang Zhong 
1017c319dc13SRichard Henderson /*
1018c319dc13SRichard Henderson  * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1019c319dc13SRichard Henderson  * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1020c319dc13SRichard Henderson  * be discarded and looked up again (e.g. via tlb_entry()).
1021c319dc13SRichard Henderson  */
1022c319dc13SRichard Henderson static void tlb_fill(CPUState *cpu, target_ulong addr, int size,
1023c319dc13SRichard Henderson                      MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1024c319dc13SRichard Henderson {
1025c319dc13SRichard Henderson     CPUClass *cc = CPU_GET_CLASS(cpu);
1026c319dc13SRichard Henderson     bool ok;
1027c319dc13SRichard Henderson 
1028c319dc13SRichard Henderson     /*
1029c319dc13SRichard Henderson      * This is not a probe, so only valid return is success; failure
1030c319dc13SRichard Henderson      * should result in exception + longjmp to the cpu loop.
1031c319dc13SRichard Henderson      */
1032c319dc13SRichard Henderson     ok = cc->tlb_fill(cpu, addr, size, access_type, mmu_idx, false, retaddr);
1033c319dc13SRichard Henderson     assert(ok);
1034c319dc13SRichard Henderson }
1035c319dc13SRichard Henderson 
1036d9bb58e5SYang Zhong static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
1037f1be3696SRichard Henderson                          int mmu_idx, target_ulong addr, uintptr_t retaddr,
1038be5c4787STony Nguyen                          MMUAccessType access_type, MemOp op)
1039d9bb58e5SYang Zhong {
104029a0af61SRichard Henderson     CPUState *cpu = env_cpu(env);
10412d54f194SPeter Maydell     hwaddr mr_offset;
10422d54f194SPeter Maydell     MemoryRegionSection *section;
10432d54f194SPeter Maydell     MemoryRegion *mr;
1044d9bb58e5SYang Zhong     uint64_t val;
1045d9bb58e5SYang Zhong     bool locked = false;
104604e3aabdSPeter Maydell     MemTxResult r;
1047d9bb58e5SYang Zhong 
10482d54f194SPeter Maydell     section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
10492d54f194SPeter Maydell     mr = section->mr;
10502d54f194SPeter Maydell     mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
1051d9bb58e5SYang Zhong     cpu->mem_io_pc = retaddr;
105208565552SRichard Henderson     if (!cpu->can_do_io) {
1053d9bb58e5SYang Zhong         cpu_io_recompile(cpu, retaddr);
1054d9bb58e5SYang Zhong     }
1055d9bb58e5SYang Zhong 
10568b812533SAlex Bennée     if (mr->global_locking && !qemu_mutex_iothread_locked()) {
1057d9bb58e5SYang Zhong         qemu_mutex_lock_iothread();
1058d9bb58e5SYang Zhong         locked = true;
1059d9bb58e5SYang Zhong     }
1060be5c4787STony Nguyen     r = memory_region_dispatch_read(mr, mr_offset, &val, op, iotlbentry->attrs);
106104e3aabdSPeter Maydell     if (r != MEMTX_OK) {
10622d54f194SPeter Maydell         hwaddr physaddr = mr_offset +
10632d54f194SPeter Maydell             section->offset_within_address_space -
10642d54f194SPeter Maydell             section->offset_within_region;
10652d54f194SPeter Maydell 
1066be5c4787STony Nguyen         cpu_transaction_failed(cpu, physaddr, addr, memop_size(op), access_type,
106704e3aabdSPeter Maydell                                mmu_idx, iotlbentry->attrs, r, retaddr);
106804e3aabdSPeter Maydell     }
1069d9bb58e5SYang Zhong     if (locked) {
1070d9bb58e5SYang Zhong         qemu_mutex_unlock_iothread();
1071d9bb58e5SYang Zhong     }
1072d9bb58e5SYang Zhong 
1073d9bb58e5SYang Zhong     return val;
1074d9bb58e5SYang Zhong }
1075d9bb58e5SYang Zhong 
1076*2f3a57eeSAlex Bennée /*
1077*2f3a57eeSAlex Bennée  * Save a potentially trashed IOTLB entry for later lookup by plugin.
1078*2f3a57eeSAlex Bennée  *
1079*2f3a57eeSAlex Bennée  * We also need to track the thread storage address because the RCU
1080*2f3a57eeSAlex Bennée  * cleanup that runs when we leave the critical region (the current
1081*2f3a57eeSAlex Bennée  * execution) is actually in a different thread.
1082*2f3a57eeSAlex Bennée  */
1083*2f3a57eeSAlex Bennée static void save_iotlb_data(CPUState *cs, hwaddr addr,
1084*2f3a57eeSAlex Bennée                             MemoryRegionSection *section, hwaddr mr_offset)
1085*2f3a57eeSAlex Bennée {
1086*2f3a57eeSAlex Bennée #ifdef CONFIG_PLUGIN
1087*2f3a57eeSAlex Bennée     SavedIOTLB *saved = &cs->saved_iotlb;
1088*2f3a57eeSAlex Bennée     saved->addr = addr;
1089*2f3a57eeSAlex Bennée     saved->section = section;
1090*2f3a57eeSAlex Bennée     saved->mr_offset = mr_offset;
1091*2f3a57eeSAlex Bennée #endif
1092*2f3a57eeSAlex Bennée }
1093*2f3a57eeSAlex Bennée 
1094d9bb58e5SYang Zhong static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
1095f1be3696SRichard Henderson                       int mmu_idx, uint64_t val, target_ulong addr,
1096be5c4787STony Nguyen                       uintptr_t retaddr, MemOp op)
1097d9bb58e5SYang Zhong {
109829a0af61SRichard Henderson     CPUState *cpu = env_cpu(env);
10992d54f194SPeter Maydell     hwaddr mr_offset;
11002d54f194SPeter Maydell     MemoryRegionSection *section;
11012d54f194SPeter Maydell     MemoryRegion *mr;
1102d9bb58e5SYang Zhong     bool locked = false;
110304e3aabdSPeter Maydell     MemTxResult r;
1104d9bb58e5SYang Zhong 
11052d54f194SPeter Maydell     section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
11062d54f194SPeter Maydell     mr = section->mr;
11072d54f194SPeter Maydell     mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
110808565552SRichard Henderson     if (!cpu->can_do_io) {
1109d9bb58e5SYang Zhong         cpu_io_recompile(cpu, retaddr);
1110d9bb58e5SYang Zhong     }
1111d9bb58e5SYang Zhong     cpu->mem_io_pc = retaddr;
1112d9bb58e5SYang Zhong 
1113*2f3a57eeSAlex Bennée     /*
1114*2f3a57eeSAlex Bennée      * The memory_region_dispatch may trigger a flush/resize
1115*2f3a57eeSAlex Bennée      * so for plugins we save the iotlb_data just in case.
1116*2f3a57eeSAlex Bennée      */
1117*2f3a57eeSAlex Bennée     save_iotlb_data(cpu, iotlbentry->addr, section, mr_offset);
1118*2f3a57eeSAlex Bennée 
11198b812533SAlex Bennée     if (mr->global_locking && !qemu_mutex_iothread_locked()) {
1120d9bb58e5SYang Zhong         qemu_mutex_lock_iothread();
1121d9bb58e5SYang Zhong         locked = true;
1122d9bb58e5SYang Zhong     }
1123be5c4787STony Nguyen     r = memory_region_dispatch_write(mr, mr_offset, val, op, iotlbentry->attrs);
112404e3aabdSPeter Maydell     if (r != MEMTX_OK) {
11252d54f194SPeter Maydell         hwaddr physaddr = mr_offset +
11262d54f194SPeter Maydell             section->offset_within_address_space -
11272d54f194SPeter Maydell             section->offset_within_region;
11282d54f194SPeter Maydell 
1129be5c4787STony Nguyen         cpu_transaction_failed(cpu, physaddr, addr, memop_size(op),
1130be5c4787STony Nguyen                                MMU_DATA_STORE, mmu_idx, iotlbentry->attrs, r,
1131be5c4787STony Nguyen                                retaddr);
113204e3aabdSPeter Maydell     }
1133d9bb58e5SYang Zhong     if (locked) {
1134d9bb58e5SYang Zhong         qemu_mutex_unlock_iothread();
1135d9bb58e5SYang Zhong     }
1136d9bb58e5SYang Zhong }
1137d9bb58e5SYang Zhong 
11384811e909SRichard Henderson static inline target_ulong tlb_read_ofs(CPUTLBEntry *entry, size_t ofs)
11394811e909SRichard Henderson {
11404811e909SRichard Henderson #if TCG_OVERSIZED_GUEST
11414811e909SRichard Henderson     return *(target_ulong *)((uintptr_t)entry + ofs);
11424811e909SRichard Henderson #else
11434811e909SRichard Henderson     /* ofs might correspond to .addr_write, so use atomic_read */
11444811e909SRichard Henderson     return atomic_read((target_ulong *)((uintptr_t)entry + ofs));
11454811e909SRichard Henderson #endif
11464811e909SRichard Henderson }
11474811e909SRichard Henderson 
1148d9bb58e5SYang Zhong /* Return true if ADDR is present in the victim tlb, and has been copied
1149d9bb58e5SYang Zhong    back to the main tlb.  */
1150d9bb58e5SYang Zhong static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index,
1151d9bb58e5SYang Zhong                            size_t elt_ofs, target_ulong page)
1152d9bb58e5SYang Zhong {
1153d9bb58e5SYang Zhong     size_t vidx;
115471aec354SEmilio G. Cota 
115529a0af61SRichard Henderson     assert_cpu_is_self(env_cpu(env));
1156d9bb58e5SYang Zhong     for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) {
1157a40ec84eSRichard Henderson         CPUTLBEntry *vtlb = &env_tlb(env)->d[mmu_idx].vtable[vidx];
1158a40ec84eSRichard Henderson         target_ulong cmp;
1159a40ec84eSRichard Henderson 
1160a40ec84eSRichard Henderson         /* elt_ofs might correspond to .addr_write, so use atomic_read */
1161a40ec84eSRichard Henderson #if TCG_OVERSIZED_GUEST
1162a40ec84eSRichard Henderson         cmp = *(target_ulong *)((uintptr_t)vtlb + elt_ofs);
1163a40ec84eSRichard Henderson #else
1164a40ec84eSRichard Henderson         cmp = atomic_read((target_ulong *)((uintptr_t)vtlb + elt_ofs));
1165a40ec84eSRichard Henderson #endif
1166d9bb58e5SYang Zhong 
1167d9bb58e5SYang Zhong         if (cmp == page) {
1168d9bb58e5SYang Zhong             /* Found entry in victim tlb, swap tlb and iotlb.  */
1169a40ec84eSRichard Henderson             CPUTLBEntry tmptlb, *tlb = &env_tlb(env)->f[mmu_idx].table[index];
1170d9bb58e5SYang Zhong 
1171a40ec84eSRichard Henderson             qemu_spin_lock(&env_tlb(env)->c.lock);
117271aec354SEmilio G. Cota             copy_tlb_helper_locked(&tmptlb, tlb);
117371aec354SEmilio G. Cota             copy_tlb_helper_locked(tlb, vtlb);
117471aec354SEmilio G. Cota             copy_tlb_helper_locked(vtlb, &tmptlb);
1175a40ec84eSRichard Henderson             qemu_spin_unlock(&env_tlb(env)->c.lock);
1176d9bb58e5SYang Zhong 
1177a40ec84eSRichard Henderson             CPUIOTLBEntry tmpio, *io = &env_tlb(env)->d[mmu_idx].iotlb[index];
1178a40ec84eSRichard Henderson             CPUIOTLBEntry *vio = &env_tlb(env)->d[mmu_idx].viotlb[vidx];
1179d9bb58e5SYang Zhong             tmpio = *io; *io = *vio; *vio = tmpio;
1180d9bb58e5SYang Zhong             return true;
1181d9bb58e5SYang Zhong         }
1182d9bb58e5SYang Zhong     }
1183d9bb58e5SYang Zhong     return false;
1184d9bb58e5SYang Zhong }
1185d9bb58e5SYang Zhong 
1186d9bb58e5SYang Zhong /* Macro to call the above, with local variables from the use context.  */
1187d9bb58e5SYang Zhong #define VICTIM_TLB_HIT(TY, ADDR) \
1188d9bb58e5SYang Zhong   victim_tlb_hit(env, mmu_idx, index, offsetof(CPUTLBEntry, TY), \
1189d9bb58e5SYang Zhong                  (ADDR) & TARGET_PAGE_MASK)
1190d9bb58e5SYang Zhong 
119130d7e098SRichard Henderson /*
119230d7e098SRichard Henderson  * Return a ram_addr_t for the virtual address for execution.
119330d7e098SRichard Henderson  *
119430d7e098SRichard Henderson  * Return -1 if we can't translate and execute from an entire page
119530d7e098SRichard Henderson  * of RAM.  This will force us to execute by loading and translating
119630d7e098SRichard Henderson  * one insn at a time, without caching.
119730d7e098SRichard Henderson  *
119830d7e098SRichard Henderson  * NOTE: This function will trigger an exception if the page is
119930d7e098SRichard Henderson  * not executable.
1200f2553f04SKONRAD Frederic  */
12014b2190daSEmilio G. Cota tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
12024b2190daSEmilio G. Cota                                         void **hostp)
1203f2553f04SKONRAD Frederic {
1204383beda9SRichard Henderson     uintptr_t mmu_idx = cpu_mmu_index(env, true);
1205383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1206383beda9SRichard Henderson     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1207f2553f04SKONRAD Frederic     void *p;
1208f2553f04SKONRAD Frederic 
1209383beda9SRichard Henderson     if (unlikely(!tlb_hit(entry->addr_code, addr))) {
1210b493ccf1SPeter Maydell         if (!VICTIM_TLB_HIT(addr_code, addr)) {
121129a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, 0, MMU_INST_FETCH, mmu_idx, 0);
12126d967cb8SEmilio G. Cota             index = tlb_index(env, mmu_idx, addr);
12136d967cb8SEmilio G. Cota             entry = tlb_entry(env, mmu_idx, addr);
121430d7e098SRichard Henderson 
121530d7e098SRichard Henderson             if (unlikely(entry->addr_code & TLB_INVALID_MASK)) {
121630d7e098SRichard Henderson                 /*
121730d7e098SRichard Henderson                  * The MMU protection covers a smaller range than a target
121830d7e098SRichard Henderson                  * page, so we must redo the MMU check for every insn.
121930d7e098SRichard Henderson                  */
122030d7e098SRichard Henderson                 return -1;
122130d7e098SRichard Henderson             }
122271b9a453SKONRAD Frederic         }
1223383beda9SRichard Henderson         assert(tlb_hit(entry->addr_code, addr));
1224f2553f04SKONRAD Frederic     }
122555df6fcfSPeter Maydell 
122630d7e098SRichard Henderson     if (unlikely(entry->addr_code & TLB_MMIO)) {
122730d7e098SRichard Henderson         /* The region is not backed by RAM.  */
12284b2190daSEmilio G. Cota         if (hostp) {
12294b2190daSEmilio G. Cota             *hostp = NULL;
12304b2190daSEmilio G. Cota         }
123120cb6ae4SPeter Maydell         return -1;
123255df6fcfSPeter Maydell     }
123355df6fcfSPeter Maydell 
1234383beda9SRichard Henderson     p = (void *)((uintptr_t)addr + entry->addend);
12354b2190daSEmilio G. Cota     if (hostp) {
12364b2190daSEmilio G. Cota         *hostp = p;
12374b2190daSEmilio G. Cota     }
1238f2553f04SKONRAD Frederic     return qemu_ram_addr_from_host_nofail(p);
1239f2553f04SKONRAD Frederic }
1240f2553f04SKONRAD Frederic 
12414b2190daSEmilio G. Cota tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)
12424b2190daSEmilio G. Cota {
12434b2190daSEmilio G. Cota     return get_page_addr_code_hostp(env, addr, NULL);
12444b2190daSEmilio G. Cota }
12454b2190daSEmilio G. Cota 
1246707526adSRichard Henderson static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
1247707526adSRichard Henderson                            CPUIOTLBEntry *iotlbentry, uintptr_t retaddr)
1248707526adSRichard Henderson {
1249707526adSRichard Henderson     ram_addr_t ram_addr = mem_vaddr + iotlbentry->addr;
1250707526adSRichard Henderson 
1251707526adSRichard Henderson     trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
1252707526adSRichard Henderson 
1253707526adSRichard Henderson     if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
1254707526adSRichard Henderson         struct page_collection *pages
1255707526adSRichard Henderson             = page_collection_lock(ram_addr, ram_addr + size);
12565a7c27bbSRichard Henderson         tb_invalidate_phys_page_fast(pages, ram_addr, size, retaddr);
1257707526adSRichard Henderson         page_collection_unlock(pages);
1258707526adSRichard Henderson     }
1259707526adSRichard Henderson 
1260707526adSRichard Henderson     /*
1261707526adSRichard Henderson      * Set both VGA and migration bits for simplicity and to remove
1262707526adSRichard Henderson      * the notdirty callback faster.
1263707526adSRichard Henderson      */
1264707526adSRichard Henderson     cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE);
1265707526adSRichard Henderson 
1266707526adSRichard Henderson     /* We remove the notdirty callback only if the code has been flushed. */
1267707526adSRichard Henderson     if (!cpu_physical_memory_is_clean(ram_addr)) {
1268707526adSRichard Henderson         trace_memory_notdirty_set_dirty(mem_vaddr);
1269707526adSRichard Henderson         tlb_set_dirty(cpu, mem_vaddr);
1270707526adSRichard Henderson     }
1271707526adSRichard Henderson }
1272707526adSRichard Henderson 
1273069cfe77SRichard Henderson static int probe_access_internal(CPUArchState *env, target_ulong addr,
1274069cfe77SRichard Henderson                                  int fault_size, MMUAccessType access_type,
1275069cfe77SRichard Henderson                                  int mmu_idx, bool nonfault,
1276069cfe77SRichard Henderson                                  void **phost, uintptr_t retaddr)
1277d9bb58e5SYang Zhong {
1278383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1279383beda9SRichard Henderson     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1280069cfe77SRichard Henderson     target_ulong tlb_addr, page_addr;
1281c25c283dSDavid Hildenbrand     size_t elt_ofs;
1282069cfe77SRichard Henderson     int flags;
1283ca86cf32SDavid Hildenbrand 
1284c25c283dSDavid Hildenbrand     switch (access_type) {
1285c25c283dSDavid Hildenbrand     case MMU_DATA_LOAD:
1286c25c283dSDavid Hildenbrand         elt_ofs = offsetof(CPUTLBEntry, addr_read);
1287c25c283dSDavid Hildenbrand         break;
1288c25c283dSDavid Hildenbrand     case MMU_DATA_STORE:
1289c25c283dSDavid Hildenbrand         elt_ofs = offsetof(CPUTLBEntry, addr_write);
1290c25c283dSDavid Hildenbrand         break;
1291c25c283dSDavid Hildenbrand     case MMU_INST_FETCH:
1292c25c283dSDavid Hildenbrand         elt_ofs = offsetof(CPUTLBEntry, addr_code);
1293c25c283dSDavid Hildenbrand         break;
1294c25c283dSDavid Hildenbrand     default:
1295c25c283dSDavid Hildenbrand         g_assert_not_reached();
1296c25c283dSDavid Hildenbrand     }
1297c25c283dSDavid Hildenbrand     tlb_addr = tlb_read_ofs(entry, elt_ofs);
1298c25c283dSDavid Hildenbrand 
1299069cfe77SRichard Henderson     page_addr = addr & TARGET_PAGE_MASK;
1300069cfe77SRichard Henderson     if (!tlb_hit_page(tlb_addr, page_addr)) {
1301069cfe77SRichard Henderson         if (!victim_tlb_hit(env, mmu_idx, index, elt_ofs, page_addr)) {
1302069cfe77SRichard Henderson             CPUState *cs = env_cpu(env);
1303069cfe77SRichard Henderson             CPUClass *cc = CPU_GET_CLASS(cs);
1304069cfe77SRichard Henderson 
1305069cfe77SRichard Henderson             if (!cc->tlb_fill(cs, addr, fault_size, access_type,
1306069cfe77SRichard Henderson                               mmu_idx, nonfault, retaddr)) {
1307069cfe77SRichard Henderson                 /* Non-faulting page table read failed.  */
1308069cfe77SRichard Henderson                 *phost = NULL;
1309069cfe77SRichard Henderson                 return TLB_INVALID_MASK;
1310069cfe77SRichard Henderson             }
1311069cfe77SRichard Henderson 
131203a98189SDavid Hildenbrand             /* TLB resize via tlb_fill may have moved the entry.  */
131303a98189SDavid Hildenbrand             entry = tlb_entry(env, mmu_idx, addr);
1314d9bb58e5SYang Zhong         }
1315c25c283dSDavid Hildenbrand         tlb_addr = tlb_read_ofs(entry, elt_ofs);
131603a98189SDavid Hildenbrand     }
1317069cfe77SRichard Henderson     flags = tlb_addr & TLB_FLAGS_MASK;
131803a98189SDavid Hildenbrand 
1319069cfe77SRichard Henderson     /* Fold all "mmio-like" bits into TLB_MMIO.  This is not RAM.  */
1320069cfe77SRichard Henderson     if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1321069cfe77SRichard Henderson         *phost = NULL;
1322069cfe77SRichard Henderson         return TLB_MMIO;
1323fef39ccdSDavid Hildenbrand     }
1324fef39ccdSDavid Hildenbrand 
1325069cfe77SRichard Henderson     /* Everything else is RAM. */
1326069cfe77SRichard Henderson     *phost = (void *)((uintptr_t)addr + entry->addend);
1327069cfe77SRichard Henderson     return flags;
1328069cfe77SRichard Henderson }
1329069cfe77SRichard Henderson 
1330069cfe77SRichard Henderson int probe_access_flags(CPUArchState *env, target_ulong addr,
1331069cfe77SRichard Henderson                        MMUAccessType access_type, int mmu_idx,
1332069cfe77SRichard Henderson                        bool nonfault, void **phost, uintptr_t retaddr)
1333069cfe77SRichard Henderson {
1334069cfe77SRichard Henderson     int flags;
1335069cfe77SRichard Henderson 
1336069cfe77SRichard Henderson     flags = probe_access_internal(env, addr, 0, access_type, mmu_idx,
1337069cfe77SRichard Henderson                                   nonfault, phost, retaddr);
1338069cfe77SRichard Henderson 
1339069cfe77SRichard Henderson     /* Handle clean RAM pages.  */
1340069cfe77SRichard Henderson     if (unlikely(flags & TLB_NOTDIRTY)) {
1341069cfe77SRichard Henderson         uintptr_t index = tlb_index(env, mmu_idx, addr);
134273bc0bd4SRichard Henderson         CPUIOTLBEntry *iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
134373bc0bd4SRichard Henderson 
1344069cfe77SRichard Henderson         notdirty_write(env_cpu(env), addr, 1, iotlbentry, retaddr);
1345069cfe77SRichard Henderson         flags &= ~TLB_NOTDIRTY;
1346069cfe77SRichard Henderson     }
1347069cfe77SRichard Henderson 
1348069cfe77SRichard Henderson     return flags;
1349069cfe77SRichard Henderson }
1350069cfe77SRichard Henderson 
1351069cfe77SRichard Henderson void *probe_access(CPUArchState *env, target_ulong addr, int size,
1352069cfe77SRichard Henderson                    MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1353069cfe77SRichard Henderson {
1354069cfe77SRichard Henderson     void *host;
1355069cfe77SRichard Henderson     int flags;
1356069cfe77SRichard Henderson 
1357069cfe77SRichard Henderson     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
1358069cfe77SRichard Henderson 
1359069cfe77SRichard Henderson     flags = probe_access_internal(env, addr, size, access_type, mmu_idx,
1360069cfe77SRichard Henderson                                   false, &host, retaddr);
1361069cfe77SRichard Henderson 
1362069cfe77SRichard Henderson     /* Per the interface, size == 0 merely faults the access. */
1363069cfe77SRichard Henderson     if (size == 0) {
136473bc0bd4SRichard Henderson         return NULL;
136573bc0bd4SRichard Henderson     }
136673bc0bd4SRichard Henderson 
1367069cfe77SRichard Henderson     if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) {
1368069cfe77SRichard Henderson         uintptr_t index = tlb_index(env, mmu_idx, addr);
1369069cfe77SRichard Henderson         CPUIOTLBEntry *iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
1370069cfe77SRichard Henderson 
137103a98189SDavid Hildenbrand         /* Handle watchpoints.  */
1372069cfe77SRichard Henderson         if (flags & TLB_WATCHPOINT) {
1373069cfe77SRichard Henderson             int wp_access = (access_type == MMU_DATA_STORE
1374069cfe77SRichard Henderson                              ? BP_MEM_WRITE : BP_MEM_READ);
137503a98189SDavid Hildenbrand             cpu_check_watchpoint(env_cpu(env), addr, size,
137673bc0bd4SRichard Henderson                                  iotlbentry->attrs, wp_access, retaddr);
1377d9bb58e5SYang Zhong         }
1378fef39ccdSDavid Hildenbrand 
137973bc0bd4SRichard Henderson         /* Handle clean RAM pages.  */
1380069cfe77SRichard Henderson         if (flags & TLB_NOTDIRTY) {
1381069cfe77SRichard Henderson             notdirty_write(env_cpu(env), addr, 1, iotlbentry, retaddr);
138273bc0bd4SRichard Henderson         }
1383fef39ccdSDavid Hildenbrand     }
1384fef39ccdSDavid Hildenbrand 
1385069cfe77SRichard Henderson     return host;
1386d9bb58e5SYang Zhong }
1387d9bb58e5SYang Zhong 
13884811e909SRichard Henderson void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
13894811e909SRichard Henderson                         MMUAccessType access_type, int mmu_idx)
13904811e909SRichard Henderson {
1391069cfe77SRichard Henderson     void *host;
1392069cfe77SRichard Henderson     int flags;
13934811e909SRichard Henderson 
1394069cfe77SRichard Henderson     flags = probe_access_internal(env, addr, 0, access_type,
1395069cfe77SRichard Henderson                                   mmu_idx, true, &host, 0);
1396069cfe77SRichard Henderson 
1397069cfe77SRichard Henderson     /* No combination of flags are expected by the caller. */
1398069cfe77SRichard Henderson     return flags ? NULL : host;
13994811e909SRichard Henderson }
14004811e909SRichard Henderson 
1401235537faSAlex Bennée #ifdef CONFIG_PLUGIN
1402235537faSAlex Bennée /*
1403235537faSAlex Bennée  * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1404235537faSAlex Bennée  * This should be a hot path as we will have just looked this path up
1405235537faSAlex Bennée  * in the softmmu lookup code (or helper). We don't handle re-fills or
1406235537faSAlex Bennée  * checking the victim table. This is purely informational.
1407235537faSAlex Bennée  *
1408*2f3a57eeSAlex Bennée  * This almost never fails as the memory access being instrumented
1409*2f3a57eeSAlex Bennée  * should have just filled the TLB. The one corner case is io_writex
1410*2f3a57eeSAlex Bennée  * which can cause TLB flushes and potential resizing of the TLBs
1411*2f3a57eeSAlex Bennée  * loosing the information we need. In those cases we need to recover
1412*2f3a57eeSAlex Bennée  * data from a copy of the io_tlb entry.
1413235537faSAlex Bennée  */
1414235537faSAlex Bennée 
1415235537faSAlex Bennée bool tlb_plugin_lookup(CPUState *cpu, target_ulong addr, int mmu_idx,
1416235537faSAlex Bennée                        bool is_store, struct qemu_plugin_hwaddr *data)
1417235537faSAlex Bennée {
1418235537faSAlex Bennée     CPUArchState *env = cpu->env_ptr;
1419235537faSAlex Bennée     CPUTLBEntry *tlbe = tlb_entry(env, mmu_idx, addr);
1420235537faSAlex Bennée     uintptr_t index = tlb_index(env, mmu_idx, addr);
1421235537faSAlex Bennée     target_ulong tlb_addr = is_store ? tlb_addr_write(tlbe) : tlbe->addr_read;
1422235537faSAlex Bennée 
1423235537faSAlex Bennée     if (likely(tlb_hit(tlb_addr, addr))) {
1424235537faSAlex Bennée         /* We must have an iotlb entry for MMIO */
1425235537faSAlex Bennée         if (tlb_addr & TLB_MMIO) {
1426235537faSAlex Bennée             CPUIOTLBEntry *iotlbentry;
1427235537faSAlex Bennée             iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
1428235537faSAlex Bennée             data->is_io = true;
1429235537faSAlex Bennée             data->v.io.section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
1430235537faSAlex Bennée             data->v.io.offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
1431235537faSAlex Bennée         } else {
1432235537faSAlex Bennée             data->is_io = false;
1433235537faSAlex Bennée             data->v.ram.hostaddr = addr + tlbe->addend;
1434235537faSAlex Bennée         }
1435235537faSAlex Bennée         return true;
1436*2f3a57eeSAlex Bennée     } else {
1437*2f3a57eeSAlex Bennée         SavedIOTLB *saved = &cpu->saved_iotlb;
1438*2f3a57eeSAlex Bennée         data->is_io = true;
1439*2f3a57eeSAlex Bennée         data->v.io.section = saved->section;
1440*2f3a57eeSAlex Bennée         data->v.io.offset = saved->mr_offset;
1441*2f3a57eeSAlex Bennée         return true;
1442235537faSAlex Bennée     }
1443235537faSAlex Bennée }
1444235537faSAlex Bennée 
1445235537faSAlex Bennée #endif
1446235537faSAlex Bennée 
1447d9bb58e5SYang Zhong /* Probe for a read-modify-write atomic operation.  Do not allow unaligned
1448d9bb58e5SYang Zhong  * operations, or io operations to proceed.  Return the host address.  */
1449d9bb58e5SYang Zhong static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
1450707526adSRichard Henderson                                TCGMemOpIdx oi, uintptr_t retaddr)
1451d9bb58e5SYang Zhong {
1452d9bb58e5SYang Zhong     size_t mmu_idx = get_mmuidx(oi);
1453383beda9SRichard Henderson     uintptr_t index = tlb_index(env, mmu_idx, addr);
1454383beda9SRichard Henderson     CPUTLBEntry *tlbe = tlb_entry(env, mmu_idx, addr);
1455403f290cSEmilio G. Cota     target_ulong tlb_addr = tlb_addr_write(tlbe);
145614776ab5STony Nguyen     MemOp mop = get_memop(oi);
1457d9bb58e5SYang Zhong     int a_bits = get_alignment_bits(mop);
1458d9bb58e5SYang Zhong     int s_bits = mop & MO_SIZE;
145934d49937SPeter Maydell     void *hostaddr;
1460d9bb58e5SYang Zhong 
1461d9bb58e5SYang Zhong     /* Adjust the given return address.  */
1462d9bb58e5SYang Zhong     retaddr -= GETPC_ADJ;
1463d9bb58e5SYang Zhong 
1464d9bb58e5SYang Zhong     /* Enforce guest required alignment.  */
1465d9bb58e5SYang Zhong     if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) {
1466d9bb58e5SYang Zhong         /* ??? Maybe indicate atomic op to cpu_unaligned_access */
146729a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE,
1468d9bb58e5SYang Zhong                              mmu_idx, retaddr);
1469d9bb58e5SYang Zhong     }
1470d9bb58e5SYang Zhong 
1471d9bb58e5SYang Zhong     /* Enforce qemu required alignment.  */
1472d9bb58e5SYang Zhong     if (unlikely(addr & ((1 << s_bits) - 1))) {
1473d9bb58e5SYang Zhong         /* We get here if guest alignment was not requested,
1474d9bb58e5SYang Zhong            or was not enforced by cpu_unaligned_access above.
1475d9bb58e5SYang Zhong            We might widen the access and emulate, but for now
1476d9bb58e5SYang Zhong            mark an exception and exit the cpu loop.  */
1477d9bb58e5SYang Zhong         goto stop_the_world;
1478d9bb58e5SYang Zhong     }
1479d9bb58e5SYang Zhong 
1480d9bb58e5SYang Zhong     /* Check TLB entry and enforce page permissions.  */
1481334692bcSPeter Maydell     if (!tlb_hit(tlb_addr, addr)) {
1482d9bb58e5SYang Zhong         if (!VICTIM_TLB_HIT(addr_write, addr)) {
148329a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, 1 << s_bits, MMU_DATA_STORE,
148498670d47SLaurent Vivier                      mmu_idx, retaddr);
14856d967cb8SEmilio G. Cota             index = tlb_index(env, mmu_idx, addr);
14866d967cb8SEmilio G. Cota             tlbe = tlb_entry(env, mmu_idx, addr);
1487d9bb58e5SYang Zhong         }
1488403f290cSEmilio G. Cota         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
1489d9bb58e5SYang Zhong     }
1490d9bb58e5SYang Zhong 
149155df6fcfSPeter Maydell     /* Notice an IO access or a needs-MMU-lookup access */
149230d7e098SRichard Henderson     if (unlikely(tlb_addr & TLB_MMIO)) {
1493d9bb58e5SYang Zhong         /* There's really nothing that can be done to
1494d9bb58e5SYang Zhong            support this apart from stop-the-world.  */
1495d9bb58e5SYang Zhong         goto stop_the_world;
1496d9bb58e5SYang Zhong     }
1497d9bb58e5SYang Zhong 
1498d9bb58e5SYang Zhong     /* Let the guest notice RMW on a write-only page.  */
149934d49937SPeter Maydell     if (unlikely(tlbe->addr_read != (tlb_addr & ~TLB_NOTDIRTY))) {
150029a0af61SRichard Henderson         tlb_fill(env_cpu(env), addr, 1 << s_bits, MMU_DATA_LOAD,
150198670d47SLaurent Vivier                  mmu_idx, retaddr);
1502d9bb58e5SYang Zhong         /* Since we don't support reads and writes to different addresses,
1503d9bb58e5SYang Zhong            and we do have the proper page loaded for write, this shouldn't
1504d9bb58e5SYang Zhong            ever return.  But just in case, handle via stop-the-world.  */
1505d9bb58e5SYang Zhong         goto stop_the_world;
1506d9bb58e5SYang Zhong     }
1507d9bb58e5SYang Zhong 
150834d49937SPeter Maydell     hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
150934d49937SPeter Maydell 
151034d49937SPeter Maydell     if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
1511707526adSRichard Henderson         notdirty_write(env_cpu(env), addr, 1 << s_bits,
1512707526adSRichard Henderson                        &env_tlb(env)->d[mmu_idx].iotlb[index], retaddr);
151334d49937SPeter Maydell     }
151434d49937SPeter Maydell 
151534d49937SPeter Maydell     return hostaddr;
1516d9bb58e5SYang Zhong 
1517d9bb58e5SYang Zhong  stop_the_world:
151829a0af61SRichard Henderson     cpu_loop_exit_atomic(env_cpu(env), retaddr);
1519d9bb58e5SYang Zhong }
1520d9bb58e5SYang Zhong 
1521eed56642SAlex Bennée /*
1522eed56642SAlex Bennée  * Load Helpers
1523eed56642SAlex Bennée  *
1524eed56642SAlex Bennée  * We support two different access types. SOFTMMU_CODE_ACCESS is
1525eed56642SAlex Bennée  * specifically for reading instructions from system memory. It is
1526eed56642SAlex Bennée  * called by the translation loop and in some helpers where the code
1527eed56642SAlex Bennée  * is disassembled. It shouldn't be called directly by guest code.
1528eed56642SAlex Bennée  */
1529d9bb58e5SYang Zhong 
15302dd92606SRichard Henderson typedef uint64_t FullLoadHelper(CPUArchState *env, target_ulong addr,
15312dd92606SRichard Henderson                                 TCGMemOpIdx oi, uintptr_t retaddr);
15322dd92606SRichard Henderson 
1533c6b716cdSRichard Henderson static inline uint64_t QEMU_ALWAYS_INLINE
153480d9d1c6SRichard Henderson load_memop(const void *haddr, MemOp op)
153580d9d1c6SRichard Henderson {
153680d9d1c6SRichard Henderson     switch (op) {
153780d9d1c6SRichard Henderson     case MO_UB:
153880d9d1c6SRichard Henderson         return ldub_p(haddr);
153980d9d1c6SRichard Henderson     case MO_BEUW:
154080d9d1c6SRichard Henderson         return lduw_be_p(haddr);
154180d9d1c6SRichard Henderson     case MO_LEUW:
154280d9d1c6SRichard Henderson         return lduw_le_p(haddr);
154380d9d1c6SRichard Henderson     case MO_BEUL:
154480d9d1c6SRichard Henderson         return (uint32_t)ldl_be_p(haddr);
154580d9d1c6SRichard Henderson     case MO_LEUL:
154680d9d1c6SRichard Henderson         return (uint32_t)ldl_le_p(haddr);
154780d9d1c6SRichard Henderson     case MO_BEQ:
154880d9d1c6SRichard Henderson         return ldq_be_p(haddr);
154980d9d1c6SRichard Henderson     case MO_LEQ:
155080d9d1c6SRichard Henderson         return ldq_le_p(haddr);
155180d9d1c6SRichard Henderson     default:
155280d9d1c6SRichard Henderson         qemu_build_not_reached();
155380d9d1c6SRichard Henderson     }
155480d9d1c6SRichard Henderson }
155580d9d1c6SRichard Henderson 
155680d9d1c6SRichard Henderson static inline uint64_t QEMU_ALWAYS_INLINE
15572dd92606SRichard Henderson load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
1558be5c4787STony Nguyen             uintptr_t retaddr, MemOp op, bool code_read,
15592dd92606SRichard Henderson             FullLoadHelper *full_load)
1560eed56642SAlex Bennée {
1561eed56642SAlex Bennée     uintptr_t mmu_idx = get_mmuidx(oi);
1562eed56642SAlex Bennée     uintptr_t index = tlb_index(env, mmu_idx, addr);
1563eed56642SAlex Bennée     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
1564eed56642SAlex Bennée     target_ulong tlb_addr = code_read ? entry->addr_code : entry->addr_read;
1565eed56642SAlex Bennée     const size_t tlb_off = code_read ?
1566eed56642SAlex Bennée         offsetof(CPUTLBEntry, addr_code) : offsetof(CPUTLBEntry, addr_read);
1567f1be3696SRichard Henderson     const MMUAccessType access_type =
1568f1be3696SRichard Henderson         code_read ? MMU_INST_FETCH : MMU_DATA_LOAD;
1569eed56642SAlex Bennée     unsigned a_bits = get_alignment_bits(get_memop(oi));
1570eed56642SAlex Bennée     void *haddr;
1571eed56642SAlex Bennée     uint64_t res;
1572be5c4787STony Nguyen     size_t size = memop_size(op);
1573d9bb58e5SYang Zhong 
1574eed56642SAlex Bennée     /* Handle CPU specific unaligned behaviour */
1575eed56642SAlex Bennée     if (addr & ((1 << a_bits) - 1)) {
157629a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, access_type,
1577eed56642SAlex Bennée                              mmu_idx, retaddr);
1578eed56642SAlex Bennée     }
1579eed56642SAlex Bennée 
1580eed56642SAlex Bennée     /* If the TLB entry is for a different page, reload and try again.  */
1581eed56642SAlex Bennée     if (!tlb_hit(tlb_addr, addr)) {
1582eed56642SAlex Bennée         if (!victim_tlb_hit(env, mmu_idx, index, tlb_off,
1583eed56642SAlex Bennée                             addr & TARGET_PAGE_MASK)) {
158429a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size,
1585f1be3696SRichard Henderson                      access_type, mmu_idx, retaddr);
1586eed56642SAlex Bennée             index = tlb_index(env, mmu_idx, addr);
1587eed56642SAlex Bennée             entry = tlb_entry(env, mmu_idx, addr);
1588eed56642SAlex Bennée         }
1589eed56642SAlex Bennée         tlb_addr = code_read ? entry->addr_code : entry->addr_read;
159030d7e098SRichard Henderson         tlb_addr &= ~TLB_INVALID_MASK;
1591eed56642SAlex Bennée     }
1592eed56642SAlex Bennée 
159350b107c5SRichard Henderson     /* Handle anything that isn't just a straight memory access.  */
1594eed56642SAlex Bennée     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
159550b107c5SRichard Henderson         CPUIOTLBEntry *iotlbentry;
15965b87b3e6SRichard Henderson         bool need_swap;
159750b107c5SRichard Henderson 
159850b107c5SRichard Henderson         /* For anything that is unaligned, recurse through full_load.  */
1599eed56642SAlex Bennée         if ((addr & (size - 1)) != 0) {
1600eed56642SAlex Bennée             goto do_unaligned_access;
1601eed56642SAlex Bennée         }
160250b107c5SRichard Henderson 
160350b107c5SRichard Henderson         iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
160450b107c5SRichard Henderson 
160550b107c5SRichard Henderson         /* Handle watchpoints.  */
160650b107c5SRichard Henderson         if (unlikely(tlb_addr & TLB_WATCHPOINT)) {
160750b107c5SRichard Henderson             /* On watchpoint hit, this will longjmp out.  */
160850b107c5SRichard Henderson             cpu_check_watchpoint(env_cpu(env), addr, size,
160950b107c5SRichard Henderson                                  iotlbentry->attrs, BP_MEM_READ, retaddr);
16105b87b3e6SRichard Henderson         }
161150b107c5SRichard Henderson 
16125b87b3e6SRichard Henderson         need_swap = size > 1 && (tlb_addr & TLB_BSWAP);
161350b107c5SRichard Henderson 
161450b107c5SRichard Henderson         /* Handle I/O access.  */
16155b87b3e6SRichard Henderson         if (likely(tlb_addr & TLB_MMIO)) {
16165b87b3e6SRichard Henderson             return io_readx(env, iotlbentry, mmu_idx, addr, retaddr,
16175b87b3e6SRichard Henderson                             access_type, op ^ (need_swap * MO_BSWAP));
16185b87b3e6SRichard Henderson         }
16195b87b3e6SRichard Henderson 
16205b87b3e6SRichard Henderson         haddr = (void *)((uintptr_t)addr + entry->addend);
16215b87b3e6SRichard Henderson 
16225b87b3e6SRichard Henderson         /*
16235b87b3e6SRichard Henderson          * Keep these two load_memop separate to ensure that the compiler
16245b87b3e6SRichard Henderson          * is able to fold the entire function to a single instruction.
16255b87b3e6SRichard Henderson          * There is a build-time assert inside to remind you of this.  ;-)
16265b87b3e6SRichard Henderson          */
16275b87b3e6SRichard Henderson         if (unlikely(need_swap)) {
16285b87b3e6SRichard Henderson             return load_memop(haddr, op ^ MO_BSWAP);
16295b87b3e6SRichard Henderson         }
16305b87b3e6SRichard Henderson         return load_memop(haddr, op);
1631eed56642SAlex Bennée     }
1632eed56642SAlex Bennée 
1633eed56642SAlex Bennée     /* Handle slow unaligned access (it spans two pages or IO).  */
1634eed56642SAlex Bennée     if (size > 1
1635eed56642SAlex Bennée         && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1
1636eed56642SAlex Bennée                     >= TARGET_PAGE_SIZE)) {
1637eed56642SAlex Bennée         target_ulong addr1, addr2;
16388c79b288SAlex Bennée         uint64_t r1, r2;
1639eed56642SAlex Bennée         unsigned shift;
1640eed56642SAlex Bennée     do_unaligned_access:
1641ab7a2009SAlex Bennée         addr1 = addr & ~((target_ulong)size - 1);
1642eed56642SAlex Bennée         addr2 = addr1 + size;
16432dd92606SRichard Henderson         r1 = full_load(env, addr1, oi, retaddr);
16442dd92606SRichard Henderson         r2 = full_load(env, addr2, oi, retaddr);
1645eed56642SAlex Bennée         shift = (addr & (size - 1)) * 8;
1646eed56642SAlex Bennée 
1647be5c4787STony Nguyen         if (memop_big_endian(op)) {
1648eed56642SAlex Bennée             /* Big-endian combine.  */
1649eed56642SAlex Bennée             res = (r1 << shift) | (r2 >> ((size * 8) - shift));
1650eed56642SAlex Bennée         } else {
1651eed56642SAlex Bennée             /* Little-endian combine.  */
1652eed56642SAlex Bennée             res = (r1 >> shift) | (r2 << ((size * 8) - shift));
1653eed56642SAlex Bennée         }
1654eed56642SAlex Bennée         return res & MAKE_64BIT_MASK(0, size * 8);
1655eed56642SAlex Bennée     }
1656eed56642SAlex Bennée 
1657eed56642SAlex Bennée     haddr = (void *)((uintptr_t)addr + entry->addend);
165880d9d1c6SRichard Henderson     return load_memop(haddr, op);
1659eed56642SAlex Bennée }
1660eed56642SAlex Bennée 
1661eed56642SAlex Bennée /*
1662eed56642SAlex Bennée  * For the benefit of TCG generated code, we want to avoid the
1663eed56642SAlex Bennée  * complication of ABI-specific return type promotion and always
1664eed56642SAlex Bennée  * return a value extended to the register size of the host. This is
1665eed56642SAlex Bennée  * tcg_target_long, except in the case of a 32-bit host and 64-bit
1666eed56642SAlex Bennée  * data, and for that we always have uint64_t.
1667eed56642SAlex Bennée  *
1668eed56642SAlex Bennée  * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1669eed56642SAlex Bennée  */
1670eed56642SAlex Bennée 
16712dd92606SRichard Henderson static uint64_t full_ldub_mmu(CPUArchState *env, target_ulong addr,
16722dd92606SRichard Henderson                               TCGMemOpIdx oi, uintptr_t retaddr)
16732dd92606SRichard Henderson {
1674be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_UB, false, full_ldub_mmu);
16752dd92606SRichard Henderson }
16762dd92606SRichard Henderson 
1677fc1bc777SRichard Henderson tcg_target_ulong helper_ret_ldub_mmu(CPUArchState *env, target_ulong addr,
1678fc1bc777SRichard Henderson                                      TCGMemOpIdx oi, uintptr_t retaddr)
1679eed56642SAlex Bennée {
16802dd92606SRichard Henderson     return full_ldub_mmu(env, addr, oi, retaddr);
16812dd92606SRichard Henderson }
16822dd92606SRichard Henderson 
16832dd92606SRichard Henderson static uint64_t full_le_lduw_mmu(CPUArchState *env, target_ulong addr,
16842dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
16852dd92606SRichard Henderson {
1686be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUW, false,
16872dd92606SRichard Henderson                        full_le_lduw_mmu);
1688eed56642SAlex Bennée }
1689eed56642SAlex Bennée 
1690fc1bc777SRichard Henderson tcg_target_ulong helper_le_lduw_mmu(CPUArchState *env, target_ulong addr,
1691fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1692eed56642SAlex Bennée {
16932dd92606SRichard Henderson     return full_le_lduw_mmu(env, addr, oi, retaddr);
16942dd92606SRichard Henderson }
16952dd92606SRichard Henderson 
16962dd92606SRichard Henderson static uint64_t full_be_lduw_mmu(CPUArchState *env, target_ulong addr,
16972dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
16982dd92606SRichard Henderson {
1699be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUW, false,
17002dd92606SRichard Henderson                        full_be_lduw_mmu);
1701eed56642SAlex Bennée }
1702eed56642SAlex Bennée 
1703fc1bc777SRichard Henderson tcg_target_ulong helper_be_lduw_mmu(CPUArchState *env, target_ulong addr,
1704fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1705eed56642SAlex Bennée {
17062dd92606SRichard Henderson     return full_be_lduw_mmu(env, addr, oi, retaddr);
17072dd92606SRichard Henderson }
17082dd92606SRichard Henderson 
17092dd92606SRichard Henderson static uint64_t full_le_ldul_mmu(CPUArchState *env, target_ulong addr,
17102dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
17112dd92606SRichard Henderson {
1712be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEUL, false,
17132dd92606SRichard Henderson                        full_le_ldul_mmu);
1714eed56642SAlex Bennée }
1715eed56642SAlex Bennée 
1716fc1bc777SRichard Henderson tcg_target_ulong helper_le_ldul_mmu(CPUArchState *env, target_ulong addr,
1717fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1718eed56642SAlex Bennée {
17192dd92606SRichard Henderson     return full_le_ldul_mmu(env, addr, oi, retaddr);
17202dd92606SRichard Henderson }
17212dd92606SRichard Henderson 
17222dd92606SRichard Henderson static uint64_t full_be_ldul_mmu(CPUArchState *env, target_ulong addr,
17232dd92606SRichard Henderson                                  TCGMemOpIdx oi, uintptr_t retaddr)
17242dd92606SRichard Henderson {
1725be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEUL, false,
17262dd92606SRichard Henderson                        full_be_ldul_mmu);
1727eed56642SAlex Bennée }
1728eed56642SAlex Bennée 
1729fc1bc777SRichard Henderson tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr,
1730fc1bc777SRichard Henderson                                     TCGMemOpIdx oi, uintptr_t retaddr)
1731eed56642SAlex Bennée {
17322dd92606SRichard Henderson     return full_be_ldul_mmu(env, addr, oi, retaddr);
1733eed56642SAlex Bennée }
1734eed56642SAlex Bennée 
1735fc1bc777SRichard Henderson uint64_t helper_le_ldq_mmu(CPUArchState *env, target_ulong addr,
1736fc1bc777SRichard Henderson                            TCGMemOpIdx oi, uintptr_t retaddr)
1737eed56642SAlex Bennée {
1738be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_LEQ, false,
17392dd92606SRichard Henderson                        helper_le_ldq_mmu);
1740eed56642SAlex Bennée }
1741eed56642SAlex Bennée 
1742fc1bc777SRichard Henderson uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr,
1743fc1bc777SRichard Henderson                            TCGMemOpIdx oi, uintptr_t retaddr)
1744eed56642SAlex Bennée {
1745be5c4787STony Nguyen     return load_helper(env, addr, oi, retaddr, MO_BEQ, false,
17462dd92606SRichard Henderson                        helper_be_ldq_mmu);
1747eed56642SAlex Bennée }
1748eed56642SAlex Bennée 
1749eed56642SAlex Bennée /*
1750eed56642SAlex Bennée  * Provide signed versions of the load routines as well.  We can of course
1751eed56642SAlex Bennée  * avoid this for 64-bit data, or for 32-bit data on 32-bit host.
1752eed56642SAlex Bennée  */
1753eed56642SAlex Bennée 
1754eed56642SAlex Bennée 
1755eed56642SAlex Bennée tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr,
1756eed56642SAlex Bennée                                      TCGMemOpIdx oi, uintptr_t retaddr)
1757eed56642SAlex Bennée {
1758eed56642SAlex Bennée     return (int8_t)helper_ret_ldub_mmu(env, addr, oi, retaddr);
1759eed56642SAlex Bennée }
1760eed56642SAlex Bennée 
1761eed56642SAlex Bennée tcg_target_ulong helper_le_ldsw_mmu(CPUArchState *env, target_ulong addr,
1762eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1763eed56642SAlex Bennée {
1764eed56642SAlex Bennée     return (int16_t)helper_le_lduw_mmu(env, addr, oi, retaddr);
1765eed56642SAlex Bennée }
1766eed56642SAlex Bennée 
1767eed56642SAlex Bennée tcg_target_ulong helper_be_ldsw_mmu(CPUArchState *env, target_ulong addr,
1768eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1769eed56642SAlex Bennée {
1770eed56642SAlex Bennée     return (int16_t)helper_be_lduw_mmu(env, addr, oi, retaddr);
1771eed56642SAlex Bennée }
1772eed56642SAlex Bennée 
1773eed56642SAlex Bennée tcg_target_ulong helper_le_ldsl_mmu(CPUArchState *env, target_ulong addr,
1774eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1775eed56642SAlex Bennée {
1776eed56642SAlex Bennée     return (int32_t)helper_le_ldul_mmu(env, addr, oi, retaddr);
1777eed56642SAlex Bennée }
1778eed56642SAlex Bennée 
1779eed56642SAlex Bennée tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr,
1780eed56642SAlex Bennée                                     TCGMemOpIdx oi, uintptr_t retaddr)
1781eed56642SAlex Bennée {
1782eed56642SAlex Bennée     return (int32_t)helper_be_ldul_mmu(env, addr, oi, retaddr);
1783eed56642SAlex Bennée }
1784eed56642SAlex Bennée 
1785eed56642SAlex Bennée /*
1786d03f1408SRichard Henderson  * Load helpers for cpu_ldst.h.
1787d03f1408SRichard Henderson  */
1788d03f1408SRichard Henderson 
1789d03f1408SRichard Henderson static inline uint64_t cpu_load_helper(CPUArchState *env, abi_ptr addr,
1790d03f1408SRichard Henderson                                        int mmu_idx, uintptr_t retaddr,
1791d03f1408SRichard Henderson                                        MemOp op, FullLoadHelper *full_load)
1792d03f1408SRichard Henderson {
1793d03f1408SRichard Henderson     uint16_t meminfo;
1794d03f1408SRichard Henderson     TCGMemOpIdx oi;
1795d03f1408SRichard Henderson     uint64_t ret;
1796d03f1408SRichard Henderson 
1797d03f1408SRichard Henderson     meminfo = trace_mem_get_info(op, mmu_idx, false);
1798d03f1408SRichard Henderson     trace_guest_mem_before_exec(env_cpu(env), addr, meminfo);
1799d03f1408SRichard Henderson 
1800d03f1408SRichard Henderson     op &= ~MO_SIGN;
1801d03f1408SRichard Henderson     oi = make_memop_idx(op, mmu_idx);
1802d03f1408SRichard Henderson     ret = full_load(env, addr, oi, retaddr);
1803d03f1408SRichard Henderson 
1804d03f1408SRichard Henderson     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, meminfo);
1805d03f1408SRichard Henderson 
1806d03f1408SRichard Henderson     return ret;
1807d03f1408SRichard Henderson }
1808d03f1408SRichard Henderson 
1809d03f1408SRichard Henderson uint32_t cpu_ldub_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1810d03f1408SRichard Henderson                             int mmu_idx, uintptr_t ra)
1811d03f1408SRichard Henderson {
1812d03f1408SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_UB, full_ldub_mmu);
1813d03f1408SRichard Henderson }
1814d03f1408SRichard Henderson 
1815d03f1408SRichard Henderson int cpu_ldsb_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1816d03f1408SRichard Henderson                        int mmu_idx, uintptr_t ra)
1817d03f1408SRichard Henderson {
1818d03f1408SRichard Henderson     return (int8_t)cpu_load_helper(env, addr, mmu_idx, ra, MO_SB,
1819d03f1408SRichard Henderson                                    full_ldub_mmu);
1820d03f1408SRichard Henderson }
1821d03f1408SRichard Henderson 
1822b9e60257SRichard Henderson uint32_t cpu_lduw_be_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1823d03f1408SRichard Henderson                                int mmu_idx, uintptr_t ra)
1824d03f1408SRichard Henderson {
1825b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_BEUW, full_be_lduw_mmu);
1826d03f1408SRichard Henderson }
1827d03f1408SRichard Henderson 
1828b9e60257SRichard Henderson int cpu_ldsw_be_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1829d03f1408SRichard Henderson                           int mmu_idx, uintptr_t ra)
1830d03f1408SRichard Henderson {
1831b9e60257SRichard Henderson     return (int16_t)cpu_load_helper(env, addr, mmu_idx, ra, MO_BESW,
1832b9e60257SRichard Henderson                                     full_be_lduw_mmu);
1833d03f1408SRichard Henderson }
1834d03f1408SRichard Henderson 
1835b9e60257SRichard Henderson uint32_t cpu_ldl_be_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1836d03f1408SRichard Henderson                               int mmu_idx, uintptr_t ra)
1837d03f1408SRichard Henderson {
1838b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_BEUL, full_be_ldul_mmu);
1839d03f1408SRichard Henderson }
1840d03f1408SRichard Henderson 
1841b9e60257SRichard Henderson uint64_t cpu_ldq_be_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1842d03f1408SRichard Henderson                               int mmu_idx, uintptr_t ra)
1843d03f1408SRichard Henderson {
1844b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_BEQ, helper_be_ldq_mmu);
1845b9e60257SRichard Henderson }
1846b9e60257SRichard Henderson 
1847b9e60257SRichard Henderson uint32_t cpu_lduw_le_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1848b9e60257SRichard Henderson                                int mmu_idx, uintptr_t ra)
1849b9e60257SRichard Henderson {
1850b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_LEUW, full_le_lduw_mmu);
1851b9e60257SRichard Henderson }
1852b9e60257SRichard Henderson 
1853b9e60257SRichard Henderson int cpu_ldsw_le_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1854b9e60257SRichard Henderson                           int mmu_idx, uintptr_t ra)
1855b9e60257SRichard Henderson {
1856b9e60257SRichard Henderson     return (int16_t)cpu_load_helper(env, addr, mmu_idx, ra, MO_LESW,
1857b9e60257SRichard Henderson                                     full_le_lduw_mmu);
1858b9e60257SRichard Henderson }
1859b9e60257SRichard Henderson 
1860b9e60257SRichard Henderson uint32_t cpu_ldl_le_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1861b9e60257SRichard Henderson                               int mmu_idx, uintptr_t ra)
1862b9e60257SRichard Henderson {
1863b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_LEUL, full_le_ldul_mmu);
1864b9e60257SRichard Henderson }
1865b9e60257SRichard Henderson 
1866b9e60257SRichard Henderson uint64_t cpu_ldq_le_mmuidx_ra(CPUArchState *env, abi_ptr addr,
1867b9e60257SRichard Henderson                               int mmu_idx, uintptr_t ra)
1868b9e60257SRichard Henderson {
1869b9e60257SRichard Henderson     return cpu_load_helper(env, addr, mmu_idx, ra, MO_LEQ, helper_le_ldq_mmu);
1870d03f1408SRichard Henderson }
1871d03f1408SRichard Henderson 
1872cfe04a4bSRichard Henderson uint32_t cpu_ldub_data_ra(CPUArchState *env, target_ulong ptr,
1873cfe04a4bSRichard Henderson                           uintptr_t retaddr)
1874cfe04a4bSRichard Henderson {
1875cfe04a4bSRichard Henderson     return cpu_ldub_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1876cfe04a4bSRichard Henderson }
1877cfe04a4bSRichard Henderson 
1878cfe04a4bSRichard Henderson int cpu_ldsb_data_ra(CPUArchState *env, target_ulong ptr, uintptr_t retaddr)
1879cfe04a4bSRichard Henderson {
1880cfe04a4bSRichard Henderson     return cpu_ldsb_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1881cfe04a4bSRichard Henderson }
1882cfe04a4bSRichard Henderson 
1883b9e60257SRichard Henderson uint32_t cpu_lduw_be_data_ra(CPUArchState *env, target_ulong ptr,
1884cfe04a4bSRichard Henderson                              uintptr_t retaddr)
1885cfe04a4bSRichard Henderson {
1886b9e60257SRichard Henderson     return cpu_lduw_be_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1887cfe04a4bSRichard Henderson }
1888cfe04a4bSRichard Henderson 
1889b9e60257SRichard Henderson int cpu_ldsw_be_data_ra(CPUArchState *env, target_ulong ptr, uintptr_t retaddr)
1890cfe04a4bSRichard Henderson {
1891b9e60257SRichard Henderson     return cpu_ldsw_be_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1892cfe04a4bSRichard Henderson }
1893cfe04a4bSRichard Henderson 
1894b9e60257SRichard Henderson uint32_t cpu_ldl_be_data_ra(CPUArchState *env, target_ulong ptr,
1895b9e60257SRichard Henderson                             uintptr_t retaddr)
1896cfe04a4bSRichard Henderson {
1897b9e60257SRichard Henderson     return cpu_ldl_be_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1898cfe04a4bSRichard Henderson }
1899cfe04a4bSRichard Henderson 
1900b9e60257SRichard Henderson uint64_t cpu_ldq_be_data_ra(CPUArchState *env, target_ulong ptr,
1901b9e60257SRichard Henderson                             uintptr_t retaddr)
1902cfe04a4bSRichard Henderson {
1903b9e60257SRichard Henderson     return cpu_ldq_be_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1904b9e60257SRichard Henderson }
1905b9e60257SRichard Henderson 
1906b9e60257SRichard Henderson uint32_t cpu_lduw_le_data_ra(CPUArchState *env, target_ulong ptr,
1907b9e60257SRichard Henderson                              uintptr_t retaddr)
1908b9e60257SRichard Henderson {
1909b9e60257SRichard Henderson     return cpu_lduw_le_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1910b9e60257SRichard Henderson }
1911b9e60257SRichard Henderson 
1912b9e60257SRichard Henderson int cpu_ldsw_le_data_ra(CPUArchState *env, target_ulong ptr, uintptr_t retaddr)
1913b9e60257SRichard Henderson {
1914b9e60257SRichard Henderson     return cpu_ldsw_le_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1915b9e60257SRichard Henderson }
1916b9e60257SRichard Henderson 
1917b9e60257SRichard Henderson uint32_t cpu_ldl_le_data_ra(CPUArchState *env, target_ulong ptr,
1918b9e60257SRichard Henderson                             uintptr_t retaddr)
1919b9e60257SRichard Henderson {
1920b9e60257SRichard Henderson     return cpu_ldl_le_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1921b9e60257SRichard Henderson }
1922b9e60257SRichard Henderson 
1923b9e60257SRichard Henderson uint64_t cpu_ldq_le_data_ra(CPUArchState *env, target_ulong ptr,
1924b9e60257SRichard Henderson                             uintptr_t retaddr)
1925b9e60257SRichard Henderson {
1926b9e60257SRichard Henderson     return cpu_ldq_le_mmuidx_ra(env, ptr, cpu_mmu_index(env, false), retaddr);
1927cfe04a4bSRichard Henderson }
1928cfe04a4bSRichard Henderson 
1929cfe04a4bSRichard Henderson uint32_t cpu_ldub_data(CPUArchState *env, target_ulong ptr)
1930cfe04a4bSRichard Henderson {
1931cfe04a4bSRichard Henderson     return cpu_ldub_data_ra(env, ptr, 0);
1932cfe04a4bSRichard Henderson }
1933cfe04a4bSRichard Henderson 
1934cfe04a4bSRichard Henderson int cpu_ldsb_data(CPUArchState *env, target_ulong ptr)
1935cfe04a4bSRichard Henderson {
1936cfe04a4bSRichard Henderson     return cpu_ldsb_data_ra(env, ptr, 0);
1937cfe04a4bSRichard Henderson }
1938cfe04a4bSRichard Henderson 
1939b9e60257SRichard Henderson uint32_t cpu_lduw_be_data(CPUArchState *env, target_ulong ptr)
1940cfe04a4bSRichard Henderson {
1941b9e60257SRichard Henderson     return cpu_lduw_be_data_ra(env, ptr, 0);
1942cfe04a4bSRichard Henderson }
1943cfe04a4bSRichard Henderson 
1944b9e60257SRichard Henderson int cpu_ldsw_be_data(CPUArchState *env, target_ulong ptr)
1945cfe04a4bSRichard Henderson {
1946b9e60257SRichard Henderson     return cpu_ldsw_be_data_ra(env, ptr, 0);
1947cfe04a4bSRichard Henderson }
1948cfe04a4bSRichard Henderson 
1949b9e60257SRichard Henderson uint32_t cpu_ldl_be_data(CPUArchState *env, target_ulong ptr)
1950cfe04a4bSRichard Henderson {
1951b9e60257SRichard Henderson     return cpu_ldl_be_data_ra(env, ptr, 0);
1952cfe04a4bSRichard Henderson }
1953cfe04a4bSRichard Henderson 
1954b9e60257SRichard Henderson uint64_t cpu_ldq_be_data(CPUArchState *env, target_ulong ptr)
1955cfe04a4bSRichard Henderson {
1956b9e60257SRichard Henderson     return cpu_ldq_be_data_ra(env, ptr, 0);
1957b9e60257SRichard Henderson }
1958b9e60257SRichard Henderson 
1959b9e60257SRichard Henderson uint32_t cpu_lduw_le_data(CPUArchState *env, target_ulong ptr)
1960b9e60257SRichard Henderson {
1961b9e60257SRichard Henderson     return cpu_lduw_le_data_ra(env, ptr, 0);
1962b9e60257SRichard Henderson }
1963b9e60257SRichard Henderson 
1964b9e60257SRichard Henderson int cpu_ldsw_le_data(CPUArchState *env, target_ulong ptr)
1965b9e60257SRichard Henderson {
1966b9e60257SRichard Henderson     return cpu_ldsw_le_data_ra(env, ptr, 0);
1967b9e60257SRichard Henderson }
1968b9e60257SRichard Henderson 
1969b9e60257SRichard Henderson uint32_t cpu_ldl_le_data(CPUArchState *env, target_ulong ptr)
1970b9e60257SRichard Henderson {
1971b9e60257SRichard Henderson     return cpu_ldl_le_data_ra(env, ptr, 0);
1972b9e60257SRichard Henderson }
1973b9e60257SRichard Henderson 
1974b9e60257SRichard Henderson uint64_t cpu_ldq_le_data(CPUArchState *env, target_ulong ptr)
1975b9e60257SRichard Henderson {
1976b9e60257SRichard Henderson     return cpu_ldq_le_data_ra(env, ptr, 0);
1977cfe04a4bSRichard Henderson }
1978cfe04a4bSRichard Henderson 
1979d03f1408SRichard Henderson /*
1980eed56642SAlex Bennée  * Store Helpers
1981eed56642SAlex Bennée  */
1982eed56642SAlex Bennée 
1983c6b716cdSRichard Henderson static inline void QEMU_ALWAYS_INLINE
198480d9d1c6SRichard Henderson store_memop(void *haddr, uint64_t val, MemOp op)
198580d9d1c6SRichard Henderson {
198680d9d1c6SRichard Henderson     switch (op) {
198780d9d1c6SRichard Henderson     case MO_UB:
198880d9d1c6SRichard Henderson         stb_p(haddr, val);
198980d9d1c6SRichard Henderson         break;
199080d9d1c6SRichard Henderson     case MO_BEUW:
199180d9d1c6SRichard Henderson         stw_be_p(haddr, val);
199280d9d1c6SRichard Henderson         break;
199380d9d1c6SRichard Henderson     case MO_LEUW:
199480d9d1c6SRichard Henderson         stw_le_p(haddr, val);
199580d9d1c6SRichard Henderson         break;
199680d9d1c6SRichard Henderson     case MO_BEUL:
199780d9d1c6SRichard Henderson         stl_be_p(haddr, val);
199880d9d1c6SRichard Henderson         break;
199980d9d1c6SRichard Henderson     case MO_LEUL:
200080d9d1c6SRichard Henderson         stl_le_p(haddr, val);
200180d9d1c6SRichard Henderson         break;
200280d9d1c6SRichard Henderson     case MO_BEQ:
200380d9d1c6SRichard Henderson         stq_be_p(haddr, val);
200480d9d1c6SRichard Henderson         break;
200580d9d1c6SRichard Henderson     case MO_LEQ:
200680d9d1c6SRichard Henderson         stq_le_p(haddr, val);
200780d9d1c6SRichard Henderson         break;
200880d9d1c6SRichard Henderson     default:
200980d9d1c6SRichard Henderson         qemu_build_not_reached();
201080d9d1c6SRichard Henderson     }
201180d9d1c6SRichard Henderson }
201280d9d1c6SRichard Henderson 
201380d9d1c6SRichard Henderson static inline void QEMU_ALWAYS_INLINE
20144601f8d1SRichard Henderson store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
2015be5c4787STony Nguyen              TCGMemOpIdx oi, uintptr_t retaddr, MemOp op)
2016eed56642SAlex Bennée {
2017eed56642SAlex Bennée     uintptr_t mmu_idx = get_mmuidx(oi);
2018eed56642SAlex Bennée     uintptr_t index = tlb_index(env, mmu_idx, addr);
2019eed56642SAlex Bennée     CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
2020eed56642SAlex Bennée     target_ulong tlb_addr = tlb_addr_write(entry);
2021eed56642SAlex Bennée     const size_t tlb_off = offsetof(CPUTLBEntry, addr_write);
2022eed56642SAlex Bennée     unsigned a_bits = get_alignment_bits(get_memop(oi));
2023eed56642SAlex Bennée     void *haddr;
2024be5c4787STony Nguyen     size_t size = memop_size(op);
2025eed56642SAlex Bennée 
2026eed56642SAlex Bennée     /* Handle CPU specific unaligned behaviour */
2027eed56642SAlex Bennée     if (addr & ((1 << a_bits) - 1)) {
202829a0af61SRichard Henderson         cpu_unaligned_access(env_cpu(env), addr, MMU_DATA_STORE,
2029eed56642SAlex Bennée                              mmu_idx, retaddr);
2030eed56642SAlex Bennée     }
2031eed56642SAlex Bennée 
2032eed56642SAlex Bennée     /* If the TLB entry is for a different page, reload and try again.  */
2033eed56642SAlex Bennée     if (!tlb_hit(tlb_addr, addr)) {
2034eed56642SAlex Bennée         if (!victim_tlb_hit(env, mmu_idx, index, tlb_off,
2035eed56642SAlex Bennée             addr & TARGET_PAGE_MASK)) {
203629a0af61SRichard Henderson             tlb_fill(env_cpu(env), addr, size, MMU_DATA_STORE,
2037eed56642SAlex Bennée                      mmu_idx, retaddr);
2038eed56642SAlex Bennée             index = tlb_index(env, mmu_idx, addr);
2039eed56642SAlex Bennée             entry = tlb_entry(env, mmu_idx, addr);
2040eed56642SAlex Bennée         }
2041eed56642SAlex Bennée         tlb_addr = tlb_addr_write(entry) & ~TLB_INVALID_MASK;
2042eed56642SAlex Bennée     }
2043eed56642SAlex Bennée 
204450b107c5SRichard Henderson     /* Handle anything that isn't just a straight memory access.  */
2045eed56642SAlex Bennée     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
204650b107c5SRichard Henderson         CPUIOTLBEntry *iotlbentry;
20475b87b3e6SRichard Henderson         bool need_swap;
204850b107c5SRichard Henderson 
204950b107c5SRichard Henderson         /* For anything that is unaligned, recurse through byte stores.  */
2050eed56642SAlex Bennée         if ((addr & (size - 1)) != 0) {
2051eed56642SAlex Bennée             goto do_unaligned_access;
2052eed56642SAlex Bennée         }
205350b107c5SRichard Henderson 
205450b107c5SRichard Henderson         iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
205550b107c5SRichard Henderson 
205650b107c5SRichard Henderson         /* Handle watchpoints.  */
205750b107c5SRichard Henderson         if (unlikely(tlb_addr & TLB_WATCHPOINT)) {
205850b107c5SRichard Henderson             /* On watchpoint hit, this will longjmp out.  */
205950b107c5SRichard Henderson             cpu_check_watchpoint(env_cpu(env), addr, size,
206050b107c5SRichard Henderson                                  iotlbentry->attrs, BP_MEM_WRITE, retaddr);
20615b87b3e6SRichard Henderson         }
206250b107c5SRichard Henderson 
20635b87b3e6SRichard Henderson         need_swap = size > 1 && (tlb_addr & TLB_BSWAP);
206450b107c5SRichard Henderson 
206550b107c5SRichard Henderson         /* Handle I/O access.  */
206608565552SRichard Henderson         if (tlb_addr & TLB_MMIO) {
20675b87b3e6SRichard Henderson             io_writex(env, iotlbentry, mmu_idx, val, addr, retaddr,
20685b87b3e6SRichard Henderson                       op ^ (need_swap * MO_BSWAP));
20695b87b3e6SRichard Henderson             return;
20705b87b3e6SRichard Henderson         }
20715b87b3e6SRichard Henderson 
20727b0d792cSRichard Henderson         /* Ignore writes to ROM.  */
20737b0d792cSRichard Henderson         if (unlikely(tlb_addr & TLB_DISCARD_WRITE)) {
20747b0d792cSRichard Henderson             return;
20757b0d792cSRichard Henderson         }
20767b0d792cSRichard Henderson 
207708565552SRichard Henderson         /* Handle clean RAM pages.  */
207808565552SRichard Henderson         if (tlb_addr & TLB_NOTDIRTY) {
2079707526adSRichard Henderson             notdirty_write(env_cpu(env), addr, size, iotlbentry, retaddr);
208008565552SRichard Henderson         }
208108565552SRichard Henderson 
2082707526adSRichard Henderson         haddr = (void *)((uintptr_t)addr + entry->addend);
208308565552SRichard Henderson 
20845b87b3e6SRichard Henderson         /*
20855b87b3e6SRichard Henderson          * Keep these two store_memop separate to ensure that the compiler
20865b87b3e6SRichard Henderson          * is able to fold the entire function to a single instruction.
20875b87b3e6SRichard Henderson          * There is a build-time assert inside to remind you of this.  ;-)
20885b87b3e6SRichard Henderson          */
20895b87b3e6SRichard Henderson         if (unlikely(need_swap)) {
20905b87b3e6SRichard Henderson             store_memop(haddr, val, op ^ MO_BSWAP);
20915b87b3e6SRichard Henderson         } else {
20925b87b3e6SRichard Henderson             store_memop(haddr, val, op);
20935b87b3e6SRichard Henderson         }
2094eed56642SAlex Bennée         return;
2095eed56642SAlex Bennée     }
2096eed56642SAlex Bennée 
2097eed56642SAlex Bennée     /* Handle slow unaligned access (it spans two pages or IO).  */
2098eed56642SAlex Bennée     if (size > 1
2099eed56642SAlex Bennée         && unlikely((addr & ~TARGET_PAGE_MASK) + size - 1
2100eed56642SAlex Bennée                      >= TARGET_PAGE_SIZE)) {
2101eed56642SAlex Bennée         int i;
2102eed56642SAlex Bennée         uintptr_t index2;
2103eed56642SAlex Bennée         CPUTLBEntry *entry2;
2104eed56642SAlex Bennée         target_ulong page2, tlb_addr2;
21058f7cd2adSRichard Henderson         size_t size2;
21068f7cd2adSRichard Henderson 
2107eed56642SAlex Bennée     do_unaligned_access:
2108eed56642SAlex Bennée         /*
2109eed56642SAlex Bennée          * Ensure the second page is in the TLB.  Note that the first page
2110eed56642SAlex Bennée          * is already guaranteed to be filled, and that the second page
2111eed56642SAlex Bennée          * cannot evict the first.
2112eed56642SAlex Bennée          */
2113eed56642SAlex Bennée         page2 = (addr + size) & TARGET_PAGE_MASK;
21148f7cd2adSRichard Henderson         size2 = (addr + size) & ~TARGET_PAGE_MASK;
2115eed56642SAlex Bennée         index2 = tlb_index(env, mmu_idx, page2);
2116eed56642SAlex Bennée         entry2 = tlb_entry(env, mmu_idx, page2);
2117eed56642SAlex Bennée         tlb_addr2 = tlb_addr_write(entry2);
211850b107c5SRichard Henderson         if (!tlb_hit_page(tlb_addr2, page2)) {
211950b107c5SRichard Henderson             if (!victim_tlb_hit(env, mmu_idx, index2, tlb_off, page2)) {
21208f7cd2adSRichard Henderson                 tlb_fill(env_cpu(env), page2, size2, MMU_DATA_STORE,
2121eed56642SAlex Bennée                          mmu_idx, retaddr);
212250b107c5SRichard Henderson                 index2 = tlb_index(env, mmu_idx, page2);
212350b107c5SRichard Henderson                 entry2 = tlb_entry(env, mmu_idx, page2);
212450b107c5SRichard Henderson             }
212550b107c5SRichard Henderson             tlb_addr2 = tlb_addr_write(entry2);
212650b107c5SRichard Henderson         }
212750b107c5SRichard Henderson 
212850b107c5SRichard Henderson         /*
212950b107c5SRichard Henderson          * Handle watchpoints.  Since this may trap, all checks
213050b107c5SRichard Henderson          * must happen before any store.
213150b107c5SRichard Henderson          */
213250b107c5SRichard Henderson         if (unlikely(tlb_addr & TLB_WATCHPOINT)) {
213350b107c5SRichard Henderson             cpu_check_watchpoint(env_cpu(env), addr, size - size2,
213450b107c5SRichard Henderson                                  env_tlb(env)->d[mmu_idx].iotlb[index].attrs,
213550b107c5SRichard Henderson                                  BP_MEM_WRITE, retaddr);
213650b107c5SRichard Henderson         }
213750b107c5SRichard Henderson         if (unlikely(tlb_addr2 & TLB_WATCHPOINT)) {
213850b107c5SRichard Henderson             cpu_check_watchpoint(env_cpu(env), page2, size2,
213950b107c5SRichard Henderson                                  env_tlb(env)->d[mmu_idx].iotlb[index2].attrs,
214050b107c5SRichard Henderson                                  BP_MEM_WRITE, retaddr);
2141eed56642SAlex Bennée         }
2142eed56642SAlex Bennée 
2143eed56642SAlex Bennée         /*
2144eed56642SAlex Bennée          * XXX: not efficient, but simple.
2145eed56642SAlex Bennée          * This loop must go in the forward direction to avoid issues
2146eed56642SAlex Bennée          * with self-modifying code in Windows 64-bit.
2147eed56642SAlex Bennée          */
2148eed56642SAlex Bennée         for (i = 0; i < size; ++i) {
2149eed56642SAlex Bennée             uint8_t val8;
2150be5c4787STony Nguyen             if (memop_big_endian(op)) {
2151eed56642SAlex Bennée                 /* Big-endian extract.  */
2152eed56642SAlex Bennée                 val8 = val >> (((size - 1) * 8) - (i * 8));
2153eed56642SAlex Bennée             } else {
2154eed56642SAlex Bennée                 /* Little-endian extract.  */
2155eed56642SAlex Bennée                 val8 = val >> (i * 8);
2156eed56642SAlex Bennée             }
21574601f8d1SRichard Henderson             helper_ret_stb_mmu(env, addr + i, val8, oi, retaddr);
2158eed56642SAlex Bennée         }
2159eed56642SAlex Bennée         return;
2160eed56642SAlex Bennée     }
2161eed56642SAlex Bennée 
2162eed56642SAlex Bennée     haddr = (void *)((uintptr_t)addr + entry->addend);
216380d9d1c6SRichard Henderson     store_memop(haddr, val, op);
2164eed56642SAlex Bennée }
2165eed56642SAlex Bennée 
2166fc1bc777SRichard Henderson void helper_ret_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val,
2167eed56642SAlex Bennée                         TCGMemOpIdx oi, uintptr_t retaddr)
2168eed56642SAlex Bennée {
2169be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_UB);
2170eed56642SAlex Bennée }
2171eed56642SAlex Bennée 
2172fc1bc777SRichard Henderson void helper_le_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
2173eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2174eed56642SAlex Bennée {
2175be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEUW);
2176eed56642SAlex Bennée }
2177eed56642SAlex Bennée 
2178fc1bc777SRichard Henderson void helper_be_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
2179eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2180eed56642SAlex Bennée {
2181be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEUW);
2182eed56642SAlex Bennée }
2183eed56642SAlex Bennée 
2184fc1bc777SRichard Henderson void helper_le_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
2185eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2186eed56642SAlex Bennée {
2187be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEUL);
2188eed56642SAlex Bennée }
2189eed56642SAlex Bennée 
2190fc1bc777SRichard Henderson void helper_be_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
2191eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2192eed56642SAlex Bennée {
2193be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEUL);
2194eed56642SAlex Bennée }
2195eed56642SAlex Bennée 
2196fc1bc777SRichard Henderson void helper_le_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
2197eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2198eed56642SAlex Bennée {
2199be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_LEQ);
2200eed56642SAlex Bennée }
2201eed56642SAlex Bennée 
2202fc1bc777SRichard Henderson void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
2203eed56642SAlex Bennée                        TCGMemOpIdx oi, uintptr_t retaddr)
2204eed56642SAlex Bennée {
2205be5c4787STony Nguyen     store_helper(env, addr, val, oi, retaddr, MO_BEQ);
2206eed56642SAlex Bennée }
2207d9bb58e5SYang Zhong 
2208d03f1408SRichard Henderson /*
2209d03f1408SRichard Henderson  * Store Helpers for cpu_ldst.h
2210d03f1408SRichard Henderson  */
2211d03f1408SRichard Henderson 
2212d03f1408SRichard Henderson static inline void QEMU_ALWAYS_INLINE
2213d03f1408SRichard Henderson cpu_store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
2214d03f1408SRichard Henderson                  int mmu_idx, uintptr_t retaddr, MemOp op)
2215d03f1408SRichard Henderson {
2216d03f1408SRichard Henderson     TCGMemOpIdx oi;
2217d03f1408SRichard Henderson     uint16_t meminfo;
2218d03f1408SRichard Henderson 
2219d03f1408SRichard Henderson     meminfo = trace_mem_get_info(op, mmu_idx, true);
2220d03f1408SRichard Henderson     trace_guest_mem_before_exec(env_cpu(env), addr, meminfo);
2221d03f1408SRichard Henderson 
2222d03f1408SRichard Henderson     oi = make_memop_idx(op, mmu_idx);
2223d03f1408SRichard Henderson     store_helper(env, addr, val, oi, retaddr, op);
2224d03f1408SRichard Henderson 
2225d03f1408SRichard Henderson     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, meminfo);
2226d03f1408SRichard Henderson }
2227d03f1408SRichard Henderson 
2228d03f1408SRichard Henderson void cpu_stb_mmuidx_ra(CPUArchState *env, target_ulong addr, uint32_t val,
2229d03f1408SRichard Henderson                        int mmu_idx, uintptr_t retaddr)
2230d03f1408SRichard Henderson {
2231d03f1408SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_UB);
2232d03f1408SRichard Henderson }
2233d03f1408SRichard Henderson 
2234b9e60257SRichard Henderson void cpu_stw_be_mmuidx_ra(CPUArchState *env, target_ulong addr, uint32_t val,
2235d03f1408SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2236d03f1408SRichard Henderson {
2237b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_BEUW);
2238d03f1408SRichard Henderson }
2239d03f1408SRichard Henderson 
2240b9e60257SRichard Henderson void cpu_stl_be_mmuidx_ra(CPUArchState *env, target_ulong addr, uint32_t val,
2241d03f1408SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2242d03f1408SRichard Henderson {
2243b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_BEUL);
2244d03f1408SRichard Henderson }
2245d03f1408SRichard Henderson 
2246b9e60257SRichard Henderson void cpu_stq_be_mmuidx_ra(CPUArchState *env, target_ulong addr, uint64_t val,
2247d03f1408SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2248d03f1408SRichard Henderson {
2249b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_BEQ);
2250b9e60257SRichard Henderson }
2251b9e60257SRichard Henderson 
2252b9e60257SRichard Henderson void cpu_stw_le_mmuidx_ra(CPUArchState *env, target_ulong addr, uint32_t val,
2253b9e60257SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2254b9e60257SRichard Henderson {
2255b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_LEUW);
2256b9e60257SRichard Henderson }
2257b9e60257SRichard Henderson 
2258b9e60257SRichard Henderson void cpu_stl_le_mmuidx_ra(CPUArchState *env, target_ulong addr, uint32_t val,
2259b9e60257SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2260b9e60257SRichard Henderson {
2261b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_LEUL);
2262b9e60257SRichard Henderson }
2263b9e60257SRichard Henderson 
2264b9e60257SRichard Henderson void cpu_stq_le_mmuidx_ra(CPUArchState *env, target_ulong addr, uint64_t val,
2265b9e60257SRichard Henderson                           int mmu_idx, uintptr_t retaddr)
2266b9e60257SRichard Henderson {
2267b9e60257SRichard Henderson     cpu_store_helper(env, addr, val, mmu_idx, retaddr, MO_LEQ);
2268d03f1408SRichard Henderson }
2269d03f1408SRichard Henderson 
2270cfe04a4bSRichard Henderson void cpu_stb_data_ra(CPUArchState *env, target_ulong ptr,
2271cfe04a4bSRichard Henderson                      uint32_t val, uintptr_t retaddr)
2272cfe04a4bSRichard Henderson {
2273cfe04a4bSRichard Henderson     cpu_stb_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2274cfe04a4bSRichard Henderson }
2275cfe04a4bSRichard Henderson 
2276b9e60257SRichard Henderson void cpu_stw_be_data_ra(CPUArchState *env, target_ulong ptr,
2277cfe04a4bSRichard Henderson                         uint32_t val, uintptr_t retaddr)
2278cfe04a4bSRichard Henderson {
2279b9e60257SRichard Henderson     cpu_stw_be_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2280cfe04a4bSRichard Henderson }
2281cfe04a4bSRichard Henderson 
2282b9e60257SRichard Henderson void cpu_stl_be_data_ra(CPUArchState *env, target_ulong ptr,
2283cfe04a4bSRichard Henderson                         uint32_t val, uintptr_t retaddr)
2284cfe04a4bSRichard Henderson {
2285b9e60257SRichard Henderson     cpu_stl_be_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2286cfe04a4bSRichard Henderson }
2287cfe04a4bSRichard Henderson 
2288b9e60257SRichard Henderson void cpu_stq_be_data_ra(CPUArchState *env, target_ulong ptr,
2289cfe04a4bSRichard Henderson                         uint64_t val, uintptr_t retaddr)
2290cfe04a4bSRichard Henderson {
2291b9e60257SRichard Henderson     cpu_stq_be_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2292b9e60257SRichard Henderson }
2293b9e60257SRichard Henderson 
2294b9e60257SRichard Henderson void cpu_stw_le_data_ra(CPUArchState *env, target_ulong ptr,
2295b9e60257SRichard Henderson                         uint32_t val, uintptr_t retaddr)
2296b9e60257SRichard Henderson {
2297b9e60257SRichard Henderson     cpu_stw_le_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2298b9e60257SRichard Henderson }
2299b9e60257SRichard Henderson 
2300b9e60257SRichard Henderson void cpu_stl_le_data_ra(CPUArchState *env, target_ulong ptr,
2301b9e60257SRichard Henderson                         uint32_t val, uintptr_t retaddr)
2302b9e60257SRichard Henderson {
2303b9e60257SRichard Henderson     cpu_stl_le_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2304b9e60257SRichard Henderson }
2305b9e60257SRichard Henderson 
2306b9e60257SRichard Henderson void cpu_stq_le_data_ra(CPUArchState *env, target_ulong ptr,
2307b9e60257SRichard Henderson                         uint64_t val, uintptr_t retaddr)
2308b9e60257SRichard Henderson {
2309b9e60257SRichard Henderson     cpu_stq_le_mmuidx_ra(env, ptr, val, cpu_mmu_index(env, false), retaddr);
2310cfe04a4bSRichard Henderson }
2311cfe04a4bSRichard Henderson 
2312cfe04a4bSRichard Henderson void cpu_stb_data(CPUArchState *env, target_ulong ptr, uint32_t val)
2313cfe04a4bSRichard Henderson {
2314cfe04a4bSRichard Henderson     cpu_stb_data_ra(env, ptr, val, 0);
2315cfe04a4bSRichard Henderson }
2316cfe04a4bSRichard Henderson 
2317b9e60257SRichard Henderson void cpu_stw_be_data(CPUArchState *env, target_ulong ptr, uint32_t val)
2318cfe04a4bSRichard Henderson {
2319b9e60257SRichard Henderson     cpu_stw_be_data_ra(env, ptr, val, 0);
2320cfe04a4bSRichard Henderson }
2321cfe04a4bSRichard Henderson 
2322b9e60257SRichard Henderson void cpu_stl_be_data(CPUArchState *env, target_ulong ptr, uint32_t val)
2323cfe04a4bSRichard Henderson {
2324b9e60257SRichard Henderson     cpu_stl_be_data_ra(env, ptr, val, 0);
2325cfe04a4bSRichard Henderson }
2326cfe04a4bSRichard Henderson 
2327b9e60257SRichard Henderson void cpu_stq_be_data(CPUArchState *env, target_ulong ptr, uint64_t val)
2328cfe04a4bSRichard Henderson {
2329b9e60257SRichard Henderson     cpu_stq_be_data_ra(env, ptr, val, 0);
2330b9e60257SRichard Henderson }
2331b9e60257SRichard Henderson 
2332b9e60257SRichard Henderson void cpu_stw_le_data(CPUArchState *env, target_ulong ptr, uint32_t val)
2333b9e60257SRichard Henderson {
2334b9e60257SRichard Henderson     cpu_stw_le_data_ra(env, ptr, val, 0);
2335b9e60257SRichard Henderson }
2336b9e60257SRichard Henderson 
2337b9e60257SRichard Henderson void cpu_stl_le_data(CPUArchState *env, target_ulong ptr, uint32_t val)
2338b9e60257SRichard Henderson {
2339b9e60257SRichard Henderson     cpu_stl_le_data_ra(env, ptr, val, 0);
2340b9e60257SRichard Henderson }
2341b9e60257SRichard Henderson 
2342b9e60257SRichard Henderson void cpu_stq_le_data(CPUArchState *env, target_ulong ptr, uint64_t val)
2343b9e60257SRichard Henderson {
2344b9e60257SRichard Henderson     cpu_stq_le_data_ra(env, ptr, val, 0);
2345cfe04a4bSRichard Henderson }
2346cfe04a4bSRichard Henderson 
2347d9bb58e5SYang Zhong /* First set of helpers allows passing in of OI and RETADDR.  This makes
2348d9bb58e5SYang Zhong    them callable from other helpers.  */
2349d9bb58e5SYang Zhong 
2350d9bb58e5SYang Zhong #define EXTRA_ARGS     , TCGMemOpIdx oi, uintptr_t retaddr
2351d9bb58e5SYang Zhong #define ATOMIC_NAME(X) \
2352d9bb58e5SYang Zhong     HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
2353707526adSRichard Henderson #define ATOMIC_MMU_DECLS
2354707526adSRichard Henderson #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, retaddr)
2355707526adSRichard Henderson #define ATOMIC_MMU_CLEANUP
2356504f73f7SAlex Bennée #define ATOMIC_MMU_IDX   get_mmuidx(oi)
2357d9bb58e5SYang Zhong 
2358cfec3885SEmilio G. Cota #include "atomic_common.inc.c"
2359d9bb58e5SYang Zhong 
2360d9bb58e5SYang Zhong #define DATA_SIZE 1
2361d9bb58e5SYang Zhong #include "atomic_template.h"
2362d9bb58e5SYang Zhong 
2363d9bb58e5SYang Zhong #define DATA_SIZE 2
2364d9bb58e5SYang Zhong #include "atomic_template.h"
2365d9bb58e5SYang Zhong 
2366d9bb58e5SYang Zhong #define DATA_SIZE 4
2367d9bb58e5SYang Zhong #include "atomic_template.h"
2368d9bb58e5SYang Zhong 
2369d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
2370d9bb58e5SYang Zhong #define DATA_SIZE 8
2371d9bb58e5SYang Zhong #include "atomic_template.h"
2372d9bb58e5SYang Zhong #endif
2373d9bb58e5SYang Zhong 
2374e6cd4bb5SRichard Henderson #if HAVE_CMPXCHG128 || HAVE_ATOMIC128
2375d9bb58e5SYang Zhong #define DATA_SIZE 16
2376d9bb58e5SYang Zhong #include "atomic_template.h"
2377d9bb58e5SYang Zhong #endif
2378d9bb58e5SYang Zhong 
2379d9bb58e5SYang Zhong /* Second set of helpers are directly callable from TCG as helpers.  */
2380d9bb58e5SYang Zhong 
2381d9bb58e5SYang Zhong #undef EXTRA_ARGS
2382d9bb58e5SYang Zhong #undef ATOMIC_NAME
2383d9bb58e5SYang Zhong #undef ATOMIC_MMU_LOOKUP
2384d9bb58e5SYang Zhong #define EXTRA_ARGS         , TCGMemOpIdx oi
2385d9bb58e5SYang Zhong #define ATOMIC_NAME(X)     HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
2386707526adSRichard Henderson #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, oi, GETPC())
2387d9bb58e5SYang Zhong 
2388d9bb58e5SYang Zhong #define DATA_SIZE 1
2389d9bb58e5SYang Zhong #include "atomic_template.h"
2390d9bb58e5SYang Zhong 
2391d9bb58e5SYang Zhong #define DATA_SIZE 2
2392d9bb58e5SYang Zhong #include "atomic_template.h"
2393d9bb58e5SYang Zhong 
2394d9bb58e5SYang Zhong #define DATA_SIZE 4
2395d9bb58e5SYang Zhong #include "atomic_template.h"
2396d9bb58e5SYang Zhong 
2397d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
2398d9bb58e5SYang Zhong #define DATA_SIZE 8
2399d9bb58e5SYang Zhong #include "atomic_template.h"
2400d9bb58e5SYang Zhong #endif
2401504f73f7SAlex Bennée #undef ATOMIC_MMU_IDX
2402d9bb58e5SYang Zhong 
2403d9bb58e5SYang Zhong /* Code access functions.  */
2404d9bb58e5SYang Zhong 
2405fc4120a3SRichard Henderson static uint64_t full_ldub_code(CPUArchState *env, target_ulong addr,
24062dd92606SRichard Henderson                                TCGMemOpIdx oi, uintptr_t retaddr)
24072dd92606SRichard Henderson {
2408fc4120a3SRichard Henderson     return load_helper(env, addr, oi, retaddr, MO_8, true, full_ldub_code);
24092dd92606SRichard Henderson }
24102dd92606SRichard Henderson 
2411fc4120a3SRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr)
2412eed56642SAlex Bennée {
2413fc4120a3SRichard Henderson     TCGMemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(env, true));
2414fc4120a3SRichard Henderson     return full_ldub_code(env, addr, oi, 0);
24152dd92606SRichard Henderson }
24162dd92606SRichard Henderson 
2417fc4120a3SRichard Henderson static uint64_t full_lduw_code(CPUArchState *env, target_ulong addr,
24184cef72d0SAlex Bennée                                TCGMemOpIdx oi, uintptr_t retaddr)
24194cef72d0SAlex Bennée {
2420fc4120a3SRichard Henderson     return load_helper(env, addr, oi, retaddr, MO_TEUW, true, full_lduw_code);
24214cef72d0SAlex Bennée }
24224cef72d0SAlex Bennée 
2423fc4120a3SRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr)
24242dd92606SRichard Henderson {
2425fc4120a3SRichard Henderson     TCGMemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(env, true));
2426fc4120a3SRichard Henderson     return full_lduw_code(env, addr, oi, 0);
2427eed56642SAlex Bennée }
2428d9bb58e5SYang Zhong 
2429fc4120a3SRichard Henderson static uint64_t full_ldl_code(CPUArchState *env, target_ulong addr,
2430fc1bc777SRichard Henderson                               TCGMemOpIdx oi, uintptr_t retaddr)
2431eed56642SAlex Bennée {
2432fc4120a3SRichard Henderson     return load_helper(env, addr, oi, retaddr, MO_TEUL, true, full_ldl_code);
24332dd92606SRichard Henderson }
24342dd92606SRichard Henderson 
2435fc4120a3SRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr)
24364cef72d0SAlex Bennée {
2437fc4120a3SRichard Henderson     TCGMemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(env, true));
2438fc4120a3SRichard Henderson     return full_ldl_code(env, addr, oi, 0);
24394cef72d0SAlex Bennée }
24404cef72d0SAlex Bennée 
2441fc4120a3SRichard Henderson static uint64_t full_ldq_code(CPUArchState *env, target_ulong addr,
24422dd92606SRichard Henderson                               TCGMemOpIdx oi, uintptr_t retaddr)
24432dd92606SRichard Henderson {
2444fc4120a3SRichard Henderson     return load_helper(env, addr, oi, retaddr, MO_TEQ, true, full_ldq_code);
2445eed56642SAlex Bennée }
2446d9bb58e5SYang Zhong 
2447fc4120a3SRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr)
2448eed56642SAlex Bennée {
2449fc4120a3SRichard Henderson     TCGMemOpIdx oi = make_memop_idx(MO_TEQ, cpu_mmu_index(env, true));
2450fc4120a3SRichard Henderson     return full_ldq_code(env, addr, oi, 0);
2451eed56642SAlex Bennée }
2452