xref: /openbmc/qemu/accel/tcg/cputlb.c (revision f4f826c0e0c189869ef55e540a5dcbd90fe392bb)
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"
27*f4f826c0SPhilippe 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)) {
1482af803a4fSRichard Henderson         notdirty_write(env_cpu(env), addr, 1, *pfull, retaddr);
1483069cfe77SRichard Henderson         flags &= ~TLB_NOTDIRTY;
1484069cfe77SRichard Henderson     }
1485069cfe77SRichard Henderson 
1486069cfe77SRichard Henderson     return flags;
1487069cfe77SRichard Henderson }
1488069cfe77SRichard Henderson 
14896d03226bSAlex Bennée int probe_access_full_mmu(CPUArchState *env, vaddr addr, int size,
14906d03226bSAlex Bennée                           MMUAccessType access_type, int mmu_idx,
14916d03226bSAlex Bennée                           void **phost, CPUTLBEntryFull **pfull)
14926d03226bSAlex Bennée {
14936d03226bSAlex Bennée     void *discard_phost;
14946d03226bSAlex Bennée     CPUTLBEntryFull *discard_tlb;
14956d03226bSAlex Bennée 
14966d03226bSAlex Bennée     /* privately handle users that don't need full results */
14976d03226bSAlex Bennée     phost = phost ? phost : &discard_phost;
14986d03226bSAlex Bennée     pfull = pfull ? pfull : &discard_tlb;
14996d03226bSAlex Bennée 
15005afec1c6SAnton Johansson     int flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15015afec1c6SAnton Johansson                                       mmu_idx, true, phost, pfull, 0, false);
15026d03226bSAlex Bennée 
15036d03226bSAlex Bennée     /* Handle clean RAM pages.  */
15046d03226bSAlex Bennée     if (unlikely(flags & TLB_NOTDIRTY)) {
15056d03226bSAlex Bennée         notdirty_write(env_cpu(env), addr, 1, *pfull, 0);
15066d03226bSAlex Bennée         flags &= ~TLB_NOTDIRTY;
15076d03226bSAlex Bennée     }
15086d03226bSAlex Bennée 
15096d03226bSAlex Bennée     return flags;
15106d03226bSAlex Bennée }
15116d03226bSAlex Bennée 
15124f8f4127SAnton Johansson int probe_access_flags(CPUArchState *env, vaddr addr, int size,
1513af803a4fSRichard Henderson                        MMUAccessType access_type, int mmu_idx,
1514af803a4fSRichard Henderson                        bool nonfault, void **phost, uintptr_t retaddr)
1515af803a4fSRichard Henderson {
1516af803a4fSRichard Henderson     CPUTLBEntryFull *full;
15171770b2f2SDaniel Henrique Barboza     int flags;
1518af803a4fSRichard Henderson 
15191770b2f2SDaniel Henrique Barboza     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
15201770b2f2SDaniel Henrique Barboza 
15215afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15225afec1c6SAnton Johansson                                   mmu_idx, nonfault, phost, &full, retaddr,
15235afec1c6SAnton Johansson                                   true);
15241770b2f2SDaniel Henrique Barboza 
15251770b2f2SDaniel Henrique Barboza     /* Handle clean RAM pages. */
15261770b2f2SDaniel Henrique Barboza     if (unlikely(flags & TLB_NOTDIRTY)) {
15271770b2f2SDaniel Henrique Barboza         notdirty_write(env_cpu(env), addr, 1, full, retaddr);
15281770b2f2SDaniel Henrique Barboza         flags &= ~TLB_NOTDIRTY;
15291770b2f2SDaniel Henrique Barboza     }
15301770b2f2SDaniel Henrique Barboza 
15311770b2f2SDaniel Henrique Barboza     return flags;
1532af803a4fSRichard Henderson }
1533af803a4fSRichard Henderson 
15344f8f4127SAnton Johansson void *probe_access(CPUArchState *env, vaddr addr, int size,
1535069cfe77SRichard Henderson                    MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1536069cfe77SRichard Henderson {
1537af803a4fSRichard Henderson     CPUTLBEntryFull *full;
1538069cfe77SRichard Henderson     void *host;
1539069cfe77SRichard Henderson     int flags;
1540069cfe77SRichard Henderson 
1541069cfe77SRichard Henderson     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
1542069cfe77SRichard Henderson 
15435afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
15445afec1c6SAnton Johansson                                   mmu_idx, false, &host, &full, retaddr,
15455afec1c6SAnton Johansson                                   true);
1546069cfe77SRichard Henderson 
1547069cfe77SRichard Henderson     /* Per the interface, size == 0 merely faults the access. */
1548069cfe77SRichard Henderson     if (size == 0) {
154973bc0bd4SRichard Henderson         return NULL;
155073bc0bd4SRichard Henderson     }
155173bc0bd4SRichard Henderson 
1552069cfe77SRichard Henderson     if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) {
155303a98189SDavid Hildenbrand         /* Handle watchpoints.  */
1554069cfe77SRichard Henderson         if (flags & TLB_WATCHPOINT) {
1555069cfe77SRichard Henderson             int wp_access = (access_type == MMU_DATA_STORE
1556069cfe77SRichard Henderson                              ? BP_MEM_WRITE : BP_MEM_READ);
155703a98189SDavid Hildenbrand             cpu_check_watchpoint(env_cpu(env), addr, size,
155825d3ec58SRichard Henderson                                  full->attrs, wp_access, retaddr);
1559d9bb58e5SYang Zhong         }
1560fef39ccdSDavid Hildenbrand 
156173bc0bd4SRichard Henderson         /* Handle clean RAM pages.  */
1562069cfe77SRichard Henderson         if (flags & TLB_NOTDIRTY) {
156325d3ec58SRichard Henderson             notdirty_write(env_cpu(env), addr, 1, full, retaddr);
156473bc0bd4SRichard Henderson         }
1565fef39ccdSDavid Hildenbrand     }
1566fef39ccdSDavid Hildenbrand 
1567069cfe77SRichard Henderson     return host;
1568d9bb58e5SYang Zhong }
1569d9bb58e5SYang Zhong 
15704811e909SRichard Henderson void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
15714811e909SRichard Henderson                         MMUAccessType access_type, int mmu_idx)
15724811e909SRichard Henderson {
1573af803a4fSRichard Henderson     CPUTLBEntryFull *full;
1574069cfe77SRichard Henderson     void *host;
1575069cfe77SRichard Henderson     int flags;
15764811e909SRichard Henderson 
15775afec1c6SAnton Johansson     flags = probe_access_internal(env_cpu(env), addr, 0, access_type,
15786d03226bSAlex Bennée                                   mmu_idx, true, &host, &full, 0, false);
1579069cfe77SRichard Henderson 
1580069cfe77SRichard Henderson     /* No combination of flags are expected by the caller. */
1581069cfe77SRichard Henderson     return flags ? NULL : host;
15824811e909SRichard Henderson }
15834811e909SRichard Henderson 
15847e0d9973SRichard Henderson /*
15857e0d9973SRichard Henderson  * Return a ram_addr_t for the virtual address for execution.
15867e0d9973SRichard Henderson  *
15877e0d9973SRichard Henderson  * Return -1 if we can't translate and execute from an entire page
15887e0d9973SRichard Henderson  * of RAM.  This will force us to execute by loading and translating
15897e0d9973SRichard Henderson  * one insn at a time, without caching.
15907e0d9973SRichard Henderson  *
15917e0d9973SRichard Henderson  * NOTE: This function will trigger an exception if the page is
15927e0d9973SRichard Henderson  * not executable.
15937e0d9973SRichard Henderson  */
15944f8f4127SAnton Johansson tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, vaddr addr,
15957e0d9973SRichard Henderson                                         void **hostp)
15967e0d9973SRichard Henderson {
1597af803a4fSRichard Henderson     CPUTLBEntryFull *full;
15987e0d9973SRichard Henderson     void *p;
15997e0d9973SRichard Henderson 
16005afec1c6SAnton Johansson     (void)probe_access_internal(env_cpu(env), addr, 1, MMU_INST_FETCH,
16016d03226bSAlex Bennée                                 cpu_mmu_index(env, true), false,
16026d03226bSAlex Bennée                                 &p, &full, 0, false);
16037e0d9973SRichard Henderson     if (p == NULL) {
16047e0d9973SRichard Henderson         return -1;
16057e0d9973SRichard Henderson     }
1606ac01ec6fSWeiwei Li 
1607ac01ec6fSWeiwei Li     if (full->lg_page_size < TARGET_PAGE_BITS) {
1608ac01ec6fSWeiwei Li         return -1;
1609ac01ec6fSWeiwei Li     }
1610ac01ec6fSWeiwei Li 
16117e0d9973SRichard Henderson     if (hostp) {
16127e0d9973SRichard Henderson         *hostp = p;
16137e0d9973SRichard Henderson     }
16147e0d9973SRichard Henderson     return qemu_ram_addr_from_host_nofail(p);
16157e0d9973SRichard Henderson }
16167e0d9973SRichard Henderson 
1617cdfac37bSRichard Henderson /* Load/store with atomicity primitives. */
1618cdfac37bSRichard Henderson #include "ldst_atomicity.c.inc"
1619cdfac37bSRichard Henderson 
1620235537faSAlex Bennée #ifdef CONFIG_PLUGIN
1621235537faSAlex Bennée /*
1622235537faSAlex Bennée  * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1623235537faSAlex Bennée  * This should be a hot path as we will have just looked this path up
1624235537faSAlex Bennée  * in the softmmu lookup code (or helper). We don't handle re-fills or
1625235537faSAlex Bennée  * checking the victim table. This is purely informational.
1626235537faSAlex Bennée  *
1627da6aef48SRichard Henderson  * The one corner case is i/o write, which can cause changes to the
1628da6aef48SRichard Henderson  * address space.  Those changes, and the corresponding tlb flush,
1629da6aef48SRichard Henderson  * should be delayed until the next TB, so even then this ought not fail.
1630da6aef48SRichard Henderson  * But check, Just in Case.
1631235537faSAlex Bennée  */
1632732d5487SAnton Johansson bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,
1633235537faSAlex Bennée                        bool is_store, struct qemu_plugin_hwaddr *data)
1634235537faSAlex Bennée {
163510b32e2cSAnton Johansson     CPUTLBEntry *tlbe = tlb_entry(cpu, mmu_idx, addr);
163610b32e2cSAnton Johansson     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1637da6aef48SRichard Henderson     MMUAccessType access_type = is_store ? MMU_DATA_STORE : MMU_DATA_LOAD;
1638da6aef48SRichard Henderson     uint64_t tlb_addr = tlb_read_idx(tlbe, access_type);
1639405c02d8SRichard Henderson     CPUTLBEntryFull *full;
1640235537faSAlex Bennée 
1641da6aef48SRichard Henderson     if (unlikely(!tlb_hit(tlb_addr, addr))) {
1642da6aef48SRichard Henderson         return false;
1643da6aef48SRichard Henderson     }
1644da6aef48SRichard Henderson 
164510b32e2cSAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1646405c02d8SRichard Henderson     data->phys_addr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
1647405c02d8SRichard Henderson 
1648235537faSAlex Bennée     /* We must have an iotlb entry for MMIO */
1649235537faSAlex Bennée     if (tlb_addr & TLB_MMIO) {
1650405c02d8SRichard Henderson         MemoryRegionSection *section =
1651405c02d8SRichard Henderson             iotlb_to_section(cpu, full->xlat_section & ~TARGET_PAGE_MASK,
1652405c02d8SRichard Henderson                              full->attrs);
1653235537faSAlex Bennée         data->is_io = true;
1654405c02d8SRichard Henderson         data->mr = section->mr;
1655235537faSAlex Bennée     } else {
1656235537faSAlex Bennée         data->is_io = false;
1657405c02d8SRichard Henderson         data->mr = NULL;
1658235537faSAlex Bennée     }
1659235537faSAlex Bennée     return true;
1660235537faSAlex Bennée }
1661235537faSAlex Bennée #endif
1662235537faSAlex Bennée 
166308dff435SRichard Henderson /*
16648cfdacaaSRichard Henderson  * Probe for a load/store operation.
16658cfdacaaSRichard Henderson  * Return the host address and into @flags.
16668cfdacaaSRichard Henderson  */
16678cfdacaaSRichard Henderson 
16688cfdacaaSRichard Henderson typedef struct MMULookupPageData {
16698cfdacaaSRichard Henderson     CPUTLBEntryFull *full;
16708cfdacaaSRichard Henderson     void *haddr;
1671fb2c53cbSAnton Johansson     vaddr addr;
16728cfdacaaSRichard Henderson     int flags;
16738cfdacaaSRichard Henderson     int size;
16748cfdacaaSRichard Henderson } MMULookupPageData;
16758cfdacaaSRichard Henderson 
16768cfdacaaSRichard Henderson typedef struct MMULookupLocals {
16778cfdacaaSRichard Henderson     MMULookupPageData page[2];
16788cfdacaaSRichard Henderson     MemOp memop;
16798cfdacaaSRichard Henderson     int mmu_idx;
16808cfdacaaSRichard Henderson } MMULookupLocals;
16818cfdacaaSRichard Henderson 
16828cfdacaaSRichard Henderson /**
16838cfdacaaSRichard Henderson  * mmu_lookup1: translate one page
1684d50ef446SAnton Johansson  * @cpu: generic cpu state
16858cfdacaaSRichard Henderson  * @data: lookup parameters
16868cfdacaaSRichard Henderson  * @mmu_idx: virtual address context
16878cfdacaaSRichard Henderson  * @access_type: load/store/code
16888cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
16898cfdacaaSRichard Henderson  *
16908cfdacaaSRichard Henderson  * Resolve the translation for the one page at @data.addr, filling in
16918cfdacaaSRichard Henderson  * the rest of @data with the results.  If the translation fails,
16928cfdacaaSRichard Henderson  * tlb_fill will longjmp out.  Return true if the softmmu tlb for
16938cfdacaaSRichard Henderson  * @mmu_idx may have resized.
16948cfdacaaSRichard Henderson  */
1695d50ef446SAnton Johansson static bool mmu_lookup1(CPUState *cpu, MMULookupPageData *data,
16968cfdacaaSRichard Henderson                         int mmu_idx, MMUAccessType access_type, uintptr_t ra)
16978cfdacaaSRichard Henderson {
1698fb2c53cbSAnton Johansson     vaddr addr = data->addr;
1699d50ef446SAnton Johansson     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1700d50ef446SAnton Johansson     CPUTLBEntry *entry = tlb_entry(cpu, mmu_idx, addr);
17019e39de98SAnton Johansson     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
17028cfdacaaSRichard Henderson     bool maybe_resized = false;
170358e8f1f6SRichard Henderson     CPUTLBEntryFull *full;
170458e8f1f6SRichard Henderson     int flags;
17058cfdacaaSRichard Henderson 
17068cfdacaaSRichard Henderson     /* If the TLB entry is for a different page, reload and try again.  */
17078cfdacaaSRichard Henderson     if (!tlb_hit(tlb_addr, addr)) {
1708d50ef446SAnton Johansson         if (!victim_tlb_hit(cpu, mmu_idx, index, access_type,
17098cfdacaaSRichard Henderson                             addr & TARGET_PAGE_MASK)) {
1710d50ef446SAnton Johansson             tlb_fill(cpu, addr, data->size, access_type, mmu_idx, ra);
17118cfdacaaSRichard Henderson             maybe_resized = true;
1712d50ef446SAnton Johansson             index = tlb_index(cpu, mmu_idx, addr);
1713d50ef446SAnton Johansson             entry = tlb_entry(cpu, mmu_idx, addr);
17148cfdacaaSRichard Henderson         }
17158cfdacaaSRichard Henderson         tlb_addr = tlb_read_idx(entry, access_type) & ~TLB_INVALID_MASK;
17168cfdacaaSRichard Henderson     }
17178cfdacaaSRichard Henderson 
1718d50ef446SAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
171958e8f1f6SRichard Henderson     flags = tlb_addr & (TLB_FLAGS_MASK & ~TLB_FORCE_SLOW);
172058e8f1f6SRichard Henderson     flags |= full->slow_flags[access_type];
172158e8f1f6SRichard Henderson 
172258e8f1f6SRichard Henderson     data->full = full;
172358e8f1f6SRichard Henderson     data->flags = flags;
17248cfdacaaSRichard Henderson     /* Compute haddr speculatively; depending on flags it might be invalid. */
17258cfdacaaSRichard Henderson     data->haddr = (void *)((uintptr_t)addr + entry->addend);
17268cfdacaaSRichard Henderson 
17278cfdacaaSRichard Henderson     return maybe_resized;
17288cfdacaaSRichard Henderson }
17298cfdacaaSRichard Henderson 
17308cfdacaaSRichard Henderson /**
17318cfdacaaSRichard Henderson  * mmu_watch_or_dirty
1732d50ef446SAnton Johansson  * @cpu: generic cpu state
17338cfdacaaSRichard Henderson  * @data: lookup parameters
17348cfdacaaSRichard Henderson  * @access_type: load/store/code
17358cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
17368cfdacaaSRichard Henderson  *
17378cfdacaaSRichard Henderson  * Trigger watchpoints for @data.addr:@data.size;
17388cfdacaaSRichard Henderson  * record writes to protected clean pages.
17398cfdacaaSRichard Henderson  */
1740d50ef446SAnton Johansson static void mmu_watch_or_dirty(CPUState *cpu, MMULookupPageData *data,
17418cfdacaaSRichard Henderson                                MMUAccessType access_type, uintptr_t ra)
17428cfdacaaSRichard Henderson {
17438cfdacaaSRichard Henderson     CPUTLBEntryFull *full = data->full;
1744fb2c53cbSAnton Johansson     vaddr addr = data->addr;
17458cfdacaaSRichard Henderson     int flags = data->flags;
17468cfdacaaSRichard Henderson     int size = data->size;
17478cfdacaaSRichard Henderson 
17488cfdacaaSRichard Henderson     /* On watchpoint hit, this will longjmp out.  */
17498cfdacaaSRichard Henderson     if (flags & TLB_WATCHPOINT) {
17508cfdacaaSRichard Henderson         int wp = access_type == MMU_DATA_STORE ? BP_MEM_WRITE : BP_MEM_READ;
1751d50ef446SAnton Johansson         cpu_check_watchpoint(cpu, addr, size, full->attrs, wp, ra);
17528cfdacaaSRichard Henderson         flags &= ~TLB_WATCHPOINT;
17538cfdacaaSRichard Henderson     }
17548cfdacaaSRichard Henderson 
17558cfdacaaSRichard Henderson     /* Note that notdirty is only set for writes. */
17568cfdacaaSRichard Henderson     if (flags & TLB_NOTDIRTY) {
1757d50ef446SAnton Johansson         notdirty_write(cpu, addr, size, full, ra);
17588cfdacaaSRichard Henderson         flags &= ~TLB_NOTDIRTY;
17598cfdacaaSRichard Henderson     }
17608cfdacaaSRichard Henderson     data->flags = flags;
17618cfdacaaSRichard Henderson }
17628cfdacaaSRichard Henderson 
17638cfdacaaSRichard Henderson /**
17648cfdacaaSRichard Henderson  * mmu_lookup: translate page(s)
1765d50ef446SAnton Johansson  * @cpu: generic cpu state
17668cfdacaaSRichard Henderson  * @addr: virtual address
17678cfdacaaSRichard Henderson  * @oi: combined mmu_idx and MemOp
17688cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
17698cfdacaaSRichard Henderson  * @access_type: load/store/code
17708cfdacaaSRichard Henderson  * @l: output result
17718cfdacaaSRichard Henderson  *
17728cfdacaaSRichard Henderson  * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
17738cfdacaaSRichard Henderson  * bytes.  Return true if the lookup crosses a page boundary.
17748cfdacaaSRichard Henderson  */
1775d50ef446SAnton Johansson static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
17768cfdacaaSRichard Henderson                        uintptr_t ra, MMUAccessType type, MMULookupLocals *l)
17778cfdacaaSRichard Henderson {
17788cfdacaaSRichard Henderson     unsigned a_bits;
17798cfdacaaSRichard Henderson     bool crosspage;
17808cfdacaaSRichard Henderson     int flags;
17818cfdacaaSRichard Henderson 
17828cfdacaaSRichard Henderson     l->memop = get_memop(oi);
17838cfdacaaSRichard Henderson     l->mmu_idx = get_mmuidx(oi);
17848cfdacaaSRichard Henderson 
17858cfdacaaSRichard Henderson     tcg_debug_assert(l->mmu_idx < NB_MMU_MODES);
17868cfdacaaSRichard Henderson 
17878cfdacaaSRichard Henderson     /* Handle CPU specific unaligned behaviour */
17888cfdacaaSRichard Henderson     a_bits = get_alignment_bits(l->memop);
17898cfdacaaSRichard Henderson     if (addr & ((1 << a_bits) - 1)) {
1790d50ef446SAnton Johansson         cpu_unaligned_access(cpu, addr, type, l->mmu_idx, ra);
17918cfdacaaSRichard Henderson     }
17928cfdacaaSRichard Henderson 
17938cfdacaaSRichard Henderson     l->page[0].addr = addr;
17948cfdacaaSRichard Henderson     l->page[0].size = memop_size(l->memop);
17958cfdacaaSRichard Henderson     l->page[1].addr = (addr + l->page[0].size - 1) & TARGET_PAGE_MASK;
17968cfdacaaSRichard Henderson     l->page[1].size = 0;
17978cfdacaaSRichard Henderson     crosspage = (addr ^ l->page[1].addr) & TARGET_PAGE_MASK;
17988cfdacaaSRichard Henderson 
17998cfdacaaSRichard Henderson     if (likely(!crosspage)) {
1800d50ef446SAnton Johansson         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
18018cfdacaaSRichard Henderson 
18028cfdacaaSRichard Henderson         flags = l->page[0].flags;
18038cfdacaaSRichard Henderson         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1804d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
18058cfdacaaSRichard Henderson         }
18068cfdacaaSRichard Henderson         if (unlikely(flags & TLB_BSWAP)) {
18078cfdacaaSRichard Henderson             l->memop ^= MO_BSWAP;
18088cfdacaaSRichard Henderson         }
18098cfdacaaSRichard Henderson     } else {
18108cfdacaaSRichard Henderson         /* Finish compute of page crossing. */
18118cfdacaaSRichard Henderson         int size0 = l->page[1].addr - addr;
18128cfdacaaSRichard Henderson         l->page[1].size = l->page[0].size - size0;
18138cfdacaaSRichard Henderson         l->page[0].size = size0;
18148cfdacaaSRichard Henderson 
18158cfdacaaSRichard Henderson         /*
18168cfdacaaSRichard Henderson          * Lookup both pages, recognizing exceptions from either.  If the
18178cfdacaaSRichard Henderson          * second lookup potentially resized, refresh first CPUTLBEntryFull.
18188cfdacaaSRichard Henderson          */
1819d50ef446SAnton Johansson         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
1820d50ef446SAnton Johansson         if (mmu_lookup1(cpu, &l->page[1], l->mmu_idx, type, ra)) {
1821d50ef446SAnton Johansson             uintptr_t index = tlb_index(cpu, l->mmu_idx, addr);
1822d50ef446SAnton Johansson             l->page[0].full = &cpu->neg.tlb.d[l->mmu_idx].fulltlb[index];
18238cfdacaaSRichard Henderson         }
18248cfdacaaSRichard Henderson 
18258cfdacaaSRichard Henderson         flags = l->page[0].flags | l->page[1].flags;
18268cfdacaaSRichard Henderson         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1827d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
1828d50ef446SAnton Johansson             mmu_watch_or_dirty(cpu, &l->page[1], type, ra);
18298cfdacaaSRichard Henderson         }
18308cfdacaaSRichard Henderson 
18318cfdacaaSRichard Henderson         /*
18328cfdacaaSRichard Henderson          * Since target/sparc is the only user of TLB_BSWAP, and all
18338cfdacaaSRichard Henderson          * Sparc accesses are aligned, any treatment across two pages
18348cfdacaaSRichard Henderson          * would be arbitrary.  Refuse it until there's a use.
18358cfdacaaSRichard Henderson          */
18368cfdacaaSRichard Henderson         tcg_debug_assert((flags & TLB_BSWAP) == 0);
18378cfdacaaSRichard Henderson     }
18388cfdacaaSRichard Henderson 
18398cfdacaaSRichard Henderson     return crosspage;
18408cfdacaaSRichard Henderson }
18418cfdacaaSRichard Henderson 
18428cfdacaaSRichard Henderson /*
184308dff435SRichard Henderson  * Probe for an atomic operation.  Do not allow unaligned operations,
184408dff435SRichard Henderson  * or io operations to proceed.  Return the host address.
184508dff435SRichard Henderson  */
1846d560225fSAnton Johansson static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
1847b0326eb9SAnton Johansson                                int size, uintptr_t retaddr)
1848d9bb58e5SYang Zhong {
1849b826044fSRichard Henderson     uintptr_t mmu_idx = get_mmuidx(oi);
185014776ab5STony Nguyen     MemOp mop = get_memop(oi);
1851d9bb58e5SYang Zhong     int a_bits = get_alignment_bits(mop);
185208dff435SRichard Henderson     uintptr_t index;
185308dff435SRichard Henderson     CPUTLBEntry *tlbe;
1854b0326eb9SAnton Johansson     vaddr tlb_addr;
185534d49937SPeter Maydell     void *hostaddr;
1856417aeaffSRichard Henderson     CPUTLBEntryFull *full;
1857d9bb58e5SYang Zhong 
1858b826044fSRichard Henderson     tcg_debug_assert(mmu_idx < NB_MMU_MODES);
1859b826044fSRichard Henderson 
1860d9bb58e5SYang Zhong     /* Adjust the given return address.  */
1861d9bb58e5SYang Zhong     retaddr -= GETPC_ADJ;
1862d9bb58e5SYang Zhong 
1863d9bb58e5SYang Zhong     /* Enforce guest required alignment.  */
1864d9bb58e5SYang Zhong     if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) {
1865d9bb58e5SYang Zhong         /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1866d560225fSAnton Johansson         cpu_unaligned_access(cpu, addr, MMU_DATA_STORE,
1867d9bb58e5SYang Zhong                              mmu_idx, retaddr);
1868d9bb58e5SYang Zhong     }
1869d9bb58e5SYang Zhong 
1870d9bb58e5SYang Zhong     /* Enforce qemu required alignment.  */
187108dff435SRichard Henderson     if (unlikely(addr & (size - 1))) {
1872d9bb58e5SYang Zhong         /* We get here if guest alignment was not requested,
1873d9bb58e5SYang Zhong            or was not enforced by cpu_unaligned_access above.
1874d9bb58e5SYang Zhong            We might widen the access and emulate, but for now
1875d9bb58e5SYang Zhong            mark an exception and exit the cpu loop.  */
1876d9bb58e5SYang Zhong         goto stop_the_world;
1877d9bb58e5SYang Zhong     }
1878d9bb58e5SYang Zhong 
1879d560225fSAnton Johansson     index = tlb_index(cpu, mmu_idx, addr);
1880d560225fSAnton Johansson     tlbe = tlb_entry(cpu, mmu_idx, addr);
188108dff435SRichard Henderson 
1882d9bb58e5SYang Zhong     /* Check TLB entry and enforce page permissions.  */
188308dff435SRichard Henderson     tlb_addr = tlb_addr_write(tlbe);
1884334692bcSPeter Maydell     if (!tlb_hit(tlb_addr, addr)) {
1885d560225fSAnton Johansson         if (!victim_tlb_hit(cpu, mmu_idx, index, MMU_DATA_STORE,
18860b3c75adSRichard Henderson                             addr & TARGET_PAGE_MASK)) {
1887d560225fSAnton Johansson             tlb_fill(cpu, addr, size,
188808dff435SRichard Henderson                      MMU_DATA_STORE, mmu_idx, retaddr);
1889d560225fSAnton Johansson             index = tlb_index(cpu, mmu_idx, addr);
1890d560225fSAnton Johansson             tlbe = tlb_entry(cpu, mmu_idx, addr);
1891d9bb58e5SYang Zhong         }
1892403f290cSEmilio G. Cota         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
1893d9bb58e5SYang Zhong     }
1894d9bb58e5SYang Zhong 
1895417aeaffSRichard Henderson     /*
1896417aeaffSRichard Henderson      * Let the guest notice RMW on a write-only page.
1897417aeaffSRichard Henderson      * We have just verified that the page is writable.
1898417aeaffSRichard Henderson      * Subpage lookups may have left TLB_INVALID_MASK set,
1899417aeaffSRichard Henderson      * but addr_read will only be -1 if PAGE_READ was unset.
1900417aeaffSRichard Henderson      */
1901417aeaffSRichard Henderson     if (unlikely(tlbe->addr_read == -1)) {
1902d560225fSAnton Johansson         tlb_fill(cpu, addr, size, MMU_DATA_LOAD, mmu_idx, retaddr);
190308dff435SRichard Henderson         /*
1904417aeaffSRichard Henderson          * Since we don't support reads and writes to different
1905417aeaffSRichard Henderson          * addresses, and we do have the proper page loaded for
1906417aeaffSRichard Henderson          * write, this shouldn't ever return.  But just in case,
1907417aeaffSRichard Henderson          * handle via stop-the-world.
190808dff435SRichard Henderson          */
190908dff435SRichard Henderson         goto stop_the_world;
191008dff435SRichard Henderson     }
1911187ba694SRichard Henderson     /* Collect tlb flags for read. */
1912417aeaffSRichard Henderson     tlb_addr |= tlbe->addr_read;
191308dff435SRichard Henderson 
191455df6fcfSPeter Maydell     /* Notice an IO access or a needs-MMU-lookup access */
19150953674eSRichard Henderson     if (unlikely(tlb_addr & (TLB_MMIO | TLB_DISCARD_WRITE))) {
1916d9bb58e5SYang Zhong         /* There's really nothing that can be done to
1917d9bb58e5SYang Zhong            support this apart from stop-the-world.  */
1918d9bb58e5SYang Zhong         goto stop_the_world;
1919d9bb58e5SYang Zhong     }
1920d9bb58e5SYang Zhong 
192134d49937SPeter Maydell     hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
1922d560225fSAnton Johansson     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
192334d49937SPeter Maydell 
192434d49937SPeter Maydell     if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
1925d560225fSAnton Johansson         notdirty_write(cpu, addr, size, full, retaddr);
1926417aeaffSRichard Henderson     }
1927417aeaffSRichard Henderson 
1928187ba694SRichard Henderson     if (unlikely(tlb_addr & TLB_FORCE_SLOW)) {
1929187ba694SRichard Henderson         int wp_flags = 0;
1930187ba694SRichard Henderson 
1931187ba694SRichard Henderson         if (full->slow_flags[MMU_DATA_STORE] & TLB_WATCHPOINT) {
1932187ba694SRichard Henderson             wp_flags |= BP_MEM_WRITE;
1933187ba694SRichard Henderson         }
1934187ba694SRichard Henderson         if (full->slow_flags[MMU_DATA_LOAD] & TLB_WATCHPOINT) {
1935187ba694SRichard Henderson             wp_flags |= BP_MEM_READ;
1936187ba694SRichard Henderson         }
1937187ba694SRichard Henderson         if (wp_flags) {
1938d560225fSAnton Johansson             cpu_check_watchpoint(cpu, addr, size,
1939187ba694SRichard Henderson                                  full->attrs, wp_flags, retaddr);
1940187ba694SRichard Henderson         }
194134d49937SPeter Maydell     }
194234d49937SPeter Maydell 
194334d49937SPeter Maydell     return hostaddr;
1944d9bb58e5SYang Zhong 
1945d9bb58e5SYang Zhong  stop_the_world:
1946d560225fSAnton Johansson     cpu_loop_exit_atomic(cpu, retaddr);
1947d9bb58e5SYang Zhong }
1948d9bb58e5SYang Zhong 
1949eed56642SAlex Bennée /*
1950eed56642SAlex Bennée  * Load Helpers
1951eed56642SAlex Bennée  *
1952eed56642SAlex Bennée  * We support two different access types. SOFTMMU_CODE_ACCESS is
1953eed56642SAlex Bennée  * specifically for reading instructions from system memory. It is
1954eed56642SAlex Bennée  * called by the translation loop and in some helpers where the code
1955eed56642SAlex Bennée  * is disassembled. It shouldn't be called directly by guest code.
1956cdfac37bSRichard Henderson  *
1957eed56642SAlex Bennée  * For the benefit of TCG generated code, we want to avoid the
1958eed56642SAlex Bennée  * complication of ABI-specific return type promotion and always
1959eed56642SAlex Bennée  * return a value extended to the register size of the host. This is
1960eed56642SAlex Bennée  * tcg_target_long, except in the case of a 32-bit host and 64-bit
1961eed56642SAlex Bennée  * data, and for that we always have uint64_t.
1962eed56642SAlex Bennée  *
1963eed56642SAlex Bennée  * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1964eed56642SAlex Bennée  */
1965eed56642SAlex Bennée 
19668cfdacaaSRichard Henderson /**
19678cfdacaaSRichard Henderson  * do_ld_mmio_beN:
1968d50ef446SAnton Johansson  * @cpu: generic cpu state
19691966855eSRichard Henderson  * @full: page parameters
19708cfdacaaSRichard Henderson  * @ret_be: accumulated data
19711966855eSRichard Henderson  * @addr: virtual address
19721966855eSRichard Henderson  * @size: number of bytes
19738cfdacaaSRichard Henderson  * @mmu_idx: virtual address context
19748cfdacaaSRichard Henderson  * @ra: return address into tcg generated code, or 0
19751966855eSRichard Henderson  * Context: iothread lock held
19768cfdacaaSRichard Henderson  *
19771966855eSRichard Henderson  * Load @size bytes from @addr, which is memory-mapped i/o.
19788cfdacaaSRichard Henderson  * The bytes are concatenated in big-endian order with @ret_be.
19798cfdacaaSRichard Henderson  */
1980d50ef446SAnton Johansson static uint64_t int_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
19811966855eSRichard Henderson                                 uint64_t ret_be, vaddr addr, int size,
19828bf67267SRichard Henderson                                 int mmu_idx, MMUAccessType type, uintptr_t ra,
19838bf67267SRichard Henderson                                 MemoryRegion *mr, hwaddr mr_offset)
19842dd92606SRichard Henderson {
1985190aba80SRichard Henderson     do {
198613e61747SRichard Henderson         MemOp this_mop;
198713e61747SRichard Henderson         unsigned this_size;
198813e61747SRichard Henderson         uint64_t val;
198913e61747SRichard Henderson         MemTxResult r;
199013e61747SRichard Henderson 
1991190aba80SRichard Henderson         /* Read aligned pieces up to 8 bytes. */
199213e61747SRichard Henderson         this_mop = ctz32(size | (int)addr | 8);
199313e61747SRichard Henderson         this_size = 1 << this_mop;
199413e61747SRichard Henderson         this_mop |= MO_BE;
199513e61747SRichard Henderson 
19968bf67267SRichard Henderson         r = memory_region_dispatch_read(mr, mr_offset, &val,
19978bf67267SRichard Henderson                                         this_mop, full->attrs);
199813e61747SRichard Henderson         if (unlikely(r != MEMTX_OK)) {
1999d50ef446SAnton Johansson             io_failed(cpu, full, addr, this_size, type, mmu_idx, r, ra);
20008cfdacaaSRichard Henderson         }
200113e61747SRichard Henderson         if (this_size == 8) {
200213e61747SRichard Henderson             return val;
200313e61747SRichard Henderson         }
200413e61747SRichard Henderson 
200513e61747SRichard Henderson         ret_be = (ret_be << (this_size * 8)) | val;
200613e61747SRichard Henderson         addr += this_size;
200713e61747SRichard Henderson         mr_offset += this_size;
200813e61747SRichard Henderson         size -= this_size;
2009190aba80SRichard Henderson     } while (size);
201013e61747SRichard Henderson 
20118cfdacaaSRichard Henderson     return ret_be;
20128cfdacaaSRichard Henderson }
20138cfdacaaSRichard Henderson 
2014d50ef446SAnton Johansson static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
20158bf67267SRichard Henderson                                uint64_t ret_be, vaddr addr, int size,
20168bf67267SRichard Henderson                                int mmu_idx, MMUAccessType type, uintptr_t ra)
20178bf67267SRichard Henderson {
20188bf67267SRichard Henderson     MemoryRegionSection *section;
20198bf67267SRichard Henderson     MemoryRegion *mr;
20208bf67267SRichard Henderson     hwaddr mr_offset;
20218bf67267SRichard Henderson     MemTxAttrs attrs;
20228bf67267SRichard Henderson     uint64_t ret;
20238bf67267SRichard Henderson 
20248bf67267SRichard Henderson     tcg_debug_assert(size > 0 && size <= 8);
20258bf67267SRichard Henderson 
20268bf67267SRichard Henderson     attrs = full->attrs;
2027d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
20288bf67267SRichard Henderson     mr = section->mr;
20298bf67267SRichard Henderson 
20308bf67267SRichard Henderson     qemu_mutex_lock_iothread();
2031d50ef446SAnton Johansson     ret = int_ld_mmio_beN(cpu, full, ret_be, addr, size, mmu_idx,
20328bf67267SRichard Henderson                           type, ra, mr, mr_offset);
20338bf67267SRichard Henderson     qemu_mutex_unlock_iothread();
20348bf67267SRichard Henderson 
20358bf67267SRichard Henderson     return ret;
20368bf67267SRichard Henderson }
20378bf67267SRichard Henderson 
2038d50ef446SAnton Johansson static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
20398bf67267SRichard Henderson                                uint64_t ret_be, vaddr addr, int size,
20408bf67267SRichard Henderson                                int mmu_idx, uintptr_t ra)
20418bf67267SRichard Henderson {
20428bf67267SRichard Henderson     MemoryRegionSection *section;
20438bf67267SRichard Henderson     MemoryRegion *mr;
20448bf67267SRichard Henderson     hwaddr mr_offset;
20458bf67267SRichard Henderson     MemTxAttrs attrs;
20468bf67267SRichard Henderson     uint64_t a, b;
20478bf67267SRichard Henderson 
20488bf67267SRichard Henderson     tcg_debug_assert(size > 8 && size <= 16);
20498bf67267SRichard Henderson 
20508bf67267SRichard Henderson     attrs = full->attrs;
2051d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
20528bf67267SRichard Henderson     mr = section->mr;
20538bf67267SRichard Henderson 
20548bf67267SRichard Henderson     qemu_mutex_lock_iothread();
2055d50ef446SAnton Johansson     a = int_ld_mmio_beN(cpu, full, ret_be, addr, size - 8, mmu_idx,
20568bf67267SRichard Henderson                         MMU_DATA_LOAD, ra, mr, mr_offset);
2057d50ef446SAnton Johansson     b = int_ld_mmio_beN(cpu, full, ret_be, addr + size - 8, 8, mmu_idx,
20588bf67267SRichard Henderson                         MMU_DATA_LOAD, ra, mr, mr_offset + size - 8);
20598bf67267SRichard Henderson     qemu_mutex_unlock_iothread();
20608bf67267SRichard Henderson 
20618bf67267SRichard Henderson     return int128_make128(b, a);
20628bf67267SRichard Henderson }
20638bf67267SRichard Henderson 
20648cfdacaaSRichard Henderson /**
20658cfdacaaSRichard Henderson  * do_ld_bytes_beN
20668cfdacaaSRichard Henderson  * @p: translation parameters
20678cfdacaaSRichard Henderson  * @ret_be: accumulated data
20688cfdacaaSRichard Henderson  *
20698cfdacaaSRichard Henderson  * Load @p->size bytes from @p->haddr, which is RAM.
20708cfdacaaSRichard Henderson  * The bytes to concatenated in big-endian order with @ret_be.
20718cfdacaaSRichard Henderson  */
20728cfdacaaSRichard Henderson static uint64_t do_ld_bytes_beN(MMULookupPageData *p, uint64_t ret_be)
20738cfdacaaSRichard Henderson {
20748cfdacaaSRichard Henderson     uint8_t *haddr = p->haddr;
20758cfdacaaSRichard Henderson     int i, size = p->size;
20768cfdacaaSRichard Henderson 
20778cfdacaaSRichard Henderson     for (i = 0; i < size; i++) {
20788cfdacaaSRichard Henderson         ret_be = (ret_be << 8) | haddr[i];
20798cfdacaaSRichard Henderson     }
20808cfdacaaSRichard Henderson     return ret_be;
20818cfdacaaSRichard Henderson }
20828cfdacaaSRichard Henderson 
2083cdfac37bSRichard Henderson /**
2084cdfac37bSRichard Henderson  * do_ld_parts_beN
2085cdfac37bSRichard Henderson  * @p: translation parameters
2086cdfac37bSRichard Henderson  * @ret_be: accumulated data
2087cdfac37bSRichard Henderson  *
2088cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but atomically on each aligned part.
2089cdfac37bSRichard Henderson  */
2090cdfac37bSRichard Henderson static uint64_t do_ld_parts_beN(MMULookupPageData *p, uint64_t ret_be)
2091cdfac37bSRichard Henderson {
2092cdfac37bSRichard Henderson     void *haddr = p->haddr;
2093cdfac37bSRichard Henderson     int size = p->size;
2094cdfac37bSRichard Henderson 
2095cdfac37bSRichard Henderson     do {
2096cdfac37bSRichard Henderson         uint64_t x;
2097cdfac37bSRichard Henderson         int n;
2098cdfac37bSRichard Henderson 
2099cdfac37bSRichard Henderson         /*
2100cdfac37bSRichard Henderson          * Find minimum of alignment and size.
2101cdfac37bSRichard Henderson          * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2102cdfac37bSRichard Henderson          * would have only checked the low bits of addr|size once at the start,
2103cdfac37bSRichard Henderson          * but is just as easy.
2104cdfac37bSRichard Henderson          */
2105cdfac37bSRichard Henderson         switch (((uintptr_t)haddr | size) & 7) {
2106cdfac37bSRichard Henderson         case 4:
2107cdfac37bSRichard Henderson             x = cpu_to_be32(load_atomic4(haddr));
2108cdfac37bSRichard Henderson             ret_be = (ret_be << 32) | x;
2109cdfac37bSRichard Henderson             n = 4;
2110cdfac37bSRichard Henderson             break;
2111cdfac37bSRichard Henderson         case 2:
2112cdfac37bSRichard Henderson         case 6:
2113cdfac37bSRichard Henderson             x = cpu_to_be16(load_atomic2(haddr));
2114cdfac37bSRichard Henderson             ret_be = (ret_be << 16) | x;
2115cdfac37bSRichard Henderson             n = 2;
2116cdfac37bSRichard Henderson             break;
2117cdfac37bSRichard Henderson         default:
2118cdfac37bSRichard Henderson             x = *(uint8_t *)haddr;
2119cdfac37bSRichard Henderson             ret_be = (ret_be << 8) | x;
2120cdfac37bSRichard Henderson             n = 1;
2121cdfac37bSRichard Henderson             break;
2122cdfac37bSRichard Henderson         case 0:
2123cdfac37bSRichard Henderson             g_assert_not_reached();
2124cdfac37bSRichard Henderson         }
2125cdfac37bSRichard Henderson         haddr += n;
2126cdfac37bSRichard Henderson         size -= n;
2127cdfac37bSRichard Henderson     } while (size != 0);
2128cdfac37bSRichard Henderson     return ret_be;
2129cdfac37bSRichard Henderson }
2130cdfac37bSRichard Henderson 
2131cdfac37bSRichard Henderson /**
2132cdfac37bSRichard Henderson  * do_ld_parts_be4
2133cdfac37bSRichard Henderson  * @p: translation parameters
2134cdfac37bSRichard Henderson  * @ret_be: accumulated data
2135cdfac37bSRichard Henderson  *
2136cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
2137cdfac37bSRichard Henderson  * Four aligned bytes are guaranteed to cover the load.
2138cdfac37bSRichard Henderson  */
2139cdfac37bSRichard Henderson static uint64_t do_ld_whole_be4(MMULookupPageData *p, uint64_t ret_be)
2140cdfac37bSRichard Henderson {
2141cdfac37bSRichard Henderson     int o = p->addr & 3;
2142cdfac37bSRichard Henderson     uint32_t x = load_atomic4(p->haddr - o);
2143cdfac37bSRichard Henderson 
2144cdfac37bSRichard Henderson     x = cpu_to_be32(x);
2145cdfac37bSRichard Henderson     x <<= o * 8;
2146cdfac37bSRichard Henderson     x >>= (4 - p->size) * 8;
2147cdfac37bSRichard Henderson     return (ret_be << (p->size * 8)) | x;
2148cdfac37bSRichard Henderson }
2149cdfac37bSRichard Henderson 
2150cdfac37bSRichard Henderson /**
2151cdfac37bSRichard Henderson  * do_ld_parts_be8
2152cdfac37bSRichard Henderson  * @p: translation parameters
2153cdfac37bSRichard Henderson  * @ret_be: accumulated data
2154cdfac37bSRichard Henderson  *
2155cdfac37bSRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
2156cdfac37bSRichard Henderson  * Eight aligned bytes are guaranteed to cover the load.
2157cdfac37bSRichard Henderson  */
2158d50ef446SAnton Johansson static uint64_t do_ld_whole_be8(CPUState *cpu, uintptr_t ra,
2159cdfac37bSRichard Henderson                                 MMULookupPageData *p, uint64_t ret_be)
2160cdfac37bSRichard Henderson {
2161cdfac37bSRichard Henderson     int o = p->addr & 7;
216273fda56fSAnton Johansson     uint64_t x = load_atomic8_or_exit(cpu, ra, p->haddr - o);
2163cdfac37bSRichard Henderson 
2164cdfac37bSRichard Henderson     x = cpu_to_be64(x);
2165cdfac37bSRichard Henderson     x <<= o * 8;
2166cdfac37bSRichard Henderson     x >>= (8 - p->size) * 8;
2167cdfac37bSRichard Henderson     return (ret_be << (p->size * 8)) | x;
2168cdfac37bSRichard Henderson }
2169cdfac37bSRichard Henderson 
217035c653c4SRichard Henderson /**
217135c653c4SRichard Henderson  * do_ld_parts_be16
217235c653c4SRichard Henderson  * @p: translation parameters
217335c653c4SRichard Henderson  * @ret_be: accumulated data
217435c653c4SRichard Henderson  *
217535c653c4SRichard Henderson  * As do_ld_bytes_beN, but with one atomic load.
217635c653c4SRichard Henderson  * 16 aligned bytes are guaranteed to cover the load.
217735c653c4SRichard Henderson  */
2178d50ef446SAnton Johansson static Int128 do_ld_whole_be16(CPUState *cpu, uintptr_t ra,
217935c653c4SRichard Henderson                                MMULookupPageData *p, uint64_t ret_be)
218035c653c4SRichard Henderson {
218135c653c4SRichard Henderson     int o = p->addr & 15;
218273fda56fSAnton Johansson     Int128 x, y = load_atomic16_or_exit(cpu, ra, p->haddr - o);
218335c653c4SRichard Henderson     int size = p->size;
218435c653c4SRichard Henderson 
218535c653c4SRichard Henderson     if (!HOST_BIG_ENDIAN) {
218635c653c4SRichard Henderson         y = bswap128(y);
218735c653c4SRichard Henderson     }
218835c653c4SRichard Henderson     y = int128_lshift(y, o * 8);
218935c653c4SRichard Henderson     y = int128_urshift(y, (16 - size) * 8);
219035c653c4SRichard Henderson     x = int128_make64(ret_be);
219135c653c4SRichard Henderson     x = int128_lshift(x, size * 8);
219235c653c4SRichard Henderson     return int128_or(x, y);
219335c653c4SRichard Henderson }
219435c653c4SRichard Henderson 
21958cfdacaaSRichard Henderson /*
21968cfdacaaSRichard Henderson  * Wrapper for the above.
21978cfdacaaSRichard Henderson  */
2198d50ef446SAnton Johansson static uint64_t do_ld_beN(CPUState *cpu, MMULookupPageData *p,
2199cdfac37bSRichard Henderson                           uint64_t ret_be, int mmu_idx, MMUAccessType type,
2200cdfac37bSRichard Henderson                           MemOp mop, uintptr_t ra)
22018cfdacaaSRichard Henderson {
2202cdfac37bSRichard Henderson     MemOp atom;
2203cdfac37bSRichard Henderson     unsigned tmp, half_size;
2204cdfac37bSRichard Henderson 
22058cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2206d50ef446SAnton Johansson         return do_ld_mmio_beN(cpu, p->full, ret_be, p->addr, p->size,
22071966855eSRichard Henderson                               mmu_idx, type, ra);
2208cdfac37bSRichard Henderson     }
2209cdfac37bSRichard Henderson 
2210cdfac37bSRichard Henderson     /*
2211cdfac37bSRichard Henderson      * It is a given that we cross a page and therefore there is no
2212cdfac37bSRichard Henderson      * atomicity for the load as a whole, but subobjects may need attention.
2213cdfac37bSRichard Henderson      */
2214cdfac37bSRichard Henderson     atom = mop & MO_ATOM_MASK;
2215cdfac37bSRichard Henderson     switch (atom) {
2216cdfac37bSRichard Henderson     case MO_ATOM_SUBALIGN:
2217cdfac37bSRichard Henderson         return do_ld_parts_beN(p, ret_be);
2218cdfac37bSRichard Henderson 
2219cdfac37bSRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
2220cdfac37bSRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
2221cdfac37bSRichard Henderson         tmp = mop & MO_SIZE;
2222cdfac37bSRichard Henderson         tmp = tmp ? tmp - 1 : 0;
2223cdfac37bSRichard Henderson         half_size = 1 << tmp;
2224cdfac37bSRichard Henderson         if (atom == MO_ATOM_IFALIGN_PAIR
2225cdfac37bSRichard Henderson             ? p->size == half_size
2226cdfac37bSRichard Henderson             : p->size >= half_size) {
2227cdfac37bSRichard Henderson             if (!HAVE_al8_fast && p->size < 4) {
2228cdfac37bSRichard Henderson                 return do_ld_whole_be4(p, ret_be);
22298cfdacaaSRichard Henderson             } else {
2230d50ef446SAnton Johansson                 return do_ld_whole_be8(cpu, ra, p, ret_be);
2231cdfac37bSRichard Henderson             }
2232cdfac37bSRichard Henderson         }
2233cdfac37bSRichard Henderson         /* fall through */
2234cdfac37bSRichard Henderson 
2235cdfac37bSRichard Henderson     case MO_ATOM_IFALIGN:
2236cdfac37bSRichard Henderson     case MO_ATOM_WITHIN16:
2237cdfac37bSRichard Henderson     case MO_ATOM_NONE:
22388cfdacaaSRichard Henderson         return do_ld_bytes_beN(p, ret_be);
2239cdfac37bSRichard Henderson 
2240cdfac37bSRichard Henderson     default:
2241cdfac37bSRichard Henderson         g_assert_not_reached();
22428cfdacaaSRichard Henderson     }
22438cfdacaaSRichard Henderson }
22448cfdacaaSRichard Henderson 
224535c653c4SRichard Henderson /*
224635c653c4SRichard Henderson  * Wrapper for the above, for 8 < size < 16.
224735c653c4SRichard Henderson  */
2248d50ef446SAnton Johansson static Int128 do_ld16_beN(CPUState *cpu, MMULookupPageData *p,
224935c653c4SRichard Henderson                           uint64_t a, int mmu_idx, MemOp mop, uintptr_t ra)
225035c653c4SRichard Henderson {
225135c653c4SRichard Henderson     int size = p->size;
225235c653c4SRichard Henderson     uint64_t b;
225335c653c4SRichard Henderson     MemOp atom;
225435c653c4SRichard Henderson 
225535c653c4SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2256d50ef446SAnton Johansson         return do_ld16_mmio_beN(cpu, p->full, a, p->addr, size, mmu_idx, ra);
225735c653c4SRichard Henderson     }
225835c653c4SRichard Henderson 
225935c653c4SRichard Henderson     /*
226035c653c4SRichard Henderson      * It is a given that we cross a page and therefore there is no
226135c653c4SRichard Henderson      * atomicity for the load as a whole, but subobjects may need attention.
226235c653c4SRichard Henderson      */
226335c653c4SRichard Henderson     atom = mop & MO_ATOM_MASK;
226435c653c4SRichard Henderson     switch (atom) {
226535c653c4SRichard Henderson     case MO_ATOM_SUBALIGN:
226635c653c4SRichard Henderson         p->size = size - 8;
226735c653c4SRichard Henderson         a = do_ld_parts_beN(p, a);
226835c653c4SRichard Henderson         p->haddr += size - 8;
226935c653c4SRichard Henderson         p->size = 8;
227035c653c4SRichard Henderson         b = do_ld_parts_beN(p, 0);
227135c653c4SRichard Henderson         break;
227235c653c4SRichard Henderson 
227335c653c4SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
227435c653c4SRichard Henderson         /* Since size > 8, this is the half that must be atomic. */
2275d50ef446SAnton Johansson         return do_ld_whole_be16(cpu, ra, p, a);
227635c653c4SRichard Henderson 
227735c653c4SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
227835c653c4SRichard Henderson         /*
227935c653c4SRichard Henderson          * Since size > 8, both halves are misaligned,
228035c653c4SRichard Henderson          * and so neither is atomic.
228135c653c4SRichard Henderson          */
228235c653c4SRichard Henderson     case MO_ATOM_IFALIGN:
228335c653c4SRichard Henderson     case MO_ATOM_WITHIN16:
228435c653c4SRichard Henderson     case MO_ATOM_NONE:
228535c653c4SRichard Henderson         p->size = size - 8;
228635c653c4SRichard Henderson         a = do_ld_bytes_beN(p, a);
228735c653c4SRichard Henderson         b = ldq_be_p(p->haddr + size - 8);
228835c653c4SRichard Henderson         break;
228935c653c4SRichard Henderson 
229035c653c4SRichard Henderson     default:
229135c653c4SRichard Henderson         g_assert_not_reached();
229235c653c4SRichard Henderson     }
229335c653c4SRichard Henderson 
229435c653c4SRichard Henderson     return int128_make128(b, a);
229535c653c4SRichard Henderson }
229635c653c4SRichard Henderson 
2297d50ef446SAnton Johansson static uint8_t do_ld_1(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
22988cfdacaaSRichard Henderson                        MMUAccessType type, uintptr_t ra)
22998cfdacaaSRichard Henderson {
23008cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2301d50ef446SAnton Johansson         return do_ld_mmio_beN(cpu, p->full, 0, p->addr, 1, mmu_idx, type, ra);
23028cfdacaaSRichard Henderson     } else {
23038cfdacaaSRichard Henderson         return *(uint8_t *)p->haddr;
23048cfdacaaSRichard Henderson     }
23058cfdacaaSRichard Henderson }
23068cfdacaaSRichard Henderson 
2307d50ef446SAnton Johansson static uint16_t do_ld_2(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23088cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23098cfdacaaSRichard Henderson {
2310f7eaf9d7SRichard Henderson     uint16_t ret;
23118cfdacaaSRichard Henderson 
23128cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2313d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 2, mmu_idx, type, ra);
2314f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2315f7eaf9d7SRichard Henderson             ret = bswap16(ret);
23168cfdacaaSRichard Henderson         }
2317f7eaf9d7SRichard Henderson     } else {
23188cfdacaaSRichard Henderson         /* Perform the load host endian, then swap if necessary. */
231973fda56fSAnton Johansson         ret = load_atom_2(cpu, ra, p->haddr, memop);
23208cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23218cfdacaaSRichard Henderson             ret = bswap16(ret);
23228cfdacaaSRichard Henderson         }
2323f7eaf9d7SRichard Henderson     }
23248cfdacaaSRichard Henderson     return ret;
23258cfdacaaSRichard Henderson }
23268cfdacaaSRichard Henderson 
2327d50ef446SAnton Johansson static uint32_t do_ld_4(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23288cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23298cfdacaaSRichard Henderson {
23308cfdacaaSRichard Henderson     uint32_t ret;
23318cfdacaaSRichard Henderson 
23328cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2333d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 4, mmu_idx, type, ra);
2334f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2335f7eaf9d7SRichard Henderson             ret = bswap32(ret);
23368cfdacaaSRichard Henderson         }
2337f7eaf9d7SRichard Henderson     } else {
23388cfdacaaSRichard Henderson         /* Perform the load host endian. */
233973fda56fSAnton Johansson         ret = load_atom_4(cpu, ra, p->haddr, memop);
23408cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23418cfdacaaSRichard Henderson             ret = bswap32(ret);
23428cfdacaaSRichard Henderson         }
2343f7eaf9d7SRichard Henderson     }
23448cfdacaaSRichard Henderson     return ret;
23458cfdacaaSRichard Henderson }
23468cfdacaaSRichard Henderson 
2347d50ef446SAnton Johansson static uint64_t do_ld_8(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
23488cfdacaaSRichard Henderson                         MMUAccessType type, MemOp memop, uintptr_t ra)
23498cfdacaaSRichard Henderson {
23508cfdacaaSRichard Henderson     uint64_t ret;
23518cfdacaaSRichard Henderson 
23528cfdacaaSRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2353d50ef446SAnton Johansson         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 8, mmu_idx, type, ra);
2354f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) == MO_LE) {
2355f7eaf9d7SRichard Henderson             ret = bswap64(ret);
23568cfdacaaSRichard Henderson         }
2357f7eaf9d7SRichard Henderson     } else {
23588cfdacaaSRichard Henderson         /* Perform the load host endian. */
235973fda56fSAnton Johansson         ret = load_atom_8(cpu, ra, p->haddr, memop);
23608cfdacaaSRichard Henderson         if (memop & MO_BSWAP) {
23618cfdacaaSRichard Henderson             ret = bswap64(ret);
23628cfdacaaSRichard Henderson         }
2363f7eaf9d7SRichard Henderson     }
23648cfdacaaSRichard Henderson     return ret;
23658cfdacaaSRichard Henderson }
23668cfdacaaSRichard Henderson 
2367d50ef446SAnton Johansson static uint8_t do_ld1_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
23688cfdacaaSRichard Henderson                           uintptr_t ra, MMUAccessType access_type)
23698cfdacaaSRichard Henderson {
23708cfdacaaSRichard Henderson     MMULookupLocals l;
23718cfdacaaSRichard Henderson     bool crosspage;
23728cfdacaaSRichard Henderson 
2373f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2374d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
23758cfdacaaSRichard Henderson     tcg_debug_assert(!crosspage);
23768cfdacaaSRichard Henderson 
2377d50ef446SAnton Johansson     return do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
23782dd92606SRichard Henderson }
23792dd92606SRichard Henderson 
2380d50ef446SAnton Johansson static uint16_t do_ld2_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
23818cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
23822dd92606SRichard Henderson {
23838cfdacaaSRichard Henderson     MMULookupLocals l;
23848cfdacaaSRichard Henderson     bool crosspage;
23858cfdacaaSRichard Henderson     uint16_t ret;
23868cfdacaaSRichard Henderson     uint8_t a, b;
23878cfdacaaSRichard Henderson 
2388f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2389d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
23908cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2391d50ef446SAnton Johansson         return do_ld_2(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
23928cfdacaaSRichard Henderson     }
23938cfdacaaSRichard Henderson 
2394d50ef446SAnton Johansson     a = do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
2395d50ef446SAnton Johansson     b = do_ld_1(cpu, &l.page[1], l.mmu_idx, access_type, ra);
23968cfdacaaSRichard Henderson 
23978cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
23988cfdacaaSRichard Henderson         ret = a | (b << 8);
23998cfdacaaSRichard Henderson     } else {
24008cfdacaaSRichard Henderson         ret = b | (a << 8);
24018cfdacaaSRichard Henderson     }
24028cfdacaaSRichard Henderson     return ret;
2403eed56642SAlex Bennée }
2404eed56642SAlex Bennée 
2405d50ef446SAnton Johansson static uint32_t do_ld4_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
24068cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
24072dd92606SRichard Henderson {
24088cfdacaaSRichard Henderson     MMULookupLocals l;
24098cfdacaaSRichard Henderson     bool crosspage;
24108cfdacaaSRichard Henderson     uint32_t ret;
24118cfdacaaSRichard Henderson 
2412f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2413d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
24148cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2415d50ef446SAnton Johansson         return do_ld_4(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
24168cfdacaaSRichard Henderson     }
24178cfdacaaSRichard Henderson 
2418d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2419d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
24208cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
24218cfdacaaSRichard Henderson         ret = bswap32(ret);
24228cfdacaaSRichard Henderson     }
24238cfdacaaSRichard Henderson     return ret;
2424eed56642SAlex Bennée }
2425eed56642SAlex Bennée 
2426d50ef446SAnton Johansson static uint64_t do_ld8_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
24278cfdacaaSRichard Henderson                            uintptr_t ra, MMUAccessType access_type)
24288cfdacaaSRichard Henderson {
24298cfdacaaSRichard Henderson     MMULookupLocals l;
24308cfdacaaSRichard Henderson     bool crosspage;
24318cfdacaaSRichard Henderson     uint64_t ret;
24328cfdacaaSRichard Henderson 
2433f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2434d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
24358cfdacaaSRichard Henderson     if (likely(!crosspage)) {
2436d50ef446SAnton Johansson         return do_ld_8(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
24378cfdacaaSRichard Henderson     }
24388cfdacaaSRichard Henderson 
2439d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2440d50ef446SAnton Johansson     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
24418cfdacaaSRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
24428cfdacaaSRichard Henderson         ret = bswap64(ret);
24438cfdacaaSRichard Henderson     }
24448cfdacaaSRichard Henderson     return ret;
2445eed56642SAlex Bennée }
2446eed56642SAlex Bennée 
2447d50ef446SAnton Johansson static Int128 do_ld16_mmu(CPUState *cpu, vaddr addr,
244835c653c4SRichard Henderson                           MemOpIdx oi, uintptr_t ra)
244935c653c4SRichard Henderson {
245035c653c4SRichard Henderson     MMULookupLocals l;
245135c653c4SRichard Henderson     bool crosspage;
245235c653c4SRichard Henderson     uint64_t a, b;
245335c653c4SRichard Henderson     Int128 ret;
245435c653c4SRichard Henderson     int first;
245535c653c4SRichard Henderson 
2456f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2457d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_LOAD, &l);
245835c653c4SRichard Henderson     if (likely(!crosspage)) {
245935c653c4SRichard Henderson         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2460d50ef446SAnton Johansson             ret = do_ld16_mmio_beN(cpu, l.page[0].full, 0, addr, 16,
24618bf67267SRichard Henderson                                    l.mmu_idx, ra);
2462f7eaf9d7SRichard Henderson             if ((l.memop & MO_BSWAP) == MO_LE) {
2463f7eaf9d7SRichard Henderson                 ret = bswap128(ret);
246435c653c4SRichard Henderson             }
2465f7eaf9d7SRichard Henderson         } else {
2466f7eaf9d7SRichard Henderson             /* Perform the load host endian. */
246773fda56fSAnton Johansson             ret = load_atom_16(cpu, ra, l.page[0].haddr, l.memop);
246835c653c4SRichard Henderson             if (l.memop & MO_BSWAP) {
246935c653c4SRichard Henderson                 ret = bswap128(ret);
247035c653c4SRichard Henderson             }
2471f7eaf9d7SRichard Henderson         }
247235c653c4SRichard Henderson         return ret;
247335c653c4SRichard Henderson     }
247435c653c4SRichard Henderson 
247535c653c4SRichard Henderson     first = l.page[0].size;
247635c653c4SRichard Henderson     if (first == 8) {
247735c653c4SRichard Henderson         MemOp mop8 = (l.memop & ~MO_SIZE) | MO_64;
247835c653c4SRichard Henderson 
2479d50ef446SAnton Johansson         a = do_ld_8(cpu, &l.page[0], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
2480d50ef446SAnton Johansson         b = do_ld_8(cpu, &l.page[1], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
248135c653c4SRichard Henderson         if ((mop8 & MO_BSWAP) == MO_LE) {
248235c653c4SRichard Henderson             ret = int128_make128(a, b);
248335c653c4SRichard Henderson         } else {
248435c653c4SRichard Henderson             ret = int128_make128(b, a);
248535c653c4SRichard Henderson         }
248635c653c4SRichard Henderson         return ret;
248735c653c4SRichard Henderson     }
248835c653c4SRichard Henderson 
248935c653c4SRichard Henderson     if (first < 8) {
2490d50ef446SAnton Johansson         a = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx,
249135c653c4SRichard Henderson                       MMU_DATA_LOAD, l.memop, ra);
2492d50ef446SAnton Johansson         ret = do_ld16_beN(cpu, &l.page[1], a, l.mmu_idx, l.memop, ra);
249335c653c4SRichard Henderson     } else {
2494d50ef446SAnton Johansson         ret = do_ld16_beN(cpu, &l.page[0], 0, l.mmu_idx, l.memop, ra);
249535c653c4SRichard Henderson         b = int128_getlo(ret);
249635c653c4SRichard Henderson         ret = int128_lshift(ret, l.page[1].size * 8);
249735c653c4SRichard Henderson         a = int128_gethi(ret);
2498d50ef446SAnton Johansson         b = do_ld_beN(cpu, &l.page[1], b, l.mmu_idx,
249935c653c4SRichard Henderson                       MMU_DATA_LOAD, l.memop, ra);
250035c653c4SRichard Henderson         ret = int128_make128(b, a);
250135c653c4SRichard Henderson     }
250235c653c4SRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
250335c653c4SRichard Henderson         ret = bswap128(ret);
250435c653c4SRichard Henderson     }
250535c653c4SRichard Henderson     return ret;
250635c653c4SRichard Henderson }
250735c653c4SRichard Henderson 
2508d03f1408SRichard Henderson /*
2509eed56642SAlex Bennée  * Store Helpers
2510eed56642SAlex Bennée  */
2511eed56642SAlex Bennée 
251259213461SRichard Henderson /**
251359213461SRichard Henderson  * do_st_mmio_leN:
2514d50ef446SAnton Johansson  * @cpu: generic cpu state
25151966855eSRichard Henderson  * @full: page parameters
251659213461SRichard Henderson  * @val_le: data to store
25171966855eSRichard Henderson  * @addr: virtual address
25181966855eSRichard Henderson  * @size: number of bytes
251959213461SRichard Henderson  * @mmu_idx: virtual address context
252059213461SRichard Henderson  * @ra: return address into tcg generated code, or 0
25211966855eSRichard Henderson  * Context: iothread lock held
252259213461SRichard Henderson  *
25231966855eSRichard Henderson  * Store @size bytes at @addr, which is memory-mapped i/o.
252459213461SRichard Henderson  * The bytes to store are extracted in little-endian order from @val_le;
252559213461SRichard Henderson  * return the bytes of @val_le beyond @p->size that have not been stored.
252659213461SRichard Henderson  */
2527d50ef446SAnton Johansson static uint64_t int_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25281966855eSRichard Henderson                                 uint64_t val_le, vaddr addr, int size,
25291f9823ceSRichard Henderson                                 int mmu_idx, uintptr_t ra,
25301f9823ceSRichard Henderson                                 MemoryRegion *mr, hwaddr mr_offset)
25316b8b622eSRichard Henderson {
2532190aba80SRichard Henderson     do {
25335646d6a7SRichard Henderson         MemOp this_mop;
25345646d6a7SRichard Henderson         unsigned this_size;
25355646d6a7SRichard Henderson         MemTxResult r;
25365646d6a7SRichard Henderson 
2537190aba80SRichard Henderson         /* Store aligned pieces up to 8 bytes. */
25385646d6a7SRichard Henderson         this_mop = ctz32(size | (int)addr | 8);
25395646d6a7SRichard Henderson         this_size = 1 << this_mop;
25405646d6a7SRichard Henderson         this_mop |= MO_LE;
25415646d6a7SRichard Henderson 
25425646d6a7SRichard Henderson         r = memory_region_dispatch_write(mr, mr_offset, val_le,
25431f9823ceSRichard Henderson                                          this_mop, full->attrs);
25445646d6a7SRichard Henderson         if (unlikely(r != MEMTX_OK)) {
2545d50ef446SAnton Johansson             io_failed(cpu, full, addr, this_size, MMU_DATA_STORE,
25465646d6a7SRichard Henderson                       mmu_idx, r, ra);
254759213461SRichard Henderson         }
25485646d6a7SRichard Henderson         if (this_size == 8) {
25495646d6a7SRichard Henderson             return 0;
25505646d6a7SRichard Henderson         }
25515646d6a7SRichard Henderson 
25525646d6a7SRichard Henderson         val_le >>= this_size * 8;
25535646d6a7SRichard Henderson         addr += this_size;
25545646d6a7SRichard Henderson         mr_offset += this_size;
25555646d6a7SRichard Henderson         size -= this_size;
2556190aba80SRichard Henderson     } while (size);
2557190aba80SRichard Henderson 
255859213461SRichard Henderson     return val_le;
255959213461SRichard Henderson }
256059213461SRichard Henderson 
2561d50ef446SAnton Johansson static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25621f9823ceSRichard Henderson                                uint64_t val_le, vaddr addr, int size,
25631f9823ceSRichard Henderson                                int mmu_idx, uintptr_t ra)
25641f9823ceSRichard Henderson {
25651f9823ceSRichard Henderson     MemoryRegionSection *section;
25661f9823ceSRichard Henderson     hwaddr mr_offset;
25671f9823ceSRichard Henderson     MemoryRegion *mr;
25681f9823ceSRichard Henderson     MemTxAttrs attrs;
25691f9823ceSRichard Henderson     uint64_t ret;
25701f9823ceSRichard Henderson 
25711f9823ceSRichard Henderson     tcg_debug_assert(size > 0 && size <= 8);
25721f9823ceSRichard Henderson 
25731f9823ceSRichard Henderson     attrs = full->attrs;
2574d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
25751f9823ceSRichard Henderson     mr = section->mr;
25761f9823ceSRichard Henderson 
25771f9823ceSRichard Henderson     qemu_mutex_lock_iothread();
2578d50ef446SAnton Johansson     ret = int_st_mmio_leN(cpu, full, val_le, addr, size, mmu_idx,
25791f9823ceSRichard Henderson                           ra, mr, mr_offset);
25801f9823ceSRichard Henderson     qemu_mutex_unlock_iothread();
25811f9823ceSRichard Henderson 
25821f9823ceSRichard Henderson     return ret;
25831f9823ceSRichard Henderson }
25841f9823ceSRichard Henderson 
2585d50ef446SAnton Johansson static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
25861f9823ceSRichard Henderson                                  Int128 val_le, vaddr addr, int size,
25871f9823ceSRichard Henderson                                  int mmu_idx, uintptr_t ra)
25881f9823ceSRichard Henderson {
25891f9823ceSRichard Henderson     MemoryRegionSection *section;
25901f9823ceSRichard Henderson     MemoryRegion *mr;
25911f9823ceSRichard Henderson     hwaddr mr_offset;
25921f9823ceSRichard Henderson     MemTxAttrs attrs;
25931f9823ceSRichard Henderson     uint64_t ret;
25941f9823ceSRichard Henderson 
25951f9823ceSRichard Henderson     tcg_debug_assert(size > 8 && size <= 16);
25961f9823ceSRichard Henderson 
25971f9823ceSRichard Henderson     attrs = full->attrs;
2598d50ef446SAnton Johansson     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
25991f9823ceSRichard Henderson     mr = section->mr;
26001f9823ceSRichard Henderson 
26011f9823ceSRichard Henderson     qemu_mutex_lock_iothread();
2602d50ef446SAnton Johansson     int_st_mmio_leN(cpu, full, int128_getlo(val_le), addr, 8,
26031f9823ceSRichard Henderson                     mmu_idx, ra, mr, mr_offset);
2604d50ef446SAnton Johansson     ret = int_st_mmio_leN(cpu, full, int128_gethi(val_le), addr + 8,
26051f9823ceSRichard Henderson                           size - 8, mmu_idx, ra, mr, mr_offset + 8);
26061f9823ceSRichard Henderson     qemu_mutex_unlock_iothread();
26071f9823ceSRichard Henderson 
26081f9823ceSRichard Henderson     return ret;
26091f9823ceSRichard Henderson }
26101f9823ceSRichard Henderson 
26116b8b622eSRichard Henderson /*
261259213461SRichard Henderson  * Wrapper for the above.
26136b8b622eSRichard Henderson  */
2614d50ef446SAnton Johansson static uint64_t do_st_leN(CPUState *cpu, MMULookupPageData *p,
26155b36f268SRichard Henderson                           uint64_t val_le, int mmu_idx,
26165b36f268SRichard Henderson                           MemOp mop, uintptr_t ra)
261759213461SRichard Henderson {
26185b36f268SRichard Henderson     MemOp atom;
26195b36f268SRichard Henderson     unsigned tmp, half_size;
26205b36f268SRichard Henderson 
262159213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2622d50ef446SAnton Johansson         return do_st_mmio_leN(cpu, p->full, val_le, p->addr,
26231966855eSRichard Henderson                               p->size, mmu_idx, ra);
262459213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
262559213461SRichard Henderson         return val_le >> (p->size * 8);
26265b36f268SRichard Henderson     }
26275b36f268SRichard Henderson 
26285b36f268SRichard Henderson     /*
26295b36f268SRichard Henderson      * It is a given that we cross a page and therefore there is no atomicity
26305b36f268SRichard Henderson      * for the store as a whole, but subobjects may need attention.
26315b36f268SRichard Henderson      */
26325b36f268SRichard Henderson     atom = mop & MO_ATOM_MASK;
26335b36f268SRichard Henderson     switch (atom) {
26345b36f268SRichard Henderson     case MO_ATOM_SUBALIGN:
26355b36f268SRichard Henderson         return store_parts_leN(p->haddr, p->size, val_le);
26365b36f268SRichard Henderson 
26375b36f268SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
26385b36f268SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
26395b36f268SRichard Henderson         tmp = mop & MO_SIZE;
26405b36f268SRichard Henderson         tmp = tmp ? tmp - 1 : 0;
26415b36f268SRichard Henderson         half_size = 1 << tmp;
26425b36f268SRichard Henderson         if (atom == MO_ATOM_IFALIGN_PAIR
26435b36f268SRichard Henderson             ? p->size == half_size
26445b36f268SRichard Henderson             : p->size >= half_size) {
26455b36f268SRichard Henderson             if (!HAVE_al8_fast && p->size <= 4) {
26465b36f268SRichard Henderson                 return store_whole_le4(p->haddr, p->size, val_le);
26475b36f268SRichard Henderson             } else if (HAVE_al8) {
26485b36f268SRichard Henderson                 return store_whole_le8(p->haddr, p->size, val_le);
26496b8b622eSRichard Henderson             } else {
2650d50ef446SAnton Johansson                 cpu_loop_exit_atomic(cpu, ra);
26515b36f268SRichard Henderson             }
26525b36f268SRichard Henderson         }
26535b36f268SRichard Henderson         /* fall through */
26545b36f268SRichard Henderson 
26555b36f268SRichard Henderson     case MO_ATOM_IFALIGN:
26565b36f268SRichard Henderson     case MO_ATOM_WITHIN16:
26575b36f268SRichard Henderson     case MO_ATOM_NONE:
26585b36f268SRichard Henderson         return store_bytes_leN(p->haddr, p->size, val_le);
26595b36f268SRichard Henderson 
26605b36f268SRichard Henderson     default:
26615b36f268SRichard Henderson         g_assert_not_reached();
26626b8b622eSRichard Henderson     }
26636b8b622eSRichard Henderson }
26646b8b622eSRichard Henderson 
266535c653c4SRichard Henderson /*
266635c653c4SRichard Henderson  * Wrapper for the above, for 8 < size < 16.
266735c653c4SRichard Henderson  */
2668d50ef446SAnton Johansson static uint64_t do_st16_leN(CPUState *cpu, MMULookupPageData *p,
266935c653c4SRichard Henderson                             Int128 val_le, int mmu_idx,
267035c653c4SRichard Henderson                             MemOp mop, uintptr_t ra)
267135c653c4SRichard Henderson {
267235c653c4SRichard Henderson     int size = p->size;
267335c653c4SRichard Henderson     MemOp atom;
267435c653c4SRichard Henderson 
267535c653c4SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2676d50ef446SAnton Johansson         return do_st16_mmio_leN(cpu, p->full, val_le, p->addr,
26771f9823ceSRichard Henderson                                 size, mmu_idx, ra);
267835c653c4SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
267935c653c4SRichard Henderson         return int128_gethi(val_le) >> ((size - 8) * 8);
268035c653c4SRichard Henderson     }
268135c653c4SRichard Henderson 
268235c653c4SRichard Henderson     /*
268335c653c4SRichard Henderson      * It is a given that we cross a page and therefore there is no atomicity
268435c653c4SRichard Henderson      * for the store as a whole, but subobjects may need attention.
268535c653c4SRichard Henderson      */
268635c653c4SRichard Henderson     atom = mop & MO_ATOM_MASK;
268735c653c4SRichard Henderson     switch (atom) {
268835c653c4SRichard Henderson     case MO_ATOM_SUBALIGN:
268935c653c4SRichard Henderson         store_parts_leN(p->haddr, 8, int128_getlo(val_le));
269035c653c4SRichard Henderson         return store_parts_leN(p->haddr + 8, p->size - 8,
269135c653c4SRichard Henderson                                int128_gethi(val_le));
269235c653c4SRichard Henderson 
269335c653c4SRichard Henderson     case MO_ATOM_WITHIN16_PAIR:
269435c653c4SRichard Henderson         /* Since size > 8, this is the half that must be atomic. */
26956046f6e9SRichard Henderson         if (!HAVE_CMPXCHG128) {
2696d50ef446SAnton Johansson             cpu_loop_exit_atomic(cpu, ra);
269735c653c4SRichard Henderson         }
269835c653c4SRichard Henderson         return store_whole_le16(p->haddr, p->size, val_le);
269935c653c4SRichard Henderson 
270035c653c4SRichard Henderson     case MO_ATOM_IFALIGN_PAIR:
270135c653c4SRichard Henderson         /*
270235c653c4SRichard Henderson          * Since size > 8, both halves are misaligned,
270335c653c4SRichard Henderson          * and so neither is atomic.
270435c653c4SRichard Henderson          */
270535c653c4SRichard Henderson     case MO_ATOM_IFALIGN:
27062be6a486SRichard Henderson     case MO_ATOM_WITHIN16:
270735c653c4SRichard Henderson     case MO_ATOM_NONE:
270835c653c4SRichard Henderson         stq_le_p(p->haddr, int128_getlo(val_le));
270935c653c4SRichard Henderson         return store_bytes_leN(p->haddr + 8, p->size - 8,
271035c653c4SRichard Henderson                                int128_gethi(val_le));
271135c653c4SRichard Henderson 
271235c653c4SRichard Henderson     default:
271335c653c4SRichard Henderson         g_assert_not_reached();
271435c653c4SRichard Henderson     }
271535c653c4SRichard Henderson }
271635c653c4SRichard Henderson 
2717d50ef446SAnton Johansson static void do_st_1(CPUState *cpu, MMULookupPageData *p, uint8_t val,
271859213461SRichard Henderson                     int mmu_idx, uintptr_t ra)
2719eed56642SAlex Bennée {
272059213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2721d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 1, mmu_idx, ra);
272259213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
272359213461SRichard Henderson         /* nothing */
27245b87b3e6SRichard Henderson     } else {
272559213461SRichard Henderson         *(uint8_t *)p->haddr = val;
27265b87b3e6SRichard Henderson     }
2727eed56642SAlex Bennée }
2728eed56642SAlex Bennée 
2729d50ef446SAnton Johansson static void do_st_2(CPUState *cpu, MMULookupPageData *p, uint16_t val,
273059213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
2731eed56642SAlex Bennée {
273259213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2733f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2734f7eaf9d7SRichard Henderson             val = bswap16(val);
2735f7eaf9d7SRichard Henderson         }
2736d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 2, mmu_idx, ra);
273759213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
273859213461SRichard Henderson         /* nothing */
273959213461SRichard Henderson     } else {
274059213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
274159213461SRichard Henderson         if (memop & MO_BSWAP) {
274259213461SRichard Henderson             val = bswap16(val);
274359213461SRichard Henderson         }
274473fda56fSAnton Johansson         store_atom_2(cpu, ra, p->haddr, memop, val);
274559213461SRichard Henderson     }
274659213461SRichard Henderson }
274759213461SRichard Henderson 
2748d50ef446SAnton Johansson static void do_st_4(CPUState *cpu, MMULookupPageData *p, uint32_t val,
274959213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
275059213461SRichard Henderson {
275159213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2752f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2753f7eaf9d7SRichard Henderson             val = bswap32(val);
2754f7eaf9d7SRichard Henderson         }
2755d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 4, mmu_idx, ra);
275659213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
275759213461SRichard Henderson         /* nothing */
275859213461SRichard Henderson     } else {
275959213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
276059213461SRichard Henderson         if (memop & MO_BSWAP) {
276159213461SRichard Henderson             val = bswap32(val);
276259213461SRichard Henderson         }
276373fda56fSAnton Johansson         store_atom_4(cpu, ra, p->haddr, memop, val);
276459213461SRichard Henderson     }
276559213461SRichard Henderson }
276659213461SRichard Henderson 
2767d50ef446SAnton Johansson static void do_st_8(CPUState *cpu, MMULookupPageData *p, uint64_t val,
276859213461SRichard Henderson                     int mmu_idx, MemOp memop, uintptr_t ra)
276959213461SRichard Henderson {
277059213461SRichard Henderson     if (unlikely(p->flags & TLB_MMIO)) {
2771f7eaf9d7SRichard Henderson         if ((memop & MO_BSWAP) != MO_LE) {
2772f7eaf9d7SRichard Henderson             val = bswap64(val);
2773f7eaf9d7SRichard Henderson         }
2774d50ef446SAnton Johansson         do_st_mmio_leN(cpu, p->full, val, p->addr, 8, mmu_idx, ra);
277559213461SRichard Henderson     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
277659213461SRichard Henderson         /* nothing */
277759213461SRichard Henderson     } else {
277859213461SRichard Henderson         /* Swap to host endian if necessary, then store. */
277959213461SRichard Henderson         if (memop & MO_BSWAP) {
278059213461SRichard Henderson             val = bswap64(val);
278159213461SRichard Henderson         }
278273fda56fSAnton Johansson         store_atom_8(cpu, ra, p->haddr, memop, val);
278359213461SRichard Henderson     }
2784eed56642SAlex Bennée }
2785eed56642SAlex Bennée 
2786e20f73fbSAnton Johansson static void do_st1_mmu(CPUState *cpu, vaddr addr, uint8_t val,
278759213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2788f83bcecbSRichard Henderson {
278959213461SRichard Henderson     MMULookupLocals l;
279059213461SRichard Henderson     bool crosspage;
279159213461SRichard Henderson 
2792f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2793e20f73fbSAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
279459213461SRichard Henderson     tcg_debug_assert(!crosspage);
279559213461SRichard Henderson 
2796e20f73fbSAnton Johansson     do_st_1(cpu, &l.page[0], val, l.mmu_idx, ra);
2797e20f73fbSAnton Johansson }
2798e20f73fbSAnton Johansson 
2799d50ef446SAnton Johansson static void do_st2_mmu(CPUState *cpu, vaddr addr, uint16_t val,
280059213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2801f83bcecbSRichard Henderson {
280259213461SRichard Henderson     MMULookupLocals l;
280359213461SRichard Henderson     bool crosspage;
280459213461SRichard Henderson     uint8_t a, b;
280559213461SRichard Henderson 
2806f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2807d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
280859213461SRichard Henderson     if (likely(!crosspage)) {
2809d50ef446SAnton Johansson         do_st_2(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
281059213461SRichard Henderson         return;
281159213461SRichard Henderson     }
281259213461SRichard Henderson 
281359213461SRichard Henderson     if ((l.memop & MO_BSWAP) == MO_LE) {
281459213461SRichard Henderson         a = val, b = val >> 8;
281559213461SRichard Henderson     } else {
281659213461SRichard Henderson         b = val, a = val >> 8;
281759213461SRichard Henderson     }
2818d50ef446SAnton Johansson     do_st_1(cpu, &l.page[0], a, l.mmu_idx, ra);
2819d50ef446SAnton Johansson     do_st_1(cpu, &l.page[1], b, l.mmu_idx, ra);
2820f83bcecbSRichard Henderson }
2821f83bcecbSRichard Henderson 
2822d50ef446SAnton Johansson static void do_st4_mmu(CPUState *cpu, vaddr addr, uint32_t val,
282359213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
2824f83bcecbSRichard Henderson {
282559213461SRichard Henderson     MMULookupLocals l;
282659213461SRichard Henderson     bool crosspage;
282759213461SRichard Henderson 
2828f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2829d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
283059213461SRichard Henderson     if (likely(!crosspage)) {
2831d50ef446SAnton Johansson         do_st_4(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
283259213461SRichard Henderson         return;
283359213461SRichard Henderson     }
283459213461SRichard Henderson 
283559213461SRichard Henderson     /* Swap to little endian for simplicity, then store by bytes. */
283659213461SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
283759213461SRichard Henderson         val = bswap32(val);
283859213461SRichard Henderson     }
2839d50ef446SAnton Johansson     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2840d50ef446SAnton Johansson     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2841eed56642SAlex Bennée }
2842eed56642SAlex Bennée 
2843d50ef446SAnton Johansson static void do_st8_mmu(CPUState *cpu, vaddr addr, uint64_t val,
284459213461SRichard Henderson                        MemOpIdx oi, uintptr_t ra)
284559213461SRichard Henderson {
284659213461SRichard Henderson     MMULookupLocals l;
284759213461SRichard Henderson     bool crosspage;
284859213461SRichard Henderson 
2849f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2850d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
285159213461SRichard Henderson     if (likely(!crosspage)) {
2852d50ef446SAnton Johansson         do_st_8(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
285359213461SRichard Henderson         return;
285459213461SRichard Henderson     }
285559213461SRichard Henderson 
285659213461SRichard Henderson     /* Swap to little endian for simplicity, then store by bytes. */
285759213461SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
285859213461SRichard Henderson         val = bswap64(val);
285959213461SRichard Henderson     }
2860d50ef446SAnton Johansson     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2861d50ef446SAnton Johansson     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2862eed56642SAlex Bennée }
2863eed56642SAlex Bennée 
2864d50ef446SAnton Johansson static void do_st16_mmu(CPUState *cpu, vaddr addr, Int128 val,
286535c653c4SRichard Henderson                         MemOpIdx oi, uintptr_t ra)
286635c653c4SRichard Henderson {
286735c653c4SRichard Henderson     MMULookupLocals l;
286835c653c4SRichard Henderson     bool crosspage;
286935c653c4SRichard Henderson     uint64_t a, b;
287035c653c4SRichard Henderson     int first;
287135c653c4SRichard Henderson 
2872f86e8f3dSRichard Henderson     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2873d50ef446SAnton Johansson     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
287435c653c4SRichard Henderson     if (likely(!crosspage)) {
2875f7eaf9d7SRichard Henderson         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2876f7eaf9d7SRichard Henderson             if ((l.memop & MO_BSWAP) != MO_LE) {
2877f7eaf9d7SRichard Henderson                 val = bswap128(val);
2878f7eaf9d7SRichard Henderson             }
2879d50ef446SAnton Johansson             do_st16_mmio_leN(cpu, l.page[0].full, val, addr, 16, l.mmu_idx, ra);
2880f7eaf9d7SRichard Henderson         } else if (unlikely(l.page[0].flags & TLB_DISCARD_WRITE)) {
2881f7eaf9d7SRichard Henderson             /* nothing */
2882f7eaf9d7SRichard Henderson         } else {
288335c653c4SRichard Henderson             /* Swap to host endian if necessary, then store. */
288435c653c4SRichard Henderson             if (l.memop & MO_BSWAP) {
288535c653c4SRichard Henderson                 val = bswap128(val);
288635c653c4SRichard Henderson             }
288773fda56fSAnton Johansson             store_atom_16(cpu, ra, l.page[0].haddr, l.memop, val);
288835c653c4SRichard Henderson         }
288935c653c4SRichard Henderson         return;
289035c653c4SRichard Henderson     }
289135c653c4SRichard Henderson 
289235c653c4SRichard Henderson     first = l.page[0].size;
289335c653c4SRichard Henderson     if (first == 8) {
289435c653c4SRichard Henderson         MemOp mop8 = (l.memop & ~(MO_SIZE | MO_BSWAP)) | MO_64;
289535c653c4SRichard Henderson 
289635c653c4SRichard Henderson         if (l.memop & MO_BSWAP) {
289735c653c4SRichard Henderson             val = bswap128(val);
289835c653c4SRichard Henderson         }
289935c653c4SRichard Henderson         if (HOST_BIG_ENDIAN) {
290035c653c4SRichard Henderson             b = int128_getlo(val), a = int128_gethi(val);
290135c653c4SRichard Henderson         } else {
290235c653c4SRichard Henderson             a = int128_getlo(val), b = int128_gethi(val);
290335c653c4SRichard Henderson         }
2904d50ef446SAnton Johansson         do_st_8(cpu, &l.page[0], a, l.mmu_idx, mop8, ra);
2905d50ef446SAnton Johansson         do_st_8(cpu, &l.page[1], b, l.mmu_idx, mop8, ra);
290635c653c4SRichard Henderson         return;
290735c653c4SRichard Henderson     }
290835c653c4SRichard Henderson 
290935c653c4SRichard Henderson     if ((l.memop & MO_BSWAP) != MO_LE) {
291035c653c4SRichard Henderson         val = bswap128(val);
291135c653c4SRichard Henderson     }
291235c653c4SRichard Henderson     if (first < 8) {
2913d50ef446SAnton Johansson         do_st_leN(cpu, &l.page[0], int128_getlo(val), l.mmu_idx, l.memop, ra);
291435c653c4SRichard Henderson         val = int128_urshift(val, first * 8);
2915d50ef446SAnton Johansson         do_st16_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
291635c653c4SRichard Henderson     } else {
2917d50ef446SAnton Johansson         b = do_st16_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2918d50ef446SAnton Johansson         do_st_leN(cpu, &l.page[1], b, l.mmu_idx, l.memop, ra);
291935c653c4SRichard Henderson     }
292035c653c4SRichard Henderson }
292135c653c4SRichard Henderson 
2922f83bcecbSRichard Henderson #include "ldst_common.c.inc"
2923cfe04a4bSRichard Henderson 
2924be9568b4SRichard Henderson /*
2925be9568b4SRichard Henderson  * First set of functions passes in OI and RETADDR.
2926be9568b4SRichard Henderson  * This makes them callable from other helpers.
2927be9568b4SRichard Henderson  */
2928d9bb58e5SYang Zhong 
2929d9bb58e5SYang Zhong #define ATOMIC_NAME(X) \
2930be9568b4SRichard Henderson     glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
2931a754f7f3SRichard Henderson 
2932707526adSRichard Henderson #define ATOMIC_MMU_CLEANUP
2933d9bb58e5SYang Zhong 
2934139c1837SPaolo Bonzini #include "atomic_common.c.inc"
2935d9bb58e5SYang Zhong 
2936d9bb58e5SYang Zhong #define DATA_SIZE 1
2937d9bb58e5SYang Zhong #include "atomic_template.h"
2938d9bb58e5SYang Zhong 
2939d9bb58e5SYang Zhong #define DATA_SIZE 2
2940d9bb58e5SYang Zhong #include "atomic_template.h"
2941d9bb58e5SYang Zhong 
2942d9bb58e5SYang Zhong #define DATA_SIZE 4
2943d9bb58e5SYang Zhong #include "atomic_template.h"
2944d9bb58e5SYang Zhong 
2945d9bb58e5SYang Zhong #ifdef CONFIG_ATOMIC64
2946d9bb58e5SYang Zhong #define DATA_SIZE 8
2947d9bb58e5SYang Zhong #include "atomic_template.h"
2948d9bb58e5SYang Zhong #endif
2949d9bb58e5SYang Zhong 
295076f9d6adSRichard Henderson #if defined(CONFIG_ATOMIC128) || HAVE_CMPXCHG128
2951d9bb58e5SYang Zhong #define DATA_SIZE 16
2952d9bb58e5SYang Zhong #include "atomic_template.h"
2953d9bb58e5SYang Zhong #endif
2954d9bb58e5SYang Zhong 
2955d9bb58e5SYang Zhong /* Code access functions.  */
2956d9bb58e5SYang Zhong 
2957fc4120a3SRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr)
2958eed56642SAlex Bennée {
29599002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(env, true));
2960d50ef446SAnton Johansson     return do_ld1_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
29614cef72d0SAlex Bennée }
29624cef72d0SAlex Bennée 
2963fc4120a3SRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr)
29642dd92606SRichard Henderson {
29659002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(env, true));
2966d50ef446SAnton Johansson     return do_ld2_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
29672dd92606SRichard Henderson }
29682dd92606SRichard Henderson 
2969fc4120a3SRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr)
29704cef72d0SAlex Bennée {
29719002ffcbSRichard Henderson     MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(env, true));
2972d50ef446SAnton Johansson     return do_ld4_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
2973eed56642SAlex Bennée }
2974d9bb58e5SYang Zhong 
2975fc4120a3SRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr)
2976eed56642SAlex Bennée {
2977fc313c64SFrédéric Pétrot     MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(env, true));
2978d50ef446SAnton Johansson     return do_ld8_mmu(env_cpu(env), addr, oi, 0, MMU_INST_FETCH);
2979eed56642SAlex Bennée }
298028990626SRichard Henderson 
298128990626SRichard Henderson uint8_t cpu_ldb_code_mmu(CPUArchState *env, abi_ptr addr,
298228990626SRichard Henderson                          MemOpIdx oi, uintptr_t retaddr)
298328990626SRichard Henderson {
2984d50ef446SAnton Johansson     return do_ld1_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
298528990626SRichard Henderson }
298628990626SRichard Henderson 
298728990626SRichard Henderson uint16_t cpu_ldw_code_mmu(CPUArchState *env, abi_ptr addr,
298828990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
298928990626SRichard Henderson {
2990d50ef446SAnton Johansson     return do_ld2_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
299128990626SRichard Henderson }
299228990626SRichard Henderson 
299328990626SRichard Henderson uint32_t cpu_ldl_code_mmu(CPUArchState *env, abi_ptr addr,
299428990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
299528990626SRichard Henderson {
2996d50ef446SAnton Johansson     return do_ld4_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
299728990626SRichard Henderson }
299828990626SRichard Henderson 
299928990626SRichard Henderson uint64_t cpu_ldq_code_mmu(CPUArchState *env, abi_ptr addr,
300028990626SRichard Henderson                           MemOpIdx oi, uintptr_t retaddr)
300128990626SRichard Henderson {
3002d50ef446SAnton Johansson     return do_ld8_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
300328990626SRichard Henderson }
3004