xref: /openbmc/qemu/accel/tcg/cputlb.c (revision e2faabee78ff127848f59892747d4c07c56de033)
1d9bb58e5SYang Zhong /*
2d9bb58e5SYang Zhong  *  Common CPU TLB handling
3d9bb58e5SYang Zhong  *
4d9bb58e5SYang Zhong  *  Copyright (c) 2003 Fabrice Bellard
5d9bb58e5SYang Zhong  *
6d9bb58e5SYang Zhong  * This library is free software; you can redistribute it and/or
7d9bb58e5SYang Zhong  * modify it under the terms of the GNU Lesser General Public
8d9bb58e5SYang Zhong  * License as published by the Free Software Foundation; either
9fb0343d5SThomas Huth  * version 2.1 of the License, or (at your option) any later version.
10d9bb58e5SYang Zhong  *
11d9bb58e5SYang Zhong  * This library is distributed in the hope that it will be useful,
12d9bb58e5SYang Zhong  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d9bb58e5SYang Zhong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14d9bb58e5SYang Zhong  * Lesser General Public License for more details.
15d9bb58e5SYang Zhong  *
16d9bb58e5SYang Zhong  * You should have received a copy of the GNU Lesser General Public
17d9bb58e5SYang Zhong  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18d9bb58e5SYang Zhong  */
19d9bb58e5SYang Zhong 
20d9bb58e5SYang Zhong #include "qemu/osdep.h"
21d9bb58e5SYang Zhong #include "qemu/main-loop.h"
2278271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
23d9bb58e5SYang Zhong #include "exec/exec-all.h"
24d9bb58e5SYang Zhong #include "exec/memory.h"
25d9bb58e5SYang Zhong #include "exec/cpu_ldst.h"
26d9bb58e5SYang Zhong #include "exec/cputlb.h"
27f4f826c0SPhilippe Mathieu-Daudé #include "exec/tb-flush.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"
33c213ee2dSRichard Henderson #include "exec/helper-proto-common.h"
34d9bb58e5SYang Zhong #include "qemu/atomic.h"
35e6cd4bb5SRichard Henderson #include "qemu/atomic128.h"
363b9bd3f4SPaolo Bonzini #include "exec/translate-all.h"
3751807763SPhilippe Mathieu-Daudé #include "trace.h"
38e5ceadffSPhilippe Mathieu-Daudé #include "tb-hash.h"
3943e7a2d3SPhilippe Mathieu-Daudé #include "internal-common.h"
404c268d6dSPhilippe Mathieu-Daudé #include "internal-target.h"
41235537faSAlex Bennée #ifdef CONFIG_PLUGIN
42235537faSAlex Bennée #include "qemu/plugin-memory.h"
43235537faSAlex Bennée #endif
44d2ba8026SRichard Henderson #include "tcg/tcg-ldst.h"
4570f168f8SRichard Henderson #include "tcg/oversized-guest.h"
46d9bb58e5SYang Zhong 
47d9bb58e5SYang Zhong /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
48d9bb58e5SYang Zhong /* #define DEBUG_TLB */
49d9bb58e5SYang Zhong /* #define DEBUG_TLB_LOG */
50d9bb58e5SYang Zhong 
51d9bb58e5SYang Zhong #ifdef DEBUG_TLB
52d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 1
53d9bb58e5SYang Zhong # ifdef DEBUG_TLB_LOG
54d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 1
55d9bb58e5SYang Zhong # else
56d9bb58e5SYang Zhong #  define DEBUG_TLB_LOG_GATE 0
57d9bb58e5SYang Zhong # endif
58d9bb58e5SYang Zhong #else
59d9bb58e5SYang Zhong # define DEBUG_TLB_GATE 0
60d9bb58e5SYang Zhong # define DEBUG_TLB_LOG_GATE 0
61d9bb58e5SYang Zhong #endif
62d9bb58e5SYang Zhong 
63d9bb58e5SYang Zhong #define tlb_debug(fmt, ...) do { \
64d9bb58e5SYang Zhong     if (DEBUG_TLB_LOG_GATE) { \
65d9bb58e5SYang Zhong         qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
66d9bb58e5SYang Zhong                       ## __VA_ARGS__); \
67d9bb58e5SYang Zhong     } else if (DEBUG_TLB_GATE) { \
68d9bb58e5SYang Zhong         fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
69d9bb58e5SYang Zhong     } \
70d9bb58e5SYang Zhong } while (0)
71d9bb58e5SYang Zhong 
72ea9025cbSEmilio G. Cota #define assert_cpu_is_self(cpu) do {                              \
73d9bb58e5SYang Zhong         if (DEBUG_TLB_GATE) {                                     \
74ea9025cbSEmilio G. Cota             g_assert(!(cpu)->created || qemu_cpu_is_self(cpu));   \
75d9bb58e5SYang Zhong         }                                                         \
76d9bb58e5SYang Zhong     } while (0)
77d9bb58e5SYang Zhong 
78d9bb58e5SYang Zhong /* run_on_cpu_data.target_ptr should always be big enough for a
79e79f8142SAnton Johansson  * vaddr even on 32 bit builds
80e79f8142SAnton Johansson  */
81e79f8142SAnton Johansson QEMU_BUILD_BUG_ON(sizeof(vaddr) > sizeof(run_on_cpu_data));
82d9bb58e5SYang Zhong 
83d9bb58e5SYang Zhong /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
84d9bb58e5SYang Zhong  */
85d9bb58e5SYang Zhong QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16);
86d9bb58e5SYang Zhong #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
87d9bb58e5SYang Zhong 
88722a1c1eSRichard Henderson static inline size_t tlb_n_entries(CPUTLBDescFast *fast)
897a1efe1bSRichard Henderson {
90722a1c1eSRichard Henderson     return (fast->mask >> CPU_TLB_ENTRY_BITS) + 1;
917a1efe1bSRichard Henderson }
927a1efe1bSRichard Henderson 
93722a1c1eSRichard Henderson static inline size_t sizeof_tlb(CPUTLBDescFast *fast)
9486e1eff8SEmilio G. Cota {
95722a1c1eSRichard Henderson     return fast->mask + (1 << CPU_TLB_ENTRY_BITS);
9686e1eff8SEmilio G. Cota }
9786e1eff8SEmilio G. Cota 
9879e42085SRichard Henderson static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns,
9986e1eff8SEmilio G. Cota                              size_t max_entries)
10086e1eff8SEmilio G. Cota {
10179e42085SRichard Henderson     desc->window_begin_ns = ns;
10279e42085SRichard Henderson     desc->window_max_entries = max_entries;
10386e1eff8SEmilio G. Cota }
10486e1eff8SEmilio G. Cota 
10506f3831cSAnton Johansson static void tb_jmp_cache_clear_page(CPUState *cpu, vaddr page_addr)
1060f4abea8SRichard Henderson {
107a976a99aSRichard Henderson     CPUJumpCache *jc = cpu->tb_jmp_cache;
10899ab4d50SEric Auger     int i, i0;
1090f4abea8SRichard Henderson 
11099ab4d50SEric Auger     if (unlikely(!jc)) {
11199ab4d50SEric Auger         return;
11299ab4d50SEric Auger     }
11399ab4d50SEric Auger 
11499ab4d50SEric Auger     i0 = tb_jmp_cache_hash_page(page_addr);
1150f4abea8SRichard Henderson     for (i = 0; i < TB_JMP_PAGE_SIZE; i++) {
116a976a99aSRichard Henderson         qatomic_set(&jc->array[i0 + i].tb, NULL);
1170f4abea8SRichard Henderson     }
1180f4abea8SRichard Henderson }
1190f4abea8SRichard Henderson 
12086e1eff8SEmilio G. Cota /**
12186e1eff8SEmilio G. Cota  * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
12271ccd47bSRichard Henderson  * @desc: The CPUTLBDesc portion of the TLB
12371ccd47bSRichard Henderson  * @fast: The CPUTLBDescFast portion of the same TLB
12486e1eff8SEmilio G. Cota  *
12586e1eff8SEmilio G. Cota  * Called with tlb_lock_held.
12686e1eff8SEmilio G. Cota  *
12786e1eff8SEmilio G. Cota  * We have two main constraints when resizing a TLB: (1) we only resize it
12886e1eff8SEmilio G. Cota  * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
12986e1eff8SEmilio G. Cota  * the array or unnecessarily flushing it), which means we do not control how
13086e1eff8SEmilio G. Cota  * frequently the resizing can occur; (2) we don't have access to the guest's
13186e1eff8SEmilio G. Cota  * future scheduling decisions, and therefore have to decide the magnitude of
13286e1eff8SEmilio G. Cota  * the resize based on past observations.
13386e1eff8SEmilio G. Cota  *
13486e1eff8SEmilio G. Cota  * In general, a memory-hungry process can benefit greatly from an appropriately
13586e1eff8SEmilio G. Cota  * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
13686e1eff8SEmilio G. Cota  * we just have to make the TLB as large as possible; while an oversized TLB
13786e1eff8SEmilio G. Cota  * results in minimal TLB miss rates, it also takes longer to be flushed
13886e1eff8SEmilio G. Cota  * (flushes can be _very_ frequent), and the reduced locality can also hurt
13986e1eff8SEmilio G. Cota  * performance.
14086e1eff8SEmilio G. Cota  *
14186e1eff8SEmilio G. Cota  * To achieve near-optimal performance for all kinds of workloads, we:
14286e1eff8SEmilio G. Cota  *
14386e1eff8SEmilio G. Cota  * 1. Aggressively increase the size of the TLB when the use rate of the
14486e1eff8SEmilio G. Cota  * TLB being flushed is high, since it is likely that in the near future this
14586e1eff8SEmilio G. Cota  * memory-hungry process will execute again, and its memory hungriness will
14686e1eff8SEmilio G. Cota  * probably be similar.
14786e1eff8SEmilio G. Cota  *
14886e1eff8SEmilio G. Cota  * 2. Slowly reduce the size of the TLB as the use rate declines over a
14986e1eff8SEmilio G. Cota  * reasonably large time window. The rationale is that if in such a time window
15086e1eff8SEmilio G. Cota  * we have not observed a high TLB use rate, it is likely that we won't observe
15186e1eff8SEmilio G. Cota  * it in the near future. In that case, once a time window expires we downsize
15286e1eff8SEmilio G. Cota  * the TLB to match the maximum use rate observed in the window.
15386e1eff8SEmilio G. Cota  *
15486e1eff8SEmilio G. Cota  * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
15586e1eff8SEmilio G. Cota  * since in that range performance is likely near-optimal. Recall that the TLB
15686e1eff8SEmilio G. Cota  * is direct mapped, so we want the use rate to be low (or at least not too
15786e1eff8SEmilio G. Cota  * high), since otherwise we are likely to have a significant amount of
15886e1eff8SEmilio G. Cota  * conflict misses.
15986e1eff8SEmilio G. Cota  */
1603c3959f2SRichard Henderson static void tlb_mmu_resize_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast,
1613c3959f2SRichard Henderson                                   int64_t now)
16286e1eff8SEmilio G. Cota {
16371ccd47bSRichard Henderson     size_t old_size = tlb_n_entries(fast);
16486e1eff8SEmilio G. Cota     size_t rate;
16586e1eff8SEmilio G. Cota     size_t new_size = old_size;
16686e1eff8SEmilio G. Cota     int64_t window_len_ms = 100;
16786e1eff8SEmilio G. Cota     int64_t window_len_ns = window_len_ms * 1000 * 1000;
16879e42085SRichard Henderson     bool window_expired = now > desc->window_begin_ns + window_len_ns;
16986e1eff8SEmilio G. Cota 
17079e42085SRichard Henderson     if (desc->n_used_entries > desc->window_max_entries) {
17179e42085SRichard Henderson         desc->window_max_entries = desc->n_used_entries;
17286e1eff8SEmilio G. Cota     }
17379e42085SRichard Henderson     rate = desc->window_max_entries * 100 / old_size;
17486e1eff8SEmilio G. Cota 
17586e1eff8SEmilio G. Cota     if (rate > 70) {
17686e1eff8SEmilio G. Cota         new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS);
17786e1eff8SEmilio G. Cota     } else if (rate < 30 && window_expired) {
17879e42085SRichard Henderson         size_t ceil = pow2ceil(desc->window_max_entries);
17979e42085SRichard Henderson         size_t expected_rate = desc->window_max_entries * 100 / ceil;
18086e1eff8SEmilio G. Cota 
18186e1eff8SEmilio G. Cota         /*
18286e1eff8SEmilio G. Cota          * Avoid undersizing when the max number of entries seen is just below
18386e1eff8SEmilio G. Cota          * a pow2. For instance, if max_entries == 1025, the expected use rate
18486e1eff8SEmilio G. Cota          * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
18586e1eff8SEmilio G. Cota          * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
18686e1eff8SEmilio G. Cota          * later. Thus, make sure that the expected use rate remains below 70%.
18786e1eff8SEmilio G. Cota          * (and since we double the size, that means the lowest rate we'd
18886e1eff8SEmilio G. Cota          * expect to get is 35%, which is still in the 30-70% range where
18986e1eff8SEmilio G. Cota          * we consider that the size is appropriate.)
19086e1eff8SEmilio G. Cota          */
19186e1eff8SEmilio G. Cota         if (expected_rate > 70) {
19286e1eff8SEmilio G. Cota             ceil *= 2;
19386e1eff8SEmilio G. Cota         }
19486e1eff8SEmilio G. Cota         new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS);
19586e1eff8SEmilio G. Cota     }
19686e1eff8SEmilio G. Cota 
19786e1eff8SEmilio G. Cota     if (new_size == old_size) {
19886e1eff8SEmilio G. Cota         if (window_expired) {
19979e42085SRichard Henderson             tlb_window_reset(desc, now, desc->n_used_entries);
20086e1eff8SEmilio G. Cota         }
20186e1eff8SEmilio G. Cota         return;
20286e1eff8SEmilio G. Cota     }
20386e1eff8SEmilio G. Cota 
20471ccd47bSRichard Henderson     g_free(fast->table);
20525d3ec58SRichard Henderson     g_free(desc->fulltlb);
20686e1eff8SEmilio G. Cota 
20779e42085SRichard Henderson     tlb_window_reset(desc, now, 0);
20886e1eff8SEmilio G. Cota     /* desc->n_used_entries is cleared by the caller */
20971ccd47bSRichard Henderson     fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
21071ccd47bSRichard Henderson     fast->table = g_try_new(CPUTLBEntry, new_size);
21125d3ec58SRichard Henderson     desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size);
21271ccd47bSRichard Henderson 
21386e1eff8SEmilio G. Cota     /*
21486e1eff8SEmilio G. Cota      * If the allocations fail, try smaller sizes. We just freed some
21586e1eff8SEmilio G. Cota      * memory, so going back to half of new_size has a good chance of working.
21686e1eff8SEmilio G. Cota      * Increased memory pressure elsewhere in the system might cause the
21786e1eff8SEmilio G. Cota      * allocations to fail though, so we progressively reduce the allocation
21886e1eff8SEmilio G. Cota      * size, aborting if we cannot even allocate the smallest TLB we support.
21986e1eff8SEmilio G. Cota      */
22025d3ec58SRichard Henderson     while (fast->table == NULL || desc->fulltlb == NULL) {
22186e1eff8SEmilio G. Cota         if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) {
22286e1eff8SEmilio G. Cota             error_report("%s: %s", __func__, strerror(errno));
22386e1eff8SEmilio G. Cota             abort();
22486e1eff8SEmilio G. Cota         }
22586e1eff8SEmilio G. Cota         new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS);
22671ccd47bSRichard Henderson         fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
22786e1eff8SEmilio G. Cota 
22871ccd47bSRichard Henderson         g_free(fast->table);
22925d3ec58SRichard Henderson         g_free(desc->fulltlb);
23071ccd47bSRichard Henderson         fast->table = g_try_new(CPUTLBEntry, new_size);
23125d3ec58SRichard Henderson         desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size);
23286e1eff8SEmilio G. Cota     }
23386e1eff8SEmilio G. Cota }
23486e1eff8SEmilio G. Cota 
235bbf021b0SRichard Henderson static void tlb_mmu_flush_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast)
23686e1eff8SEmilio G. Cota {
2375c948e31SRichard Henderson     desc->n_used_entries = 0;
2385c948e31SRichard Henderson     desc->large_page_addr = -1;
2395c948e31SRichard Henderson     desc->large_page_mask = -1;
2405c948e31SRichard Henderson     desc->vindex = 0;
2415c948e31SRichard Henderson     memset(fast->table, -1, sizeof_tlb(fast));
2425c948e31SRichard Henderson     memset(desc->vtable, -1, sizeof(desc->vtable));
24386e1eff8SEmilio G. Cota }
24486e1eff8SEmilio G. Cota 
24510b32e2cSAnton Johansson static void tlb_flush_one_mmuidx_locked(CPUState *cpu, int mmu_idx,
2463c3959f2SRichard Henderson                                         int64_t now)
247bbf021b0SRichard Henderson {
24810b32e2cSAnton Johansson     CPUTLBDesc *desc = &cpu->neg.tlb.d[mmu_idx];
24910b32e2cSAnton Johansson     CPUTLBDescFast *fast = &cpu->neg.tlb.f[mmu_idx];
250bbf021b0SRichard Henderson 
2513c3959f2SRichard Henderson     tlb_mmu_resize_locked(desc, fast, now);
252bbf021b0SRichard Henderson     tlb_mmu_flush_locked(desc, fast);
253bbf021b0SRichard Henderson }
254bbf021b0SRichard Henderson 
25556e89f76SRichard Henderson static void tlb_mmu_init(CPUTLBDesc *desc, CPUTLBDescFast *fast, int64_t now)
25656e89f76SRichard Henderson {
25756e89f76SRichard Henderson     size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS;
25856e89f76SRichard Henderson 
25956e89f76SRichard Henderson     tlb_window_reset(desc, now, 0);
26056e89f76SRichard Henderson     desc->n_used_entries = 0;
26156e89f76SRichard Henderson     fast->mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS;
26256e89f76SRichard Henderson     fast->table = g_new(CPUTLBEntry, n_entries);
26325d3ec58SRichard Henderson     desc->fulltlb = g_new(CPUTLBEntryFull, n_entries);
2643c16304aSRichard Henderson     tlb_mmu_flush_locked(desc, fast);
26556e89f76SRichard Henderson }
26656e89f76SRichard Henderson 
26710b32e2cSAnton Johansson static inline void tlb_n_used_entries_inc(CPUState *cpu, uintptr_t mmu_idx)
26886e1eff8SEmilio G. Cota {
26910b32e2cSAnton Johansson     cpu->neg.tlb.d[mmu_idx].n_used_entries++;
27086e1eff8SEmilio G. Cota }
27186e1eff8SEmilio G. Cota 
27210b32e2cSAnton Johansson static inline void tlb_n_used_entries_dec(CPUState *cpu, uintptr_t mmu_idx)
27386e1eff8SEmilio G. Cota {
27410b32e2cSAnton Johansson     cpu->neg.tlb.d[mmu_idx].n_used_entries--;
27586e1eff8SEmilio G. Cota }
27686e1eff8SEmilio G. Cota 
2775005e253SEmilio G. Cota void tlb_init(CPUState *cpu)
2785005e253SEmilio G. Cota {
27956e89f76SRichard Henderson     int64_t now = get_clock_realtime();
28056e89f76SRichard Henderson     int i;
28171aec354SEmilio G. Cota 
28210b32e2cSAnton Johansson     qemu_spin_init(&cpu->neg.tlb.c.lock);
2833d1523ceSRichard Henderson 
2843c16304aSRichard Henderson     /* All tlbs are initialized flushed. */
28510b32e2cSAnton Johansson     cpu->neg.tlb.c.dirty = 0;
28686e1eff8SEmilio G. Cota 
28756e89f76SRichard Henderson     for (i = 0; i < NB_MMU_MODES; i++) {
28810b32e2cSAnton Johansson         tlb_mmu_init(&cpu->neg.tlb.d[i], &cpu->neg.tlb.f[i], now);
28956e89f76SRichard Henderson     }
2905005e253SEmilio G. Cota }
2915005e253SEmilio G. Cota 
292816d9be5SEmilio G. Cota void tlb_destroy(CPUState *cpu)
293816d9be5SEmilio G. Cota {
294816d9be5SEmilio G. Cota     int i;
295816d9be5SEmilio G. Cota 
29610b32e2cSAnton Johansson     qemu_spin_destroy(&cpu->neg.tlb.c.lock);
297816d9be5SEmilio G. Cota     for (i = 0; i < NB_MMU_MODES; i++) {
29810b32e2cSAnton Johansson         CPUTLBDesc *desc = &cpu->neg.tlb.d[i];
29910b32e2cSAnton Johansson         CPUTLBDescFast *fast = &cpu->neg.tlb.f[i];
300816d9be5SEmilio G. Cota 
301816d9be5SEmilio G. Cota         g_free(fast->table);
30225d3ec58SRichard Henderson         g_free(desc->fulltlb);
303816d9be5SEmilio G. Cota     }
304816d9be5SEmilio G. Cota }
305816d9be5SEmilio G. Cota 
306d9bb58e5SYang Zhong /* flush_all_helper: run fn across all cpus
307d9bb58e5SYang Zhong  *
308d9bb58e5SYang Zhong  * If the wait flag is set then the src cpu's helper will be queued as
309d9bb58e5SYang Zhong  * "safe" work and the loop exited creating a synchronisation point
310d9bb58e5SYang Zhong  * where all queued work will be finished before execution starts
311d9bb58e5SYang Zhong  * again.
312d9bb58e5SYang Zhong  */
313d9bb58e5SYang Zhong static void flush_all_helper(CPUState *src, run_on_cpu_func fn,
314d9bb58e5SYang Zhong                              run_on_cpu_data d)
315d9bb58e5SYang Zhong {
316d9bb58e5SYang Zhong     CPUState *cpu;
317d9bb58e5SYang Zhong 
318d9bb58e5SYang Zhong     CPU_FOREACH(cpu) {
319d9bb58e5SYang Zhong         if (cpu != src) {
320d9bb58e5SYang Zhong             async_run_on_cpu(cpu, fn, d);
321d9bb58e5SYang Zhong         }
322d9bb58e5SYang Zhong     }
323d9bb58e5SYang Zhong }
324d9bb58e5SYang Zhong 
325d9bb58e5SYang Zhong static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)
326d9bb58e5SYang Zhong {
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 
33510b32e2cSAnton Johansson     qemu_spin_lock(&cpu->neg.tlb.c.lock);
33660a2ad7dSRichard Henderson 
33710b32e2cSAnton Johansson     all_dirty = cpu->neg.tlb.c.dirty;
3383d1523ceSRichard Henderson     to_clean = asked & all_dirty;
3393d1523ceSRichard Henderson     all_dirty &= ~to_clean;
34010b32e2cSAnton Johansson     cpu->neg.tlb.c.dirty = all_dirty;
3413d1523ceSRichard Henderson 
3423d1523ceSRichard Henderson     for (work = to_clean; work != 0; work &= work - 1) {
3433d1523ceSRichard Henderson         int mmu_idx = ctz32(work);
34410b32e2cSAnton Johansson         tlb_flush_one_mmuidx_locked(cpu, mmu_idx, now);
345d9bb58e5SYang Zhong     }
3463d1523ceSRichard Henderson 
34710b32e2cSAnton Johansson     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
348d9bb58e5SYang Zhong 
349a976a99aSRichard Henderson     tcg_flush_jmp_cache(cpu);
35064f2674bSRichard Henderson 
3513d1523ceSRichard Henderson     if (to_clean == ALL_MMUIDX_BITS) {
35210b32e2cSAnton Johansson         qatomic_set(&cpu->neg.tlb.c.full_flush_count,
35310b32e2cSAnton Johansson                     cpu->neg.tlb.c.full_flush_count + 1);
354e09de0a2SRichard Henderson     } else {
35510b32e2cSAnton Johansson         qatomic_set(&cpu->neg.tlb.c.part_flush_count,
35610b32e2cSAnton Johansson                     cpu->neg.tlb.c.part_flush_count + ctpop16(to_clean));
3573d1523ceSRichard Henderson         if (to_clean != asked) {
35810b32e2cSAnton Johansson             qatomic_set(&cpu->neg.tlb.c.elide_flush_count,
35910b32e2cSAnton Johansson                         cpu->neg.tlb.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 
4123ab6e68cSRichard Henderson static bool tlb_hit_page_mask_anyprot(CPUTLBEntry *tlb_entry,
413732d5487SAnton Johansson                                       vaddr page, vaddr mask)
4143ab6e68cSRichard Henderson {
4153ab6e68cSRichard Henderson     page &= mask;
4163ab6e68cSRichard Henderson     mask &= TARGET_PAGE_MASK | TLB_INVALID_MASK;
4173ab6e68cSRichard Henderson 
4183ab6e68cSRichard Henderson     return (page == (tlb_entry->addr_read & mask) ||
4193ab6e68cSRichard Henderson             page == (tlb_addr_write(tlb_entry) & mask) ||
4203ab6e68cSRichard Henderson             page == (tlb_entry->addr_code & mask));
4213ab6e68cSRichard Henderson }
4223ab6e68cSRichard Henderson 
423732d5487SAnton Johansson static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry, vaddr page)
424d9bb58e5SYang Zhong {
4253ab6e68cSRichard Henderson     return tlb_hit_page_mask_anyprot(tlb_entry, page, -1);
42668fea038SRichard Henderson }
42768fea038SRichard Henderson 
4283cea94bbSEmilio G. Cota /**
4293cea94bbSEmilio G. Cota  * tlb_entry_is_empty - return true if the entry is not in use
4303cea94bbSEmilio G. Cota  * @te: pointer to CPUTLBEntry
4313cea94bbSEmilio G. Cota  */
4323cea94bbSEmilio G. Cota static inline bool tlb_entry_is_empty(const CPUTLBEntry *te)
4333cea94bbSEmilio G. Cota {
4343cea94bbSEmilio G. Cota     return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1;
4353cea94bbSEmilio G. Cota }
4363cea94bbSEmilio G. Cota 
43753d28455SRichard Henderson /* Called with tlb_c.lock held */
4383ab6e68cSRichard Henderson static bool tlb_flush_entry_mask_locked(CPUTLBEntry *tlb_entry,
439732d5487SAnton Johansson                                         vaddr page,
440732d5487SAnton Johansson                                         vaddr mask)
44168fea038SRichard Henderson {
4423ab6e68cSRichard Henderson     if (tlb_hit_page_mask_anyprot(tlb_entry, page, mask)) {
443d9bb58e5SYang Zhong         memset(tlb_entry, -1, sizeof(*tlb_entry));
44486e1eff8SEmilio G. Cota         return true;
445d9bb58e5SYang Zhong     }
44686e1eff8SEmilio G. Cota     return false;
447d9bb58e5SYang Zhong }
448d9bb58e5SYang Zhong 
449732d5487SAnton Johansson static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry, vaddr page)
45068fea038SRichard Henderson {
4513ab6e68cSRichard Henderson     return tlb_flush_entry_mask_locked(tlb_entry, page, -1);
4523ab6e68cSRichard Henderson }
4533ab6e68cSRichard Henderson 
4543ab6e68cSRichard Henderson /* Called with tlb_c.lock held */
45510b32e2cSAnton Johansson static void tlb_flush_vtlb_page_mask_locked(CPUState *cpu, int mmu_idx,
456732d5487SAnton Johansson                                             vaddr page,
457732d5487SAnton Johansson                                             vaddr mask)
4583ab6e68cSRichard Henderson {
45910b32e2cSAnton Johansson     CPUTLBDesc *d = &cpu->neg.tlb.d[mmu_idx];
46068fea038SRichard Henderson     int k;
46171aec354SEmilio G. Cota 
46210b32e2cSAnton Johansson     assert_cpu_is_self(cpu);
46368fea038SRichard Henderson     for (k = 0; k < CPU_VTLB_SIZE; k++) {
4643ab6e68cSRichard Henderson         if (tlb_flush_entry_mask_locked(&d->vtable[k], page, mask)) {
46510b32e2cSAnton Johansson             tlb_n_used_entries_dec(cpu, mmu_idx);
46686e1eff8SEmilio G. Cota         }
46768fea038SRichard Henderson     }
46868fea038SRichard Henderson }
46968fea038SRichard Henderson 
47010b32e2cSAnton Johansson static inline void tlb_flush_vtlb_page_locked(CPUState *cpu, int mmu_idx,
471732d5487SAnton Johansson                                               vaddr page)
4723ab6e68cSRichard Henderson {
47310b32e2cSAnton Johansson     tlb_flush_vtlb_page_mask_locked(cpu, mmu_idx, page, -1);
4743ab6e68cSRichard Henderson }
4753ab6e68cSRichard Henderson 
47610b32e2cSAnton Johansson static void tlb_flush_page_locked(CPUState *cpu, int midx, vaddr page)
4771308e026SRichard Henderson {
47810b32e2cSAnton Johansson     vaddr lp_addr = cpu->neg.tlb.d[midx].large_page_addr;
47910b32e2cSAnton Johansson     vaddr lp_mask = cpu->neg.tlb.d[midx].large_page_mask;
4801308e026SRichard Henderson 
4811308e026SRichard Henderson     /* Check if we need to flush due to large pages.  */
4821308e026SRichard Henderson     if ((page & lp_mask) == lp_addr) {
4838c605cf1SAnton Johansson         tlb_debug("forcing full flush midx %d (%016"
4848c605cf1SAnton Johansson                   VADDR_PRIx "/%016" VADDR_PRIx ")\n",
4851308e026SRichard Henderson                   midx, lp_addr, lp_mask);
48610b32e2cSAnton Johansson         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
4871308e026SRichard Henderson     } else {
48810b32e2cSAnton Johansson         if (tlb_flush_entry_locked(tlb_entry(cpu, midx, page), page)) {
48910b32e2cSAnton Johansson             tlb_n_used_entries_dec(cpu, midx);
49086e1eff8SEmilio G. Cota         }
49110b32e2cSAnton Johansson         tlb_flush_vtlb_page_locked(cpu, midx, page);
4921308e026SRichard Henderson     }
4931308e026SRichard Henderson }
4941308e026SRichard Henderson 
4957b7d00e0SRichard Henderson /**
4967b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_0:
4977b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
4987b7d00e0SRichard Henderson  * @addr: page of virtual address to flush
4997b7d00e0SRichard Henderson  * @idxmap: set of mmu_idx to flush
5007b7d00e0SRichard Henderson  *
5017b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
5027b7d00e0SRichard Henderson  * at @addr from the tlbs indicated by @idxmap from @cpu.
503d9bb58e5SYang Zhong  */
5047b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_0(CPUState *cpu,
505732d5487SAnton Johansson                                              vaddr addr,
5067b7d00e0SRichard Henderson                                              uint16_t idxmap)
507d9bb58e5SYang Zhong {
508d9bb58e5SYang Zhong     int mmu_idx;
509d9bb58e5SYang Zhong 
510d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
511d9bb58e5SYang Zhong 
5128c605cf1SAnton Johansson     tlb_debug("page addr: %016" VADDR_PRIx " mmu_map:0x%x\n", addr, idxmap);
513d9bb58e5SYang Zhong 
51410b32e2cSAnton Johansson     qemu_spin_lock(&cpu->neg.tlb.c.lock);
515d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
5167b7d00e0SRichard Henderson         if ((idxmap >> mmu_idx) & 1) {
51710b32e2cSAnton Johansson             tlb_flush_page_locked(cpu, mmu_idx, addr);
518d9bb58e5SYang Zhong         }
519d9bb58e5SYang Zhong     }
52010b32e2cSAnton Johansson     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
521d9bb58e5SYang Zhong 
5221d41a79bSRichard Henderson     /*
5231d41a79bSRichard Henderson      * Discard jump cache entries for any tb which might potentially
5241d41a79bSRichard Henderson      * overlap the flushed page, which includes the previous.
5251d41a79bSRichard Henderson      */
5261d41a79bSRichard Henderson     tb_jmp_cache_clear_page(cpu, addr - TARGET_PAGE_SIZE);
5271d41a79bSRichard Henderson     tb_jmp_cache_clear_page(cpu, addr);
528d9bb58e5SYang Zhong }
529d9bb58e5SYang Zhong 
5307b7d00e0SRichard Henderson /**
5317b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_1:
5327b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
5337b7d00e0SRichard Henderson  * @data: encoded addr + idxmap
5347b7d00e0SRichard Henderson  *
5357b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, called through
5367b7d00e0SRichard Henderson  * async_run_on_cpu.  The idxmap parameter is encoded in the page
5377b7d00e0SRichard Henderson  * offset of the target_ptr field.  This limits the set of mmu_idx
5387b7d00e0SRichard Henderson  * that can be passed via this method.
5397b7d00e0SRichard Henderson  */
5407b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_1(CPUState *cpu,
5417b7d00e0SRichard Henderson                                              run_on_cpu_data data)
5427b7d00e0SRichard Henderson {
543732d5487SAnton Johansson     vaddr addr_and_idxmap = data.target_ptr;
544732d5487SAnton Johansson     vaddr addr = addr_and_idxmap & TARGET_PAGE_MASK;
5457b7d00e0SRichard Henderson     uint16_t idxmap = addr_and_idxmap & ~TARGET_PAGE_MASK;
5467b7d00e0SRichard Henderson 
5477b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
5487b7d00e0SRichard Henderson }
5497b7d00e0SRichard Henderson 
5507b7d00e0SRichard Henderson typedef struct {
551732d5487SAnton Johansson     vaddr addr;
5527b7d00e0SRichard Henderson     uint16_t idxmap;
5537b7d00e0SRichard Henderson } TLBFlushPageByMMUIdxData;
5547b7d00e0SRichard Henderson 
5557b7d00e0SRichard Henderson /**
5567b7d00e0SRichard Henderson  * tlb_flush_page_by_mmuidx_async_2:
5577b7d00e0SRichard Henderson  * @cpu: cpu on which to flush
5587b7d00e0SRichard Henderson  * @data: allocated addr + idxmap
5597b7d00e0SRichard Henderson  *
5607b7d00e0SRichard Henderson  * Helper for tlb_flush_page_by_mmuidx and friends, called through
5617b7d00e0SRichard Henderson  * async_run_on_cpu.  The addr+idxmap parameters are stored in a
5627b7d00e0SRichard Henderson  * TLBFlushPageByMMUIdxData structure that has been allocated
5637b7d00e0SRichard Henderson  * specifically for this helper.  Free the structure when done.
5647b7d00e0SRichard Henderson  */
5657b7d00e0SRichard Henderson static void tlb_flush_page_by_mmuidx_async_2(CPUState *cpu,
5667b7d00e0SRichard Henderson                                              run_on_cpu_data data)
5677b7d00e0SRichard Henderson {
5687b7d00e0SRichard Henderson     TLBFlushPageByMMUIdxData *d = data.host_ptr;
5697b7d00e0SRichard Henderson 
5707b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(cpu, d->addr, d->idxmap);
5717b7d00e0SRichard Henderson     g_free(d);
5727b7d00e0SRichard Henderson }
5737b7d00e0SRichard Henderson 
574732d5487SAnton Johansson void tlb_flush_page_by_mmuidx(CPUState *cpu, vaddr addr, uint16_t idxmap)
575d9bb58e5SYang Zhong {
5768c605cf1SAnton Johansson     tlb_debug("addr: %016" VADDR_PRIx " mmu_idx:%" PRIx16 "\n", addr, idxmap);
577d9bb58e5SYang Zhong 
578d9bb58e5SYang Zhong     /* This should already be page aligned */
5797b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
580d9bb58e5SYang Zhong 
5817b7d00e0SRichard Henderson     if (qemu_cpu_is_self(cpu)) {
5827b7d00e0SRichard Henderson         tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
5837b7d00e0SRichard Henderson     } else if (idxmap < TARGET_PAGE_SIZE) {
5847b7d00e0SRichard Henderson         /*
5857b7d00e0SRichard Henderson          * Most targets have only a few mmu_idx.  In the case where
5867b7d00e0SRichard Henderson          * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid
5877b7d00e0SRichard Henderson          * allocating memory for this operation.
5887b7d00e0SRichard Henderson          */
5897b7d00e0SRichard Henderson         async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_1,
5907b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
591d9bb58e5SYang Zhong     } else {
5927b7d00e0SRichard Henderson         TLBFlushPageByMMUIdxData *d = g_new(TLBFlushPageByMMUIdxData, 1);
5937b7d00e0SRichard Henderson 
5947b7d00e0SRichard Henderson         /* Otherwise allocate a structure, freed by the worker.  */
5957b7d00e0SRichard Henderson         d->addr = addr;
5967b7d00e0SRichard Henderson         d->idxmap = idxmap;
5977b7d00e0SRichard Henderson         async_run_on_cpu(cpu, tlb_flush_page_by_mmuidx_async_2,
5987b7d00e0SRichard Henderson                          RUN_ON_CPU_HOST_PTR(d));
599d9bb58e5SYang Zhong     }
600d9bb58e5SYang Zhong }
601d9bb58e5SYang Zhong 
602732d5487SAnton Johansson void tlb_flush_page(CPUState *cpu, vaddr addr)
603f8144c6cSRichard Henderson {
604f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS);
605f8144c6cSRichard Henderson }
606f8144c6cSRichard Henderson 
607732d5487SAnton Johansson void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, vaddr addr,
608d9bb58e5SYang Zhong                                        uint16_t idxmap)
609d9bb58e5SYang Zhong {
6108c605cf1SAnton Johansson     tlb_debug("addr: %016" VADDR_PRIx " mmu_idx:%"PRIx16"\n", addr, idxmap);
611d9bb58e5SYang Zhong 
612d9bb58e5SYang Zhong     /* This should already be page aligned */
6137b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
614d9bb58e5SYang Zhong 
6157b7d00e0SRichard Henderson     /*
6167b7d00e0SRichard Henderson      * Allocate memory to hold addr+idxmap only when needed.
6177b7d00e0SRichard Henderson      * See tlb_flush_page_by_mmuidx for details.
6187b7d00e0SRichard Henderson      */
6197b7d00e0SRichard Henderson     if (idxmap < TARGET_PAGE_SIZE) {
6207b7d00e0SRichard Henderson         flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1,
6217b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
6227b7d00e0SRichard Henderson     } else {
6237b7d00e0SRichard Henderson         CPUState *dst_cpu;
6247b7d00e0SRichard Henderson 
6257b7d00e0SRichard Henderson         /* Allocate a separate data block for each destination cpu.  */
6267b7d00e0SRichard Henderson         CPU_FOREACH(dst_cpu) {
6277b7d00e0SRichard Henderson             if (dst_cpu != src_cpu) {
6287b7d00e0SRichard Henderson                 TLBFlushPageByMMUIdxData *d
6297b7d00e0SRichard Henderson                     = g_new(TLBFlushPageByMMUIdxData, 1);
6307b7d00e0SRichard Henderson 
6317b7d00e0SRichard Henderson                 d->addr = addr;
6327b7d00e0SRichard Henderson                 d->idxmap = idxmap;
6337b7d00e0SRichard Henderson                 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2,
6347b7d00e0SRichard Henderson                                  RUN_ON_CPU_HOST_PTR(d));
6357b7d00e0SRichard Henderson             }
6367b7d00e0SRichard Henderson         }
6377b7d00e0SRichard Henderson     }
6387b7d00e0SRichard Henderson 
6397b7d00e0SRichard Henderson     tlb_flush_page_by_mmuidx_async_0(src_cpu, addr, idxmap);
640d9bb58e5SYang Zhong }
641d9bb58e5SYang Zhong 
642732d5487SAnton Johansson void tlb_flush_page_all_cpus(CPUState *src, vaddr addr)
643f8144c6cSRichard Henderson {
644f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus(src, addr, ALL_MMUIDX_BITS);
645f8144c6cSRichard Henderson }
646f8144c6cSRichard Henderson 
647d9bb58e5SYang Zhong void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
648732d5487SAnton Johansson                                               vaddr addr,
649d9bb58e5SYang Zhong                                               uint16_t idxmap)
650d9bb58e5SYang Zhong {
6518c605cf1SAnton Johansson     tlb_debug("addr: %016" VADDR_PRIx " mmu_idx:%"PRIx16"\n", addr, idxmap);
652d9bb58e5SYang Zhong 
653d9bb58e5SYang Zhong     /* This should already be page aligned */
6547b7d00e0SRichard Henderson     addr &= TARGET_PAGE_MASK;
655d9bb58e5SYang Zhong 
6567b7d00e0SRichard Henderson     /*
6577b7d00e0SRichard Henderson      * Allocate memory to hold addr+idxmap only when needed.
6587b7d00e0SRichard Henderson      * See tlb_flush_page_by_mmuidx for details.
6597b7d00e0SRichard Henderson      */
6607b7d00e0SRichard Henderson     if (idxmap < TARGET_PAGE_SIZE) {
6617b7d00e0SRichard Henderson         flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1,
6627b7d00e0SRichard Henderson                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
6637b7d00e0SRichard Henderson         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_1,
6647b7d00e0SRichard Henderson                               RUN_ON_CPU_TARGET_PTR(addr | idxmap));
6657b7d00e0SRichard Henderson     } else {
6667b7d00e0SRichard Henderson         CPUState *dst_cpu;
6677b7d00e0SRichard Henderson         TLBFlushPageByMMUIdxData *d;
6687b7d00e0SRichard Henderson 
6697b7d00e0SRichard Henderson         /* Allocate a separate data block for each destination cpu.  */
6707b7d00e0SRichard Henderson         CPU_FOREACH(dst_cpu) {
6717b7d00e0SRichard Henderson             if (dst_cpu != src_cpu) {
6727b7d00e0SRichard Henderson                 d = g_new(TLBFlushPageByMMUIdxData, 1);
6737b7d00e0SRichard Henderson                 d->addr = addr;
6747b7d00e0SRichard Henderson                 d->idxmap = idxmap;
6757b7d00e0SRichard Henderson                 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2,
6767b7d00e0SRichard Henderson                                  RUN_ON_CPU_HOST_PTR(d));
6777b7d00e0SRichard Henderson             }
6787b7d00e0SRichard Henderson         }
6797b7d00e0SRichard Henderson 
6807b7d00e0SRichard Henderson         d = g_new(TLBFlushPageByMMUIdxData, 1);
6817b7d00e0SRichard Henderson         d->addr = addr;
6827b7d00e0SRichard Henderson         d->idxmap = idxmap;
6837b7d00e0SRichard Henderson         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_2,
6847b7d00e0SRichard Henderson                               RUN_ON_CPU_HOST_PTR(d));
6857b7d00e0SRichard Henderson     }
686d9bb58e5SYang Zhong }
687d9bb58e5SYang Zhong 
688732d5487SAnton Johansson void tlb_flush_page_all_cpus_synced(CPUState *src, vaddr addr)
689d9bb58e5SYang Zhong {
690f8144c6cSRichard Henderson     tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS);
691d9bb58e5SYang Zhong }
692d9bb58e5SYang Zhong 
69310b32e2cSAnton Johansson static void tlb_flush_range_locked(CPUState *cpu, int midx,
694732d5487SAnton Johansson                                    vaddr addr, vaddr len,
6953c4ddec1SRichard Henderson                                    unsigned bits)
6963ab6e68cSRichard Henderson {
69710b32e2cSAnton Johansson     CPUTLBDesc *d = &cpu->neg.tlb.d[midx];
69810b32e2cSAnton Johansson     CPUTLBDescFast *f = &cpu->neg.tlb.f[midx];
699732d5487SAnton Johansson     vaddr mask = MAKE_64BIT_MASK(0, bits);
7003ab6e68cSRichard Henderson 
7013ab6e68cSRichard Henderson     /*
7023ab6e68cSRichard Henderson      * If @bits is smaller than the tlb size, there may be multiple entries
7033ab6e68cSRichard Henderson      * within the TLB; otherwise all addresses that match under @mask hit
7043ab6e68cSRichard Henderson      * the same TLB entry.
7053ab6e68cSRichard Henderson      * TODO: Perhaps allow bits to be a few bits less than the size.
7063ab6e68cSRichard Henderson      * For now, just flush the entire TLB.
7073c4ddec1SRichard Henderson      *
7083c4ddec1SRichard Henderson      * If @len is larger than the tlb size, then it will take longer to
7093c4ddec1SRichard Henderson      * test all of the entries in the TLB than it will to flush it all.
7103ab6e68cSRichard Henderson      */
7113c4ddec1SRichard Henderson     if (mask < f->mask || len > f->mask) {
7123ab6e68cSRichard Henderson         tlb_debug("forcing full flush midx %d ("
7138c605cf1SAnton Johansson                   "%016" VADDR_PRIx "/%016" VADDR_PRIx "+%016" VADDR_PRIx ")\n",
7143c4ddec1SRichard Henderson                   midx, addr, mask, len);
71510b32e2cSAnton Johansson         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
7163ab6e68cSRichard Henderson         return;
7173ab6e68cSRichard Henderson     }
7183ab6e68cSRichard Henderson 
7193c4ddec1SRichard Henderson     /*
7203c4ddec1SRichard Henderson      * Check if we need to flush due to large pages.
7213c4ddec1SRichard Henderson      * Because large_page_mask contains all 1's from the msb,
7223c4ddec1SRichard Henderson      * we only need to test the end of the range.
7233c4ddec1SRichard Henderson      */
7243c4ddec1SRichard Henderson     if (((addr + len - 1) & d->large_page_mask) == d->large_page_addr) {
7253ab6e68cSRichard Henderson         tlb_debug("forcing full flush midx %d ("
7268c605cf1SAnton Johansson                   "%016" VADDR_PRIx "/%016" VADDR_PRIx ")\n",
7273ab6e68cSRichard Henderson                   midx, d->large_page_addr, d->large_page_mask);
72810b32e2cSAnton Johansson         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
7293ab6e68cSRichard Henderson         return;
7303ab6e68cSRichard Henderson     }
7313ab6e68cSRichard Henderson 
732732d5487SAnton Johansson     for (vaddr i = 0; i < len; i += TARGET_PAGE_SIZE) {
733732d5487SAnton Johansson         vaddr page = addr + i;
73410b32e2cSAnton Johansson         CPUTLBEntry *entry = tlb_entry(cpu, midx, page);
7353c4ddec1SRichard Henderson 
7363c4ddec1SRichard Henderson         if (tlb_flush_entry_mask_locked(entry, page, mask)) {
73710b32e2cSAnton Johansson             tlb_n_used_entries_dec(cpu, midx);
7383ab6e68cSRichard Henderson         }
73910b32e2cSAnton Johansson         tlb_flush_vtlb_page_mask_locked(cpu, midx, page, mask);
7403ab6e68cSRichard Henderson     }
7413c4ddec1SRichard Henderson }
7423ab6e68cSRichard Henderson 
7433ab6e68cSRichard Henderson typedef struct {
744732d5487SAnton Johansson     vaddr addr;
745732d5487SAnton Johansson     vaddr len;
7463ab6e68cSRichard Henderson     uint16_t idxmap;
7473ab6e68cSRichard Henderson     uint16_t bits;
7483960a59fSRichard Henderson } TLBFlushRangeData;
7493ab6e68cSRichard Henderson 
7506be48e45SRichard Henderson static void tlb_flush_range_by_mmuidx_async_0(CPUState *cpu,
7513960a59fSRichard Henderson                                               TLBFlushRangeData d)
7523ab6e68cSRichard Henderson {
7533ab6e68cSRichard Henderson     int mmu_idx;
7543ab6e68cSRichard Henderson 
7553ab6e68cSRichard Henderson     assert_cpu_is_self(cpu);
7563ab6e68cSRichard Henderson 
7578c605cf1SAnton Johansson     tlb_debug("range: %016" VADDR_PRIx "/%u+%016" VADDR_PRIx " mmu_map:0x%x\n",
7583c4ddec1SRichard Henderson               d.addr, d.bits, d.len, d.idxmap);
7593ab6e68cSRichard Henderson 
76010b32e2cSAnton Johansson     qemu_spin_lock(&cpu->neg.tlb.c.lock);
7613ab6e68cSRichard Henderson     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
7623ab6e68cSRichard Henderson         if ((d.idxmap >> mmu_idx) & 1) {
76310b32e2cSAnton Johansson             tlb_flush_range_locked(cpu, mmu_idx, d.addr, d.len, d.bits);
7643ab6e68cSRichard Henderson         }
7653ab6e68cSRichard Henderson     }
76610b32e2cSAnton Johansson     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
7673ab6e68cSRichard Henderson 
768cfc2a2d6SIdan Horowitz     /*
769cfc2a2d6SIdan Horowitz      * If the length is larger than the jump cache size, then it will take
770cfc2a2d6SIdan Horowitz      * longer to clear each entry individually than it will to clear it all.
771cfc2a2d6SIdan Horowitz      */
772cfc2a2d6SIdan Horowitz     if (d.len >= (TARGET_PAGE_SIZE * TB_JMP_CACHE_SIZE)) {
773a976a99aSRichard Henderson         tcg_flush_jmp_cache(cpu);
774cfc2a2d6SIdan Horowitz         return;
775cfc2a2d6SIdan Horowitz     }
776cfc2a2d6SIdan Horowitz 
7771d41a79bSRichard Henderson     /*
7781d41a79bSRichard Henderson      * Discard jump cache entries for any tb which might potentially
7791d41a79bSRichard Henderson      * overlap the flushed pages, which includes the previous.
7801d41a79bSRichard Henderson      */
7811d41a79bSRichard Henderson     d.addr -= TARGET_PAGE_SIZE;
782732d5487SAnton Johansson     for (vaddr i = 0, n = d.len / TARGET_PAGE_SIZE + 1; i < n; i++) {
7831d41a79bSRichard Henderson         tb_jmp_cache_clear_page(cpu, d.addr);
7841d41a79bSRichard Henderson         d.addr += TARGET_PAGE_SIZE;
7853c4ddec1SRichard Henderson     }
7863ab6e68cSRichard Henderson }
7873ab6e68cSRichard Henderson 
788206a583dSRichard Henderson static void tlb_flush_range_by_mmuidx_async_1(CPUState *cpu,
7893ab6e68cSRichard Henderson                                               run_on_cpu_data data)
7903ab6e68cSRichard Henderson {
7913960a59fSRichard Henderson     TLBFlushRangeData *d = data.host_ptr;
7926be48e45SRichard Henderson     tlb_flush_range_by_mmuidx_async_0(cpu, *d);
7933ab6e68cSRichard Henderson     g_free(d);
7943ab6e68cSRichard Henderson }
7953ab6e68cSRichard Henderson 
796732d5487SAnton Johansson void tlb_flush_range_by_mmuidx(CPUState *cpu, vaddr addr,
797732d5487SAnton Johansson                                vaddr len, uint16_t idxmap,
798e5b1921bSRichard Henderson                                unsigned bits)
7993ab6e68cSRichard Henderson {
8003960a59fSRichard Henderson     TLBFlushRangeData d;
8013ab6e68cSRichard Henderson 
802e5b1921bSRichard Henderson     /*
803e5b1921bSRichard Henderson      * If all bits are significant, and len is small,
804e5b1921bSRichard Henderson      * this devolves to tlb_flush_page.
805e5b1921bSRichard Henderson      */
806e5b1921bSRichard Henderson     if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) {
8073ab6e68cSRichard Henderson         tlb_flush_page_by_mmuidx(cpu, addr, idxmap);
8083ab6e68cSRichard Henderson         return;
8093ab6e68cSRichard Henderson     }
8103ab6e68cSRichard Henderson     /* If no page bits are significant, this devolves to tlb_flush. */
8113ab6e68cSRichard Henderson     if (bits < TARGET_PAGE_BITS) {
8123ab6e68cSRichard Henderson         tlb_flush_by_mmuidx(cpu, idxmap);
8133ab6e68cSRichard Henderson         return;
8143ab6e68cSRichard Henderson     }
8153ab6e68cSRichard Henderson 
8163ab6e68cSRichard Henderson     /* This should already be page aligned */
8173ab6e68cSRichard Henderson     d.addr = addr & TARGET_PAGE_MASK;
818e5b1921bSRichard Henderson     d.len = len;
8193ab6e68cSRichard Henderson     d.idxmap = idxmap;
8203ab6e68cSRichard Henderson     d.bits = bits;
8213ab6e68cSRichard Henderson 
8223ab6e68cSRichard Henderson     if (qemu_cpu_is_self(cpu)) {
8236be48e45SRichard Henderson         tlb_flush_range_by_mmuidx_async_0(cpu, d);
8243ab6e68cSRichard Henderson     } else {
8253ab6e68cSRichard Henderson         /* Otherwise allocate a structure, freed by the worker.  */
8263960a59fSRichard Henderson         TLBFlushRangeData *p = g_memdup(&d, sizeof(d));
827206a583dSRichard Henderson         async_run_on_cpu(cpu, tlb_flush_range_by_mmuidx_async_1,
8283ab6e68cSRichard Henderson                          RUN_ON_CPU_HOST_PTR(p));
8293ab6e68cSRichard Henderson     }
8303ab6e68cSRichard Henderson }
8313ab6e68cSRichard Henderson 
832732d5487SAnton Johansson void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, vaddr addr,
833e5b1921bSRichard Henderson                                    uint16_t idxmap, unsigned bits)
834e5b1921bSRichard Henderson {
835e5b1921bSRichard Henderson     tlb_flush_range_by_mmuidx(cpu, addr, TARGET_PAGE_SIZE, idxmap, bits);
836e5b1921bSRichard Henderson }
837e5b1921bSRichard Henderson 
838600b819fSRichard Henderson void tlb_flush_range_by_mmuidx_all_cpus(CPUState *src_cpu,
839732d5487SAnton Johansson                                         vaddr addr, vaddr len,
840600b819fSRichard Henderson                                         uint16_t idxmap, unsigned bits)
8413ab6e68cSRichard Henderson {
8423960a59fSRichard Henderson     TLBFlushRangeData d;
843d34e4d1aSRichard Henderson     CPUState *dst_cpu;
8443ab6e68cSRichard Henderson 
845600b819fSRichard Henderson     /*
846600b819fSRichard Henderson      * If all bits are significant, and len is small,
847600b819fSRichard Henderson      * this devolves to tlb_flush_page.
848600b819fSRichard Henderson      */
849600b819fSRichard Henderson     if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) {
8503ab6e68cSRichard Henderson         tlb_flush_page_by_mmuidx_all_cpus(src_cpu, addr, idxmap);
8513ab6e68cSRichard Henderson         return;
8523ab6e68cSRichard Henderson     }
8533ab6e68cSRichard Henderson     /* If no page bits are significant, this devolves to tlb_flush. */
8543ab6e68cSRichard Henderson     if (bits < TARGET_PAGE_BITS) {
8553ab6e68cSRichard Henderson         tlb_flush_by_mmuidx_all_cpus(src_cpu, idxmap);
8563ab6e68cSRichard Henderson         return;
8573ab6e68cSRichard Henderson     }
8583ab6e68cSRichard Henderson 
8593ab6e68cSRichard Henderson     /* This should already be page aligned */
8603ab6e68cSRichard Henderson     d.addr = addr & TARGET_PAGE_MASK;
861600b819fSRichard Henderson     d.len = len;
8623ab6e68cSRichard Henderson     d.idxmap = idxmap;
8633ab6e68cSRichard Henderson     d.bits = bits;
8643ab6e68cSRichard Henderson 
8653ab6e68cSRichard Henderson     /* Allocate a separate data block for each destination cpu.  */
8663ab6e68cSRichard Henderson     CPU_FOREACH(dst_cpu) {
8673ab6e68cSRichard Henderson         if (dst_cpu != src_cpu) {
8683960a59fSRichard Henderson             TLBFlushRangeData *p = g_memdup(&d, sizeof(d));
8693ab6e68cSRichard Henderson             async_run_on_cpu(dst_cpu,
870206a583dSRichard Henderson                              tlb_flush_range_by_mmuidx_async_1,
8713ab6e68cSRichard Henderson                              RUN_ON_CPU_HOST_PTR(p));
8723ab6e68cSRichard Henderson         }
8733ab6e68cSRichard Henderson     }
8743ab6e68cSRichard Henderson 
8756be48e45SRichard Henderson     tlb_flush_range_by_mmuidx_async_0(src_cpu, d);
8763ab6e68cSRichard Henderson }
8773ab6e68cSRichard Henderson 
878600b819fSRichard Henderson void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState *src_cpu,
879732d5487SAnton Johansson                                             vaddr addr, uint16_t idxmap,
880732d5487SAnton Johansson                                             unsigned bits)
881600b819fSRichard Henderson {
882600b819fSRichard Henderson     tlb_flush_range_by_mmuidx_all_cpus(src_cpu, addr, TARGET_PAGE_SIZE,
883600b819fSRichard Henderson                                        idxmap, bits);
884600b819fSRichard Henderson }
885600b819fSRichard Henderson 
886c13b27d8SRichard Henderson void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
887732d5487SAnton Johansson                                                vaddr addr,
888732d5487SAnton Johansson                                                vaddr len,
8893ab6e68cSRichard Henderson                                                uint16_t idxmap,
8903ab6e68cSRichard Henderson                                                unsigned bits)
8913ab6e68cSRichard Henderson {
892d34e4d1aSRichard Henderson     TLBFlushRangeData d, *p;
893d34e4d1aSRichard Henderson     CPUState *dst_cpu;
8943ab6e68cSRichard Henderson 
895c13b27d8SRichard Henderson     /*
896c13b27d8SRichard Henderson      * If all bits are significant, and len is small,
897c13b27d8SRichard Henderson      * this devolves to tlb_flush_page.
898c13b27d8SRichard Henderson      */
899c13b27d8SRichard Henderson     if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) {
9003ab6e68cSRichard Henderson         tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu, addr, idxmap);
9013ab6e68cSRichard Henderson         return;
9023ab6e68cSRichard Henderson     }
9033ab6e68cSRichard Henderson     /* If no page bits are significant, this devolves to tlb_flush. */
9043ab6e68cSRichard Henderson     if (bits < TARGET_PAGE_BITS) {
9053ab6e68cSRichard Henderson         tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, idxmap);
9063ab6e68cSRichard Henderson         return;
9073ab6e68cSRichard Henderson     }
9083ab6e68cSRichard Henderson 
9093ab6e68cSRichard Henderson     /* This should already be page aligned */
9103ab6e68cSRichard Henderson     d.addr = addr & TARGET_PAGE_MASK;
911c13b27d8SRichard Henderson     d.len = len;
9123ab6e68cSRichard Henderson     d.idxmap = idxmap;
9133ab6e68cSRichard Henderson     d.bits = bits;
9143ab6e68cSRichard Henderson 
9153ab6e68cSRichard Henderson     /* Allocate a separate data block for each destination cpu.  */
9163ab6e68cSRichard Henderson     CPU_FOREACH(dst_cpu) {
9173ab6e68cSRichard Henderson         if (dst_cpu != src_cpu) {
9186d244788SRichard Henderson             p = g_memdup(&d, sizeof(d));
919206a583dSRichard Henderson             async_run_on_cpu(dst_cpu, tlb_flush_range_by_mmuidx_async_1,
9203ab6e68cSRichard Henderson                              RUN_ON_CPU_HOST_PTR(p));
9213ab6e68cSRichard Henderson         }
9223ab6e68cSRichard Henderson     }
9233ab6e68cSRichard Henderson 
9246d244788SRichard Henderson     p = g_memdup(&d, sizeof(d));
925206a583dSRichard Henderson     async_safe_run_on_cpu(src_cpu, tlb_flush_range_by_mmuidx_async_1,
9263ab6e68cSRichard Henderson                           RUN_ON_CPU_HOST_PTR(p));
9273ab6e68cSRichard Henderson }
9283ab6e68cSRichard Henderson 
929c13b27d8SRichard Henderson void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
930732d5487SAnton Johansson                                                    vaddr addr,
931c13b27d8SRichard Henderson                                                    uint16_t idxmap,
932c13b27d8SRichard Henderson                                                    unsigned bits)
933c13b27d8SRichard Henderson {
934c13b27d8SRichard Henderson     tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu, addr, TARGET_PAGE_SIZE,
935c13b27d8SRichard Henderson                                               idxmap, bits);
936c13b27d8SRichard Henderson }
937c13b27d8SRichard Henderson 
938d9bb58e5SYang Zhong /* update the TLBs so that writes to code in the virtual page 'addr'
939d9bb58e5SYang Zhong    can be detected */
940d9bb58e5SYang Zhong void tlb_protect_code(ram_addr_t ram_addr)
941d9bb58e5SYang Zhong {
94293b99616SRichard Henderson     cpu_physical_memory_test_and_clear_dirty(ram_addr & TARGET_PAGE_MASK,
94393b99616SRichard Henderson                                              TARGET_PAGE_SIZE,
944d9bb58e5SYang Zhong                                              DIRTY_MEMORY_CODE);
945d9bb58e5SYang Zhong }
946d9bb58e5SYang Zhong 
947d9bb58e5SYang Zhong /* update the TLB so that writes in physical page 'phys_addr' are no longer
948d9bb58e5SYang Zhong    tested for self modifying code */
949d9bb58e5SYang Zhong void tlb_unprotect_code(ram_addr_t ram_addr)
950d9bb58e5SYang Zhong {
951d9bb58e5SYang Zhong     cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
952d9bb58e5SYang Zhong }
953d9bb58e5SYang Zhong 
954d9bb58e5SYang Zhong 
955d9bb58e5SYang Zhong /*
956d9bb58e5SYang Zhong  * Dirty write flag handling
957d9bb58e5SYang Zhong  *
958d9bb58e5SYang Zhong  * When the TCG code writes to a location it looks up the address in
959d9bb58e5SYang Zhong  * the TLB and uses that data to compute the final address. If any of
960d9bb58e5SYang Zhong  * the lower bits of the address are set then the slow path is forced.
961d9bb58e5SYang Zhong  * There are a number of reasons to do this but for normal RAM the
962d9bb58e5SYang Zhong  * most usual is detecting writes to code regions which may invalidate
963d9bb58e5SYang Zhong  * generated code.
964d9bb58e5SYang Zhong  *
96571aec354SEmilio G. Cota  * Other vCPUs might be reading their TLBs during guest execution, so we update
966d73415a3SStefan Hajnoczi  * te->addr_write with qatomic_set. We don't need to worry about this for
96771aec354SEmilio G. Cota  * oversized guests as MTTCG is disabled for them.
968d9bb58e5SYang Zhong  *
96953d28455SRichard Henderson  * Called with tlb_c.lock held.
970d9bb58e5SYang Zhong  */
97171aec354SEmilio G. Cota static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry,
97271aec354SEmilio G. Cota                                          uintptr_t start, uintptr_t length)
973d9bb58e5SYang Zhong {
974d9bb58e5SYang Zhong     uintptr_t addr = tlb_entry->addr_write;
975d9bb58e5SYang Zhong 
9767b0d792cSRichard Henderson     if ((addr & (TLB_INVALID_MASK | TLB_MMIO |
9777b0d792cSRichard Henderson                  TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) {
978d9bb58e5SYang Zhong         addr &= TARGET_PAGE_MASK;
979d9bb58e5SYang Zhong         addr += tlb_entry->addend;
980d9bb58e5SYang Zhong         if ((addr - start) < length) {
981238f4380SRichard Henderson #if TARGET_LONG_BITS == 32
982238f4380SRichard Henderson             uint32_t *ptr_write = (uint32_t *)&tlb_entry->addr_write;
983238f4380SRichard Henderson             ptr_write += HOST_BIG_ENDIAN;
984238f4380SRichard Henderson             qatomic_set(ptr_write, *ptr_write | TLB_NOTDIRTY);
985238f4380SRichard Henderson #elif TCG_OVERSIZED_GUEST
98671aec354SEmilio G. Cota             tlb_entry->addr_write |= TLB_NOTDIRTY;
987d9bb58e5SYang Zhong #else
988d73415a3SStefan Hajnoczi             qatomic_set(&tlb_entry->addr_write,
98971aec354SEmilio G. Cota                         tlb_entry->addr_write | TLB_NOTDIRTY);
990d9bb58e5SYang Zhong #endif
991d9bb58e5SYang Zhong         }
99271aec354SEmilio G. Cota     }
99371aec354SEmilio G. Cota }
99471aec354SEmilio G. Cota 
99571aec354SEmilio G. Cota /*
99653d28455SRichard Henderson  * Called with tlb_c.lock held.
99771aec354SEmilio G. Cota  * Called only from the vCPU context, i.e. the TLB's owner thread.
99871aec354SEmilio G. Cota  */
99971aec354SEmilio G. Cota static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s)
100071aec354SEmilio G. Cota {
100171aec354SEmilio G. Cota     *d = *s;
100271aec354SEmilio G. Cota }
1003d9bb58e5SYang Zhong 
1004d9bb58e5SYang Zhong /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
100571aec354SEmilio G. Cota  * the target vCPU).
100653d28455SRichard Henderson  * We must take tlb_c.lock to avoid racing with another vCPU update. The only
100771aec354SEmilio G. Cota  * thing actually updated is the target TLB entry ->addr_write flags.
1008d9bb58e5SYang Zhong  */
1009d9bb58e5SYang Zhong void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length)
1010d9bb58e5SYang Zhong {
1011d9bb58e5SYang Zhong     int mmu_idx;
1012d9bb58e5SYang Zhong 
101310b32e2cSAnton Johansson     qemu_spin_lock(&cpu->neg.tlb.c.lock);
1014d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1015d9bb58e5SYang Zhong         unsigned int i;
101610b32e2cSAnton Johansson         unsigned int n = tlb_n_entries(&cpu->neg.tlb.f[mmu_idx]);
1017d9bb58e5SYang Zhong 
101886e1eff8SEmilio G. Cota         for (i = 0; i < n; i++) {
101910b32e2cSAnton Johansson             tlb_reset_dirty_range_locked(&cpu->neg.tlb.f[mmu_idx].table[i],
1020a40ec84eSRichard Henderson                                          start1, length);
1021d9bb58e5SYang Zhong         }
1022d9bb58e5SYang Zhong 
1023d9bb58e5SYang Zhong         for (i = 0; i < CPU_VTLB_SIZE; i++) {
102410b32e2cSAnton Johansson             tlb_reset_dirty_range_locked(&cpu->neg.tlb.d[mmu_idx].vtable[i],
1025a40ec84eSRichard Henderson                                          start1, length);
1026d9bb58e5SYang Zhong         }
1027d9bb58e5SYang Zhong     }
102810b32e2cSAnton Johansson     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
1029d9bb58e5SYang Zhong }
1030d9bb58e5SYang Zhong 
103153d28455SRichard Henderson /* Called with tlb_c.lock held */
103271aec354SEmilio G. Cota static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry,
1033732d5487SAnton Johansson                                          vaddr addr)
1034d9bb58e5SYang Zhong {
1035732d5487SAnton Johansson     if (tlb_entry->addr_write == (addr | TLB_NOTDIRTY)) {
1036732d5487SAnton Johansson         tlb_entry->addr_write = addr;
1037d9bb58e5SYang Zhong     }
1038d9bb58e5SYang Zhong }
1039d9bb58e5SYang Zhong 
1040d9bb58e5SYang Zhong /* update the TLB corresponding to virtual page vaddr
1041d9bb58e5SYang Zhong    so that it is no longer dirty */
1042732d5487SAnton Johansson void tlb_set_dirty(CPUState *cpu, vaddr addr)
1043d9bb58e5SYang Zhong {
1044d9bb58e5SYang Zhong     int mmu_idx;
1045d9bb58e5SYang Zhong 
1046d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
1047d9bb58e5SYang Zhong 
1048732d5487SAnton Johansson     addr &= TARGET_PAGE_MASK;
104910b32e2cSAnton Johansson     qemu_spin_lock(&cpu->neg.tlb.c.lock);
1050d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
105110b32e2cSAnton Johansson         tlb_set_dirty1_locked(tlb_entry(cpu, mmu_idx, addr), addr);
1052d9bb58e5SYang Zhong     }
1053d9bb58e5SYang Zhong 
1054d9bb58e5SYang Zhong     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1055d9bb58e5SYang Zhong         int k;
1056d9bb58e5SYang Zhong         for (k = 0; k < CPU_VTLB_SIZE; k++) {
105710b32e2cSAnton Johansson             tlb_set_dirty1_locked(&cpu->neg.tlb.d[mmu_idx].vtable[k], addr);
1058d9bb58e5SYang Zhong         }
1059d9bb58e5SYang Zhong     }
106010b32e2cSAnton Johansson     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
1061d9bb58e5SYang Zhong }
1062d9bb58e5SYang Zhong 
1063d9bb58e5SYang Zhong /* Our TLB does not support large pages, so remember the area covered by
1064d9bb58e5SYang Zhong    large pages and trigger a full TLB flush if these are invalidated.  */
106510b32e2cSAnton Johansson static void tlb_add_large_page(CPUState *cpu, int mmu_idx,
1066732d5487SAnton Johansson                                vaddr addr, uint64_t size)
1067d9bb58e5SYang Zhong {
106810b32e2cSAnton Johansson     vaddr lp_addr = cpu->neg.tlb.d[mmu_idx].large_page_addr;
1069732d5487SAnton Johansson     vaddr lp_mask = ~(size - 1);
1070d9bb58e5SYang Zhong 
1071732d5487SAnton Johansson     if (lp_addr == (vaddr)-1) {
10721308e026SRichard Henderson         /* No previous large page.  */
1073732d5487SAnton Johansson         lp_addr = addr;
10741308e026SRichard Henderson     } else {
1075d9bb58e5SYang Zhong         /* Extend the existing region to include the new page.
10761308e026SRichard Henderson            This is a compromise between unnecessary flushes and
10771308e026SRichard Henderson            the cost of maintaining a full variable size TLB.  */
107810b32e2cSAnton Johansson         lp_mask &= cpu->neg.tlb.d[mmu_idx].large_page_mask;
1079732d5487SAnton Johansson         while (((lp_addr ^ addr) & lp_mask) != 0) {
10801308e026SRichard Henderson             lp_mask <<= 1;
1081d9bb58e5SYang Zhong         }
10821308e026SRichard Henderson     }
108310b32e2cSAnton Johansson     cpu->neg.tlb.d[mmu_idx].large_page_addr = lp_addr & lp_mask;
108410b32e2cSAnton Johansson     cpu->neg.tlb.d[mmu_idx].large_page_mask = lp_mask;
1085d9bb58e5SYang Zhong }
1086d9bb58e5SYang Zhong 
108758e8f1f6SRichard Henderson static inline void tlb_set_compare(CPUTLBEntryFull *full, CPUTLBEntry *ent,
1088d712b116SAnton Johansson                                    vaddr address, int flags,
108958e8f1f6SRichard Henderson                                    MMUAccessType access_type, bool enable)
109058e8f1f6SRichard Henderson {
109158e8f1f6SRichard Henderson     if (enable) {
109258e8f1f6SRichard Henderson         address |= flags & TLB_FLAGS_MASK;
109358e8f1f6SRichard Henderson         flags &= TLB_SLOW_FLAGS_MASK;
109458e8f1f6SRichard Henderson         if (flags) {
109558e8f1f6SRichard Henderson             address |= TLB_FORCE_SLOW;
109658e8f1f6SRichard Henderson         }
109758e8f1f6SRichard Henderson     } else {
109858e8f1f6SRichard Henderson         address = -1;
109958e8f1f6SRichard Henderson         flags = 0;
110058e8f1f6SRichard Henderson     }
110158e8f1f6SRichard Henderson     ent->addr_idx[access_type] = address;
110258e8f1f6SRichard Henderson     full->slow_flags[access_type] = flags;
110358e8f1f6SRichard Henderson }
110458e8f1f6SRichard Henderson 
110540473689SRichard Henderson /*
110640473689SRichard Henderson  * Add a new TLB entry. At most one entry for a given virtual address
1107d9bb58e5SYang Zhong  * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
1108d9bb58e5SYang Zhong  * supplied size is only used by tlb_flush_page.
1109d9bb58e5SYang Zhong  *
1110d9bb58e5SYang Zhong  * Called from TCG-generated code, which is under an RCU read-side
1111d9bb58e5SYang Zhong  * critical section.
1112d9bb58e5SYang Zhong  */
111340473689SRichard Henderson void tlb_set_page_full(CPUState *cpu, int mmu_idx,
1114732d5487SAnton Johansson                        vaddr addr, CPUTLBEntryFull *full)
1115d9bb58e5SYang Zhong {
111610b32e2cSAnton Johansson     CPUTLB *tlb = &cpu->neg.tlb;
1117a40ec84eSRichard Henderson     CPUTLBDesc *desc = &tlb->d[mmu_idx];
1118d9bb58e5SYang Zhong     MemoryRegionSection *section;
111958e8f1f6SRichard Henderson     unsigned int index, read_flags, write_flags;
1120d9bb58e5SYang Zhong     uintptr_t addend;
112168fea038SRichard Henderson     CPUTLBEntry *te, tn;
112255df6fcfSPeter Maydell     hwaddr iotlb, xlat, sz, paddr_page;
1123732d5487SAnton Johansson     vaddr addr_page;
112440473689SRichard Henderson     int asidx, wp_flags, prot;
11258f5db641SRichard Henderson     bool is_ram, is_romd;
1126d9bb58e5SYang Zhong 
1127d9bb58e5SYang Zhong     assert_cpu_is_self(cpu);
112855df6fcfSPeter Maydell 
112940473689SRichard Henderson     if (full->lg_page_size <= TARGET_PAGE_BITS) {
113055df6fcfSPeter Maydell         sz = TARGET_PAGE_SIZE;
113155df6fcfSPeter Maydell     } else {
113240473689SRichard Henderson         sz = (hwaddr)1 << full->lg_page_size;
113310b32e2cSAnton Johansson         tlb_add_large_page(cpu, mmu_idx, addr, sz);
113455df6fcfSPeter Maydell     }
1135732d5487SAnton Johansson     addr_page = addr & TARGET_PAGE_MASK;
113640473689SRichard Henderson     paddr_page = full->phys_addr & TARGET_PAGE_MASK;
113755df6fcfSPeter Maydell 
113840473689SRichard Henderson     prot = full->prot;
113940473689SRichard Henderson     asidx = cpu_asidx_from_attrs(cpu, full->attrs);
114055df6fcfSPeter Maydell     section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
114140473689SRichard Henderson                                                 &xlat, &sz, full->attrs, &prot);
1142d9bb58e5SYang Zhong     assert(sz >= TARGET_PAGE_SIZE);
1143d9bb58e5SYang Zhong 
11448c605cf1SAnton Johansson     tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
1145d9bb58e5SYang Zhong               " prot=%x idx=%d\n",
1146732d5487SAnton Johansson               addr, full->phys_addr, prot, mmu_idx);
1147d9bb58e5SYang Zhong 
114858e8f1f6SRichard Henderson     read_flags = 0;
114940473689SRichard Henderson     if (full->lg_page_size < TARGET_PAGE_BITS) {
115030d7e098SRichard Henderson         /* Repeat the MMU check and TLB fill on every access.  */
115158e8f1f6SRichard Henderson         read_flags |= TLB_INVALID_MASK;
115255df6fcfSPeter Maydell     }
115340473689SRichard Henderson     if (full->attrs.byte_swap) {
115458e8f1f6SRichard Henderson         read_flags |= TLB_BSWAP;
1155a26fc6f5STony Nguyen     }
11568f5db641SRichard Henderson 
11578f5db641SRichard Henderson     is_ram = memory_region_is_ram(section->mr);
11588f5db641SRichard Henderson     is_romd = memory_region_is_romd(section->mr);
11598f5db641SRichard Henderson 
11608f5db641SRichard Henderson     if (is_ram || is_romd) {
11618f5db641SRichard Henderson         /* RAM and ROMD both have associated host memory. */
1162d9bb58e5SYang Zhong         addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
11638f5db641SRichard Henderson     } else {
11648f5db641SRichard Henderson         /* I/O does not; force the host address to NULL. */
11658f5db641SRichard Henderson         addend = 0;
1166d9bb58e5SYang Zhong     }
1167d9bb58e5SYang Zhong 
116858e8f1f6SRichard Henderson     write_flags = read_flags;
11698f5db641SRichard Henderson     if (is_ram) {
11708f5db641SRichard Henderson         iotlb = memory_region_get_ram_addr(section->mr) + xlat;
1171dff1ab68SLIU Zhiwei         assert(!(iotlb & ~TARGET_PAGE_MASK));
11728f5db641SRichard Henderson         /*
11738f5db641SRichard Henderson          * Computing is_clean is expensive; avoid all that unless
11748f5db641SRichard Henderson          * the page is actually writable.
11758f5db641SRichard Henderson          */
11768f5db641SRichard Henderson         if (prot & PAGE_WRITE) {
11778f5db641SRichard Henderson             if (section->readonly) {
117858e8f1f6SRichard Henderson                 write_flags |= TLB_DISCARD_WRITE;
11798f5db641SRichard Henderson             } else if (cpu_physical_memory_is_clean(iotlb)) {
118058e8f1f6SRichard Henderson                 write_flags |= TLB_NOTDIRTY;
11818f5db641SRichard Henderson             }
11828f5db641SRichard Henderson         }
11838f5db641SRichard Henderson     } else {
11848f5db641SRichard Henderson         /* I/O or ROMD */
11858f5db641SRichard Henderson         iotlb = memory_region_section_get_iotlb(cpu, section) + xlat;
11868f5db641SRichard Henderson         /*
11878f5db641SRichard Henderson          * Writes to romd devices must go through MMIO to enable write.
11888f5db641SRichard Henderson          * Reads to romd devices go through the ram_ptr found above,
11898f5db641SRichard Henderson          * but of course reads to I/O must go through MMIO.
11908f5db641SRichard Henderson          */
119158e8f1f6SRichard Henderson         write_flags |= TLB_MMIO;
11928f5db641SRichard Henderson         if (!is_romd) {
119358e8f1f6SRichard Henderson             read_flags = write_flags;
11948f5db641SRichard Henderson         }
11958f5db641SRichard Henderson     }
11968f5db641SRichard Henderson 
1197732d5487SAnton Johansson     wp_flags = cpu_watchpoint_address_matches(cpu, addr_page,
119850b107c5SRichard Henderson                                               TARGET_PAGE_SIZE);
1199d9bb58e5SYang Zhong 
120010b32e2cSAnton Johansson     index = tlb_index(cpu, mmu_idx, addr_page);
120110b32e2cSAnton Johansson     te = tlb_entry(cpu, mmu_idx, addr_page);
1202d9bb58e5SYang Zhong 
120368fea038SRichard Henderson     /*
120471aec354SEmilio G. Cota      * Hold the TLB lock for the rest of the function. We could acquire/release
120571aec354SEmilio G. Cota      * the lock several times in the function, but it is faster to amortize the
120671aec354SEmilio G. Cota      * acquisition cost by acquiring it just once. Note that this leads to
120771aec354SEmilio G. Cota      * a longer critical section, but this is not a concern since the TLB lock
120871aec354SEmilio G. Cota      * is unlikely to be contended.
120971aec354SEmilio G. Cota      */
1210a40ec84eSRichard Henderson     qemu_spin_lock(&tlb->c.lock);
121171aec354SEmilio G. Cota 
12123d1523ceSRichard Henderson     /* Note that the tlb is no longer clean.  */
1213a40ec84eSRichard Henderson     tlb->c.dirty |= 1 << mmu_idx;
12143d1523ceSRichard Henderson 
121571aec354SEmilio G. Cota     /* Make sure there's no cached translation for the new page.  */
121610b32e2cSAnton Johansson     tlb_flush_vtlb_page_locked(cpu, mmu_idx, addr_page);
121771aec354SEmilio G. Cota 
121871aec354SEmilio G. Cota     /*
121968fea038SRichard Henderson      * Only evict the old entry to the victim tlb if it's for a
122068fea038SRichard Henderson      * different page; otherwise just overwrite the stale data.
122168fea038SRichard Henderson      */
1222732d5487SAnton Johansson     if (!tlb_hit_page_anyprot(te, addr_page) && !tlb_entry_is_empty(te)) {
1223a40ec84eSRichard Henderson         unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE;
1224a40ec84eSRichard Henderson         CPUTLBEntry *tv = &desc->vtable[vidx];
122568fea038SRichard Henderson 
122668fea038SRichard Henderson         /* Evict the old entry into the victim tlb.  */
122771aec354SEmilio G. Cota         copy_tlb_helper_locked(tv, te);
122825d3ec58SRichard Henderson         desc->vfulltlb[vidx] = desc->fulltlb[index];
122910b32e2cSAnton Johansson         tlb_n_used_entries_dec(cpu, mmu_idx);
123068fea038SRichard Henderson     }
1231d9bb58e5SYang Zhong 
1232d9bb58e5SYang Zhong     /* refill the tlb */
1233ace41090SPeter Maydell     /*
1234dff1ab68SLIU Zhiwei      * When memory region is ram, iotlb contains a TARGET_PAGE_BITS
1235dff1ab68SLIU Zhiwei      * aligned ram_addr_t of the page base of the target RAM.
1236dff1ab68SLIU Zhiwei      * Otherwise, iotlb contains
1237dff1ab68SLIU Zhiwei      *  - a physical section number in the lower TARGET_PAGE_BITS
1238dff1ab68SLIU Zhiwei      *  - the offset within section->mr of the page base (I/O, ROMD) with the
1239dff1ab68SLIU Zhiwei      *    TARGET_PAGE_BITS masked off.
124058e8f1f6SRichard Henderson      * We subtract addr_page (which is page aligned and thus won't
1241ace41090SPeter Maydell      * disturb the low bits) to give an offset which can be added to the
1242ace41090SPeter Maydell      * (non-page-aligned) vaddr of the eventual memory access to get
1243ace41090SPeter Maydell      * the MemoryRegion offset for the access. Note that the vaddr we
1244ace41090SPeter Maydell      * subtract here is that of the page base, and not the same as the
1245fb3cb376SRichard Henderson      * vaddr we add back in io_prepare()/get_page_addr_code().
1246ace41090SPeter Maydell      */
124740473689SRichard Henderson     desc->fulltlb[index] = *full;
124858e8f1f6SRichard Henderson     full = &desc->fulltlb[index];
124958e8f1f6SRichard Henderson     full->xlat_section = iotlb - addr_page;
125058e8f1f6SRichard Henderson     full->phys_addr = paddr_page;
1251d9bb58e5SYang Zhong 
1252d9bb58e5SYang Zhong     /* Now calculate the new entry */
1253732d5487SAnton Johansson     tn.addend = addend - addr_page;
125458e8f1f6SRichard Henderson 
125558e8f1f6SRichard Henderson     tlb_set_compare(full, &tn, addr_page, read_flags,
125658e8f1f6SRichard Henderson                     MMU_INST_FETCH, prot & PAGE_EXEC);
125758e8f1f6SRichard Henderson 
125850b107c5SRichard Henderson     if (wp_flags & BP_MEM_READ) {
125958e8f1f6SRichard Henderson         read_flags |= TLB_WATCHPOINT;
126050b107c5SRichard Henderson     }
126158e8f1f6SRichard Henderson     tlb_set_compare(full, &tn, addr_page, read_flags,
126258e8f1f6SRichard Henderson                     MMU_DATA_LOAD, prot & PAGE_READ);
1263d9bb58e5SYang Zhong 
1264f52bfb12SDavid Hildenbrand     if (prot & PAGE_WRITE_INV) {
126558e8f1f6SRichard Henderson         write_flags |= TLB_INVALID_MASK;
1266f52bfb12SDavid Hildenbrand     }
126750b107c5SRichard Henderson     if (wp_flags & BP_MEM_WRITE) {
126858e8f1f6SRichard Henderson         write_flags |= TLB_WATCHPOINT;
126950b107c5SRichard Henderson     }
127058e8f1f6SRichard Henderson     tlb_set_compare(full, &tn, addr_page, write_flags,
127158e8f1f6SRichard Henderson                     MMU_DATA_STORE, prot & PAGE_WRITE);
1272d9bb58e5SYang Zhong 
127371aec354SEmilio G. Cota     copy_tlb_helper_locked(te, &tn);
127410b32e2cSAnton Johansson     tlb_n_used_entries_inc(cpu, mmu_idx);
1275a40ec84eSRichard Henderson     qemu_spin_unlock(&tlb->c.lock);
1276d9bb58e5SYang Zhong }
1277d9bb58e5SYang Zhong 
1278732d5487SAnton Johansson void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
127940473689SRichard Henderson                              hwaddr paddr, MemTxAttrs attrs, int prot,
1280732d5487SAnton Johansson                              int mmu_idx, uint64_t size)
128140473689SRichard Henderson {
128240473689SRichard Henderson     CPUTLBEntryFull full = {
128340473689SRichard Henderson         .phys_addr = paddr,
128440473689SRichard Henderson         .attrs = attrs,
128540473689SRichard Henderson         .prot = prot,
128640473689SRichard Henderson         .lg_page_size = ctz64(size)
128740473689SRichard Henderson     };
128840473689SRichard Henderson 
128940473689SRichard Henderson     assert(is_power_of_2(size));
1290732d5487SAnton Johansson     tlb_set_page_full(cpu, mmu_idx, addr, &full);
129140473689SRichard Henderson }
129240473689SRichard Henderson 
1293732d5487SAnton Johansson void tlb_set_page(CPUState *cpu, vaddr addr,
1294d9bb58e5SYang Zhong                   hwaddr paddr, int prot,
1295732d5487SAnton Johansson                   int mmu_idx, uint64_t size)
1296d9bb58e5SYang Zhong {
1297732d5487SAnton Johansson     tlb_set_page_with_attrs(cpu, addr, paddr, MEMTXATTRS_UNSPECIFIED,
1298d9bb58e5SYang Zhong                             prot, mmu_idx, size);
1299d9bb58e5SYang Zhong }
1300d9bb58e5SYang Zhong 
1301c319dc13SRichard Henderson /*
1302c319dc13SRichard Henderson  * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1303c319dc13SRichard Henderson  * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1304c319dc13SRichard Henderson  * be discarded and looked up again (e.g. via tlb_entry()).
1305c319dc13SRichard Henderson  */
1306732d5487SAnton Johansson static void tlb_fill(CPUState *cpu, vaddr addr, int size,
1307c319dc13SRichard Henderson                      MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1308c319dc13SRichard Henderson {
1309c319dc13SRichard Henderson     bool ok;
1310c319dc13SRichard Henderson 
1311c319dc13SRichard Henderson     /*
1312c319dc13SRichard Henderson      * This is not a probe, so only valid return is success; failure
1313c319dc13SRichard Henderson      * should result in exception + longjmp to the cpu loop.
1314c319dc13SRichard Henderson      */
13158810ee2aSAlex Bennée     ok = cpu->cc->tcg_ops->tlb_fill(cpu, addr, size,
1316e124536fSEduardo Habkost                                     access_type, mmu_idx, false, retaddr);
1317c319dc13SRichard Henderson     assert(ok);
1318c319dc13SRichard Henderson }
1319c319dc13SRichard Henderson 
132078271684SClaudio Fontana static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
132178271684SClaudio Fontana                                         MMUAccessType access_type,
132278271684SClaudio Fontana                                         int mmu_idx, uintptr_t retaddr)
132378271684SClaudio Fontana {
13248810ee2aSAlex Bennée     cpu->cc->tcg_ops->do_unaligned_access(cpu, addr, access_type,
13258810ee2aSAlex Bennée                                           mmu_idx, retaddr);
132678271684SClaudio Fontana }
132778271684SClaudio Fontana 
1328fb3cb376SRichard Henderson static MemoryRegionSection *
1329d50ef446SAnton Johansson io_prepare(hwaddr *out_offset, CPUState *cpu, hwaddr xlat,
1330fb3cb376SRichard Henderson            MemTxAttrs attrs, vaddr addr, uintptr_t retaddr)
1331d9bb58e5SYang Zhong {
13322d54f194SPeter Maydell     MemoryRegionSection *section;
1333fb3cb376SRichard Henderson     hwaddr mr_offset;
1334d9bb58e5SYang Zhong 
1335fb3cb376SRichard Henderson     section = iotlb_to_section(cpu, xlat, attrs);
1336fb3cb376SRichard Henderson     mr_offset = (xlat & TARGET_PAGE_MASK) + addr;
1337d9bb58e5SYang Zhong     cpu->mem_io_pc = retaddr;
1338464dacf6SRichard Henderson     if (!cpu->neg.can_do_io) {
1339d9bb58e5SYang Zhong         cpu_io_recompile(cpu, retaddr);
1340d9bb58e5SYang Zhong     }
1341d9bb58e5SYang Zhong 
1342fb3cb376SRichard Henderson     *out_offset = mr_offset;
1343fb3cb376SRichard Henderson     return section;
1344fb3cb376SRichard Henderson }
1345fb3cb376SRichard Henderson 
1346d50ef446SAnton Johansson static void io_failed(CPUState *cpu, CPUTLBEntryFull *full, vaddr addr,
1347fb3cb376SRichard Henderson                       unsigned size, MMUAccessType access_type, int mmu_idx,
13480e114440SRichard Henderson                       MemTxResult response, uintptr_t retaddr)
1349fb3cb376SRichard Henderson {
1350d50ef446SAnton Johansson     if (!cpu->ignore_memory_transaction_failures
1351d50ef446SAnton Johansson         && cpu->cc->tcg_ops->do_transaction_failed) {
13520e114440SRichard Henderson         hwaddr physaddr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
1353bef0c216SRichard Henderson 
1354d50ef446SAnton Johansson         cpu->cc->tcg_ops->do_transaction_failed(cpu, physaddr, addr, size,
1355bef0c216SRichard Henderson                                                 access_type, mmu_idx,
1356bef0c216SRichard Henderson                                                 full->attrs, response, retaddr);
1357bef0c216SRichard Henderson     }
1358bef0c216SRichard Henderson }
1359fb3cb376SRichard Henderson 
1360d9bb58e5SYang Zhong /* Return true if ADDR is present in the victim tlb, and has been copied
1361d9bb58e5SYang Zhong    back to the main tlb.  */
136210b32e2cSAnton Johansson static bool victim_tlb_hit(CPUState *cpu, size_t mmu_idx, size_t index,
1363732d5487SAnton Johansson                            MMUAccessType access_type, vaddr page)
1364d9bb58e5SYang Zhong {
1365d9bb58e5SYang Zhong     size_t vidx;
136671aec354SEmilio G. Cota 
136710b32e2cSAnton Johansson     assert_cpu_is_self(cpu);
1368d9bb58e5SYang Zhong     for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) {
136910b32e2cSAnton Johansson         CPUTLBEntry *vtlb = &cpu->neg.tlb.d[mmu_idx].vtable[vidx];
13709e39de98SAnton Johansson         uint64_t cmp = tlb_read_idx(vtlb, access_type);
1371d9bb58e5SYang Zhong 
1372d9bb58e5SYang Zhong         if (cmp == page) {
1373d9bb58e5SYang Zhong             /* Found entry in victim tlb, swap tlb and iotlb.  */
137410b32e2cSAnton Johansson             CPUTLBEntry tmptlb, *tlb = &cpu->neg.tlb.f[mmu_idx].table[index];
1375d9bb58e5SYang Zhong 
137610b32e2cSAnton Johansson             qemu_spin_lock(&cpu->neg.tlb.c.lock);
137771aec354SEmilio G. Cota             copy_tlb_helper_locked(&tmptlb, tlb);
137871aec354SEmilio G. Cota             copy_tlb_helper_locked(tlb, vtlb);
137971aec354SEmilio G. Cota             copy_tlb_helper_locked(vtlb, &tmptlb);
138010b32e2cSAnton Johansson             qemu_spin_unlock(&cpu->neg.tlb.c.lock);
1381d9bb58e5SYang Zhong 
138210b32e2cSAnton Johansson             CPUTLBEntryFull *f1 = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
138310b32e2cSAnton Johansson             CPUTLBEntryFull *f2 = &cpu->neg.tlb.d[mmu_idx].vfulltlb[vidx];
138425d3ec58SRichard Henderson             CPUTLBEntryFull tmpf;
138525d3ec58SRichard Henderson             tmpf = *f1; *f1 = *f2; *f2 = tmpf;
1386d9bb58e5SYang Zhong             return true;
1387d9bb58e5SYang Zhong         }
1388d9bb58e5SYang Zhong     }
1389d9bb58e5SYang Zhong     return false;
1390d9bb58e5SYang Zhong }
1391d9bb58e5SYang Zhong 
1392707526adSRichard Henderson static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
139325d3ec58SRichard Henderson                            CPUTLBEntryFull *full, uintptr_t retaddr)
1394707526adSRichard Henderson {
139525d3ec58SRichard Henderson     ram_addr_t ram_addr = mem_vaddr + full->xlat_section;
1396707526adSRichard Henderson 
1397707526adSRichard Henderson     trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
1398707526adSRichard Henderson 
1399707526adSRichard Henderson     if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
1400f349e92eSPhilippe Mathieu-Daudé         tb_invalidate_phys_range_fast(ram_addr, size, retaddr);
1401707526adSRichard Henderson     }
1402707526adSRichard Henderson 
1403707526adSRichard Henderson     /*
1404707526adSRichard Henderson      * Set both VGA and migration bits for simplicity and to remove
1405707526adSRichard Henderson      * the notdirty callback faster.
1406707526adSRichard Henderson      */
1407707526adSRichard Henderson     cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE);
1408707526adSRichard Henderson 
1409707526adSRichard Henderson     /* We remove the notdirty callback only if the code has been flushed. */
1410707526adSRichard Henderson     if (!cpu_physical_memory_is_clean(ram_addr)) {
1411707526adSRichard Henderson         trace_memory_notdirty_set_dirty(mem_vaddr);
1412707526adSRichard Henderson         tlb_set_dirty(cpu, mem_vaddr);
1413707526adSRichard Henderson     }
1414707526adSRichard Henderson }
1415707526adSRichard Henderson 
14165afec1c6SAnton Johansson static int probe_access_internal(CPUState *cpu, vaddr addr,
1417069cfe77SRichard Henderson                                  int fault_size, MMUAccessType access_type,
1418069cfe77SRichard Henderson                                  int mmu_idx, bool nonfault,
1419af803a4fSRichard Henderson                                  void **phost, CPUTLBEntryFull **pfull,
14206d03226bSAlex Bennée                                  uintptr_t retaddr, bool check_mem_cbs)
1421d9bb58e5SYang Zhong {
14225afec1c6SAnton Johansson     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
14235afec1c6SAnton Johansson     CPUTLBEntry *entry = tlb_entry(cpu, mmu_idx, addr);
14249e39de98SAnton Johansson     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
14254f8f4127SAnton Johansson     vaddr page_addr = addr & TARGET_PAGE_MASK;
142658e8f1f6SRichard Henderson     int flags = TLB_FLAGS_MASK & ~TLB_FORCE_SLOW;
14275afec1c6SAnton Johansson     bool force_mmio = check_mem_cbs && cpu_plugin_mem_cbs_enabled(cpu);
142858e8f1f6SRichard Henderson     CPUTLBEntryFull *full;
1429ca86cf32SDavid Hildenbrand 
1430069cfe77SRichard Henderson     if (!tlb_hit_page(tlb_addr, page_addr)) {
14315afec1c6SAnton Johansson         if (!victim_tlb_hit(cpu, mmu_idx, index, access_type, page_addr)) {
14325afec1c6SAnton Johansson             if (!cpu->cc->tcg_ops->tlb_fill(cpu, addr, fault_size, access_type,
1433069cfe77SRichard Henderson                                             mmu_idx, nonfault, retaddr)) {
1434069cfe77SRichard Henderson                 /* Non-faulting page table read failed.  */
1435069cfe77SRichard Henderson                 *phost = NULL;
1436af803a4fSRichard Henderson                 *pfull = NULL;
1437069cfe77SRichard Henderson                 return TLB_INVALID_MASK;
1438069cfe77SRichard Henderson             }
1439069cfe77SRichard Henderson 
144003a98189SDavid Hildenbrand             /* TLB resize via tlb_fill may have moved the entry.  */
14415afec1c6SAnton Johansson             index = tlb_index(cpu, mmu_idx, addr);
14425afec1c6SAnton Johansson             entry = tlb_entry(cpu, mmu_idx, addr);
1443c3c8bf57SRichard Henderson 
1444c3c8bf57SRichard Henderson             /*
1445c3c8bf57SRichard Henderson              * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately,
1446c3c8bf57SRichard Henderson              * to force the next access through tlb_fill.  We've just
1447c3c8bf57SRichard Henderson              * called tlb_fill, so we know that this entry *is* valid.
1448c3c8bf57SRichard Henderson              */
1449c3c8bf57SRichard Henderson             flags &= ~TLB_INVALID_MASK;
1450d9bb58e5SYang Zhong         }
14510b3c75adSRichard Henderson         tlb_addr = tlb_read_idx(entry, access_type);
145203a98189SDavid Hildenbrand     }
1453c3c8bf57SRichard Henderson     flags &= tlb_addr;
145403a98189SDavid Hildenbrand 
14555afec1c6SAnton Johansson     *pfull = full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
145658e8f1f6SRichard Henderson     flags |= full->slow_flags[access_type];
1457af803a4fSRichard Henderson 
1458069cfe77SRichard Henderson     /* Fold all "mmio-like" bits into TLB_MMIO.  This is not RAM.  */
14596d03226bSAlex Bennée     if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY))
14606d03226bSAlex Bennée         ||
14616d03226bSAlex Bennée         (access_type != MMU_INST_FETCH && force_mmio)) {
1462069cfe77SRichard Henderson         *phost = NULL;
1463069cfe77SRichard Henderson         return TLB_MMIO;
1464fef39ccdSDavid Hildenbrand     }
1465fef39ccdSDavid Hildenbrand 
1466069cfe77SRichard Henderson     /* Everything else is RAM. */
1467069cfe77SRichard Henderson     *phost = (void *)((uintptr_t)addr + entry->addend);
1468069cfe77SRichard Henderson     return flags;
1469069cfe77SRichard Henderson }
1470069cfe77SRichard Henderson 
14714f8f4127SAnton Johansson int probe_access_full(CPUArchState *env, vaddr addr, int size,
1472069cfe77SRichard Henderson                       MMUAccessType access_type, int mmu_idx,
1473af803a4fSRichard Henderson                       bool nonfault, void **phost, CPUTLBEntryFull **pfull,
1474af803a4fSRichard Henderson                       uintptr_t retaddr)
1475069cfe77SRichard Henderson {
14765afec1c6SAnton Johansson     int flags = probe_access_internal(env_cpu(env), addr, size, access_type,
14775afec1c6SAnton Johansson                                       mmu_idx, nonfault, phost, pfull, retaddr,
14785afec1c6SAnton Johansson                                       true);
1479069cfe77SRichard Henderson 
1480069cfe77SRichard Henderson     /* Handle clean RAM pages.  */
1481069cfe77SRichard Henderson     if (unlikely(flags & TLB_NOTDIRTY)) {
1482*e2faabeeSJessica Clarke         int dirtysize = size == 0 ? 1 : size;
1483*e2faabeeSJessica Clarke         notdirty_write(env_cpu(env), addr, dirtysize, *pfull, retaddr);
1484069cfe77SRichard Henderson         flags &= ~TLB_NOTDIRTY;
1485069cfe77SRichard Henderson     }
1486069cfe77SRichard Henderson 
1487069cfe77SRichard Henderson     return flags;
1488069cfe77SRichard Henderson }
1489069cfe77SRichard Henderson 
14906d03226bSAlex Bennée int probe_access_full_mmu(CPUArchState *env, vaddr addr, int size,
14916d03226bSAlex Bennée                           MMUAccessType access_type, int mmu_idx,
14926d03226bSAlex Bennée                           void **phost, CPUTLBEntryFull **pfull)
14936d03226bSAlex Bennée {
14946d03226bSAlex Bennée     void *discard_phost;
14956d03226bSAlex Bennée     CPUTLBEntryFull *discard_tlb;
14966d03226bSAlex Bennée 
14976d03226bSAlex Bennée     /* privately handle users that don't need full results */
14986d03226bSAlex Bennée     phost = phost ? phost : &discard_phost;
14996d03226bSAlex Bennée     pfull = pfull ? pfull : &discard_tlb;
15006d03226bSAlex Bennée 
15015afec1c6SAnton Johansson     int flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15025afec1c6SAnton Johansson                                       mmu_idx, true, phost, pfull, 0, false);
15036d03226bSAlex Bennée 
15046d03226bSAlex Bennée     /* Handle clean RAM pages.  */
15056d03226bSAlex Bennée     if (unlikely(flags & TLB_NOTDIRTY)) {
1506*e2faabeeSJessica Clarke         int dirtysize = size == 0 ? 1 : size;
1507*e2faabeeSJessica Clarke         notdirty_write(env_cpu(env), addr, dirtysize, *pfull, 0);
15086d03226bSAlex Bennée         flags &= ~TLB_NOTDIRTY;
15096d03226bSAlex Bennée     }
15106d03226bSAlex Bennée 
15116d03226bSAlex Bennée     return flags;
15126d03226bSAlex Bennée }
15136d03226bSAlex Bennée 
15144f8f4127SAnton Johansson int probe_access_flags(CPUArchState *env, vaddr addr, int size,
1515af803a4fSRichard Henderson                        MMUAccessType access_type, int mmu_idx,
1516af803a4fSRichard Henderson                        bool nonfault, void **phost, uintptr_t retaddr)
1517af803a4fSRichard Henderson {
1518af803a4fSRichard Henderson     CPUTLBEntryFull *full;
15191770b2f2SDaniel Henrique Barboza     int flags;
1520af803a4fSRichard Henderson 
15211770b2f2SDaniel Henrique Barboza     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
15221770b2f2SDaniel Henrique Barboza 
15235afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15245afec1c6SAnton Johansson                                   mmu_idx, nonfault, phost, &full, retaddr,
15255afec1c6SAnton Johansson                                   true);
15261770b2f2SDaniel Henrique Barboza 
15271770b2f2SDaniel Henrique Barboza     /* Handle clean RAM pages. */
15281770b2f2SDaniel Henrique Barboza     if (unlikely(flags & TLB_NOTDIRTY)) {
1529*e2faabeeSJessica Clarke         int dirtysize = size == 0 ? 1 : size;
1530*e2faabeeSJessica Clarke         notdirty_write(env_cpu(env), addr, dirtysize, full, retaddr);
15311770b2f2SDaniel Henrique Barboza         flags &= ~TLB_NOTDIRTY;
15321770b2f2SDaniel Henrique Barboza     }
15331770b2f2SDaniel Henrique Barboza 
15341770b2f2SDaniel Henrique Barboza     return flags;
1535af803a4fSRichard Henderson }
1536af803a4fSRichard Henderson 
15374f8f4127SAnton Johansson void *probe_access(CPUArchState *env, vaddr addr, int size,
1538069cfe77SRichard Henderson                    MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1539069cfe77SRichard Henderson {
1540af803a4fSRichard Henderson     CPUTLBEntryFull *full;
1541069cfe77SRichard Henderson     void *host;
1542069cfe77SRichard Henderson     int flags;
1543069cfe77SRichard Henderson 
1544069cfe77SRichard Henderson     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
1545069cfe77SRichard Henderson 
15465afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15475afec1c6SAnton Johansson                                   mmu_idx, false, &host, &full, retaddr,
15485afec1c6SAnton Johansson                                   true);
1549069cfe77SRichard Henderson 
1550069cfe77SRichard Henderson     /* Per the interface, size == 0 merely faults the access. */
1551069cfe77SRichard Henderson     if (size == 0) {
155273bc0bd4SRichard Henderson         return NULL;
155373bc0bd4SRichard Henderson     }
155473bc0bd4SRichard Henderson 
1555069cfe77SRichard Henderson     if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) {
155603a98189SDavid Hildenbrand         /* Handle watchpoints.  */
1557069cfe77SRichard Henderson         if (flags & TLB_WATCHPOINT) {
1558069cfe77SRichard Henderson             int wp_access = (access_type == MMU_DATA_STORE
1559069cfe77SRichard Henderson                              ? BP_MEM_WRITE : BP_MEM_READ);
156003a98189SDavid Hildenbrand             cpu_check_watchpoint(env_cpu(env), addr, size,
156125d3ec58SRichard Henderson                                  full->attrs, wp_access, retaddr);
1562d9bb58e5SYang Zhong         }
1563fef39ccdSDavid Hildenbrand 
156473bc0bd4SRichard Henderson         /* Handle clean RAM pages.  */
1565069cfe77SRichard Henderson         if (flags & TLB_NOTDIRTY) {
1566*e2faabeeSJessica Clarke             notdirty_write(env_cpu(env), addr, size, full, retaddr);
156773bc0bd4SRichard Henderson         }
1568fef39ccdSDavid Hildenbrand     }
1569fef39ccdSDavid Hildenbrand 
1570069cfe77SRichard Henderson     return host;
1571d9bb58e5SYang Zhong }
1572d9bb58e5SYang Zhong 
15734811e909SRichard Henderson void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
15744811e909SRichard Henderson                         MMUAccessType access_type, int mmu_idx)
15754811e909SRichard Henderson {
1576af803a4fSRichard Henderson     CPUTLBEntryFull *full;
1577069cfe77SRichard Henderson     void *host;
1578069cfe77SRichard Henderson     int flags;
15794811e909SRichard Henderson 
15805afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, 0, access_type,
15816d03226bSAlex Bennée                                   mmu_idx, true, &host, &full, 0, false);
1582069cfe77SRichard Henderson 
1583069cfe77SRichard Henderson     /* No combination of flags are expected by the caller. */
1584069cfe77SRichard Henderson     return flags ? NULL : host;
15854811e909SRichard Henderson }
15864811e909SRichard Henderson 
15877e0d9973SRichard Henderson /*
15887e0d9973SRichard Henderson  * Return a ram_addr_t for the virtual address for execution.
15897e0d9973SRichard Henderson  *
15907e0d9973SRichard Henderson  * Return -1 if we can't translate and execute from an entire page
15917e0d9973SRichard Henderson  * of RAM.  This will force us to execute by loading and translating
15927e0d9973SRichard Henderson  * one insn at a time, without caching.
15937e0d9973SRichard Henderson  *
15947e0d9973SRichard Henderson  * NOTE: This function will trigger an exception if the page is
15957e0d9973SRichard Henderson  * not executable.
15967e0d9973SRichard Henderson  */
15974f8f4127SAnton Johansson tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, vaddr addr,
15987e0d9973SRichard Henderson                                         void **hostp)
15997e0d9973SRichard Henderson {
1600af803a4fSRichard Henderson     CPUTLBEntryFull *full;
16017e0d9973SRichard Henderson     void *p;
16027e0d9973SRichard Henderson 
16035afec1c6SAnton Johansson     (void)probe_access_internal(env_cpu(env), addr, 1, MMU_INST_FETCH,
16046d03226bSAlex Bennée                                 cpu_mmu_index(env, true), false,
16056d03226bSAlex Bennée                                 &p, &full, 0, false);
16067e0d9973SRichard Henderson     if (p == NULL) {
16077e0d9973SRichard Henderson         return -1;
16087e0d9973SRichard Henderson     }
1609ac01ec6fSWeiwei Li 
1610ac01ec6fSWeiwei Li     if (full->lg_page_size < TARGET_PAGE_BITS) {
1611ac01ec6fSWeiwei Li         return -1;
1612ac01ec6fSWeiwei Li     }
1613ac01ec6fSWeiwei Li 
16147e0d9973SRichard Henderson     if (hostp) {
16157e0d9973SRichard Henderson         *hostp = p;
16167e0d9973SRichard Henderson     }
16177e0d9973SRichard Henderson     return qemu_ram_addr_from_host_nofail(p);
16187e0d9973SRichard Henderson }
16197e0d9973SRichard Henderson 
1620cdfac37bSRichard Henderson /* Load/store with atomicity primitives. */
1621cdfac37bSRichard Henderson #include "ldst_atomicity.c.inc"
1622cdfac37bSRichard Henderson 
1623235537faSAlex Bennée #ifdef CONFIG_PLUGIN
1624235537faSAlex Bennée /*
1625235537faSAlex Bennée  * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1626235537faSAlex Bennée  * This should be a hot path as we will have just looked this path up
1627235537faSAlex Bennée  * in the softmmu lookup code (or helper). We don't handle re-fills or
1628235537faSAlex Bennée  * checking the victim table. This is purely informational.
1629235537faSAlex Bennée  *
1630da6aef48SRichard Henderson  * The one corner case is i/o write, which can cause changes to the
1631da6aef48SRichard Henderson  * address space.  Those changes, and the corresponding tlb flush,
1632da6aef48SRichard Henderson  * should be delayed until the next TB, so even then this ought not fail.
1633da6aef48SRichard Henderson  * But check, Just in Case.
1634235537faSAlex Bennée  */
1635732d5487SAnton Johansson bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,
1636235537faSAlex Bennée                        bool is_store, struct qemu_plugin_hwaddr *data)
1637235537faSAlex Bennée {
163810b32e2cSAnton Johansson     CPUTLBEntry *tlbe = tlb_entry(cpu, mmu_idx, addr);
163910b32e2cSAnton Johansson     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1640da6aef48SRichard Henderson     MMUAccessType access_type = is_store ? MMU_DATA_STORE : MMU_DATA_LOAD;
1641da6aef48SRichard Henderson     uint64_t tlb_addr = tlb_read_idx(tlbe, access_type);
1642405c02d8SRichard Henderson     CPUTLBEntryFull *full;
1643235537faSAlex Bennée 
1644da6aef48SRichard Henderson     if (unlikely(!tlb_hit(tlb_addr, addr))) {
1645da6aef48SRichard Henderson         return false;
1646da6aef48SRichard Henderson     }
1647da6aef48SRichard Henderson 
164810b32e2cSAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1649405c02d8SRichard Henderson     data->phys_addr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
1650405c02d8SRichard Henderson 
1651235537faSAlex Bennée     /* We must have an iotlb entry for MMIO */
1652235537faSAlex Bennée     if (tlb_addr & TLB_MMIO) {
1653405c02d8SRichard Henderson         MemoryRegionSection *section =
1654405c02d8SRichard Henderson             iotlb_to_section(cpu, full->xlat_section & ~TARGET_PAGE_MASK,
1655405c02d8SRichard Henderson                              full->attrs);
1656235537faSAlex Bennée         data->is_io = true;
1657405c02d8SRichard Henderson         data->mr = section->mr;
1658235537faSAlex Bennée     } else {
1659235537faSAlex Bennée         data->is_io = false;
1660405c02d8SRichard Henderson         data->mr = NULL;
1661235537faSAlex Bennée     }
1662235537faSAlex Bennée     return true;
1663235537faSAlex Bennée }
1664235537faSAlex Bennée #endif
1665235537faSAlex Bennée 
166608dff435SRichard Henderson /*
16678cfdacaaSRichard Henderson  * Probe for a load/store operation.
16688cfdacaaSRichard Henderson  * Return the host address and into @flags.
16698cfdacaaSRichard Henderson  */
16708cfdacaaSRichard Henderson 
16718cfdacaaSRichard Henderson typedef struct MMULookupPageData {
16728cfdacaaSRichard Henderson     CPUTLBEntryFull *full;
16738cfdacaaSRichard Henderson     void *haddr;
1674fb2c53cbSAnton Johansson     vaddr addr;
16758cfdacaaSRichard Henderson     int flags;
16768cfdacaaSRichard Henderson     int size;
16778cfdacaaSRichard Henderson } MMULookupPageData;
16788cfdacaaSRichard Henderson 
16798cfdacaaSRichard Henderson typedef struct MMULookupLocals {
16808cfdacaaSRichard Henderson     MMULookupPageData page[2];
16818cfdacaaSRichard Henderson     MemOp memop;
16828cfdacaaSRichard Henderson     int mmu_idx;
16838cfdacaaSRichard Henderson } MMULookupLocals;
16848cfdacaaSRichard Henderson 
16858cfdacaaSRichard Henderson /**
16868cfdacaaSRichard Henderson  * mmu_lookup1: translate one page
1687d50ef446SAnton Johansson  * @cpu: generic cpu state
16888cfdacaaSRichard Henderson  * @data: lookup parameters
16898cfdacaaSRichard Henderson  * @mmu_idx: virtual address context
16908cfdacaaSRichard Henderson  * @access_type: load/store/code
16918cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
16928cfdacaaSRichard Henderson  *
16938cfdacaaSRichard Henderson  * Resolve the translation for the one page at @data.addr, filling in
16948cfdacaaSRichard Henderson  * the rest of @data with the results.  If the translation fails,
16958cfdacaaSRichard Henderson  * tlb_fill will longjmp out.  Return true if the softmmu tlb for
16968cfdacaaSRichard Henderson  * @mmu_idx may have resized.
16978cfdacaaSRichard Henderson  */
1698d50ef446SAnton Johansson static bool mmu_lookup1(CPUState *cpu, MMULookupPageData *data,
16998cfdacaaSRichard Henderson                         int mmu_idx, MMUAccessType access_type, uintptr_t ra)
17008cfdacaaSRichard Henderson {
1701fb2c53cbSAnton Johansson     vaddr addr = data->addr;
1702d50ef446SAnton Johansson     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1703d50ef446SAnton Johansson     CPUTLBEntry *entry = tlb_entry(cpu, mmu_idx, addr);
17049e39de98SAnton Johansson     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
17058cfdacaaSRichard Henderson     bool maybe_resized = false;
170658e8f1f6SRichard Henderson     CPUTLBEntryFull *full;
170758e8f1f6SRichard Henderson     int flags;
17088cfdacaaSRichard Henderson 
17098cfdacaaSRichard Henderson     /* If the TLB entry is for a different page, reload and try again.  */
17108cfdacaaSRichard Henderson     if (!tlb_hit(tlb_addr, addr)) {
1711d50ef446SAnton Johansson         if (!victim_tlb_hit(cpu, mmu_idx, index, access_type,
17128cfdacaaSRichard Henderson                             addr & TARGET_PAGE_MASK)) {
1713d50ef446SAnton Johansson             tlb_fill(cpu, addr, data->size, access_type, mmu_idx, ra);
17148cfdacaaSRichard Henderson             maybe_resized = true;
1715d50ef446SAnton Johansson             index = tlb_index(cpu, mmu_idx, addr);
1716d50ef446SAnton Johansson             entry = tlb_entry(cpu, mmu_idx, addr);
17178cfdacaaSRichard Henderson         }
17188cfdacaaSRichard Henderson         tlb_addr = tlb_read_idx(entry, access_type) & ~TLB_INVALID_MASK;
17198cfdacaaSRichard Henderson     }
17208cfdacaaSRichard Henderson 
1721d50ef446SAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
172258e8f1f6SRichard Henderson     flags = tlb_addr & (TLB_FLAGS_MASK & ~TLB_FORCE_SLOW);
172358e8f1f6SRichard Henderson     flags |= full->slow_flags[access_type];
172458e8f1f6SRichard Henderson 
172558e8f1f6SRichard Henderson     data->full = full;
172658e8f1f6SRichard Henderson     data->flags = flags;
17278cfdacaaSRichard Henderson     /* Compute haddr speculatively; depending on flags it might be invalid. */
17288cfdacaaSRichard Henderson     data->haddr = (void *)((uintptr_t)addr + entry->addend);
17298cfdacaaSRichard Henderson 
17308cfdacaaSRichard Henderson     return maybe_resized;
17318cfdacaaSRichard Henderson }
17328cfdacaaSRichard Henderson 
17338cfdacaaSRichard Henderson /**
17348cfdacaaSRichard Henderson  * mmu_watch_or_dirty
1735d50ef446SAnton Johansson  * @cpu: generic cpu state
17368cfdacaaSRichard Henderson  * @data: lookup parameters
17378cfdacaaSRichard Henderson  * @access_type: load/store/code
17388cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
17398cfdacaaSRichard Henderson  *
17408cfdacaaSRichard Henderson  * Trigger watchpoints for @data.addr:@data.size;
17418cfdacaaSRichard Henderson  * record writes to protected clean pages.
17428cfdacaaSRichard Henderson  */
1743d50ef446SAnton Johansson static void mmu_watch_or_dirty(CPUState *cpu, MMULookupPageData *data,
17448cfdacaaSRichard Henderson                                MMUAccessType access_type, uintptr_t ra)
17458cfdacaaSRichard Henderson {
17468cfdacaaSRichard Henderson     CPUTLBEntryFull *full = data->full;
1747fb2c53cbSAnton Johansson     vaddr addr = data->addr;
17488cfdacaaSRichard Henderson     int flags = data->flags;
17498cfdacaaSRichard Henderson     int size = data->size;
17508cfdacaaSRichard Henderson 
17518cfdacaaSRichard Henderson     /* On watchpoint hit, this will longjmp out.  */
17528cfdacaaSRichard Henderson     if (flags & TLB_WATCHPOINT) {
17538cfdacaaSRichard Henderson         int wp = access_type == MMU_DATA_STORE ? BP_MEM_WRITE : BP_MEM_READ;
1754d50ef446SAnton Johansson         cpu_check_watchpoint(cpu, addr, size, full->attrs, wp, ra);
17558cfdacaaSRichard Henderson         flags &= ~TLB_WATCHPOINT;
17568cfdacaaSRichard Henderson     }
17578cfdacaaSRichard Henderson 
17588cfdacaaSRichard Henderson     /* Note that notdirty is only set for writes. */
17598cfdacaaSRichard Henderson     if (flags & TLB_NOTDIRTY) {
1760d50ef446SAnton Johansson         notdirty_write(cpu, addr, size, full, ra);
17618cfdacaaSRichard Henderson         flags &= ~TLB_NOTDIRTY;
17628cfdacaaSRichard Henderson     }
17638cfdacaaSRichard Henderson     data->flags = flags;
17648cfdacaaSRichard Henderson }
17658cfdacaaSRichard Henderson 
17668cfdacaaSRichard Henderson /**
17678cfdacaaSRichard Henderson  * mmu_lookup: translate page(s)
1768d50ef446SAnton Johansson  * @cpu: generic cpu state
17698cfdacaaSRichard Henderson  * @addr: virtual address
17708cfdacaaSRichard Henderson  * @oi: combined mmu_idx and MemOp
17718cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
17728cfdacaaSRichard Henderson  * @access_type: load/store/code
17738cfdacaaSRichard Henderson  * @l: output result
17748cfdacaaSRichard Henderson  *
17758cfdacaaSRichard Henderson  * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
17768cfdacaaSRichard Henderson  * bytes.  Return true if the lookup crosses a page boundary.
17778cfdacaaSRichard Henderson  */
1778d50ef446SAnton Johansson static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
17798cfdacaaSRichard Henderson                        uintptr_t ra, MMUAccessType type, MMULookupLocals *l)
17808cfdacaaSRichard Henderson {
17818cfdacaaSRichard Henderson     unsigned a_bits;
17828cfdacaaSRichard Henderson     bool crosspage;
17838cfdacaaSRichard Henderson     int flags;
17848cfdacaaSRichard Henderson 
17858cfdacaaSRichard Henderson     l->memop = get_memop(oi);
17868cfdacaaSRichard Henderson     l->mmu_idx = get_mmuidx(oi);
17878cfdacaaSRichard Henderson 
17888cfdacaaSRichard Henderson     tcg_debug_assert(l->mmu_idx < NB_MMU_MODES);
17898cfdacaaSRichard Henderson 
17908cfdacaaSRichard Henderson     /* Handle CPU specific unaligned behaviour */
17918cfdacaaSRichard Henderson     a_bits = get_alignment_bits(l->memop);
17928cfdacaaSRichard Henderson     if (addr & ((1 << a_bits) - 1)) {
1793d50ef446SAnton Johansson         cpu_unaligned_access(cpu, addr, type, l->mmu_idx, ra);
17948cfdacaaSRichard Henderson     }
17958cfdacaaSRichard Henderson 
17968cfdacaaSRichard Henderson     l->page[0].addr = addr;
17978cfdacaaSRichard Henderson     l->page[0].size = memop_size(l->memop);
17988cfdacaaSRichard Henderson     l->page[1].addr = (addr + l->page[0].size - 1) & TARGET_PAGE_MASK;
17998cfdacaaSRichard Henderson     l->page[1].size = 0;
18008cfdacaaSRichard Henderson     crosspage = (addr ^ l->page[1].addr) & TARGET_PAGE_MASK;
18018cfdacaaSRichard Henderson 
18028cfdacaaSRichard Henderson     if (likely(!crosspage)) {
1803d50ef446SAnton Johansson         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
18048cfdacaaSRichard Henderson 
18058cfdacaaSRichard Henderson         flags = l->page[0].flags;
18068cfdacaaSRichard Henderson         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1807d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
18088cfdacaaSRichard Henderson         }
18098cfdacaaSRichard Henderson         if (unlikely(flags & TLB_BSWAP)) {
18108cfdacaaSRichard Henderson             l->memop ^= MO_BSWAP;
18118cfdacaaSRichard Henderson         }
18128cfdacaaSRichard Henderson     } else {
18138cfdacaaSRichard Henderson         /* Finish compute of page crossing. */
18148cfdacaaSRichard Henderson         int size0 = l->page[1].addr - addr;
18158cfdacaaSRichard Henderson         l->page[1].size = l->page[0].size - size0;
18168cfdacaaSRichard Henderson         l->page[0].size = size0;
18178cfdacaaSRichard Henderson 
18188cfdacaaSRichard Henderson         /*
18198cfdacaaSRichard Henderson          * Lookup both pages, recognizing exceptions from either.  If the
18208cfdacaaSRichard Henderson          * second lookup potentially resized, refresh first CPUTLBEntryFull.
18218cfdacaaSRichard Henderson          */
1822d50ef446SAnton Johansson         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
1823d50ef446SAnton Johansson         if (mmu_lookup1(cpu, &l->page[1], l->mmu_idx, type, ra)) {
1824d50ef446SAnton Johansson             uintptr_t index = tlb_index(cpu, l->mmu_idx, addr);
1825d50ef446SAnton Johansson             l->page[0].full = &cpu->neg.tlb.d[l->mmu_idx].fulltlb[index];
18268cfdacaaSRichard Henderson         }
18278cfdacaaSRichard Henderson 
18288cfdacaaSRichard Henderson         flags = l->page[0].flags | l->page[1].flags;
18298cfdacaaSRichard Henderson         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1830d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
1831d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[1], type, ra);
18328cfdacaaSRichard Henderson         }
18338cfdacaaSRichard Henderson 
18348cfdacaaSRichard Henderson         /*
18358cfdacaaSRichard Henderson          * Since target/sparc is the only user of TLB_BSWAP, and all
18368cfdacaaSRichard Henderson          * Sparc accesses are aligned, any treatment across two pages
18378cfdacaaSRichard Henderson          * would be arbitrary.  Refuse it until there's a use.
18388cfdacaaSRichard Henderson          */
18398cfdacaaSRichard Henderson         tcg_debug_assert((flags & TLB_BSWAP) == 0);
18408cfdacaaSRichard Henderson     }
18418cfdacaaSRichard Henderson 
18428cfdacaaSRichard Henderson     return crosspage;
18438cfdacaaSRichard Henderson }
18448cfdacaaSRichard Henderson 
18458cfdacaaSRichard Henderson /*
184608dff435SRichard Henderson  * Probe for an atomic operation.  Do not allow unaligned operations,
184708dff435SRichard Henderson  * or io operations to proceed.  Return the host address.
184808dff435SRichard Henderson  */
1849d560225fSAnton Johansson static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
1850b0326eb9SAnton Johansson                                int size, uintptr_t retaddr)
1851d9bb58e5SYang Zhong {
1852b826044fSRichard Henderson     uintptr_t mmu_idx = get_mmuidx(oi);
185314776ab5STony Nguyen     MemOp mop = get_memop(oi);
1854d9bb58e5SYang Zhong     int a_bits = get_alignment_bits(mop);
185508dff435SRichard Henderson     uintptr_t index;
185608dff435SRichard Henderson     CPUTLBEntry *tlbe;
1857b0326eb9SAnton Johansson     vaddr tlb_addr;
185834d49937SPeter Maydell     void *hostaddr;
1859417aeaffSRichard Henderson     CPUTLBEntryFull *full;
1860d9bb58e5SYang Zhong 
1861b826044fSRichard Henderson     tcg_debug_assert(mmu_idx < NB_MMU_MODES);
1862b826044fSRichard Henderson 
1863d9bb58e5SYang Zhong     /* Adjust the given return address.  */
1864d9bb58e5SYang Zhong     retaddr -= GETPC_ADJ;
1865d9bb58e5SYang Zhong 
1866d9bb58e5SYang Zhong     /* Enforce guest required alignment.  */
1867d9bb58e5SYang Zhong     if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) {
1868d9bb58e5SYang Zhong         /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1869d560225fSAnton Johansson         cpu_unaligned_access(cpu, addr, MMU_DATA_STORE,
1870d9bb58e5SYang Zhong                              mmu_idx, retaddr);
1871d9bb58e5SYang Zhong     }
1872d9bb58e5SYang Zhong 
1873d9bb58e5SYang Zhong     /* Enforce qemu required alignment.  */
187408dff435SRichard Henderson     if (unlikely(addr & (size - 1))) {
1875d9bb58e5SYang Zhong         /* We get here if guest alignment was not requested,
1876d9bb58e5SYang Zhong            or was not enforced by cpu_unaligned_access above.
1877d9bb58e5SYang Zhong            We might widen the access and emulate, but for now
1878d9bb58e5SYang Zhong            mark an exception and exit the cpu loop.  */
1879d9bb58e5SYang Zhong         goto stop_the_world;
1880d9bb58e5SYang Zhong     }
1881d9bb58e5SYang Zhong 
1882d560225fSAnton Johansson     index = tlb_index(cpu, mmu_idx, addr);
1883d560225fSAnton Johansson     tlbe = tlb_entry(cpu, mmu_idx, addr);
188408dff435SRichard Henderson 
1885d9bb58e5SYang Zhong     /* Check TLB entry and enforce page permissions.  */
188608dff435SRichard Henderson     tlb_addr = tlb_addr_write(tlbe);
1887334692bcSPeter Maydell     if (!tlb_hit(tlb_addr, addr)) {
1888d560225fSAnton Johansson         if (!victim_tlb_hit(cpu, mmu_idx, index, MMU_DATA_STORE,
18890b3c75adSRichard Henderson                             addr & TARGET_PAGE_MASK)) {
1890d560225fSAnton Johansson             tlb_fill(cpu, addr, size,
189108dff435SRichard Henderson                      MMU_DATA_STORE, mmu_idx, retaddr);
1892d560225fSAnton Johansson             index = tlb_index(cpu, mmu_idx, addr);
1893d560225fSAnton Johansson             tlbe = tlb_entry(cpu, mmu_idx, addr);
1894d9bb58e5SYang Zhong         }
1895403f290cSEmilio G. Cota         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
1896d9bb58e5SYang Zhong     }
1897d9bb58e5SYang Zhong 
1898417aeaffSRichard Henderson     /*
1899417aeaffSRichard Henderson      * Let the guest notice RMW on a write-only page.
1900417aeaffSRichard Henderson      * We have just verified that the page is writable.
1901417aeaffSRichard Henderson      * Subpage lookups may have left TLB_INVALID_MASK set,
1902417aeaffSRichard Henderson      * but addr_read will only be -1 if PAGE_READ was unset.
1903417aeaffSRichard Henderson      */
1904417aeaffSRichard Henderson     if (unlikely(tlbe->addr_read == -1)) {
1905d560225fSAnton Johansson         tlb_fill(cpu, addr, size, MMU_DATA_LOAD, mmu_idx, retaddr);
190608dff435SRichard Henderson         /*
1907417aeaffSRichard Henderson          * Since we don't support reads and writes to different
1908417aeaffSRichard Henderson          * addresses, and we do have the proper page loaded for
1909417aeaffSRichard Henderson          * write, this shouldn't ever return.  But just in case,
1910417aeaffSRichard Henderson          * handle via stop-the-world.
191108dff435SRichard Henderson          */
191208dff435SRichard Henderson         goto stop_the_world;
191308dff435SRichard Henderson     }
1914187ba694SRichard Henderson     /* Collect tlb flags for read. */
1915417aeaffSRichard Henderson     tlb_addr |= tlbe->addr_read;
191608dff435SRichard Henderson 
191755df6fcfSPeter Maydell     /* Notice an IO access or a needs-MMU-lookup access */
19180953674eSRichard Henderson     if (unlikely(tlb_addr & (TLB_MMIO | TLB_DISCARD_WRITE))) {
1919d9bb58e5SYang Zhong         /* There's really nothing that can be done to
1920d9bb58e5SYang Zhong            support this apart from stop-the-world.  */
1921d9bb58e5SYang Zhong         goto stop_the_world;
1922d9bb58e5SYang Zhong     }
1923d9bb58e5SYang Zhong 
192434d49937SPeter Maydell     hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
1925d560225fSAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
192634d49937SPeter Maydell 
192734d49937SPeter Maydell     if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
1928d560225fSAnton Johansson         notdirty_write(cpu, addr, size, full, retaddr);
1929417aeaffSRichard Henderson     }
1930417aeaffSRichard Henderson 
1931187ba694SRichard Henderson     if (unlikely(tlb_addr & TLB_FORCE_SLOW)) {
1932187ba694SRichard Henderson         int wp_flags = 0;
1933187ba694SRichard Henderson 
1934187ba694SRichard Henderson         if (full->slow_flags[MMU_DATA_STORE] & TLB_WATCHPOINT) {
1935187ba694SRichard Henderson             wp_flags |= BP_MEM_WRITE;
1936187ba694SRichard Henderson         }
1937187ba694SRichard Henderson         if (full->slow_flags[MMU_DATA_LOAD] & TLB_WATCHPOINT) {
1938187ba694SRichard Henderson             wp_flags |= BP_MEM_READ;
1939187ba694SRichard Henderson         }
1940187ba694SRichard Henderson         if (wp_flags) {
1941d560225fSAnton Johansson             cpu_check_watchpoint(cpu, addr, size,
1942187ba694SRichard Henderson                                  full->attrs, wp_flags, retaddr);
1943187ba694SRichard Henderson         }
194434d49937SPeter Maydell     }
194534d49937SPeter Maydell 
194634d49937SPeter Maydell     return hostaddr;
1947d9bb58e5SYang Zhong 
1948d9bb58e5SYang Zhong  stop_the_world:
1949d560225fSAnton Johansson     cpu_loop_exit_atomic(cpu, retaddr);
1950d9bb58e5SYang Zhong }
1951d9bb58e5SYang Zhong 
1952eed56642SAlex Bennée /*
1953eed56642SAlex Bennée  * Load Helpers
1954eed56642SAlex Bennée  *
1955eed56642SAlex Bennée  * We support two different access types. SOFTMMU_CODE_ACCESS is
1956eed56642SAlex Bennée  * specifically for reading instructions from system memory. It is
1957eed56642SAlex Bennée  * called by the translation loop and in some helpers where the code
1958eed56642SAlex Bennée  * is disassembled. It shouldn't be called directly by guest code.
1959cdfac37bSRichard Henderson  *
1960eed56642SAlex Bennée  * For the benefit of TCG generated code, we want to avoid the
1961eed56642SAlex Bennée  * complication of ABI-specific return type promotion and always
1962eed56642SAlex Bennée  * return a value extended to the register size of the host. This is
1963eed56642SAlex Bennée  * tcg_target_long, except in the case of a 32-bit host and 64-bit
1964eed56642SAlex Bennée  * data, and for that we always have uint64_t.
1965eed56642SAlex Bennée  *
1966eed56642SAlex Bennée  * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1967eed56642SAlex Bennée  */
1968eed56642SAlex Bennée 
19698cfdacaaSRichard Henderson /**
19708cfdacaaSRichard Henderson  * do_ld_mmio_beN:
1971d50ef446SAnton Johansson  * @cpu: generic cpu state
19721966855eSRichard Henderson  * @full: page parameters
19738cfdacaaSRichard Henderson  * @ret_be: accumulated data
19741966855eSRichard Henderson  * @addr: virtual address
19751966855eSRichard Henderson  * @size: number of bytes
19768cfdacaaSRichard Henderson  * @mmu_idx: virtual address context
19778cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
19781966855eSRichard Henderson  * Context: iothread lock held
19798cfdacaaSRichard Henderson  *
19801966855eSRichard Henderson  * Load @size bytes from @addr, which is memory-mapped i/o.
19818cfdacaaSRichard Henderson  * The bytes are concatenated in big-endian order with @ret_be.
19828cfdacaaSRichard Henderson  */
1983d50ef446SAnton Johansson static uint64_t int_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
19841966855eSRichard Henderson                                 uint64_t ret_be, vaddr addr, int size,
19858bf67267SRichard Henderson                                 int mmu_idx, MMUAccessType type, uintptr_t ra,
19868bf67267SRichard Henderson                                 MemoryRegion *mr, hwaddr mr_offset)
19872dd92606SRichard Henderson {
1988190aba80SRichard Henderson     do {
198913e61747SRichard Henderson         MemOp this_mop;
199013e61747SRichard Henderson         unsigned this_size;
199113e61747SRichard Henderson         uint64_t val;
199213e61747SRichard Henderson         MemTxResult r;
199313e61747SRichard Henderson 
1994190aba80SRichard Henderson         /* Read aligned pieces up to 8 bytes. */
199513e61747SRichard Henderson         this_mop = ctz32(size | (int)addr | 8);
199613e61747SRichard Henderson         this_size = 1 << this_mop;
199713e61747SRichard Henderson         this_mop |= MO_BE;
199813e61747SRichard Henderson 
19998bf67267SRichard Henderson         r = memory_region_dispatch_read(mr, mr_offset, &val,
20008bf67267SRichard Henderson                                         this_mop, full->attrs);
200113e61747SRichard Henderson         if (unlikely(r != MEMTX_OK)) {
2002d50ef446SAnton Johansson             io_failed(cpu, full, addr, this_size, type, mmu_idx, r, ra);
20038cfdacaaSRichard Henderson         }
200413e61747SRichard Henderson         if (this_size == 8) {
200513e61747SRichard Henderson             return val;
200613e61747SRichard Henderson         }
200713e61747SRichard Henderson 
200813e61747SRichard Henderson         ret_be = (ret_be << (this_size * 8)) | val;
200913e61747SRichard Henderson         addr += this_size;
201013e61747SRichard Henderson         mr_offset += this_size;
201113e61747SRichard Henderson         size -= this_size;
2012190aba80SRichard Henderson     } while (size);
201313e61747SRichard Henderson 
20148cfdacaaSRichard Henderson     return ret_be;
20158cfdacaaSRichard Henderson }
20168cfdacaaSRichard Henderson 
2017d50ef446SAnton Johansson static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
20188bf67267SRichard Henderson                                uint64_t ret_be, vaddr addr, int size,
20198bf67267SRichard Henderson                                int mmu_idx, MMUAccessType type, uintptr_t ra)
20208bf67267SRichard Henderson {
20218bf67267SRichard Henderson     MemoryRegionSection *section;
20228bf67267SRichard Henderson     MemoryRegion *mr;
20238bf67267SRichard Henderson     hwaddr mr_offset;
20248bf67267SRichard Henderson     MemTxAttrs attrs;
20258bf67267SRichard Henderson     uint64_t ret;
20268bf67267SRichard Henderson 
20278bf67267SRichard Henderson     tcg_debug_assert(size > 0 && size <= 8);
20288bf67267SRichard Henderson 
20298bf67267SRichard Henderson     attrs = full->attrs;
2030d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
20318bf67267SRichard Henderson     mr = section->mr;
20328bf67267SRichard Henderson 
20338bf67267SRichard Henderson     qemu_mutex_lock_iothread();
2034d50ef446SAnton Johansson     ret = int_ld_mmio_beN(cpu, full, ret_be, addr, size, mmu_idx,
20358bf67267SRichard Henderson                           type, ra, mr, mr_offset);
20368bf67267SRichard Henderson     qemu_mutex_unlock_iothread();
20378bf67267SRichard Henderson 
20388bf67267SRichard Henderson     return ret;
20398bf67267SRichard Henderson }
20408bf67267SRichard Henderson 
2041d50ef446SAnton Johansson static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
20428bf67267SRichard Henderson                                uint64_t ret_be, vaddr addr, int size,
20438bf67267SRichard Henderson                                int mmu_idx, uintptr_t ra)
20448bf67267SRichard Henderson {
20458bf67267SRichard Henderson     MemoryRegionSection *section;
20468bf67267SRichard Henderson     MemoryRegion *mr;
20478bf67267SRichard Henderson     hwaddr mr_offset;
20488bf67267SRichard Henderson     MemTxAttrs attrs;
20498bf67267SRichard Henderson     uint64_t a, b;
20508bf67267SRichard Henderson 
20518bf67267SRichard Henderson     tcg_debug_assert(size > 8 && size <= 16);
20528bf67267SRichard Henderson 
20538bf67267SRichard Henderson     attrs = full->attrs;
2054d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
20558bf67267SRichard Henderson     mr = section->mr;
20568bf67267SRichard Henderson 
20578bf67267SRichard Henderson     qemu_mutex_lock_iothread();
2058d50ef446SAnton Johansson     a = int_ld_mmio_beN(cpu, full, ret_be, addr, size - 8, mmu_idx,
20598bf67267SRichard Henderson                         MMU_DATA_LOAD, ra, mr, mr_offset);
2060d50ef446SAnton Johansson     b = int_ld_mmio_beN(cpu, full, ret_be, addr + size - 8, 8, mmu_idx,
20618bf67267SRichard Henderson                         MMU_DATA_LOAD, ra, mr, mr_offset + size - 8);
20628bf67267SRichard Henderson     qemu_mutex_unlock_iothread();
20638bf67267SRichard Henderson 
20648bf67267SRichard Henderson     return int128_make128(b, a);
20658bf67267SRichard Henderson }
20668bf67267SRichard Henderson 
20678cfdacaaSRichard Henderson /**
20688cfdacaaSRichard Henderson  * do_ld_bytes_beN
20698cfdacaaSRichard Henderson  * @p: translation parameters
20708cfdacaaSRichard Henderson  * @ret_be: accumulated data
20718cfdacaaSRichard Henderson  *
20728cfdacaaSRichard Henderson  * Load @p->size bytes from @p->haddr, which is RAM.
20738cfdacaaSRichard Henderson  * The bytes to concatenated in big-endian order with @ret_be.
20748cfdacaaSRichard Henderson  */
20758cfdacaaSRichard Henderson static uint64_t do_ld_bytes_beN(MMULookupPageData *p, uint64_t ret_be)
20768cfdacaaSRichard Henderson {
20778cfdacaaSRichard Henderson     uint8_t *haddr = p->haddr;
20788cfdacaaSRichard Henderson     int i, size = p->size;
20798cfdacaaSRichard Henderson 
20808cfdacaaSRichard Henderson     for (i = 0; i < size; i++) {
20818cfdacaaSRichard Henderson         ret_be = (ret_be << 8) | haddr[i];
20828cfdacaaSRichard Henderson     }
20838cfdacaaSRichard Henderson     return ret_be;
20848cfdacaaSRichard Henderson }
20858cfdacaaSRichard Henderson 
2086cdfac37bSRichard Henderson /**
2087cdfac37bSRichard Henderson  * do_ld_parts_beN
2088cdfac37bSRichard Henderson  * @p: translation parameters
2089cdfac37bSRichard Henderson  * @ret_be: accumulated data
2090cdfac37bSRichard Henderson  *
2091cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but atomically on each aligned part.
2092cdfac37bSRichard Henderson  */
2093cdfac37bSRichard Henderson static uint64_t do_ld_parts_beN(MMULookupPageData *p, uint64_t ret_be)
2094cdfac37bSRichard Henderson {
2095cdfac37bSRichard Henderson     void *haddr = p->haddr;
2096cdfac37bSRichard Henderson     int size = p->size;
2097cdfac37bSRichard Henderson 
2098cdfac37bSRichard Henderson     do {
2099cdfac37bSRichard Henderson         uint64_t x;
2100cdfac37bSRichard Henderson         int n;
2101cdfac37bSRichard Henderson 
2102cdfac37bSRichard Henderson         /*
2103cdfac37bSRichard Henderson          * Find minimum of alignment and size.
2104cdfac37bSRichard Henderson          * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2105cdfac37bSRichard Henderson          * would have only checked the low bits of addr|size once at the start,
2106cdfac37bSRichard Henderson          * but is just as easy.
2107cdfac37bSRichard Henderson          */
2108cdfac37bSRichard Henderson         switch (((uintptr_t)haddr | size) & 7) {
2109cdfac37bSRichard Henderson         case 4:
2110cdfac37bSRichard Henderson             x = cpu_to_be32(load_atomic4(haddr));
2111cdfac37bSRichard Henderson             ret_be = (ret_be << 32) | x;
2112cdfac37bSRichard Henderson             n = 4;
2113cdfac37bSRichard Henderson             break;
2114cdfac37bSRichard Henderson         case 2:
2115cdfac37bSRichard Henderson         case 6:
2116cdfac37bSRichard Henderson             x = cpu_to_be16(load_atomic2(haddr));
2117cdfac37bSRichard Henderson             ret_be = (ret_be << 16) | x;
2118cdfac37bSRichard Henderson             n = 2;
2119cdfac37bSRichard Henderson             break;
2120cdfac37bSRichard Henderson         default:
2121cdfac37bSRichard Henderson             x = *(uint8_t *)haddr;
2122cdfac37bSRichard Henderson             ret_be = (ret_be << 8) | x;
2123cdfac37bSRichard Henderson             n = 1;
2124cdfac37bSRichard Henderson             break;
2125cdfac37bSRichard Henderson         case 0:
2126cdfac37bSRichard Henderson             g_assert_not_reached();
2127cdfac37bSRichard Henderson         }
2128cdfac37bSRichard Henderson         haddr += n;
2129cdfac37bSRichard Henderson         size -= n;
2130cdfac37bSRichard Henderson     } while (size != 0);
2131cdfac37bSRichard Henderson     return ret_be;
2132cdfac37bSRichard Henderson }
2133cdfac37bSRichard Henderson 
2134cdfac37bSRichard Henderson /**
2135cdfac37bSRichard Henderson  * do_ld_parts_be4
2136cdfac37bSRichard Henderson  * @p: translation parameters
2137cdfac37bSRichard Henderson  * @ret_be: accumulated data
2138cdfac37bSRichard Henderson  *
2139cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
2140cdfac37bSRichard Henderson  * Four aligned bytes are guaranteed to cover the load.
2141cdfac37bSRichard Henderson  */
2142cdfac37bSRichard Henderson static uint64_t do_ld_whole_be4(MMULookupPageData *p, uint64_t ret_be)
2143cdfac37bSRichard Henderson {
2144cdfac37bSRichard Henderson     int o = p->addr & 3;
2145cdfac37bSRichard Henderson     uint32_t x = load_atomic4(p->haddr - o);
2146cdfac37bSRichard Henderson 
2147cdfac37bSRichard Henderson     x = cpu_to_be32(x);
2148cdfac37bSRichard Henderson     x <<= o * 8;
2149cdfac37bSRichard Henderson     x >>= (4 - p->size) * 8;
2150cdfac37bSRichard Henderson     return (ret_be << (p->size * 8)) | x;
2151cdfac37bSRichard Henderson }
2152cdfac37bSRichard Henderson 
2153cdfac37bSRichard Henderson /**
2154cdfac37bSRichard Henderson  * do_ld_parts_be8
2155cdfac37bSRichard Henderson  * @p: translation parameters
2156cdfac37bSRichard Henderson  * @ret_be: accumulated data
2157cdfac37bSRichard Henderson  *
2158cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
2159cdfac37bSRichard Henderson  * Eight aligned bytes are guaranteed to cover the load.
2160cdfac37bSRichard Henderson  */
2161d50ef446SAnton Johansson static uint64_t do_ld_whole_be8(CPUState *cpu, uintptr_t ra,
2162cdfac37bSRichard Henderson                                 MMULookupPageData *p, uint64_t ret_be)
2163cdfac37bSRichard Henderson {
2164cdfac37bSRichard Henderson     int o = p->addr & 7;
216573fda56fSAnton Johansson     uint64_t x = load_atomic8_or_exit(cpu, ra, p->haddr - o);
2166cdfac37bSRichard Henderson 
2167cdfac37bSRichard Henderson     x = cpu_to_be64(x);
2168cdfac37bSRichard Henderson     x <<= o * 8;
2169cdfac37bSRichard Henderson     x >>= (8 - p->size) * 8;
2170cdfac37bSRichard Henderson     return (ret_be << (p->size * 8)) | x;
2171cdfac37bSRichard Henderson }
2172cdfac37bSRichard Henderson 
217335c653c4SRichard Henderson /**
217435c653c4SRichard Henderson  * do_ld_parts_be16
217535c653c4SRichard Henderson  * @p: translation parameters
217635c653c4SRichard Henderson  * @ret_be: accumulated data
217735c653c4SRichard Henderson  *
217835c653c4SRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
217935c653c4SRichard Henderson  * 16 aligned bytes are guaranteed to cover the load.
218035c653c4SRichard Henderson  */
2181d50ef446SAnton Johansson static Int128 do_ld_whole_be16(CPUState *cpu, uintptr_t ra,
218235c653c4SRichard Henderson                                MMULookupPageData *p, uint64_t ret_be)
218335c653c4SRichard Henderson {
218435c653c4SRichard Henderson     int o = p->addr & 15;
218573fda56fSAnton Johansson     Int128 x, y = load_atomic16_or_exit(cpu, ra, p->haddr - o);
218635c653c4SRichard Henderson     int size = p->size;
218735c653c4SRichard Henderson 
218835c653c4SRichard Henderson     if (!HOST_BIG_ENDIAN) {
218935c653c4SRichard Henderson         y = bswap128(y);
219035c653c4SRichard Henderson     }
219135c653c4SRichard Henderson     y = int128_lshift(y, o * 8);
219235c653c4SRichard Henderson     y = int128_urshift(y, (16 - size) * 8);
219335c653c4SRichard Henderson     x = int128_make64(ret_be);
219435c653c4SRichard Henderson     x = int128_lshift(x, size * 8);
219535c653c4SRichard Henderson     return int128_or(x, y);
219635c653c4SRichard Henderson }
219735c653c4SRichard Henderson 
21988cfdacaaSRichard Henderson /*
21998cfdacaaSRichard Henderson  * Wrapper for the above.
22008cfdacaaSRichard Henderson  */
2201d50ef446SAnton Johansson static uint64_t do_ld_beN(CPUState *cpu, MMULookupPageData *p,
2202cdfac37bSRichard Henderson                           uint64_t ret_be, int mmu_idx, MMUAccessType type,
2203cdfac37bSRichard Henderson                           MemOp mop, uintptr_t ra)
22048cfdacaaSRichard Henderson {
2205cdfac37bSRichard Henderson     MemOp atom;
2206cdfac37bSRichard Henderson     unsigned tmp, half_size;
2207cdfac37bSRichard Henderson 
22088cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2209d50ef446SAnton Johansson         return do_ld_mmio_beN(cpu, p->full, ret_be, p->addr, p->size,
22101966855eSRichard Henderson                               mmu_idx, type, ra);
2211cdfac37bSRichard Henderson     }
2212cdfac37bSRichard Henderson 
2213cdfac37bSRichard Henderson     /*
2214cdfac37bSRichard Henderson      * It is a given that we cross a page and therefore there is no
2215cdfac37bSRichard Henderson      * atomicity for the load as a whole, but subobjects may need attention.
2216cdfac37bSRichard Henderson      */
2217cdfac37bSRichard Henderson     atom = mop & MO_ATOM_MASK;
2218cdfac37bSRichard Henderson     switch (atom) {
2219cdfac37bSRichard Henderson     case MO_ATOM_SUBALIGN:
2220cdfac37bSRichard Henderson         return do_ld_parts_beN(p, ret_be);
2221cdfac37bSRichard Henderson 
2222cdfac37bSRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
2223cdfac37bSRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
2224cdfac37bSRichard Henderson         tmp = mop & MO_SIZE;
2225cdfac37bSRichard Henderson         tmp = tmp ? tmp - 1 : 0;
2226cdfac37bSRichard Henderson         half_size = 1 << tmp;
2227cdfac37bSRichard Henderson         if (atom == MO_ATOM_IFALIGN_PAIR
2228cdfac37bSRichard Henderson             ? p->size == half_size
2229cdfac37bSRichard Henderson             : p->size >= half_size) {
2230cdfac37bSRichard Henderson             if (!HAVE_al8_fast && p->size < 4) {
2231cdfac37bSRichard Henderson                 return do_ld_whole_be4(p, ret_be);
22328cfdacaaSRichard Henderson             } else {
2233d50ef446SAnton Johansson                 return do_ld_whole_be8(cpu, ra, p, ret_be);
2234cdfac37bSRichard Henderson             }
2235cdfac37bSRichard Henderson         }
2236cdfac37bSRichard Henderson         /* fall through */
2237cdfac37bSRichard Henderson 
2238cdfac37bSRichard Henderson     case MO_ATOM_IFALIGN:
2239cdfac37bSRichard Henderson     case MO_ATOM_WITHIN16:
2240cdfac37bSRichard Henderson     case MO_ATOM_NONE:
22418cfdacaaSRichard Henderson         return do_ld_bytes_beN(p, ret_be);
2242cdfac37bSRichard Henderson 
2243cdfac37bSRichard Henderson     default:
2244cdfac37bSRichard Henderson         g_assert_not_reached();
22458cfdacaaSRichard Henderson     }
22468cfdacaaSRichard Henderson }
22478cfdacaaSRichard Henderson 
224835c653c4SRichard Henderson /*
224935c653c4SRichard Henderson  * Wrapper for the above, for 8 < size < 16.
225035c653c4SRichard Henderson  */
2251d50ef446SAnton Johansson static Int128 do_ld16_beN(CPUState *cpu, MMULookupPageData *p,
225235c653c4SRichard Henderson                           uint64_t a, int mmu_idx, MemOp mop, uintptr_t ra)
225335c653c4SRichard Henderson {
225435c653c4SRichard Henderson     int size = p->size;
225535c653c4SRichard Henderson     uint64_t b;
225635c653c4SRichard Henderson     MemOp atom;
225735c653c4SRichard Henderson 
225835c653c4SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2259d50ef446SAnton Johansson         return do_ld16_mmio_beN(cpu, p->full, a, p->addr, size, mmu_idx, ra);
226035c653c4SRichard Henderson     }
226135c653c4SRichard Henderson 
226235c653c4SRichard Henderson     /*
226335c653c4SRichard Henderson      * It is a given that we cross a page and therefore there is no
226435c653c4SRichard Henderson      * atomicity for the load as a whole, but subobjects may need attention.
226535c653c4SRichard Henderson      */
226635c653c4SRichard Henderson     atom = mop & MO_ATOM_MASK;
226735c653c4SRichard Henderson     switch (atom) {
226835c653c4SRichard Henderson     case MO_ATOM_SUBALIGN:
226935c653c4SRichard Henderson         p->size = size - 8;
227035c653c4SRichard Henderson         a = do_ld_parts_beN(p, a);
227135c653c4SRichard Henderson         p->haddr += size - 8;
227235c653c4SRichard Henderson         p->size = 8;
227335c653c4SRichard Henderson         b = do_ld_parts_beN(p, 0);
227435c653c4SRichard Henderson         break;
227535c653c4SRichard Henderson 
227635c653c4SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
227735c653c4SRichard Henderson         /* Since size > 8, this is the half that must be atomic. */
2278d50ef446SAnton Johansson         return do_ld_whole_be16(cpu, ra, p, a);
227935c653c4SRichard Henderson 
228035c653c4SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
228135c653c4SRichard Henderson         /*
228235c653c4SRichard Henderson          * Since size > 8, both halves are misaligned,
228335c653c4SRichard Henderson          * and so neither is atomic.
228435c653c4SRichard Henderson          */
228535c653c4SRichard Henderson     case MO_ATOM_IFALIGN:
228635c653c4SRichard Henderson     case MO_ATOM_WITHIN16:
228735c653c4SRichard Henderson     case MO_ATOM_NONE:
228835c653c4SRichard Henderson         p->size = size - 8;
228935c653c4SRichard Henderson         a = do_ld_bytes_beN(p, a);
229035c653c4SRichard Henderson         b = ldq_be_p(p->haddr + size - 8);
229135c653c4SRichard Henderson         break;
229235c653c4SRichard Henderson 
229335c653c4SRichard Henderson     default:
229435c653c4SRichard Henderson         g_assert_not_reached();
229535c653c4SRichard Henderson     }
229635c653c4SRichard Henderson 
229735c653c4SRichard Henderson     return int128_make128(b, a);
229835c653c4SRichard Henderson }
229935c653c4SRichard Henderson 
2300d50ef446SAnton Johansson static uint8_t do_ld_1(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23018cfdacaaSRichard Henderson                        MMUAccessType type, uintptr_t ra)
23028cfdacaaSRichard Henderson {
23038cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2304d50ef446SAnton Johansson         return do_ld_mmio_beN(cpu, p->full, 0, p->addr, 1, mmu_idx, type, ra);
23058cfdacaaSRichard Henderson     } else {
23068cfdacaaSRichard Henderson         return *(uint8_t *)p->haddr;
23078cfdacaaSRichard Henderson     }
23088cfdacaaSRichard Henderson }
23098cfdacaaSRichard Henderson 
2310d50ef446SAnton Johansson static uint16_t do_ld_2(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23118cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23128cfdacaaSRichard Henderson {
2313f7eaf9d7SRichard Henderson     uint16_t ret;
23148cfdacaaSRichard Henderson 
23158cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2316d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 2, mmu_idx, type, ra);
2317f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2318f7eaf9d7SRichard Henderson             ret = bswap16(ret);
23198cfdacaaSRichard Henderson         }
2320f7eaf9d7SRichard Henderson     } else {
23218cfdacaaSRichard Henderson         /* Perform the load host endian, then swap if necessary. */
232273fda56fSAnton Johansson         ret = load_atom_2(cpu, ra, p->haddr, memop);
23238cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23248cfdacaaSRichard Henderson             ret = bswap16(ret);
23258cfdacaaSRichard Henderson         }
2326f7eaf9d7SRichard Henderson     }
23278cfdacaaSRichard Henderson     return ret;
23288cfdacaaSRichard Henderson }
23298cfdacaaSRichard Henderson 
2330d50ef446SAnton Johansson static uint32_t do_ld_4(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23318cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23328cfdacaaSRichard Henderson {
23338cfdacaaSRichard Henderson     uint32_t ret;
23348cfdacaaSRichard Henderson 
23358cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2336d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 4, mmu_idx, type, ra);
2337f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2338f7eaf9d7SRichard Henderson             ret = bswap32(ret);
23398cfdacaaSRichard Henderson         }
2340f7eaf9d7SRichard Henderson     } else {
23418cfdacaaSRichard Henderson         /* Perform the load host endian. */
234273fda56fSAnton Johansson         ret = load_atom_4(cpu, ra, p->haddr, memop);
23438cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23448cfdacaaSRichard Henderson             ret = bswap32(ret);
23458cfdacaaSRichard Henderson         }
2346f7eaf9d7SRichard Henderson     }
23478cfdacaaSRichard Henderson     return ret;
23488cfdacaaSRichard Henderson }
23498cfdacaaSRichard Henderson 
2350d50ef446SAnton Johansson static uint64_t do_ld_8(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23518cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23528cfdacaaSRichard Henderson {
23538cfdacaaSRichard Henderson     uint64_t ret;
23548cfdacaaSRichard Henderson 
23558cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2356d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 8, mmu_idx, type, ra);
2357f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2358f7eaf9d7SRichard Henderson             ret = bswap64(ret);
23598cfdacaaSRichard Henderson         }
2360f7eaf9d7SRichard Henderson     } else {
23618cfdacaaSRichard Henderson         /* Perform the load host endian. */
236273fda56fSAnton Johansson         ret = load_atom_8(cpu, ra, p->haddr, memop);
23638cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23648cfdacaaSRichard Henderson             ret = bswap64(ret);
23658cfdacaaSRichard Henderson         }
2366f7eaf9d7SRichard Henderson     }
23678cfdacaaSRichard Henderson     return ret;
23688cfdacaaSRichard Henderson }
23698cfdacaaSRichard Henderson 
2370d50ef446SAnton Johansson static uint8_t do_ld1_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
23718cfdacaaSRichard Henderson                           uintptr_t ra, MMUAccessType access_type)
23728cfdacaaSRichard Henderson {
23738cfdacaaSRichard Henderson     MMULookupLocals l;
23748cfdacaaSRichard Henderson     bool crosspage;
23758cfdacaaSRichard Henderson 
2376f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2377d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
23788cfdacaaSRichard Henderson     tcg_debug_assert(!crosspage);
23798cfdacaaSRichard Henderson 
2380d50ef446SAnton Johansson     return do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
23812dd92606SRichard Henderson }
23822dd92606SRichard Henderson 
2383d50ef446SAnton Johansson static uint16_t do_ld2_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
23848cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
23852dd92606SRichard Henderson {
23868cfdacaaSRichard Henderson     MMULookupLocals l;
23878cfdacaaSRichard Henderson     bool crosspage;
23888cfdacaaSRichard Henderson     uint16_t ret;
23898cfdacaaSRichard Henderson     uint8_t a, b;
23908cfdacaaSRichard Henderson 
2391f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2392d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
23938cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2394d50ef446SAnton Johansson         return do_ld_2(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
23958cfdacaaSRichard Henderson     }
23968cfdacaaSRichard Henderson 
2397d50ef446SAnton Johansson     a = do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
2398d50ef446SAnton Johansson     b = do_ld_1(cpu, &l.page[1], l.mmu_idx, access_type, ra);
23998cfdacaaSRichard Henderson 
24008cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
24018cfdacaaSRichard Henderson         ret = a | (b << 8);
24028cfdacaaSRichard Henderson     } else {
24038cfdacaaSRichard Henderson         ret = b | (a << 8);
24048cfdacaaSRichard Henderson     }
24058cfdacaaSRichard Henderson     return ret;
2406eed56642SAlex Bennée }
2407eed56642SAlex Bennée 
2408d50ef446SAnton Johansson static uint32_t do_ld4_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
24098cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
24102dd92606SRichard Henderson {
24118cfdacaaSRichard Henderson     MMULookupLocals l;
24128cfdacaaSRichard Henderson     bool crosspage;
24138cfdacaaSRichard Henderson     uint32_t ret;
24148cfdacaaSRichard Henderson 
2415f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2416d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
24178cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2418d50ef446SAnton Johansson         return do_ld_4(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
24198cfdacaaSRichard Henderson     }
24208cfdacaaSRichard Henderson 
2421d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2422d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
24238cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
24248cfdacaaSRichard Henderson         ret = bswap32(ret);
24258cfdacaaSRichard Henderson     }
24268cfdacaaSRichard Henderson     return ret;
2427eed56642SAlex Bennée }
2428eed56642SAlex Bennée 
2429d50ef446SAnton Johansson static uint64_t do_ld8_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
24308cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
24318cfdacaaSRichard Henderson {
24328cfdacaaSRichard Henderson     MMULookupLocals l;
24338cfdacaaSRichard Henderson     bool crosspage;
24348cfdacaaSRichard Henderson     uint64_t ret;
24358cfdacaaSRichard Henderson 
2436f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2437d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
24388cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2439d50ef446SAnton Johansson         return do_ld_8(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
24408cfdacaaSRichard Henderson     }
24418cfdacaaSRichard Henderson 
2442d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2443d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
24448cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
24458cfdacaaSRichard Henderson         ret = bswap64(ret);
24468cfdacaaSRichard Henderson     }
24478cfdacaaSRichard Henderson     return ret;
2448eed56642SAlex Bennée }
2449eed56642SAlex Bennée 
2450d50ef446SAnton Johansson static Int128 do_ld16_mmu(CPUState *cpu, vaddr addr,
245135c653c4SRichard Henderson                           MemOpIdx oi, uintptr_t ra)
245235c653c4SRichard Henderson {
245335c653c4SRichard Henderson     MMULookupLocals l;
245435c653c4SRichard Henderson     bool crosspage;
245535c653c4SRichard Henderson     uint64_t a, b;
245635c653c4SRichard Henderson     Int128 ret;
245735c653c4SRichard Henderson     int first;
245835c653c4SRichard Henderson 
2459f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2460d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_LOAD, &l);
246135c653c4SRichard Henderson     if (likely(!crosspage)) {
246235c653c4SRichard Henderson         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2463d50ef446SAnton Johansson             ret = do_ld16_mmio_beN(cpu, l.page[0].full, 0, addr, 16,
24648bf67267SRichard Henderson                                    l.mmu_idx, ra);
2465f7eaf9d7SRichard Henderson             if ((l.memop & MO_BSWAP) == MO_LE) {
2466f7eaf9d7SRichard Henderson                 ret = bswap128(ret);
246735c653c4SRichard Henderson             }
2468f7eaf9d7SRichard Henderson         } else {
2469f7eaf9d7SRichard Henderson             /* Perform the load host endian. */
247073fda56fSAnton Johansson             ret = load_atom_16(cpu, ra, l.page[0].haddr, l.memop);
247135c653c4SRichard Henderson             if (l.memop & MO_BSWAP) {
247235c653c4SRichard Henderson                 ret = bswap128(ret);
247335c653c4SRichard Henderson             }
2474f7eaf9d7SRichard Henderson         }
247535c653c4SRichard Henderson         return ret;
247635c653c4SRichard Henderson     }
247735c653c4SRichard Henderson 
247835c653c4SRichard Henderson     first = l.page[0].size;
247935c653c4SRichard Henderson     if (first == 8) {
248035c653c4SRichard Henderson         MemOp mop8 = (l.memop & ~MO_SIZE) | MO_64;
248135c653c4SRichard Henderson 
2482d50ef446SAnton Johansson         a = do_ld_8(cpu, &l.page[0], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
2483d50ef446SAnton Johansson         b = do_ld_8(cpu, &l.page[1], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
248435c653c4SRichard Henderson         if ((mop8 & MO_BSWAP) == MO_LE) {
248535c653c4SRichard Henderson             ret = int128_make128(a, b);
248635c653c4SRichard Henderson         } else {
248735c653c4SRichard Henderson             ret = int128_make128(b, a);
248835c653c4SRichard Henderson         }
248935c653c4SRichard Henderson         return ret;
249035c653c4SRichard Henderson     }
249135c653c4SRichard Henderson 
249235c653c4SRichard Henderson     if (first < 8) {
2493d50ef446SAnton Johansson         a = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx,
249435c653c4SRichard Henderson                       MMU_DATA_LOAD, l.memop, ra);
2495d50ef446SAnton Johansson         ret = do_ld16_beN(cpu, &l.page[1], a, l.mmu_idx, l.memop, ra);
249635c653c4SRichard Henderson     } else {
2497d50ef446SAnton Johansson         ret = do_ld16_beN(cpu, &l.page[0], 0, l.mmu_idx, l.memop, ra);
249835c653c4SRichard Henderson         b = int128_getlo(ret);
249935c653c4SRichard Henderson         ret = int128_lshift(ret, l.page[1].size * 8);
250035c653c4SRichard Henderson         a = int128_gethi(ret);
2501d50ef446SAnton Johansson         b = do_ld_beN(cpu, &l.page[1], b, l.mmu_idx,
250235c653c4SRichard Henderson                       MMU_DATA_LOAD, l.memop, ra);
250335c653c4SRichard Henderson         ret = int128_make128(b, a);
250435c653c4SRichard Henderson     }
250535c653c4SRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
250635c653c4SRichard Henderson         ret = bswap128(ret);
250735c653c4SRichard Henderson     }
250835c653c4SRichard Henderson     return ret;
250935c653c4SRichard Henderson }
251035c653c4SRichard Henderson 
2511d03f1408SRichard Henderson /*
2512eed56642SAlex Bennée  * Store Helpers
2513eed56642SAlex Bennée  */
2514eed56642SAlex Bennée 
251559213461SRichard Henderson /**
251659213461SRichard Henderson  * do_st_mmio_leN:
2517d50ef446SAnton Johansson  * @cpu: generic cpu state
25181966855eSRichard Henderson  * @full: page parameters
251959213461SRichard Henderson  * @val_le: data to store
25201966855eSRichard Henderson  * @addr: virtual address
25211966855eSRichard Henderson  * @size: number of bytes
252259213461SRichard Henderson  * @mmu_idx: virtual address context
252359213461SRichard Henderson  * @ra: return address into tcg generated code, or 0
25241966855eSRichard Henderson  * Context: iothread lock held
252559213461SRichard Henderson  *
25261966855eSRichard Henderson  * Store @size bytes at @addr, which is memory-mapped i/o.
252759213461SRichard Henderson  * The bytes to store are extracted in little-endian order from @val_le;
252859213461SRichard Henderson  * return the bytes of @val_le beyond @p->size that have not been stored.
252959213461SRichard Henderson  */
2530d50ef446SAnton Johansson static uint64_t int_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25311966855eSRichard Henderson                                 uint64_t val_le, vaddr addr, int size,
25321f9823ceSRichard Henderson                                 int mmu_idx, uintptr_t ra,
25331f9823ceSRichard Henderson                                 MemoryRegion *mr, hwaddr mr_offset)
25346b8b622eSRichard Henderson {
2535190aba80SRichard Henderson     do {
25365646d6a7SRichard Henderson         MemOp this_mop;
25375646d6a7SRichard Henderson         unsigned this_size;
25385646d6a7SRichard Henderson         MemTxResult r;
25395646d6a7SRichard Henderson 
2540190aba80SRichard Henderson         /* Store aligned pieces up to 8 bytes. */
25415646d6a7SRichard Henderson         this_mop = ctz32(size | (int)addr | 8);
25425646d6a7SRichard Henderson         this_size = 1 << this_mop;
25435646d6a7SRichard Henderson         this_mop |= MO_LE;
25445646d6a7SRichard Henderson 
25455646d6a7SRichard Henderson         r = memory_region_dispatch_write(mr, mr_offset, val_le,
25461f9823ceSRichard Henderson                                          this_mop, full->attrs);
25475646d6a7SRichard Henderson         if (unlikely(r != MEMTX_OK)) {
2548d50ef446SAnton Johansson             io_failed(cpu, full, addr, this_size, MMU_DATA_STORE,
25495646d6a7SRichard Henderson                       mmu_idx, r, ra);
255059213461SRichard Henderson         }
25515646d6a7SRichard Henderson         if (this_size == 8) {
25525646d6a7SRichard Henderson             return 0;
25535646d6a7SRichard Henderson         }
25545646d6a7SRichard Henderson 
25555646d6a7SRichard Henderson         val_le >>= this_size * 8;
25565646d6a7SRichard Henderson         addr += this_size;
25575646d6a7SRichard Henderson         mr_offset += this_size;
25585646d6a7SRichard Henderson         size -= this_size;
2559190aba80SRichard Henderson     } while (size);
2560190aba80SRichard Henderson 
256159213461SRichard Henderson     return val_le;
256259213461SRichard Henderson }
256359213461SRichard Henderson 
2564d50ef446SAnton Johansson static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25651f9823ceSRichard Henderson                                uint64_t val_le, vaddr addr, int size,
25661f9823ceSRichard Henderson                                int mmu_idx, uintptr_t ra)
25671f9823ceSRichard Henderson {
25681f9823ceSRichard Henderson     MemoryRegionSection *section;
25691f9823ceSRichard Henderson     hwaddr mr_offset;
25701f9823ceSRichard Henderson     MemoryRegion *mr;
25711f9823ceSRichard Henderson     MemTxAttrs attrs;
25721f9823ceSRichard Henderson     uint64_t ret;
25731f9823ceSRichard Henderson 
25741f9823ceSRichard Henderson     tcg_debug_assert(size > 0 && size <= 8);
25751f9823ceSRichard Henderson 
25761f9823ceSRichard Henderson     attrs = full->attrs;
2577d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
25781f9823ceSRichard Henderson     mr = section->mr;
25791f9823ceSRichard Henderson 
25801f9823ceSRichard Henderson     qemu_mutex_lock_iothread();
2581d50ef446SAnton Johansson     ret = int_st_mmio_leN(cpu, full, val_le, addr, size, mmu_idx,
25821f9823ceSRichard Henderson                           ra, mr, mr_offset);
25831f9823ceSRichard Henderson     qemu_mutex_unlock_iothread();
25841f9823ceSRichard Henderson 
25851f9823ceSRichard Henderson     return ret;
25861f9823ceSRichard Henderson }
25871f9823ceSRichard Henderson 
2588d50ef446SAnton Johansson static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25891f9823ceSRichard Henderson                                  Int128 val_le, vaddr addr, int size,
25901f9823ceSRichard Henderson                                  int mmu_idx, uintptr_t ra)
25911f9823ceSRichard Henderson {
25921f9823ceSRichard Henderson     MemoryRegionSection *section;
25931f9823ceSRichard Henderson     MemoryRegion *mr;
25941f9823ceSRichard Henderson     hwaddr mr_offset;
25951f9823ceSRichard Henderson     MemTxAttrs attrs;
25961f9823ceSRichard Henderson     uint64_t ret;
25971f9823ceSRichard Henderson 
25981f9823ceSRichard Henderson     tcg_debug_assert(size > 8 && size <= 16);
25991f9823ceSRichard Henderson 
26001f9823ceSRichard Henderson     attrs = full->attrs;
2601d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
26021f9823ceSRichard Henderson     mr = section->mr;
26031f9823ceSRichard Henderson 
26041f9823ceSRichard Henderson     qemu_mutex_lock_iothread();
2605d50ef446SAnton Johansson     int_st_mmio_leN(cpu, full, int128_getlo(val_le), addr, 8,
26061f9823ceSRichard Henderson                     mmu_idx, ra, mr, mr_offset);
2607d50ef446SAnton Johansson     ret = int_st_mmio_leN(cpu, full, int128_gethi(val_le), addr + 8,
26081f9823ceSRichard Henderson                           size - 8, mmu_idx, ra, mr, mr_offset + 8);
26091f9823ceSRichard Henderson     qemu_mutex_unlock_iothread();
26101f9823ceSRichard Henderson 
26111f9823ceSRichard Henderson     return ret;
26121f9823ceSRichard Henderson }
26131f9823ceSRichard Henderson 
26146b8b622eSRichard Henderson /*
261559213461SRichard Henderson  * Wrapper for the above.
26166b8b622eSRichard Henderson  */
2617d50ef446SAnton Johansson static uint64_t do_st_leN(CPUState *cpu, MMULookupPageData *p,
26185b36f268SRichard Henderson                           uint64_t val_le, int mmu_idx,
26195b36f268SRichard Henderson                           MemOp mop, uintptr_t ra)
262059213461SRichard Henderson {
26215b36f268SRichard Henderson     MemOp atom;
26225b36f268SRichard Henderson     unsigned tmp, half_size;
26235b36f268SRichard Henderson 
262459213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2625d50ef446SAnton Johansson         return do_st_mmio_leN(cpu, p->full, val_le, p->addr,
26261966855eSRichard Henderson                               p->size, mmu_idx, ra);
262759213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
262859213461SRichard Henderson         return val_le >> (p->size * 8);
26295b36f268SRichard Henderson     }
26305b36f268SRichard Henderson 
26315b36f268SRichard Henderson     /*
26325b36f268SRichard Henderson      * It is a given that we cross a page and therefore there is no atomicity
26335b36f268SRichard Henderson      * for the store as a whole, but subobjects may need attention.
26345b36f268SRichard Henderson      */
26355b36f268SRichard Henderson     atom = mop & MO_ATOM_MASK;
26365b36f268SRichard Henderson     switch (atom) {
26375b36f268SRichard Henderson     case MO_ATOM_SUBALIGN:
26385b36f268SRichard Henderson         return store_parts_leN(p->haddr, p->size, val_le);
26395b36f268SRichard Henderson 
26405b36f268SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
26415b36f268SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
26425b36f268SRichard Henderson         tmp = mop & MO_SIZE;
26435b36f268SRichard Henderson         tmp = tmp ? tmp - 1 : 0;
26445b36f268SRichard Henderson         half_size = 1 << tmp;
26455b36f268SRichard Henderson         if (atom == MO_ATOM_IFALIGN_PAIR
26465b36f268SRichard Henderson             ? p->size == half_size
26475b36f268SRichard Henderson             : p->size >= half_size) {
26485b36f268SRichard Henderson             if (!HAVE_al8_fast && p->size <= 4) {
26495b36f268SRichard Henderson                 return store_whole_le4(p->haddr, p->size, val_le);
26505b36f268SRichard Henderson             } else if (HAVE_al8) {
26515b36f268SRichard Henderson                 return store_whole_le8(p->haddr, p->size, val_le);
26526b8b622eSRichard Henderson             } else {
2653d50ef446SAnton Johansson                 cpu_loop_exit_atomic(cpu, ra);
26545b36f268SRichard Henderson             }
26555b36f268SRichard Henderson         }
26565b36f268SRichard Henderson         /* fall through */
26575b36f268SRichard Henderson 
26585b36f268SRichard Henderson     case MO_ATOM_IFALIGN:
26595b36f268SRichard Henderson     case MO_ATOM_WITHIN16:
26605b36f268SRichard Henderson     case MO_ATOM_NONE:
26615b36f268SRichard Henderson         return store_bytes_leN(p->haddr, p->size, val_le);
26625b36f268SRichard Henderson 
26635b36f268SRichard Henderson     default:
26645b36f268SRichard Henderson         g_assert_not_reached();
26656b8b622eSRichard Henderson     }
26666b8b622eSRichard Henderson }
26676b8b622eSRichard Henderson 
266835c653c4SRichard Henderson /*
266935c653c4SRichard Henderson  * Wrapper for the above, for 8 < size < 16.
267035c653c4SRichard Henderson  */
2671d50ef446SAnton Johansson static uint64_t do_st16_leN(CPUState *cpu, MMULookupPageData *p,
267235c653c4SRichard Henderson                             Int128 val_le, int mmu_idx,
267335c653c4SRichard Henderson                             MemOp mop, uintptr_t ra)
267435c653c4SRichard Henderson {
267535c653c4SRichard Henderson     int size = p->size;
267635c653c4SRichard Henderson     MemOp atom;
267735c653c4SRichard Henderson 
267835c653c4SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2679d50ef446SAnton Johansson         return do_st16_mmio_leN(cpu, p->full, val_le, p->addr,
26801f9823ceSRichard Henderson                                 size, mmu_idx, ra);
268135c653c4SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
268235c653c4SRichard Henderson         return int128_gethi(val_le) >> ((size - 8) * 8);
268335c653c4SRichard Henderson     }
268435c653c4SRichard Henderson 
268535c653c4SRichard Henderson     /*
268635c653c4SRichard Henderson      * It is a given that we cross a page and therefore there is no atomicity
268735c653c4SRichard Henderson      * for the store as a whole, but subobjects may need attention.
268835c653c4SRichard Henderson      */
268935c653c4SRichard Henderson     atom = mop & MO_ATOM_MASK;
269035c653c4SRichard Henderson     switch (atom) {
269135c653c4SRichard Henderson     case MO_ATOM_SUBALIGN:
269235c653c4SRichard Henderson         store_parts_leN(p->haddr, 8, int128_getlo(val_le));
269335c653c4SRichard Henderson         return store_parts_leN(p->haddr + 8, p->size - 8,
269435c653c4SRichard Henderson                                int128_gethi(val_le));
269535c653c4SRichard Henderson 
269635c653c4SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
269735c653c4SRichard Henderson         /* Since size > 8, this is the half that must be atomic. */
26986046f6e9SRichard Henderson         if (!HAVE_CMPXCHG128) {
2699d50ef446SAnton Johansson             cpu_loop_exit_atomic(cpu, ra);
270035c653c4SRichard Henderson         }
270135c653c4SRichard Henderson         return store_whole_le16(p->haddr, p->size, val_le);
270235c653c4SRichard Henderson 
270335c653c4SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
270435c653c4SRichard Henderson         /*
270535c653c4SRichard Henderson          * Since size > 8, both halves are misaligned,
270635c653c4SRichard Henderson          * and so neither is atomic.
270735c653c4SRichard Henderson          */
270835c653c4SRichard Henderson     case MO_ATOM_IFALIGN:
27092be6a486SRichard Henderson     case MO_ATOM_WITHIN16:
271035c653c4SRichard Henderson     case MO_ATOM_NONE:
271135c653c4SRichard Henderson         stq_le_p(p->haddr, int128_getlo(val_le));
271235c653c4SRichard Henderson         return store_bytes_leN(p->haddr + 8, p->size - 8,
271335c653c4SRichard Henderson                                int128_gethi(val_le));
271435c653c4SRichard Henderson 
271535c653c4SRichard Henderson     default:
271635c653c4SRichard Henderson         g_assert_not_reached();
271735c653c4SRichard Henderson     }
271835c653c4SRichard Henderson }
271935c653c4SRichard Henderson 
2720d50ef446SAnton Johansson static void do_st_1(CPUState *cpu, MMULookupPageData *p, uint8_t val,
272159213461SRichard Henderson                     int mmu_idx, uintptr_t ra)
2722eed56642SAlex Bennée {
272359213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2724d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 1, mmu_idx, ra);
272559213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
272659213461SRichard Henderson         /* nothing */
27275b87b3e6SRichard Henderson     } else {
272859213461SRichard Henderson         *(uint8_t *)p->haddr = val;
27295b87b3e6SRichard Henderson     }
2730eed56642SAlex Bennée }
2731eed56642SAlex Bennée 
2732d50ef446SAnton Johansson static void do_st_2(CPUState *cpu, MMULookupPageData *p, uint16_t val,
273359213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
2734eed56642SAlex Bennée {
273559213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2736f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2737f7eaf9d7SRichard Henderson             val = bswap16(val);
2738f7eaf9d7SRichard Henderson         }
2739d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 2, mmu_idx, ra);
274059213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
274159213461SRichard Henderson         /* nothing */
274259213461SRichard Henderson     } else {
274359213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
274459213461SRichard Henderson         if (memop & MO_BSWAP) {
274559213461SRichard Henderson             val = bswap16(val);
274659213461SRichard Henderson         }
274773fda56fSAnton Johansson         store_atom_2(cpu, ra, p->haddr, memop, val);
274859213461SRichard Henderson     }
274959213461SRichard Henderson }
275059213461SRichard Henderson 
2751d50ef446SAnton Johansson static void do_st_4(CPUState *cpu, MMULookupPageData *p, uint32_t val,
275259213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
275359213461SRichard Henderson {
275459213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2755f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2756f7eaf9d7SRichard Henderson             val = bswap32(val);
2757f7eaf9d7SRichard Henderson         }
2758d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 4, mmu_idx, ra);
275959213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
276059213461SRichard Henderson         /* nothing */
276159213461SRichard Henderson     } else {
276259213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
276359213461SRichard Henderson         if (memop & MO_BSWAP) {
276459213461SRichard Henderson             val = bswap32(val);
276559213461SRichard Henderson         }
276673fda56fSAnton Johansson         store_atom_4(cpu, ra, p->haddr, memop, val);
276759213461SRichard Henderson     }
276859213461SRichard Henderson }
276959213461SRichard Henderson 
2770d50ef446SAnton Johansson static void do_st_8(CPUState *cpu, MMULookupPageData *p, uint64_t val,
277159213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
277259213461SRichard Henderson {
277359213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2774f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2775f7eaf9d7SRichard Henderson             val = bswap64(val);
2776f7eaf9d7SRichard Henderson         }
2777d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 8, mmu_idx, ra);
277859213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
277959213461SRichard Henderson         /* nothing */
278059213461SRichard Henderson     } else {
278159213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
278259213461SRichard Henderson         if (memop & MO_BSWAP) {
278359213461SRichard Henderson             val = bswap64(val);
278459213461SRichard Henderson         }
278573fda56fSAnton Johansson         store_atom_8(cpu, ra, p->haddr, memop, val);
278659213461SRichard Henderson     }
2787eed56642SAlex Bennée }
2788eed56642SAlex Bennée 
2789e20f73fbSAnton Johansson static void do_st1_mmu(CPUState *cpu, vaddr addr, uint8_t val,
279059213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2791f83bcecbSRichard Henderson {
279259213461SRichard Henderson     MMULookupLocals l;
279359213461SRichard Henderson     bool crosspage;
279459213461SRichard Henderson 
2795f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2796e20f73fbSAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
279759213461SRichard Henderson     tcg_debug_assert(!crosspage);
279859213461SRichard Henderson 
2799e20f73fbSAnton Johansson     do_st_1(cpu, &l.page[0], val, l.mmu_idx, ra);
2800e20f73fbSAnton Johansson }
2801e20f73fbSAnton Johansson 
2802d50ef446SAnton Johansson static void do_st2_mmu(CPUState *cpu, vaddr addr, uint16_t val,
280359213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2804f83bcecbSRichard Henderson {
280559213461SRichard Henderson     MMULookupLocals l;
280659213461SRichard Henderson     bool crosspage;
280759213461SRichard Henderson     uint8_t a, b;
280859213461SRichard Henderson 
2809f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2810d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
281159213461SRichard Henderson     if (likely(!crosspage)) {
2812d50ef446SAnton Johansson         do_st_2(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
281359213461SRichard Henderson         return;
281459213461SRichard Henderson     }
281559213461SRichard Henderson 
281659213461SRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
281759213461SRichard Henderson         a = val, b = val >> 8;
281859213461SRichard Henderson     } else {
281959213461SRichard Henderson         b = val, a = val >> 8;
282059213461SRichard Henderson     }
2821d50ef446SAnton Johansson     do_st_1(cpu, &l.page[0], a, l.mmu_idx, ra);
2822d50ef446SAnton Johansson     do_st_1(cpu, &l.page[1], b, l.mmu_idx, ra);
2823f83bcecbSRichard Henderson }
2824f83bcecbSRichard Henderson 
2825d50ef446SAnton Johansson static void do_st4_mmu(CPUState *cpu, vaddr addr, uint32_t val,
282659213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2827f83bcecbSRichard Henderson {
282859213461SRichard Henderson     MMULookupLocals l;
282959213461SRichard Henderson     bool crosspage;
283059213461SRichard Henderson 
2831f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2832d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
283359213461SRichard Henderson     if (likely(!crosspage)) {
2834d50ef446SAnton Johansson         do_st_4(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
283559213461SRichard Henderson         return;
283659213461SRichard Henderson     }
283759213461SRichard Henderson 
283859213461SRichard Henderson     /* Swap to little endian for simplicity, then store by bytes. */
283959213461SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
284059213461SRichard Henderson         val = bswap32(val);
284159213461SRichard Henderson     }
2842d50ef446SAnton Johansson     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2843d50ef446SAnton Johansson     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2844eed56642SAlex Bennée }
2845eed56642SAlex Bennée 
2846d50ef446SAnton Johansson static void do_st8_mmu(CPUState *cpu, vaddr addr, uint64_t val,
284759213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
284859213461SRichard Henderson {
284959213461SRichard Henderson     MMULookupLocals l;
285059213461SRichard Henderson     bool crosspage;
285159213461SRichard Henderson 
2852f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2853d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
285459213461SRichard Henderson     if (likely(!crosspage)) {
2855d50ef446SAnton Johansson         do_st_8(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
285659213461SRichard Henderson         return;
285759213461SRichard Henderson     }
285859213461SRichard Henderson 
285959213461SRichard Henderson     /* Swap to little endian for simplicity, then store by bytes. */
286059213461SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
286159213461SRichard Henderson         val = bswap64(val);
286259213461SRichard Henderson     }
2863d50ef446SAnton Johansson     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2864d50ef446SAnton Johansson     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2865eed56642SAlex Bennée }
2866eed56642SAlex Bennée 
2867d50ef446SAnton Johansson static void do_st16_mmu(CPUState *cpu, vaddr addr, Int128 val,
286835c653c4SRichard Henderson                         MemOpIdx oi, uintptr_t ra)
286935c653c4SRichard Henderson {
287035c653c4SRichard Henderson     MMULookupLocals l;
287135c653c4SRichard Henderson     bool crosspage;
287235c653c4SRichard Henderson     uint64_t a, b;
287335c653c4SRichard Henderson     int first;
287435c653c4SRichard Henderson 
2875f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2876d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
287735c653c4SRichard Henderson     if (likely(!crosspage)) {
2878f7eaf9d7SRichard Henderson         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2879f7eaf9d7SRichard Henderson             if ((l.memop & MO_BSWAP) != MO_LE) {
2880f7eaf9d7SRichard Henderson                 val = bswap128(val);
2881f7eaf9d7SRichard Henderson             }
2882d50ef446SAnton Johansson             do_st16_mmio_leN(cpu, l.page[0].full, val, addr, 16, l.mmu_idx, ra);
2883f7eaf9d7SRichard Henderson         } else if (unlikely(l.page[0].flags & TLB_DISCARD_WRITE)) {
2884f7eaf9d7SRichard Henderson             /* nothing */
2885f7eaf9d7SRichard Henderson         } else {
288635c653c4SRichard Henderson             /* Swap to host endian if necessary, then store. */
288735c653c4SRichard Henderson             if (l.memop & MO_BSWAP) {
288835c653c4SRichard Henderson                 val = bswap128(val);
288935c653c4SRichard Henderson             }
289073fda56fSAnton Johansson             store_atom_16(cpu, ra, l.page[0].haddr, l.memop, val);
289135c653c4SRichard Henderson         }
289235c653c4SRichard Henderson         return;
289335c653c4SRichard Henderson     }
289435c653c4SRichard Henderson 
289535c653c4SRichard Henderson     first = l.page[0].size;
289635c653c4SRichard Henderson     if (first == 8) {
289735c653c4SRichard Henderson         MemOp mop8 = (l.memop & ~(MO_SIZE | MO_BSWAP)) | MO_64;
289835c653c4SRichard Henderson 
289935c653c4SRichard Henderson         if (l.memop & MO_BSWAP) {
290035c653c4SRichard Henderson             val = bswap128(val);
290135c653c4SRichard Henderson         }
290235c653c4SRichard Henderson         if (HOST_BIG_ENDIAN) {
290335c653c4SRichard Henderson             b = int128_getlo(val), a = int128_gethi(val);
290435c653c4SRichard Henderson         } else {
290535c653c4SRichard Henderson             a = int128_getlo(val), b = int128_gethi(val);
290635c653c4SRichard Henderson         }
2907d50ef446SAnton Johansson         do_st_8(cpu, &l.page[0], a, l.mmu_idx, mop8, ra);
2908d50ef446SAnton Johansson         do_st_8(cpu, &l.page[1], b, l.mmu_idx, mop8, ra);
290935c653c4SRichard Henderson         return;
291035c653c4SRichard Henderson     }
291135c653c4SRichard Henderson 
291235c653c4SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
291335c653c4SRichard Henderson         val = bswap128(val);
291435c653c4SRichard Henderson     }
291535c653c4SRichard Henderson     if (first < 8) {
2916d50ef446SAnton Johansson         do_st_leN(cpu, &l.page[0], int128_getlo(val), l.mmu_idx, l.memop, ra);
291735c653c4SRichard Henderson         val = int128_urshift(val, first * 8);
2918d50ef446SAnton Johansson         do_st16_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
291935c653c4SRichard Henderson     } else {
2920d50ef446SAnton Johansson         b = do_st16_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2921d50ef446SAnton Johansson         do_st_leN(cpu, &l.page[1], b, l.mmu_idx, l.memop, ra);
292235c653c4SRichard Henderson     }
292335c653c4SRichard Henderson }
292435c653c4SRichard Henderson 
2925f83bcecbSRichard Henderson #include "ldst_common.c.inc"
2926cfe04a4bSRichard Henderson 
2927be9568b4SRichard Henderson /*
2928be9568b4SRichard Henderson  * First set of functions passes in OI and RETADDR.
2929be9568b4SRichard Henderson  * This makes them callable from other helpers.
2930be9568b4SRichard Henderson  */
2931d9bb58e5SYang Zhong 
2932d9bb58e5SYang Zhong #define ATOMIC_NAME(X) \
2933be9568b4SRichard Henderson     glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
2934a754f7f3SRichard Henderson 
2935707526adSRichard Henderson #define ATOMIC_MMU_CLEANUP
2936d9bb58e5SYang Zhong 
2937139c1837SPaolo Bonzini #include "atomic_common.c.inc"
2938d9bb58e5SYang Zhong 
2939d9bb58e5SYang Zhong #define DATA_SIZE 1
2940d9bb58e5SYang Zhong #include "atomic_template.h"
2941d9bb58e5SYang Zhong 
2942d9bb58e5SYang Zhong #define DATA_SIZE 2
2943d9bb58e5SYang Zhong #include "atomic_template.h"
2944d9bb58e5SYang Zhong 
2945d9bb58e5SYang Zhong #define DATA_SIZE 4
2946d9bb58e5SYang Zhong #include "atomic_template.h"
2947d9bb58e5SYang Zhong 
2948d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
2949d9bb58e5SYang Zhong #define DATA_SIZE 8
2950d9bb58e5SYang Zhong #include "atomic_template.h"
2951d9bb58e5SYang Zhong #endif
2952d9bb58e5SYang Zhong 
295376f9d6adSRichard Henderson #if defined(CONFIG_ATOMIC128) || HAVE_CMPXCHG128
2954d9bb58e5SYang Zhong #define DATA_SIZE 16
2955d9bb58e5SYang Zhong #include "atomic_template.h"
2956d9bb58e5SYang Zhong #endif
2957d9bb58e5SYang Zhong 
2958d9bb58e5SYang Zhong /* Code access functions.  */
2959d9bb58e5SYang Zhong 
2960fc4120a3SRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr)
2961eed56642SAlex Bennée {
29629002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(env, true));
2963d50ef446SAnton Johansson     return do_ld1_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
29644cef72d0SAlex Bennée }
29654cef72d0SAlex Bennée 
2966fc4120a3SRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr)
29672dd92606SRichard Henderson {
29689002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(env, true));
2969d50ef446SAnton Johansson     return do_ld2_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
29702dd92606SRichard Henderson }
29712dd92606SRichard Henderson 
2972fc4120a3SRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr)
29734cef72d0SAlex Bennée {
29749002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(env, true));
2975d50ef446SAnton Johansson     return do_ld4_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
2976eed56642SAlex Bennée }
2977d9bb58e5SYang Zhong 
2978fc4120a3SRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr)
2979eed56642SAlex Bennée {
2980fc313c64SFrédéric Pétrot     MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(env, true));
2981d50ef446SAnton Johansson     return do_ld8_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
2982eed56642SAlex Bennée }
298328990626SRichard Henderson 
298428990626SRichard Henderson uint8_t cpu_ldb_code_mmu(CPUArchState *env, abi_ptr addr,
298528990626SRichard Henderson                          MemOpIdx oi, uintptr_t retaddr)
298628990626SRichard Henderson {
2987d50ef446SAnton Johansson     return do_ld1_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
298828990626SRichard Henderson }
298928990626SRichard Henderson 
299028990626SRichard Henderson uint16_t cpu_ldw_code_mmu(CPUArchState *env, abi_ptr addr,
299128990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
299228990626SRichard Henderson {
2993d50ef446SAnton Johansson     return do_ld2_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
299428990626SRichard Henderson }
299528990626SRichard Henderson 
299628990626SRichard Henderson uint32_t cpu_ldl_code_mmu(CPUArchState *env, abi_ptr addr,
299728990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
299828990626SRichard Henderson {
2999d50ef446SAnton Johansson     return do_ld4_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
300028990626SRichard Henderson }
300128990626SRichard Henderson 
300228990626SRichard Henderson uint64_t cpu_ldq_code_mmu(CPUArchState *env, abi_ptr addr,
300328990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
300428990626SRichard Henderson {
3005d50ef446SAnton Johansson     return do_ld8_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
300628990626SRichard Henderson }
3007