xref: /openbmc/qemu/accel/tcg/cputlb.c (revision da335fe1)
1 /*
2  *  Common CPU TLB handling
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/main-loop.h"
22 #include "hw/core/tcg-cpu-ops.h"
23 #include "exec/exec-all.h"
24 #include "exec/page-protection.h"
25 #include "exec/memory.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/cputlb.h"
28 #include "exec/tb-flush.h"
29 #include "exec/memory-internal.h"
30 #include "exec/ram_addr.h"
31 #include "exec/mmu-access-type.h"
32 #include "exec/tlb-common.h"
33 #include "exec/vaddr.h"
34 #include "tcg/tcg.h"
35 #include "qemu/error-report.h"
36 #include "exec/log.h"
37 #include "exec/helper-proto-common.h"
38 #include "qemu/atomic.h"
39 #include "qemu/atomic128.h"
40 #include "exec/translate-all.h"
41 #include "trace.h"
42 #include "tb-hash.h"
43 #include "internal-common.h"
44 #include "internal-target.h"
45 #ifdef CONFIG_PLUGIN
46 #include "qemu/plugin-memory.h"
47 #endif
48 #include "tcg/tcg-ldst.h"
49 #include "tcg/oversized-guest.h"
50 
51 /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
52 /* #define DEBUG_TLB */
53 /* #define DEBUG_TLB_LOG */
54 
55 #ifdef DEBUG_TLB
56 # define DEBUG_TLB_GATE 1
57 # ifdef DEBUG_TLB_LOG
58 #  define DEBUG_TLB_LOG_GATE 1
59 # else
60 #  define DEBUG_TLB_LOG_GATE 0
61 # endif
62 #else
63 # define DEBUG_TLB_GATE 0
64 # define DEBUG_TLB_LOG_GATE 0
65 #endif
66 
67 #define tlb_debug(fmt, ...) do { \
68     if (DEBUG_TLB_LOG_GATE) { \
69         qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
70                       ## __VA_ARGS__); \
71     } else if (DEBUG_TLB_GATE) { \
72         fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
73     } \
74 } while (0)
75 
76 #define assert_cpu_is_self(cpu) do {                              \
77         if (DEBUG_TLB_GATE) {                                     \
78             g_assert(!(cpu)->created || qemu_cpu_is_self(cpu));   \
79         }                                                         \
80     } while (0)
81 
82 /* run_on_cpu_data.target_ptr should always be big enough for a
83  * vaddr even on 32 bit builds
84  */
85 QEMU_BUILD_BUG_ON(sizeof(vaddr) > sizeof(run_on_cpu_data));
86 
87 /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
88  */
89 QEMU_BUILD_BUG_ON(NB_MMU_MODES > 16);
90 #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
91 
92 static inline size_t tlb_n_entries(CPUTLBDescFast *fast)
93 {
94     return (fast->mask >> CPU_TLB_ENTRY_BITS) + 1;
95 }
96 
97 static inline size_t sizeof_tlb(CPUTLBDescFast *fast)
98 {
99     return fast->mask + (1 << CPU_TLB_ENTRY_BITS);
100 }
101 
102 static inline uint64_t tlb_read_idx(const CPUTLBEntry *entry,
103                                     MMUAccessType access_type)
104 {
105     /* Do not rearrange the CPUTLBEntry structure members. */
106     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_read) !=
107                       MMU_DATA_LOAD * sizeof(uint64_t));
108     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_write) !=
109                       MMU_DATA_STORE * sizeof(uint64_t));
110     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_code) !=
111                       MMU_INST_FETCH * sizeof(uint64_t));
112 
113 #if TARGET_LONG_BITS == 32
114     /* Use qatomic_read, in case of addr_write; only care about low bits. */
115     const uint32_t *ptr = (uint32_t *)&entry->addr_idx[access_type];
116     ptr += HOST_BIG_ENDIAN;
117     return qatomic_read(ptr);
118 #else
119     const uint64_t *ptr = &entry->addr_idx[access_type];
120 # if TCG_OVERSIZED_GUEST
121     return *ptr;
122 # else
123     /* ofs might correspond to .addr_write, so use qatomic_read */
124     return qatomic_read(ptr);
125 # endif
126 #endif
127 }
128 
129 static inline uint64_t tlb_addr_write(const CPUTLBEntry *entry)
130 {
131     return tlb_read_idx(entry, MMU_DATA_STORE);
132 }
133 
134 /* Find the TLB index corresponding to the mmu_idx + address pair.  */
135 static inline uintptr_t tlb_index(CPUState *cpu, uintptr_t mmu_idx,
136                                   vaddr addr)
137 {
138     uintptr_t size_mask = cpu->neg.tlb.f[mmu_idx].mask >> CPU_TLB_ENTRY_BITS;
139 
140     return (addr >> TARGET_PAGE_BITS) & size_mask;
141 }
142 
143 /* Find the TLB entry corresponding to the mmu_idx + address pair.  */
144 static inline CPUTLBEntry *tlb_entry(CPUState *cpu, uintptr_t mmu_idx,
145                                      vaddr addr)
146 {
147     return &cpu->neg.tlb.f[mmu_idx].table[tlb_index(cpu, mmu_idx, addr)];
148 }
149 
150 static void tlb_window_reset(CPUTLBDesc *desc, int64_t ns,
151                              size_t max_entries)
152 {
153     desc->window_begin_ns = ns;
154     desc->window_max_entries = max_entries;
155 }
156 
157 static void tb_jmp_cache_clear_page(CPUState *cpu, vaddr page_addr)
158 {
159     CPUJumpCache *jc = cpu->tb_jmp_cache;
160     int i, i0;
161 
162     if (unlikely(!jc)) {
163         return;
164     }
165 
166     i0 = tb_jmp_cache_hash_page(page_addr);
167     for (i = 0; i < TB_JMP_PAGE_SIZE; i++) {
168         qatomic_set(&jc->array[i0 + i].tb, NULL);
169     }
170 }
171 
172 /**
173  * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
174  * @desc: The CPUTLBDesc portion of the TLB
175  * @fast: The CPUTLBDescFast portion of the same TLB
176  *
177  * Called with tlb_lock_held.
178  *
179  * We have two main constraints when resizing a TLB: (1) we only resize it
180  * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
181  * the array or unnecessarily flushing it), which means we do not control how
182  * frequently the resizing can occur; (2) we don't have access to the guest's
183  * future scheduling decisions, and therefore have to decide the magnitude of
184  * the resize based on past observations.
185  *
186  * In general, a memory-hungry process can benefit greatly from an appropriately
187  * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
188  * we just have to make the TLB as large as possible; while an oversized TLB
189  * results in minimal TLB miss rates, it also takes longer to be flushed
190  * (flushes can be _very_ frequent), and the reduced locality can also hurt
191  * performance.
192  *
193  * To achieve near-optimal performance for all kinds of workloads, we:
194  *
195  * 1. Aggressively increase the size of the TLB when the use rate of the
196  * TLB being flushed is high, since it is likely that in the near future this
197  * memory-hungry process will execute again, and its memory hungriness will
198  * probably be similar.
199  *
200  * 2. Slowly reduce the size of the TLB as the use rate declines over a
201  * reasonably large time window. The rationale is that if in such a time window
202  * we have not observed a high TLB use rate, it is likely that we won't observe
203  * it in the near future. In that case, once a time window expires we downsize
204  * the TLB to match the maximum use rate observed in the window.
205  *
206  * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
207  * since in that range performance is likely near-optimal. Recall that the TLB
208  * is direct mapped, so we want the use rate to be low (or at least not too
209  * high), since otherwise we are likely to have a significant amount of
210  * conflict misses.
211  */
212 static void tlb_mmu_resize_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast,
213                                   int64_t now)
214 {
215     size_t old_size = tlb_n_entries(fast);
216     size_t rate;
217     size_t new_size = old_size;
218     int64_t window_len_ms = 100;
219     int64_t window_len_ns = window_len_ms * 1000 * 1000;
220     bool window_expired = now > desc->window_begin_ns + window_len_ns;
221 
222     if (desc->n_used_entries > desc->window_max_entries) {
223         desc->window_max_entries = desc->n_used_entries;
224     }
225     rate = desc->window_max_entries * 100 / old_size;
226 
227     if (rate > 70) {
228         new_size = MIN(old_size << 1, 1 << CPU_TLB_DYN_MAX_BITS);
229     } else if (rate < 30 && window_expired) {
230         size_t ceil = pow2ceil(desc->window_max_entries);
231         size_t expected_rate = desc->window_max_entries * 100 / ceil;
232 
233         /*
234          * Avoid undersizing when the max number of entries seen is just below
235          * a pow2. For instance, if max_entries == 1025, the expected use rate
236          * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
237          * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
238          * later. Thus, make sure that the expected use rate remains below 70%.
239          * (and since we double the size, that means the lowest rate we'd
240          * expect to get is 35%, which is still in the 30-70% range where
241          * we consider that the size is appropriate.)
242          */
243         if (expected_rate > 70) {
244             ceil *= 2;
245         }
246         new_size = MAX(ceil, 1 << CPU_TLB_DYN_MIN_BITS);
247     }
248 
249     if (new_size == old_size) {
250         if (window_expired) {
251             tlb_window_reset(desc, now, desc->n_used_entries);
252         }
253         return;
254     }
255 
256     g_free(fast->table);
257     g_free(desc->fulltlb);
258 
259     tlb_window_reset(desc, now, 0);
260     /* desc->n_used_entries is cleared by the caller */
261     fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
262     fast->table = g_try_new(CPUTLBEntry, new_size);
263     desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size);
264 
265     /*
266      * If the allocations fail, try smaller sizes. We just freed some
267      * memory, so going back to half of new_size has a good chance of working.
268      * Increased memory pressure elsewhere in the system might cause the
269      * allocations to fail though, so we progressively reduce the allocation
270      * size, aborting if we cannot even allocate the smallest TLB we support.
271      */
272     while (fast->table == NULL || desc->fulltlb == NULL) {
273         if (new_size == (1 << CPU_TLB_DYN_MIN_BITS)) {
274             error_report("%s: %s", __func__, strerror(errno));
275             abort();
276         }
277         new_size = MAX(new_size >> 1, 1 << CPU_TLB_DYN_MIN_BITS);
278         fast->mask = (new_size - 1) << CPU_TLB_ENTRY_BITS;
279 
280         g_free(fast->table);
281         g_free(desc->fulltlb);
282         fast->table = g_try_new(CPUTLBEntry, new_size);
283         desc->fulltlb = g_try_new(CPUTLBEntryFull, new_size);
284     }
285 }
286 
287 static void tlb_mmu_flush_locked(CPUTLBDesc *desc, CPUTLBDescFast *fast)
288 {
289     desc->n_used_entries = 0;
290     desc->large_page_addr = -1;
291     desc->large_page_mask = -1;
292     desc->vindex = 0;
293     memset(fast->table, -1, sizeof_tlb(fast));
294     memset(desc->vtable, -1, sizeof(desc->vtable));
295 }
296 
297 static void tlb_flush_one_mmuidx_locked(CPUState *cpu, int mmu_idx,
298                                         int64_t now)
299 {
300     CPUTLBDesc *desc = &cpu->neg.tlb.d[mmu_idx];
301     CPUTLBDescFast *fast = &cpu->neg.tlb.f[mmu_idx];
302 
303     tlb_mmu_resize_locked(desc, fast, now);
304     tlb_mmu_flush_locked(desc, fast);
305 }
306 
307 static void tlb_mmu_init(CPUTLBDesc *desc, CPUTLBDescFast *fast, int64_t now)
308 {
309     size_t n_entries = 1 << CPU_TLB_DYN_DEFAULT_BITS;
310 
311     tlb_window_reset(desc, now, 0);
312     desc->n_used_entries = 0;
313     fast->mask = (n_entries - 1) << CPU_TLB_ENTRY_BITS;
314     fast->table = g_new(CPUTLBEntry, n_entries);
315     desc->fulltlb = g_new(CPUTLBEntryFull, n_entries);
316     tlb_mmu_flush_locked(desc, fast);
317 }
318 
319 static inline void tlb_n_used_entries_inc(CPUState *cpu, uintptr_t mmu_idx)
320 {
321     cpu->neg.tlb.d[mmu_idx].n_used_entries++;
322 }
323 
324 static inline void tlb_n_used_entries_dec(CPUState *cpu, uintptr_t mmu_idx)
325 {
326     cpu->neg.tlb.d[mmu_idx].n_used_entries--;
327 }
328 
329 void tlb_init(CPUState *cpu)
330 {
331     int64_t now = get_clock_realtime();
332     int i;
333 
334     qemu_spin_init(&cpu->neg.tlb.c.lock);
335 
336     /* All tlbs are initialized flushed. */
337     cpu->neg.tlb.c.dirty = 0;
338 
339     for (i = 0; i < NB_MMU_MODES; i++) {
340         tlb_mmu_init(&cpu->neg.tlb.d[i], &cpu->neg.tlb.f[i], now);
341     }
342 }
343 
344 void tlb_destroy(CPUState *cpu)
345 {
346     int i;
347 
348     qemu_spin_destroy(&cpu->neg.tlb.c.lock);
349     for (i = 0; i < NB_MMU_MODES; i++) {
350         CPUTLBDesc *desc = &cpu->neg.tlb.d[i];
351         CPUTLBDescFast *fast = &cpu->neg.tlb.f[i];
352 
353         g_free(fast->table);
354         g_free(desc->fulltlb);
355     }
356 }
357 
358 /* flush_all_helper: run fn across all cpus
359  *
360  * If the wait flag is set then the src cpu's helper will be queued as
361  * "safe" work and the loop exited creating a synchronisation point
362  * where all queued work will be finished before execution starts
363  * again.
364  */
365 static void flush_all_helper(CPUState *src, run_on_cpu_func fn,
366                              run_on_cpu_data d)
367 {
368     CPUState *cpu;
369 
370     CPU_FOREACH(cpu) {
371         if (cpu != src) {
372             async_run_on_cpu(cpu, fn, d);
373         }
374     }
375 }
376 
377 static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)
378 {
379     uint16_t asked = data.host_int;
380     uint16_t all_dirty, work, to_clean;
381     int64_t now = get_clock_realtime();
382 
383     assert_cpu_is_self(cpu);
384 
385     tlb_debug("mmu_idx:0x%04" PRIx16 "\n", asked);
386 
387     qemu_spin_lock(&cpu->neg.tlb.c.lock);
388 
389     all_dirty = cpu->neg.tlb.c.dirty;
390     to_clean = asked & all_dirty;
391     all_dirty &= ~to_clean;
392     cpu->neg.tlb.c.dirty = all_dirty;
393 
394     for (work = to_clean; work != 0; work &= work - 1) {
395         int mmu_idx = ctz32(work);
396         tlb_flush_one_mmuidx_locked(cpu, mmu_idx, now);
397     }
398 
399     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
400 
401     tcg_flush_jmp_cache(cpu);
402 
403     if (to_clean == ALL_MMUIDX_BITS) {
404         qatomic_set(&cpu->neg.tlb.c.full_flush_count,
405                     cpu->neg.tlb.c.full_flush_count + 1);
406     } else {
407         qatomic_set(&cpu->neg.tlb.c.part_flush_count,
408                     cpu->neg.tlb.c.part_flush_count + ctpop16(to_clean));
409         if (to_clean != asked) {
410             qatomic_set(&cpu->neg.tlb.c.elide_flush_count,
411                         cpu->neg.tlb.c.elide_flush_count +
412                         ctpop16(asked & ~to_clean));
413         }
414     }
415 }
416 
417 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
418 {
419     tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap);
420 
421     assert_cpu_is_self(cpu);
422 
423     tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap));
424 }
425 
426 void tlb_flush(CPUState *cpu)
427 {
428     tlb_flush_by_mmuidx(cpu, ALL_MMUIDX_BITS);
429 }
430 
431 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu, uint16_t idxmap)
432 {
433     const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
434 
435     tlb_debug("mmu_idx: 0x%"PRIx16"\n", idxmap);
436 
437     flush_all_helper(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
438     async_safe_run_on_cpu(src_cpu, fn, RUN_ON_CPU_HOST_INT(idxmap));
439 }
440 
441 void tlb_flush_all_cpus_synced(CPUState *src_cpu)
442 {
443     tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS);
444 }
445 
446 static bool tlb_hit_page_mask_anyprot(CPUTLBEntry *tlb_entry,
447                                       vaddr page, vaddr mask)
448 {
449     page &= mask;
450     mask &= TARGET_PAGE_MASK | TLB_INVALID_MASK;
451 
452     return (page == (tlb_entry->addr_read & mask) ||
453             page == (tlb_addr_write(tlb_entry) & mask) ||
454             page == (tlb_entry->addr_code & mask));
455 }
456 
457 static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry, vaddr page)
458 {
459     return tlb_hit_page_mask_anyprot(tlb_entry, page, -1);
460 }
461 
462 /**
463  * tlb_entry_is_empty - return true if the entry is not in use
464  * @te: pointer to CPUTLBEntry
465  */
466 static inline bool tlb_entry_is_empty(const CPUTLBEntry *te)
467 {
468     return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1;
469 }
470 
471 /* Called with tlb_c.lock held */
472 static bool tlb_flush_entry_mask_locked(CPUTLBEntry *tlb_entry,
473                                         vaddr page,
474                                         vaddr mask)
475 {
476     if (tlb_hit_page_mask_anyprot(tlb_entry, page, mask)) {
477         memset(tlb_entry, -1, sizeof(*tlb_entry));
478         return true;
479     }
480     return false;
481 }
482 
483 static inline bool tlb_flush_entry_locked(CPUTLBEntry *tlb_entry, vaddr page)
484 {
485     return tlb_flush_entry_mask_locked(tlb_entry, page, -1);
486 }
487 
488 /* Called with tlb_c.lock held */
489 static void tlb_flush_vtlb_page_mask_locked(CPUState *cpu, int mmu_idx,
490                                             vaddr page,
491                                             vaddr mask)
492 {
493     CPUTLBDesc *d = &cpu->neg.tlb.d[mmu_idx];
494     int k;
495 
496     assert_cpu_is_self(cpu);
497     for (k = 0; k < CPU_VTLB_SIZE; k++) {
498         if (tlb_flush_entry_mask_locked(&d->vtable[k], page, mask)) {
499             tlb_n_used_entries_dec(cpu, mmu_idx);
500         }
501     }
502 }
503 
504 static inline void tlb_flush_vtlb_page_locked(CPUState *cpu, int mmu_idx,
505                                               vaddr page)
506 {
507     tlb_flush_vtlb_page_mask_locked(cpu, mmu_idx, page, -1);
508 }
509 
510 static void tlb_flush_page_locked(CPUState *cpu, int midx, vaddr page)
511 {
512     vaddr lp_addr = cpu->neg.tlb.d[midx].large_page_addr;
513     vaddr lp_mask = cpu->neg.tlb.d[midx].large_page_mask;
514 
515     /* Check if we need to flush due to large pages.  */
516     if ((page & lp_mask) == lp_addr) {
517         tlb_debug("forcing full flush midx %d (%016"
518                   VADDR_PRIx "/%016" VADDR_PRIx ")\n",
519                   midx, lp_addr, lp_mask);
520         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
521     } else {
522         if (tlb_flush_entry_locked(tlb_entry(cpu, midx, page), page)) {
523             tlb_n_used_entries_dec(cpu, midx);
524         }
525         tlb_flush_vtlb_page_locked(cpu, midx, page);
526     }
527 }
528 
529 /**
530  * tlb_flush_page_by_mmuidx_async_0:
531  * @cpu: cpu on which to flush
532  * @addr: page of virtual address to flush
533  * @idxmap: set of mmu_idx to flush
534  *
535  * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
536  * at @addr from the tlbs indicated by @idxmap from @cpu.
537  */
538 static void tlb_flush_page_by_mmuidx_async_0(CPUState *cpu,
539                                              vaddr addr,
540                                              uint16_t idxmap)
541 {
542     int mmu_idx;
543 
544     assert_cpu_is_self(cpu);
545 
546     tlb_debug("page addr: %016" VADDR_PRIx " mmu_map:0x%x\n", addr, idxmap);
547 
548     qemu_spin_lock(&cpu->neg.tlb.c.lock);
549     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
550         if ((idxmap >> mmu_idx) & 1) {
551             tlb_flush_page_locked(cpu, mmu_idx, addr);
552         }
553     }
554     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
555 
556     /*
557      * Discard jump cache entries for any tb which might potentially
558      * overlap the flushed page, which includes the previous.
559      */
560     tb_jmp_cache_clear_page(cpu, addr - TARGET_PAGE_SIZE);
561     tb_jmp_cache_clear_page(cpu, addr);
562 }
563 
564 /**
565  * tlb_flush_page_by_mmuidx_async_1:
566  * @cpu: cpu on which to flush
567  * @data: encoded addr + idxmap
568  *
569  * Helper for tlb_flush_page_by_mmuidx and friends, called through
570  * async_run_on_cpu.  The idxmap parameter is encoded in the page
571  * offset of the target_ptr field.  This limits the set of mmu_idx
572  * that can be passed via this method.
573  */
574 static void tlb_flush_page_by_mmuidx_async_1(CPUState *cpu,
575                                              run_on_cpu_data data)
576 {
577     vaddr addr_and_idxmap = data.target_ptr;
578     vaddr addr = addr_and_idxmap & TARGET_PAGE_MASK;
579     uint16_t idxmap = addr_and_idxmap & ~TARGET_PAGE_MASK;
580 
581     tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
582 }
583 
584 typedef struct {
585     vaddr addr;
586     uint16_t idxmap;
587 } TLBFlushPageByMMUIdxData;
588 
589 /**
590  * tlb_flush_page_by_mmuidx_async_2:
591  * @cpu: cpu on which to flush
592  * @data: allocated addr + idxmap
593  *
594  * Helper for tlb_flush_page_by_mmuidx and friends, called through
595  * async_run_on_cpu.  The addr+idxmap parameters are stored in a
596  * TLBFlushPageByMMUIdxData structure that has been allocated
597  * specifically for this helper.  Free the structure when done.
598  */
599 static void tlb_flush_page_by_mmuidx_async_2(CPUState *cpu,
600                                              run_on_cpu_data data)
601 {
602     TLBFlushPageByMMUIdxData *d = data.host_ptr;
603 
604     tlb_flush_page_by_mmuidx_async_0(cpu, d->addr, d->idxmap);
605     g_free(d);
606 }
607 
608 void tlb_flush_page_by_mmuidx(CPUState *cpu, vaddr addr, uint16_t idxmap)
609 {
610     tlb_debug("addr: %016" VADDR_PRIx " mmu_idx:%" PRIx16 "\n", addr, idxmap);
611 
612     assert_cpu_is_self(cpu);
613 
614     /* This should already be page aligned */
615     addr &= TARGET_PAGE_MASK;
616 
617     tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
618 }
619 
620 void tlb_flush_page(CPUState *cpu, vaddr addr)
621 {
622     tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS);
623 }
624 
625 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
626                                               vaddr addr,
627                                               uint16_t idxmap)
628 {
629     tlb_debug("addr: %016" VADDR_PRIx " mmu_idx:%"PRIx16"\n", addr, idxmap);
630 
631     /* This should already be page aligned */
632     addr &= TARGET_PAGE_MASK;
633 
634     /*
635      * Allocate memory to hold addr+idxmap only when needed.
636      * See tlb_flush_page_by_mmuidx for details.
637      */
638     if (idxmap < TARGET_PAGE_SIZE) {
639         flush_all_helper(src_cpu, tlb_flush_page_by_mmuidx_async_1,
640                          RUN_ON_CPU_TARGET_PTR(addr | idxmap));
641         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_1,
642                               RUN_ON_CPU_TARGET_PTR(addr | idxmap));
643     } else {
644         CPUState *dst_cpu;
645         TLBFlushPageByMMUIdxData *d;
646 
647         /* Allocate a separate data block for each destination cpu.  */
648         CPU_FOREACH(dst_cpu) {
649             if (dst_cpu != src_cpu) {
650                 d = g_new(TLBFlushPageByMMUIdxData, 1);
651                 d->addr = addr;
652                 d->idxmap = idxmap;
653                 async_run_on_cpu(dst_cpu, tlb_flush_page_by_mmuidx_async_2,
654                                  RUN_ON_CPU_HOST_PTR(d));
655             }
656         }
657 
658         d = g_new(TLBFlushPageByMMUIdxData, 1);
659         d->addr = addr;
660         d->idxmap = idxmap;
661         async_safe_run_on_cpu(src_cpu, tlb_flush_page_by_mmuidx_async_2,
662                               RUN_ON_CPU_HOST_PTR(d));
663     }
664 }
665 
666 void tlb_flush_page_all_cpus_synced(CPUState *src, vaddr addr)
667 {
668     tlb_flush_page_by_mmuidx_all_cpus_synced(src, addr, ALL_MMUIDX_BITS);
669 }
670 
671 static void tlb_flush_range_locked(CPUState *cpu, int midx,
672                                    vaddr addr, vaddr len,
673                                    unsigned bits)
674 {
675     CPUTLBDesc *d = &cpu->neg.tlb.d[midx];
676     CPUTLBDescFast *f = &cpu->neg.tlb.f[midx];
677     vaddr mask = MAKE_64BIT_MASK(0, bits);
678 
679     /*
680      * If @bits is smaller than the tlb size, there may be multiple entries
681      * within the TLB; otherwise all addresses that match under @mask hit
682      * the same TLB entry.
683      * TODO: Perhaps allow bits to be a few bits less than the size.
684      * For now, just flush the entire TLB.
685      *
686      * If @len is larger than the tlb size, then it will take longer to
687      * test all of the entries in the TLB than it will to flush it all.
688      */
689     if (mask < f->mask || len > f->mask) {
690         tlb_debug("forcing full flush midx %d ("
691                   "%016" VADDR_PRIx "/%016" VADDR_PRIx "+%016" VADDR_PRIx ")\n",
692                   midx, addr, mask, len);
693         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
694         return;
695     }
696 
697     /*
698      * Check if we need to flush due to large pages.
699      * Because large_page_mask contains all 1's from the msb,
700      * we only need to test the end of the range.
701      */
702     if (((addr + len - 1) & d->large_page_mask) == d->large_page_addr) {
703         tlb_debug("forcing full flush midx %d ("
704                   "%016" VADDR_PRIx "/%016" VADDR_PRIx ")\n",
705                   midx, d->large_page_addr, d->large_page_mask);
706         tlb_flush_one_mmuidx_locked(cpu, midx, get_clock_realtime());
707         return;
708     }
709 
710     for (vaddr i = 0; i < len; i += TARGET_PAGE_SIZE) {
711         vaddr page = addr + i;
712         CPUTLBEntry *entry = tlb_entry(cpu, midx, page);
713 
714         if (tlb_flush_entry_mask_locked(entry, page, mask)) {
715             tlb_n_used_entries_dec(cpu, midx);
716         }
717         tlb_flush_vtlb_page_mask_locked(cpu, midx, page, mask);
718     }
719 }
720 
721 typedef struct {
722     vaddr addr;
723     vaddr len;
724     uint16_t idxmap;
725     uint16_t bits;
726 } TLBFlushRangeData;
727 
728 static void tlb_flush_range_by_mmuidx_async_0(CPUState *cpu,
729                                               TLBFlushRangeData d)
730 {
731     int mmu_idx;
732 
733     assert_cpu_is_self(cpu);
734 
735     tlb_debug("range: %016" VADDR_PRIx "/%u+%016" VADDR_PRIx " mmu_map:0x%x\n",
736               d.addr, d.bits, d.len, d.idxmap);
737 
738     qemu_spin_lock(&cpu->neg.tlb.c.lock);
739     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
740         if ((d.idxmap >> mmu_idx) & 1) {
741             tlb_flush_range_locked(cpu, mmu_idx, d.addr, d.len, d.bits);
742         }
743     }
744     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
745 
746     /*
747      * If the length is larger than the jump cache size, then it will take
748      * longer to clear each entry individually than it will to clear it all.
749      */
750     if (d.len >= (TARGET_PAGE_SIZE * TB_JMP_CACHE_SIZE)) {
751         tcg_flush_jmp_cache(cpu);
752         return;
753     }
754 
755     /*
756      * Discard jump cache entries for any tb which might potentially
757      * overlap the flushed pages, which includes the previous.
758      */
759     d.addr -= TARGET_PAGE_SIZE;
760     for (vaddr i = 0, n = d.len / TARGET_PAGE_SIZE + 1; i < n; i++) {
761         tb_jmp_cache_clear_page(cpu, d.addr);
762         d.addr += TARGET_PAGE_SIZE;
763     }
764 }
765 
766 static void tlb_flush_range_by_mmuidx_async_1(CPUState *cpu,
767                                               run_on_cpu_data data)
768 {
769     TLBFlushRangeData *d = data.host_ptr;
770     tlb_flush_range_by_mmuidx_async_0(cpu, *d);
771     g_free(d);
772 }
773 
774 void tlb_flush_range_by_mmuidx(CPUState *cpu, vaddr addr,
775                                vaddr len, uint16_t idxmap,
776                                unsigned bits)
777 {
778     TLBFlushRangeData d;
779 
780     assert_cpu_is_self(cpu);
781 
782     /*
783      * If all bits are significant, and len is small,
784      * this devolves to tlb_flush_page.
785      */
786     if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) {
787         tlb_flush_page_by_mmuidx(cpu, addr, idxmap);
788         return;
789     }
790     /* If no page bits are significant, this devolves to tlb_flush. */
791     if (bits < TARGET_PAGE_BITS) {
792         tlb_flush_by_mmuidx(cpu, idxmap);
793         return;
794     }
795 
796     /* This should already be page aligned */
797     d.addr = addr & TARGET_PAGE_MASK;
798     d.len = len;
799     d.idxmap = idxmap;
800     d.bits = bits;
801 
802     tlb_flush_range_by_mmuidx_async_0(cpu, d);
803 }
804 
805 void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, vaddr addr,
806                                    uint16_t idxmap, unsigned bits)
807 {
808     tlb_flush_range_by_mmuidx(cpu, addr, TARGET_PAGE_SIZE, idxmap, bits);
809 }
810 
811 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
812                                                vaddr addr,
813                                                vaddr len,
814                                                uint16_t idxmap,
815                                                unsigned bits)
816 {
817     TLBFlushRangeData d, *p;
818     CPUState *dst_cpu;
819 
820     /*
821      * If all bits are significant, and len is small,
822      * this devolves to tlb_flush_page.
823      */
824     if (bits >= TARGET_LONG_BITS && len <= TARGET_PAGE_SIZE) {
825         tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu, addr, idxmap);
826         return;
827     }
828     /* If no page bits are significant, this devolves to tlb_flush. */
829     if (bits < TARGET_PAGE_BITS) {
830         tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, idxmap);
831         return;
832     }
833 
834     /* This should already be page aligned */
835     d.addr = addr & TARGET_PAGE_MASK;
836     d.len = len;
837     d.idxmap = idxmap;
838     d.bits = bits;
839 
840     /* Allocate a separate data block for each destination cpu.  */
841     CPU_FOREACH(dst_cpu) {
842         if (dst_cpu != src_cpu) {
843             p = g_memdup(&d, sizeof(d));
844             async_run_on_cpu(dst_cpu, tlb_flush_range_by_mmuidx_async_1,
845                              RUN_ON_CPU_HOST_PTR(p));
846         }
847     }
848 
849     p = g_memdup(&d, sizeof(d));
850     async_safe_run_on_cpu(src_cpu, tlb_flush_range_by_mmuidx_async_1,
851                           RUN_ON_CPU_HOST_PTR(p));
852 }
853 
854 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *src_cpu,
855                                                    vaddr addr,
856                                                    uint16_t idxmap,
857                                                    unsigned bits)
858 {
859     tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu, addr, TARGET_PAGE_SIZE,
860                                               idxmap, bits);
861 }
862 
863 /* update the TLBs so that writes to code in the virtual page 'addr'
864    can be detected */
865 void tlb_protect_code(ram_addr_t ram_addr)
866 {
867     cpu_physical_memory_test_and_clear_dirty(ram_addr & TARGET_PAGE_MASK,
868                                              TARGET_PAGE_SIZE,
869                                              DIRTY_MEMORY_CODE);
870 }
871 
872 /* update the TLB so that writes in physical page 'phys_addr' are no longer
873    tested for self modifying code */
874 void tlb_unprotect_code(ram_addr_t ram_addr)
875 {
876     cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
877 }
878 
879 
880 /*
881  * Dirty write flag handling
882  *
883  * When the TCG code writes to a location it looks up the address in
884  * the TLB and uses that data to compute the final address. If any of
885  * the lower bits of the address are set then the slow path is forced.
886  * There are a number of reasons to do this but for normal RAM the
887  * most usual is detecting writes to code regions which may invalidate
888  * generated code.
889  *
890  * Other vCPUs might be reading their TLBs during guest execution, so we update
891  * te->addr_write with qatomic_set. We don't need to worry about this for
892  * oversized guests as MTTCG is disabled for them.
893  *
894  * Called with tlb_c.lock held.
895  */
896 static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry,
897                                          uintptr_t start, uintptr_t length)
898 {
899     uintptr_t addr = tlb_entry->addr_write;
900 
901     if ((addr & (TLB_INVALID_MASK | TLB_MMIO |
902                  TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) {
903         addr &= TARGET_PAGE_MASK;
904         addr += tlb_entry->addend;
905         if ((addr - start) < length) {
906 #if TARGET_LONG_BITS == 32
907             uint32_t *ptr_write = (uint32_t *)&tlb_entry->addr_write;
908             ptr_write += HOST_BIG_ENDIAN;
909             qatomic_set(ptr_write, *ptr_write | TLB_NOTDIRTY);
910 #elif TCG_OVERSIZED_GUEST
911             tlb_entry->addr_write |= TLB_NOTDIRTY;
912 #else
913             qatomic_set(&tlb_entry->addr_write,
914                         tlb_entry->addr_write | TLB_NOTDIRTY);
915 #endif
916         }
917     }
918 }
919 
920 /*
921  * Called with tlb_c.lock held.
922  * Called only from the vCPU context, i.e. the TLB's owner thread.
923  */
924 static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s)
925 {
926     *d = *s;
927 }
928 
929 /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
930  * the target vCPU).
931  * We must take tlb_c.lock to avoid racing with another vCPU update. The only
932  * thing actually updated is the target TLB entry ->addr_write flags.
933  */
934 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length)
935 {
936     int mmu_idx;
937 
938     qemu_spin_lock(&cpu->neg.tlb.c.lock);
939     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
940         unsigned int i;
941         unsigned int n = tlb_n_entries(&cpu->neg.tlb.f[mmu_idx]);
942 
943         for (i = 0; i < n; i++) {
944             tlb_reset_dirty_range_locked(&cpu->neg.tlb.f[mmu_idx].table[i],
945                                          start1, length);
946         }
947 
948         for (i = 0; i < CPU_VTLB_SIZE; i++) {
949             tlb_reset_dirty_range_locked(&cpu->neg.tlb.d[mmu_idx].vtable[i],
950                                          start1, length);
951         }
952     }
953     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
954 }
955 
956 /* Called with tlb_c.lock held */
957 static inline void tlb_set_dirty1_locked(CPUTLBEntry *tlb_entry,
958                                          vaddr addr)
959 {
960     if (tlb_entry->addr_write == (addr | TLB_NOTDIRTY)) {
961         tlb_entry->addr_write = addr;
962     }
963 }
964 
965 /* update the TLB corresponding to virtual page vaddr
966    so that it is no longer dirty */
967 static void tlb_set_dirty(CPUState *cpu, vaddr addr)
968 {
969     int mmu_idx;
970 
971     assert_cpu_is_self(cpu);
972 
973     addr &= TARGET_PAGE_MASK;
974     qemu_spin_lock(&cpu->neg.tlb.c.lock);
975     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
976         tlb_set_dirty1_locked(tlb_entry(cpu, mmu_idx, addr), addr);
977     }
978 
979     for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
980         int k;
981         for (k = 0; k < CPU_VTLB_SIZE; k++) {
982             tlb_set_dirty1_locked(&cpu->neg.tlb.d[mmu_idx].vtable[k], addr);
983         }
984     }
985     qemu_spin_unlock(&cpu->neg.tlb.c.lock);
986 }
987 
988 /* Our TLB does not support large pages, so remember the area covered by
989    large pages and trigger a full TLB flush if these are invalidated.  */
990 static void tlb_add_large_page(CPUState *cpu, int mmu_idx,
991                                vaddr addr, uint64_t size)
992 {
993     vaddr lp_addr = cpu->neg.tlb.d[mmu_idx].large_page_addr;
994     vaddr lp_mask = ~(size - 1);
995 
996     if (lp_addr == (vaddr)-1) {
997         /* No previous large page.  */
998         lp_addr = addr;
999     } else {
1000         /* Extend the existing region to include the new page.
1001            This is a compromise between unnecessary flushes and
1002            the cost of maintaining a full variable size TLB.  */
1003         lp_mask &= cpu->neg.tlb.d[mmu_idx].large_page_mask;
1004         while (((lp_addr ^ addr) & lp_mask) != 0) {
1005             lp_mask <<= 1;
1006         }
1007     }
1008     cpu->neg.tlb.d[mmu_idx].large_page_addr = lp_addr & lp_mask;
1009     cpu->neg.tlb.d[mmu_idx].large_page_mask = lp_mask;
1010 }
1011 
1012 static inline void tlb_set_compare(CPUTLBEntryFull *full, CPUTLBEntry *ent,
1013                                    vaddr address, int flags,
1014                                    MMUAccessType access_type, bool enable)
1015 {
1016     if (enable) {
1017         address |= flags & TLB_FLAGS_MASK;
1018         flags &= TLB_SLOW_FLAGS_MASK;
1019         if (flags) {
1020             address |= TLB_FORCE_SLOW;
1021         }
1022     } else {
1023         address = -1;
1024         flags = 0;
1025     }
1026     ent->addr_idx[access_type] = address;
1027     full->slow_flags[access_type] = flags;
1028 }
1029 
1030 /*
1031  * Add a new TLB entry. At most one entry for a given virtual address
1032  * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
1033  * supplied size is only used by tlb_flush_page.
1034  *
1035  * Called from TCG-generated code, which is under an RCU read-side
1036  * critical section.
1037  */
1038 void tlb_set_page_full(CPUState *cpu, int mmu_idx,
1039                        vaddr addr, CPUTLBEntryFull *full)
1040 {
1041     CPUTLB *tlb = &cpu->neg.tlb;
1042     CPUTLBDesc *desc = &tlb->d[mmu_idx];
1043     MemoryRegionSection *section;
1044     unsigned int index, read_flags, write_flags;
1045     uintptr_t addend;
1046     CPUTLBEntry *te, tn;
1047     hwaddr iotlb, xlat, sz, paddr_page;
1048     vaddr addr_page;
1049     int asidx, wp_flags, prot;
1050     bool is_ram, is_romd;
1051 
1052     assert_cpu_is_self(cpu);
1053 
1054     if (full->lg_page_size <= TARGET_PAGE_BITS) {
1055         sz = TARGET_PAGE_SIZE;
1056     } else {
1057         sz = (hwaddr)1 << full->lg_page_size;
1058         tlb_add_large_page(cpu, mmu_idx, addr, sz);
1059     }
1060     addr_page = addr & TARGET_PAGE_MASK;
1061     paddr_page = full->phys_addr & TARGET_PAGE_MASK;
1062 
1063     prot = full->prot;
1064     asidx = cpu_asidx_from_attrs(cpu, full->attrs);
1065     section = address_space_translate_for_iotlb(cpu, asidx, paddr_page,
1066                                                 &xlat, &sz, full->attrs, &prot);
1067     assert(sz >= TARGET_PAGE_SIZE);
1068 
1069     tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
1070               " prot=%x idx=%d\n",
1071               addr, full->phys_addr, prot, mmu_idx);
1072 
1073     read_flags = full->tlb_fill_flags;
1074     if (full->lg_page_size < TARGET_PAGE_BITS) {
1075         /* Repeat the MMU check and TLB fill on every access.  */
1076         read_flags |= TLB_INVALID_MASK;
1077     }
1078 
1079     is_ram = memory_region_is_ram(section->mr);
1080     is_romd = memory_region_is_romd(section->mr);
1081 
1082     if (is_ram || is_romd) {
1083         /* RAM and ROMD both have associated host memory. */
1084         addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
1085     } else {
1086         /* I/O does not; force the host address to NULL. */
1087         addend = 0;
1088     }
1089 
1090     write_flags = read_flags;
1091     if (is_ram) {
1092         iotlb = memory_region_get_ram_addr(section->mr) + xlat;
1093         assert(!(iotlb & ~TARGET_PAGE_MASK));
1094         /*
1095          * Computing is_clean is expensive; avoid all that unless
1096          * the page is actually writable.
1097          */
1098         if (prot & PAGE_WRITE) {
1099             if (section->readonly) {
1100                 write_flags |= TLB_DISCARD_WRITE;
1101             } else if (cpu_physical_memory_is_clean(iotlb)) {
1102                 write_flags |= TLB_NOTDIRTY;
1103             }
1104         }
1105     } else {
1106         /* I/O or ROMD */
1107         iotlb = memory_region_section_get_iotlb(cpu, section) + xlat;
1108         /*
1109          * Writes to romd devices must go through MMIO to enable write.
1110          * Reads to romd devices go through the ram_ptr found above,
1111          * but of course reads to I/O must go through MMIO.
1112          */
1113         write_flags |= TLB_MMIO;
1114         if (!is_romd) {
1115             read_flags = write_flags;
1116         }
1117     }
1118 
1119     wp_flags = cpu_watchpoint_address_matches(cpu, addr_page,
1120                                               TARGET_PAGE_SIZE);
1121 
1122     index = tlb_index(cpu, mmu_idx, addr_page);
1123     te = tlb_entry(cpu, mmu_idx, addr_page);
1124 
1125     /*
1126      * Hold the TLB lock for the rest of the function. We could acquire/release
1127      * the lock several times in the function, but it is faster to amortize the
1128      * acquisition cost by acquiring it just once. Note that this leads to
1129      * a longer critical section, but this is not a concern since the TLB lock
1130      * is unlikely to be contended.
1131      */
1132     qemu_spin_lock(&tlb->c.lock);
1133 
1134     /* Note that the tlb is no longer clean.  */
1135     tlb->c.dirty |= 1 << mmu_idx;
1136 
1137     /* Make sure there's no cached translation for the new page.  */
1138     tlb_flush_vtlb_page_locked(cpu, mmu_idx, addr_page);
1139 
1140     /*
1141      * Only evict the old entry to the victim tlb if it's for a
1142      * different page; otherwise just overwrite the stale data.
1143      */
1144     if (!tlb_hit_page_anyprot(te, addr_page) && !tlb_entry_is_empty(te)) {
1145         unsigned vidx = desc->vindex++ % CPU_VTLB_SIZE;
1146         CPUTLBEntry *tv = &desc->vtable[vidx];
1147 
1148         /* Evict the old entry into the victim tlb.  */
1149         copy_tlb_helper_locked(tv, te);
1150         desc->vfulltlb[vidx] = desc->fulltlb[index];
1151         tlb_n_used_entries_dec(cpu, mmu_idx);
1152     }
1153 
1154     /* refill the tlb */
1155     /*
1156      * When memory region is ram, iotlb contains a TARGET_PAGE_BITS
1157      * aligned ram_addr_t of the page base of the target RAM.
1158      * Otherwise, iotlb contains
1159      *  - a physical section number in the lower TARGET_PAGE_BITS
1160      *  - the offset within section->mr of the page base (I/O, ROMD) with the
1161      *    TARGET_PAGE_BITS masked off.
1162      * We subtract addr_page (which is page aligned and thus won't
1163      * disturb the low bits) to give an offset which can be added to the
1164      * (non-page-aligned) vaddr of the eventual memory access to get
1165      * the MemoryRegion offset for the access. Note that the vaddr we
1166      * subtract here is that of the page base, and not the same as the
1167      * vaddr we add back in io_prepare()/get_page_addr_code().
1168      */
1169     desc->fulltlb[index] = *full;
1170     full = &desc->fulltlb[index];
1171     full->xlat_section = iotlb - addr_page;
1172     full->phys_addr = paddr_page;
1173 
1174     /* Now calculate the new entry */
1175     tn.addend = addend - addr_page;
1176 
1177     tlb_set_compare(full, &tn, addr_page, read_flags,
1178                     MMU_INST_FETCH, prot & PAGE_EXEC);
1179 
1180     if (wp_flags & BP_MEM_READ) {
1181         read_flags |= TLB_WATCHPOINT;
1182     }
1183     tlb_set_compare(full, &tn, addr_page, read_flags,
1184                     MMU_DATA_LOAD, prot & PAGE_READ);
1185 
1186     if (prot & PAGE_WRITE_INV) {
1187         write_flags |= TLB_INVALID_MASK;
1188     }
1189     if (wp_flags & BP_MEM_WRITE) {
1190         write_flags |= TLB_WATCHPOINT;
1191     }
1192     tlb_set_compare(full, &tn, addr_page, write_flags,
1193                     MMU_DATA_STORE, prot & PAGE_WRITE);
1194 
1195     copy_tlb_helper_locked(te, &tn);
1196     tlb_n_used_entries_inc(cpu, mmu_idx);
1197     qemu_spin_unlock(&tlb->c.lock);
1198 }
1199 
1200 void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
1201                              hwaddr paddr, MemTxAttrs attrs, int prot,
1202                              int mmu_idx, uint64_t size)
1203 {
1204     CPUTLBEntryFull full = {
1205         .phys_addr = paddr,
1206         .attrs = attrs,
1207         .prot = prot,
1208         .lg_page_size = ctz64(size)
1209     };
1210 
1211     assert(is_power_of_2(size));
1212     tlb_set_page_full(cpu, mmu_idx, addr, &full);
1213 }
1214 
1215 void tlb_set_page(CPUState *cpu, vaddr addr,
1216                   hwaddr paddr, int prot,
1217                   int mmu_idx, uint64_t size)
1218 {
1219     tlb_set_page_with_attrs(cpu, addr, paddr, MEMTXATTRS_UNSPECIFIED,
1220                             prot, mmu_idx, size);
1221 }
1222 
1223 /*
1224  * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1225  * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1226  * be discarded and looked up again (e.g. via tlb_entry()).
1227  */
1228 static void tlb_fill(CPUState *cpu, vaddr addr, int size,
1229                      MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1230 {
1231     bool ok;
1232 
1233     /*
1234      * This is not a probe, so only valid return is success; failure
1235      * should result in exception + longjmp to the cpu loop.
1236      */
1237     ok = cpu->cc->tcg_ops->tlb_fill(cpu, addr, size,
1238                                     access_type, mmu_idx, false, retaddr);
1239     assert(ok);
1240 }
1241 
1242 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
1243                                         MMUAccessType access_type,
1244                                         int mmu_idx, uintptr_t retaddr)
1245 {
1246     cpu->cc->tcg_ops->do_unaligned_access(cpu, addr, access_type,
1247                                           mmu_idx, retaddr);
1248 }
1249 
1250 static MemoryRegionSection *
1251 io_prepare(hwaddr *out_offset, CPUState *cpu, hwaddr xlat,
1252            MemTxAttrs attrs, vaddr addr, uintptr_t retaddr)
1253 {
1254     MemoryRegionSection *section;
1255     hwaddr mr_offset;
1256 
1257     section = iotlb_to_section(cpu, xlat, attrs);
1258     mr_offset = (xlat & TARGET_PAGE_MASK) + addr;
1259     cpu->mem_io_pc = retaddr;
1260     if (!cpu->neg.can_do_io) {
1261         cpu_io_recompile(cpu, retaddr);
1262     }
1263 
1264     *out_offset = mr_offset;
1265     return section;
1266 }
1267 
1268 static void io_failed(CPUState *cpu, CPUTLBEntryFull *full, vaddr addr,
1269                       unsigned size, MMUAccessType access_type, int mmu_idx,
1270                       MemTxResult response, uintptr_t retaddr)
1271 {
1272     if (!cpu->ignore_memory_transaction_failures
1273         && cpu->cc->tcg_ops->do_transaction_failed) {
1274         hwaddr physaddr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
1275 
1276         cpu->cc->tcg_ops->do_transaction_failed(cpu, physaddr, addr, size,
1277                                                 access_type, mmu_idx,
1278                                                 full->attrs, response, retaddr);
1279     }
1280 }
1281 
1282 /* Return true if ADDR is present in the victim tlb, and has been copied
1283    back to the main tlb.  */
1284 static bool victim_tlb_hit(CPUState *cpu, size_t mmu_idx, size_t index,
1285                            MMUAccessType access_type, vaddr page)
1286 {
1287     size_t vidx;
1288 
1289     assert_cpu_is_self(cpu);
1290     for (vidx = 0; vidx < CPU_VTLB_SIZE; ++vidx) {
1291         CPUTLBEntry *vtlb = &cpu->neg.tlb.d[mmu_idx].vtable[vidx];
1292         uint64_t cmp = tlb_read_idx(vtlb, access_type);
1293 
1294         if (cmp == page) {
1295             /* Found entry in victim tlb, swap tlb and iotlb.  */
1296             CPUTLBEntry tmptlb, *tlb = &cpu->neg.tlb.f[mmu_idx].table[index];
1297 
1298             qemu_spin_lock(&cpu->neg.tlb.c.lock);
1299             copy_tlb_helper_locked(&tmptlb, tlb);
1300             copy_tlb_helper_locked(tlb, vtlb);
1301             copy_tlb_helper_locked(vtlb, &tmptlb);
1302             qemu_spin_unlock(&cpu->neg.tlb.c.lock);
1303 
1304             CPUTLBEntryFull *f1 = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1305             CPUTLBEntryFull *f2 = &cpu->neg.tlb.d[mmu_idx].vfulltlb[vidx];
1306             CPUTLBEntryFull tmpf;
1307             tmpf = *f1; *f1 = *f2; *f2 = tmpf;
1308             return true;
1309         }
1310     }
1311     return false;
1312 }
1313 
1314 static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
1315                            CPUTLBEntryFull *full, uintptr_t retaddr)
1316 {
1317     ram_addr_t ram_addr = mem_vaddr + full->xlat_section;
1318 
1319     trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
1320 
1321     if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
1322         tb_invalidate_phys_range_fast(ram_addr, size, retaddr);
1323     }
1324 
1325     /*
1326      * Set both VGA and migration bits for simplicity and to remove
1327      * the notdirty callback faster.
1328      */
1329     cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE);
1330 
1331     /* We remove the notdirty callback only if the code has been flushed. */
1332     if (!cpu_physical_memory_is_clean(ram_addr)) {
1333         trace_memory_notdirty_set_dirty(mem_vaddr);
1334         tlb_set_dirty(cpu, mem_vaddr);
1335     }
1336 }
1337 
1338 static int probe_access_internal(CPUState *cpu, vaddr addr,
1339                                  int fault_size, MMUAccessType access_type,
1340                                  int mmu_idx, bool nonfault,
1341                                  void **phost, CPUTLBEntryFull **pfull,
1342                                  uintptr_t retaddr, bool check_mem_cbs)
1343 {
1344     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1345     CPUTLBEntry *entry = tlb_entry(cpu, mmu_idx, addr);
1346     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
1347     vaddr page_addr = addr & TARGET_PAGE_MASK;
1348     int flags = TLB_FLAGS_MASK & ~TLB_FORCE_SLOW;
1349     bool force_mmio = check_mem_cbs && cpu_plugin_mem_cbs_enabled(cpu);
1350     CPUTLBEntryFull *full;
1351 
1352     if (!tlb_hit_page(tlb_addr, page_addr)) {
1353         if (!victim_tlb_hit(cpu, mmu_idx, index, access_type, page_addr)) {
1354             if (!cpu->cc->tcg_ops->tlb_fill(cpu, addr, fault_size, access_type,
1355                                             mmu_idx, nonfault, retaddr)) {
1356                 /* Non-faulting page table read failed.  */
1357                 *phost = NULL;
1358                 *pfull = NULL;
1359                 return TLB_INVALID_MASK;
1360             }
1361 
1362             /* TLB resize via tlb_fill may have moved the entry.  */
1363             index = tlb_index(cpu, mmu_idx, addr);
1364             entry = tlb_entry(cpu, mmu_idx, addr);
1365 
1366             /*
1367              * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately,
1368              * to force the next access through tlb_fill.  We've just
1369              * called tlb_fill, so we know that this entry *is* valid.
1370              */
1371             flags &= ~TLB_INVALID_MASK;
1372         }
1373         tlb_addr = tlb_read_idx(entry, access_type);
1374     }
1375     flags &= tlb_addr;
1376 
1377     *pfull = full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1378     flags |= full->slow_flags[access_type];
1379 
1380     /* Fold all "mmio-like" bits into TLB_MMIO.  This is not RAM.  */
1381     if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY | TLB_CHECK_ALIGNED))
1382         || (access_type != MMU_INST_FETCH && force_mmio)) {
1383         *phost = NULL;
1384         return TLB_MMIO;
1385     }
1386 
1387     /* Everything else is RAM. */
1388     *phost = (void *)((uintptr_t)addr + entry->addend);
1389     return flags;
1390 }
1391 
1392 int probe_access_full(CPUArchState *env, vaddr addr, int size,
1393                       MMUAccessType access_type, int mmu_idx,
1394                       bool nonfault, void **phost, CPUTLBEntryFull **pfull,
1395                       uintptr_t retaddr)
1396 {
1397     int flags = probe_access_internal(env_cpu(env), addr, size, access_type,
1398                                       mmu_idx, nonfault, phost, pfull, retaddr,
1399                                       true);
1400 
1401     /* Handle clean RAM pages.  */
1402     if (unlikely(flags & TLB_NOTDIRTY)) {
1403         int dirtysize = size == 0 ? 1 : size;
1404         notdirty_write(env_cpu(env), addr, dirtysize, *pfull, retaddr);
1405         flags &= ~TLB_NOTDIRTY;
1406     }
1407 
1408     return flags;
1409 }
1410 
1411 int probe_access_full_mmu(CPUArchState *env, vaddr addr, int size,
1412                           MMUAccessType access_type, int mmu_idx,
1413                           void **phost, CPUTLBEntryFull **pfull)
1414 {
1415     void *discard_phost;
1416     CPUTLBEntryFull *discard_tlb;
1417 
1418     /* privately handle users that don't need full results */
1419     phost = phost ? phost : &discard_phost;
1420     pfull = pfull ? pfull : &discard_tlb;
1421 
1422     int flags = probe_access_internal(env_cpu(env), addr, size, access_type,
1423                                       mmu_idx, true, phost, pfull, 0, false);
1424 
1425     /* Handle clean RAM pages.  */
1426     if (unlikely(flags & TLB_NOTDIRTY)) {
1427         int dirtysize = size == 0 ? 1 : size;
1428         notdirty_write(env_cpu(env), addr, dirtysize, *pfull, 0);
1429         flags &= ~TLB_NOTDIRTY;
1430     }
1431 
1432     return flags;
1433 }
1434 
1435 int probe_access_flags(CPUArchState *env, vaddr addr, int size,
1436                        MMUAccessType access_type, int mmu_idx,
1437                        bool nonfault, void **phost, uintptr_t retaddr)
1438 {
1439     CPUTLBEntryFull *full;
1440     int flags;
1441 
1442     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
1443 
1444     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
1445                                   mmu_idx, nonfault, phost, &full, retaddr,
1446                                   true);
1447 
1448     /* Handle clean RAM pages. */
1449     if (unlikely(flags & TLB_NOTDIRTY)) {
1450         int dirtysize = size == 0 ? 1 : size;
1451         notdirty_write(env_cpu(env), addr, dirtysize, full, retaddr);
1452         flags &= ~TLB_NOTDIRTY;
1453     }
1454 
1455     return flags;
1456 }
1457 
1458 void *probe_access(CPUArchState *env, vaddr addr, int size,
1459                    MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
1460 {
1461     CPUTLBEntryFull *full;
1462     void *host;
1463     int flags;
1464 
1465     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
1466 
1467     flags = probe_access_internal(env_cpu(env), addr, size, access_type,
1468                                   mmu_idx, false, &host, &full, retaddr,
1469                                   true);
1470 
1471     /* Per the interface, size == 0 merely faults the access. */
1472     if (size == 0) {
1473         return NULL;
1474     }
1475 
1476     if (unlikely(flags & (TLB_NOTDIRTY | TLB_WATCHPOINT))) {
1477         /* Handle watchpoints.  */
1478         if (flags & TLB_WATCHPOINT) {
1479             int wp_access = (access_type == MMU_DATA_STORE
1480                              ? BP_MEM_WRITE : BP_MEM_READ);
1481             cpu_check_watchpoint(env_cpu(env), addr, size,
1482                                  full->attrs, wp_access, retaddr);
1483         }
1484 
1485         /* Handle clean RAM pages.  */
1486         if (flags & TLB_NOTDIRTY) {
1487             notdirty_write(env_cpu(env), addr, size, full, retaddr);
1488         }
1489     }
1490 
1491     return host;
1492 }
1493 
1494 void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
1495                         MMUAccessType access_type, int mmu_idx)
1496 {
1497     CPUTLBEntryFull *full;
1498     void *host;
1499     int flags;
1500 
1501     flags = probe_access_internal(env_cpu(env), addr, 0, access_type,
1502                                   mmu_idx, true, &host, &full, 0, false);
1503 
1504     /* No combination of flags are expected by the caller. */
1505     return flags ? NULL : host;
1506 }
1507 
1508 /*
1509  * Return a ram_addr_t for the virtual address for execution.
1510  *
1511  * Return -1 if we can't translate and execute from an entire page
1512  * of RAM.  This will force us to execute by loading and translating
1513  * one insn at a time, without caching.
1514  *
1515  * NOTE: This function will trigger an exception if the page is
1516  * not executable.
1517  */
1518 tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, vaddr addr,
1519                                         void **hostp)
1520 {
1521     CPUTLBEntryFull *full;
1522     void *p;
1523 
1524     (void)probe_access_internal(env_cpu(env), addr, 1, MMU_INST_FETCH,
1525                                 cpu_mmu_index(env_cpu(env), true), false,
1526                                 &p, &full, 0, false);
1527     if (p == NULL) {
1528         return -1;
1529     }
1530 
1531     if (full->lg_page_size < TARGET_PAGE_BITS) {
1532         return -1;
1533     }
1534 
1535     if (hostp) {
1536         *hostp = p;
1537     }
1538     return qemu_ram_addr_from_host_nofail(p);
1539 }
1540 
1541 /* Load/store with atomicity primitives. */
1542 #include "ldst_atomicity.c.inc"
1543 
1544 #ifdef CONFIG_PLUGIN
1545 /*
1546  * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1547  * This should be a hot path as we will have just looked this path up
1548  * in the softmmu lookup code (or helper). We don't handle re-fills or
1549  * checking the victim table. This is purely informational.
1550  *
1551  * The one corner case is i/o write, which can cause changes to the
1552  * address space.  Those changes, and the corresponding tlb flush,
1553  * should be delayed until the next TB, so even then this ought not fail.
1554  * But check, Just in Case.
1555  */
1556 bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,
1557                        bool is_store, struct qemu_plugin_hwaddr *data)
1558 {
1559     CPUTLBEntry *tlbe = tlb_entry(cpu, mmu_idx, addr);
1560     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1561     MMUAccessType access_type = is_store ? MMU_DATA_STORE : MMU_DATA_LOAD;
1562     uint64_t tlb_addr = tlb_read_idx(tlbe, access_type);
1563     CPUTLBEntryFull *full;
1564 
1565     if (unlikely(!tlb_hit(tlb_addr, addr))) {
1566         return false;
1567     }
1568 
1569     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1570     data->phys_addr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
1571 
1572     /* We must have an iotlb entry for MMIO */
1573     if (tlb_addr & TLB_MMIO) {
1574         MemoryRegionSection *section =
1575             iotlb_to_section(cpu, full->xlat_section & ~TARGET_PAGE_MASK,
1576                              full->attrs);
1577         data->is_io = true;
1578         data->mr = section->mr;
1579     } else {
1580         data->is_io = false;
1581         data->mr = NULL;
1582     }
1583     return true;
1584 }
1585 #endif
1586 
1587 /*
1588  * Probe for a load/store operation.
1589  * Return the host address and into @flags.
1590  */
1591 
1592 typedef struct MMULookupPageData {
1593     CPUTLBEntryFull *full;
1594     void *haddr;
1595     vaddr addr;
1596     int flags;
1597     int size;
1598 } MMULookupPageData;
1599 
1600 typedef struct MMULookupLocals {
1601     MMULookupPageData page[2];
1602     MemOp memop;
1603     int mmu_idx;
1604 } MMULookupLocals;
1605 
1606 /**
1607  * mmu_lookup1: translate one page
1608  * @cpu: generic cpu state
1609  * @data: lookup parameters
1610  * @mmu_idx: virtual address context
1611  * @access_type: load/store/code
1612  * @ra: return address into tcg generated code, or 0
1613  *
1614  * Resolve the translation for the one page at @data.addr, filling in
1615  * the rest of @data with the results.  If the translation fails,
1616  * tlb_fill will longjmp out.  Return true if the softmmu tlb for
1617  * @mmu_idx may have resized.
1618  */
1619 static bool mmu_lookup1(CPUState *cpu, MMULookupPageData *data,
1620                         int mmu_idx, MMUAccessType access_type, uintptr_t ra)
1621 {
1622     vaddr addr = data->addr;
1623     uintptr_t index = tlb_index(cpu, mmu_idx, addr);
1624     CPUTLBEntry *entry = tlb_entry(cpu, mmu_idx, addr);
1625     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
1626     bool maybe_resized = false;
1627     CPUTLBEntryFull *full;
1628     int flags;
1629 
1630     /* If the TLB entry is for a different page, reload and try again.  */
1631     if (!tlb_hit(tlb_addr, addr)) {
1632         if (!victim_tlb_hit(cpu, mmu_idx, index, access_type,
1633                             addr & TARGET_PAGE_MASK)) {
1634             tlb_fill(cpu, addr, data->size, access_type, mmu_idx, ra);
1635             maybe_resized = true;
1636             index = tlb_index(cpu, mmu_idx, addr);
1637             entry = tlb_entry(cpu, mmu_idx, addr);
1638         }
1639         tlb_addr = tlb_read_idx(entry, access_type) & ~TLB_INVALID_MASK;
1640     }
1641 
1642     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1643     flags = tlb_addr & (TLB_FLAGS_MASK & ~TLB_FORCE_SLOW);
1644     flags |= full->slow_flags[access_type];
1645 
1646     data->full = full;
1647     data->flags = flags;
1648     /* Compute haddr speculatively; depending on flags it might be invalid. */
1649     data->haddr = (void *)((uintptr_t)addr + entry->addend);
1650 
1651     return maybe_resized;
1652 }
1653 
1654 /**
1655  * mmu_watch_or_dirty
1656  * @cpu: generic cpu state
1657  * @data: lookup parameters
1658  * @access_type: load/store/code
1659  * @ra: return address into tcg generated code, or 0
1660  *
1661  * Trigger watchpoints for @data.addr:@data.size;
1662  * record writes to protected clean pages.
1663  */
1664 static void mmu_watch_or_dirty(CPUState *cpu, MMULookupPageData *data,
1665                                MMUAccessType access_type, uintptr_t ra)
1666 {
1667     CPUTLBEntryFull *full = data->full;
1668     vaddr addr = data->addr;
1669     int flags = data->flags;
1670     int size = data->size;
1671 
1672     /* On watchpoint hit, this will longjmp out.  */
1673     if (flags & TLB_WATCHPOINT) {
1674         int wp = access_type == MMU_DATA_STORE ? BP_MEM_WRITE : BP_MEM_READ;
1675         cpu_check_watchpoint(cpu, addr, size, full->attrs, wp, ra);
1676         flags &= ~TLB_WATCHPOINT;
1677     }
1678 
1679     /* Note that notdirty is only set for writes. */
1680     if (flags & TLB_NOTDIRTY) {
1681         notdirty_write(cpu, addr, size, full, ra);
1682         flags &= ~TLB_NOTDIRTY;
1683     }
1684     data->flags = flags;
1685 }
1686 
1687 /**
1688  * mmu_lookup: translate page(s)
1689  * @cpu: generic cpu state
1690  * @addr: virtual address
1691  * @oi: combined mmu_idx and MemOp
1692  * @ra: return address into tcg generated code, or 0
1693  * @access_type: load/store/code
1694  * @l: output result
1695  *
1696  * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
1697  * bytes.  Return true if the lookup crosses a page boundary.
1698  */
1699 static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
1700                        uintptr_t ra, MMUAccessType type, MMULookupLocals *l)
1701 {
1702     unsigned a_bits;
1703     bool crosspage;
1704     int flags;
1705 
1706     l->memop = get_memop(oi);
1707     l->mmu_idx = get_mmuidx(oi);
1708 
1709     tcg_debug_assert(l->mmu_idx < NB_MMU_MODES);
1710 
1711     /* Handle CPU specific unaligned behaviour */
1712     a_bits = get_alignment_bits(l->memop);
1713     if (addr & ((1 << a_bits) - 1)) {
1714         cpu_unaligned_access(cpu, addr, type, l->mmu_idx, ra);
1715     }
1716 
1717     l->page[0].addr = addr;
1718     l->page[0].size = memop_size(l->memop);
1719     l->page[1].addr = (addr + l->page[0].size - 1) & TARGET_PAGE_MASK;
1720     l->page[1].size = 0;
1721     crosspage = (addr ^ l->page[1].addr) & TARGET_PAGE_MASK;
1722 
1723     if (likely(!crosspage)) {
1724         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
1725 
1726         flags = l->page[0].flags;
1727         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1728             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
1729         }
1730         if (unlikely(flags & TLB_BSWAP)) {
1731             l->memop ^= MO_BSWAP;
1732         }
1733     } else {
1734         /* Finish compute of page crossing. */
1735         int size0 = l->page[1].addr - addr;
1736         l->page[1].size = l->page[0].size - size0;
1737         l->page[0].size = size0;
1738 
1739         /*
1740          * Lookup both pages, recognizing exceptions from either.  If the
1741          * second lookup potentially resized, refresh first CPUTLBEntryFull.
1742          */
1743         mmu_lookup1(cpu, &l->page[0], l->mmu_idx, type, ra);
1744         if (mmu_lookup1(cpu, &l->page[1], l->mmu_idx, type, ra)) {
1745             uintptr_t index = tlb_index(cpu, l->mmu_idx, addr);
1746             l->page[0].full = &cpu->neg.tlb.d[l->mmu_idx].fulltlb[index];
1747         }
1748 
1749         flags = l->page[0].flags | l->page[1].flags;
1750         if (unlikely(flags & (TLB_WATCHPOINT | TLB_NOTDIRTY))) {
1751             mmu_watch_or_dirty(cpu, &l->page[0], type, ra);
1752             mmu_watch_or_dirty(cpu, &l->page[1], type, ra);
1753         }
1754 
1755         /*
1756          * Since target/sparc is the only user of TLB_BSWAP, and all
1757          * Sparc accesses are aligned, any treatment across two pages
1758          * would be arbitrary.  Refuse it until there's a use.
1759          */
1760         tcg_debug_assert((flags & TLB_BSWAP) == 0);
1761     }
1762 
1763     /*
1764      * This alignment check differs from the one above, in that this is
1765      * based on the atomicity of the operation. The intended use case is
1766      * the ARM memory type field of each PTE, where access to pages with
1767      * Device memory type require alignment.
1768      */
1769     if (unlikely(flags & TLB_CHECK_ALIGNED)) {
1770         MemOp size = l->memop & MO_SIZE;
1771 
1772         switch (l->memop & MO_ATOM_MASK) {
1773         case MO_ATOM_NONE:
1774             size = MO_8;
1775             break;
1776         case MO_ATOM_IFALIGN_PAIR:
1777         case MO_ATOM_WITHIN16_PAIR:
1778             size = size ? size - 1 : 0;
1779             break;
1780         default:
1781             break;
1782         }
1783         if (addr & ((1 << size) - 1)) {
1784             cpu_unaligned_access(cpu, addr, type, l->mmu_idx, ra);
1785         }
1786     }
1787 
1788     return crosspage;
1789 }
1790 
1791 /*
1792  * Probe for an atomic operation.  Do not allow unaligned operations,
1793  * or io operations to proceed.  Return the host address.
1794  */
1795 static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
1796                                int size, uintptr_t retaddr)
1797 {
1798     uintptr_t mmu_idx = get_mmuidx(oi);
1799     MemOp mop = get_memop(oi);
1800     int a_bits = get_alignment_bits(mop);
1801     uintptr_t index;
1802     CPUTLBEntry *tlbe;
1803     vaddr tlb_addr;
1804     void *hostaddr;
1805     CPUTLBEntryFull *full;
1806 
1807     tcg_debug_assert(mmu_idx < NB_MMU_MODES);
1808 
1809     /* Adjust the given return address.  */
1810     retaddr -= GETPC_ADJ;
1811 
1812     /* Enforce guest required alignment.  */
1813     if (unlikely(a_bits > 0 && (addr & ((1 << a_bits) - 1)))) {
1814         /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1815         cpu_unaligned_access(cpu, addr, MMU_DATA_STORE,
1816                              mmu_idx, retaddr);
1817     }
1818 
1819     /* Enforce qemu required alignment.  */
1820     if (unlikely(addr & (size - 1))) {
1821         /* We get here if guest alignment was not requested,
1822            or was not enforced by cpu_unaligned_access above.
1823            We might widen the access and emulate, but for now
1824            mark an exception and exit the cpu loop.  */
1825         goto stop_the_world;
1826     }
1827 
1828     index = tlb_index(cpu, mmu_idx, addr);
1829     tlbe = tlb_entry(cpu, mmu_idx, addr);
1830 
1831     /* Check TLB entry and enforce page permissions.  */
1832     tlb_addr = tlb_addr_write(tlbe);
1833     if (!tlb_hit(tlb_addr, addr)) {
1834         if (!victim_tlb_hit(cpu, mmu_idx, index, MMU_DATA_STORE,
1835                             addr & TARGET_PAGE_MASK)) {
1836             tlb_fill(cpu, addr, size,
1837                      MMU_DATA_STORE, mmu_idx, retaddr);
1838             index = tlb_index(cpu, mmu_idx, addr);
1839             tlbe = tlb_entry(cpu, mmu_idx, addr);
1840         }
1841         tlb_addr = tlb_addr_write(tlbe) & ~TLB_INVALID_MASK;
1842     }
1843 
1844     /*
1845      * Let the guest notice RMW on a write-only page.
1846      * We have just verified that the page is writable.
1847      * Subpage lookups may have left TLB_INVALID_MASK set,
1848      * but addr_read will only be -1 if PAGE_READ was unset.
1849      */
1850     if (unlikely(tlbe->addr_read == -1)) {
1851         tlb_fill(cpu, addr, size, MMU_DATA_LOAD, mmu_idx, retaddr);
1852         /*
1853          * Since we don't support reads and writes to different
1854          * addresses, and we do have the proper page loaded for
1855          * write, this shouldn't ever return.
1856          */
1857         g_assert_not_reached();
1858     }
1859     /* Collect tlb flags for read. */
1860     tlb_addr |= tlbe->addr_read;
1861 
1862     /* Notice an IO access or a needs-MMU-lookup access */
1863     if (unlikely(tlb_addr & (TLB_MMIO | TLB_DISCARD_WRITE))) {
1864         /* There's really nothing that can be done to
1865            support this apart from stop-the-world.  */
1866         goto stop_the_world;
1867     }
1868 
1869     hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
1870     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
1871 
1872     if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
1873         notdirty_write(cpu, addr, size, full, retaddr);
1874     }
1875 
1876     if (unlikely(tlb_addr & TLB_FORCE_SLOW)) {
1877         int wp_flags = 0;
1878 
1879         if (full->slow_flags[MMU_DATA_STORE] & TLB_WATCHPOINT) {
1880             wp_flags |= BP_MEM_WRITE;
1881         }
1882         if (full->slow_flags[MMU_DATA_LOAD] & TLB_WATCHPOINT) {
1883             wp_flags |= BP_MEM_READ;
1884         }
1885         if (wp_flags) {
1886             cpu_check_watchpoint(cpu, addr, size,
1887                                  full->attrs, wp_flags, retaddr);
1888         }
1889     }
1890 
1891     return hostaddr;
1892 
1893  stop_the_world:
1894     cpu_loop_exit_atomic(cpu, retaddr);
1895 }
1896 
1897 /*
1898  * Load Helpers
1899  *
1900  * We support two different access types. SOFTMMU_CODE_ACCESS is
1901  * specifically for reading instructions from system memory. It is
1902  * called by the translation loop and in some helpers where the code
1903  * is disassembled. It shouldn't be called directly by guest code.
1904  *
1905  * For the benefit of TCG generated code, we want to avoid the
1906  * complication of ABI-specific return type promotion and always
1907  * return a value extended to the register size of the host. This is
1908  * tcg_target_long, except in the case of a 32-bit host and 64-bit
1909  * data, and for that we always have uint64_t.
1910  *
1911  * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1912  */
1913 
1914 /**
1915  * do_ld_mmio_beN:
1916  * @cpu: generic cpu state
1917  * @full: page parameters
1918  * @ret_be: accumulated data
1919  * @addr: virtual address
1920  * @size: number of bytes
1921  * @mmu_idx: virtual address context
1922  * @ra: return address into tcg generated code, or 0
1923  * Context: BQL held
1924  *
1925  * Load @size bytes from @addr, which is memory-mapped i/o.
1926  * The bytes are concatenated in big-endian order with @ret_be.
1927  */
1928 static uint64_t int_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
1929                                 uint64_t ret_be, vaddr addr, int size,
1930                                 int mmu_idx, MMUAccessType type, uintptr_t ra,
1931                                 MemoryRegion *mr, hwaddr mr_offset)
1932 {
1933     do {
1934         MemOp this_mop;
1935         unsigned this_size;
1936         uint64_t val;
1937         MemTxResult r;
1938 
1939         /* Read aligned pieces up to 8 bytes. */
1940         this_mop = ctz32(size | (int)addr | 8);
1941         this_size = 1 << this_mop;
1942         this_mop |= MO_BE;
1943 
1944         r = memory_region_dispatch_read(mr, mr_offset, &val,
1945                                         this_mop, full->attrs);
1946         if (unlikely(r != MEMTX_OK)) {
1947             io_failed(cpu, full, addr, this_size, type, mmu_idx, r, ra);
1948         }
1949         if (this_size == 8) {
1950             return val;
1951         }
1952 
1953         ret_be = (ret_be << (this_size * 8)) | val;
1954         addr += this_size;
1955         mr_offset += this_size;
1956         size -= this_size;
1957     } while (size);
1958 
1959     return ret_be;
1960 }
1961 
1962 static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
1963                                uint64_t ret_be, vaddr addr, int size,
1964                                int mmu_idx, MMUAccessType type, uintptr_t ra)
1965 {
1966     MemoryRegionSection *section;
1967     MemoryRegion *mr;
1968     hwaddr mr_offset;
1969     MemTxAttrs attrs;
1970 
1971     tcg_debug_assert(size > 0 && size <= 8);
1972 
1973     attrs = full->attrs;
1974     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
1975     mr = section->mr;
1976 
1977     BQL_LOCK_GUARD();
1978     return int_ld_mmio_beN(cpu, full, ret_be, addr, size, mmu_idx,
1979                            type, ra, mr, mr_offset);
1980 }
1981 
1982 static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,
1983                                uint64_t ret_be, vaddr addr, int size,
1984                                int mmu_idx, uintptr_t ra)
1985 {
1986     MemoryRegionSection *section;
1987     MemoryRegion *mr;
1988     hwaddr mr_offset;
1989     MemTxAttrs attrs;
1990     uint64_t a, b;
1991 
1992     tcg_debug_assert(size > 8 && size <= 16);
1993 
1994     attrs = full->attrs;
1995     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
1996     mr = section->mr;
1997 
1998     BQL_LOCK_GUARD();
1999     a = int_ld_mmio_beN(cpu, full, ret_be, addr, size - 8, mmu_idx,
2000                         MMU_DATA_LOAD, ra, mr, mr_offset);
2001     b = int_ld_mmio_beN(cpu, full, ret_be, addr + size - 8, 8, mmu_idx,
2002                         MMU_DATA_LOAD, ra, mr, mr_offset + size - 8);
2003     return int128_make128(b, a);
2004 }
2005 
2006 /**
2007  * do_ld_bytes_beN
2008  * @p: translation parameters
2009  * @ret_be: accumulated data
2010  *
2011  * Load @p->size bytes from @p->haddr, which is RAM.
2012  * The bytes to concatenated in big-endian order with @ret_be.
2013  */
2014 static uint64_t do_ld_bytes_beN(MMULookupPageData *p, uint64_t ret_be)
2015 {
2016     uint8_t *haddr = p->haddr;
2017     int i, size = p->size;
2018 
2019     for (i = 0; i < size; i++) {
2020         ret_be = (ret_be << 8) | haddr[i];
2021     }
2022     return ret_be;
2023 }
2024 
2025 /**
2026  * do_ld_parts_beN
2027  * @p: translation parameters
2028  * @ret_be: accumulated data
2029  *
2030  * As do_ld_bytes_beN, but atomically on each aligned part.
2031  */
2032 static uint64_t do_ld_parts_beN(MMULookupPageData *p, uint64_t ret_be)
2033 {
2034     void *haddr = p->haddr;
2035     int size = p->size;
2036 
2037     do {
2038         uint64_t x;
2039         int n;
2040 
2041         /*
2042          * Find minimum of alignment and size.
2043          * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2044          * would have only checked the low bits of addr|size once at the start,
2045          * but is just as easy.
2046          */
2047         switch (((uintptr_t)haddr | size) & 7) {
2048         case 4:
2049             x = cpu_to_be32(load_atomic4(haddr));
2050             ret_be = (ret_be << 32) | x;
2051             n = 4;
2052             break;
2053         case 2:
2054         case 6:
2055             x = cpu_to_be16(load_atomic2(haddr));
2056             ret_be = (ret_be << 16) | x;
2057             n = 2;
2058             break;
2059         default:
2060             x = *(uint8_t *)haddr;
2061             ret_be = (ret_be << 8) | x;
2062             n = 1;
2063             break;
2064         case 0:
2065             g_assert_not_reached();
2066         }
2067         haddr += n;
2068         size -= n;
2069     } while (size != 0);
2070     return ret_be;
2071 }
2072 
2073 /**
2074  * do_ld_parts_be4
2075  * @p: translation parameters
2076  * @ret_be: accumulated data
2077  *
2078  * As do_ld_bytes_beN, but with one atomic load.
2079  * Four aligned bytes are guaranteed to cover the load.
2080  */
2081 static uint64_t do_ld_whole_be4(MMULookupPageData *p, uint64_t ret_be)
2082 {
2083     int o = p->addr & 3;
2084     uint32_t x = load_atomic4(p->haddr - o);
2085 
2086     x = cpu_to_be32(x);
2087     x <<= o * 8;
2088     x >>= (4 - p->size) * 8;
2089     return (ret_be << (p->size * 8)) | x;
2090 }
2091 
2092 /**
2093  * do_ld_parts_be8
2094  * @p: translation parameters
2095  * @ret_be: accumulated data
2096  *
2097  * As do_ld_bytes_beN, but with one atomic load.
2098  * Eight aligned bytes are guaranteed to cover the load.
2099  */
2100 static uint64_t do_ld_whole_be8(CPUState *cpu, uintptr_t ra,
2101                                 MMULookupPageData *p, uint64_t ret_be)
2102 {
2103     int o = p->addr & 7;
2104     uint64_t x = load_atomic8_or_exit(cpu, ra, p->haddr - o);
2105 
2106     x = cpu_to_be64(x);
2107     x <<= o * 8;
2108     x >>= (8 - p->size) * 8;
2109     return (ret_be << (p->size * 8)) | x;
2110 }
2111 
2112 /**
2113  * do_ld_parts_be16
2114  * @p: translation parameters
2115  * @ret_be: accumulated data
2116  *
2117  * As do_ld_bytes_beN, but with one atomic load.
2118  * 16 aligned bytes are guaranteed to cover the load.
2119  */
2120 static Int128 do_ld_whole_be16(CPUState *cpu, uintptr_t ra,
2121                                MMULookupPageData *p, uint64_t ret_be)
2122 {
2123     int o = p->addr & 15;
2124     Int128 x, y = load_atomic16_or_exit(cpu, ra, p->haddr - o);
2125     int size = p->size;
2126 
2127     if (!HOST_BIG_ENDIAN) {
2128         y = bswap128(y);
2129     }
2130     y = int128_lshift(y, o * 8);
2131     y = int128_urshift(y, (16 - size) * 8);
2132     x = int128_make64(ret_be);
2133     x = int128_lshift(x, size * 8);
2134     return int128_or(x, y);
2135 }
2136 
2137 /*
2138  * Wrapper for the above.
2139  */
2140 static uint64_t do_ld_beN(CPUState *cpu, MMULookupPageData *p,
2141                           uint64_t ret_be, int mmu_idx, MMUAccessType type,
2142                           MemOp mop, uintptr_t ra)
2143 {
2144     MemOp atom;
2145     unsigned tmp, half_size;
2146 
2147     if (unlikely(p->flags & TLB_MMIO)) {
2148         return do_ld_mmio_beN(cpu, p->full, ret_be, p->addr, p->size,
2149                               mmu_idx, type, ra);
2150     }
2151 
2152     /*
2153      * It is a given that we cross a page and therefore there is no
2154      * atomicity for the load as a whole, but subobjects may need attention.
2155      */
2156     atom = mop & MO_ATOM_MASK;
2157     switch (atom) {
2158     case MO_ATOM_SUBALIGN:
2159         return do_ld_parts_beN(p, ret_be);
2160 
2161     case MO_ATOM_IFALIGN_PAIR:
2162     case MO_ATOM_WITHIN16_PAIR:
2163         tmp = mop & MO_SIZE;
2164         tmp = tmp ? tmp - 1 : 0;
2165         half_size = 1 << tmp;
2166         if (atom == MO_ATOM_IFALIGN_PAIR
2167             ? p->size == half_size
2168             : p->size >= half_size) {
2169             if (!HAVE_al8_fast && p->size < 4) {
2170                 return do_ld_whole_be4(p, ret_be);
2171             } else {
2172                 return do_ld_whole_be8(cpu, ra, p, ret_be);
2173             }
2174         }
2175         /* fall through */
2176 
2177     case MO_ATOM_IFALIGN:
2178     case MO_ATOM_WITHIN16:
2179     case MO_ATOM_NONE:
2180         return do_ld_bytes_beN(p, ret_be);
2181 
2182     default:
2183         g_assert_not_reached();
2184     }
2185 }
2186 
2187 /*
2188  * Wrapper for the above, for 8 < size < 16.
2189  */
2190 static Int128 do_ld16_beN(CPUState *cpu, MMULookupPageData *p,
2191                           uint64_t a, int mmu_idx, MemOp mop, uintptr_t ra)
2192 {
2193     int size = p->size;
2194     uint64_t b;
2195     MemOp atom;
2196 
2197     if (unlikely(p->flags & TLB_MMIO)) {
2198         return do_ld16_mmio_beN(cpu, p->full, a, p->addr, size, mmu_idx, ra);
2199     }
2200 
2201     /*
2202      * It is a given that we cross a page and therefore there is no
2203      * atomicity for the load as a whole, but subobjects may need attention.
2204      */
2205     atom = mop & MO_ATOM_MASK;
2206     switch (atom) {
2207     case MO_ATOM_SUBALIGN:
2208         p->size = size - 8;
2209         a = do_ld_parts_beN(p, a);
2210         p->haddr += size - 8;
2211         p->size = 8;
2212         b = do_ld_parts_beN(p, 0);
2213         break;
2214 
2215     case MO_ATOM_WITHIN16_PAIR:
2216         /* Since size > 8, this is the half that must be atomic. */
2217         return do_ld_whole_be16(cpu, ra, p, a);
2218 
2219     case MO_ATOM_IFALIGN_PAIR:
2220         /*
2221          * Since size > 8, both halves are misaligned,
2222          * and so neither is atomic.
2223          */
2224     case MO_ATOM_IFALIGN:
2225     case MO_ATOM_WITHIN16:
2226     case MO_ATOM_NONE:
2227         p->size = size - 8;
2228         a = do_ld_bytes_beN(p, a);
2229         b = ldq_be_p(p->haddr + size - 8);
2230         break;
2231 
2232     default:
2233         g_assert_not_reached();
2234     }
2235 
2236     return int128_make128(b, a);
2237 }
2238 
2239 static uint8_t do_ld_1(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
2240                        MMUAccessType type, uintptr_t ra)
2241 {
2242     if (unlikely(p->flags & TLB_MMIO)) {
2243         return do_ld_mmio_beN(cpu, p->full, 0, p->addr, 1, mmu_idx, type, ra);
2244     } else {
2245         return *(uint8_t *)p->haddr;
2246     }
2247 }
2248 
2249 static uint16_t do_ld_2(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
2250                         MMUAccessType type, MemOp memop, uintptr_t ra)
2251 {
2252     uint16_t ret;
2253 
2254     if (unlikely(p->flags & TLB_MMIO)) {
2255         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 2, mmu_idx, type, ra);
2256         if ((memop & MO_BSWAP) == MO_LE) {
2257             ret = bswap16(ret);
2258         }
2259     } else {
2260         /* Perform the load host endian, then swap if necessary. */
2261         ret = load_atom_2(cpu, ra, p->haddr, memop);
2262         if (memop & MO_BSWAP) {
2263             ret = bswap16(ret);
2264         }
2265     }
2266     return ret;
2267 }
2268 
2269 static uint32_t do_ld_4(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
2270                         MMUAccessType type, MemOp memop, uintptr_t ra)
2271 {
2272     uint32_t ret;
2273 
2274     if (unlikely(p->flags & TLB_MMIO)) {
2275         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 4, mmu_idx, type, ra);
2276         if ((memop & MO_BSWAP) == MO_LE) {
2277             ret = bswap32(ret);
2278         }
2279     } else {
2280         /* Perform the load host endian. */
2281         ret = load_atom_4(cpu, ra, p->haddr, memop);
2282         if (memop & MO_BSWAP) {
2283             ret = bswap32(ret);
2284         }
2285     }
2286     return ret;
2287 }
2288 
2289 static uint64_t do_ld_8(CPUState *cpu, MMULookupPageData *p, int mmu_idx,
2290                         MMUAccessType type, MemOp memop, uintptr_t ra)
2291 {
2292     uint64_t ret;
2293 
2294     if (unlikely(p->flags & TLB_MMIO)) {
2295         ret = do_ld_mmio_beN(cpu, p->full, 0, p->addr, 8, mmu_idx, type, ra);
2296         if ((memop & MO_BSWAP) == MO_LE) {
2297             ret = bswap64(ret);
2298         }
2299     } else {
2300         /* Perform the load host endian. */
2301         ret = load_atom_8(cpu, ra, p->haddr, memop);
2302         if (memop & MO_BSWAP) {
2303             ret = bswap64(ret);
2304         }
2305     }
2306     return ret;
2307 }
2308 
2309 static uint8_t do_ld1_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
2310                           uintptr_t ra, MMUAccessType access_type)
2311 {
2312     MMULookupLocals l;
2313     bool crosspage;
2314 
2315     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2316     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
2317     tcg_debug_assert(!crosspage);
2318 
2319     return do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
2320 }
2321 
2322 static uint16_t do_ld2_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
2323                            uintptr_t ra, MMUAccessType access_type)
2324 {
2325     MMULookupLocals l;
2326     bool crosspage;
2327     uint16_t ret;
2328     uint8_t a, b;
2329 
2330     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2331     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
2332     if (likely(!crosspage)) {
2333         return do_ld_2(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
2334     }
2335 
2336     a = do_ld_1(cpu, &l.page[0], l.mmu_idx, access_type, ra);
2337     b = do_ld_1(cpu, &l.page[1], l.mmu_idx, access_type, ra);
2338 
2339     if ((l.memop & MO_BSWAP) == MO_LE) {
2340         ret = a | (b << 8);
2341     } else {
2342         ret = b | (a << 8);
2343     }
2344     return ret;
2345 }
2346 
2347 static uint32_t do_ld4_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
2348                            uintptr_t ra, MMUAccessType access_type)
2349 {
2350     MMULookupLocals l;
2351     bool crosspage;
2352     uint32_t ret;
2353 
2354     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2355     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
2356     if (likely(!crosspage)) {
2357         return do_ld_4(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
2358     }
2359 
2360     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2361     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
2362     if ((l.memop & MO_BSWAP) == MO_LE) {
2363         ret = bswap32(ret);
2364     }
2365     return ret;
2366 }
2367 
2368 static uint64_t do_ld8_mmu(CPUState *cpu, vaddr addr, MemOpIdx oi,
2369                            uintptr_t ra, MMUAccessType access_type)
2370 {
2371     MMULookupLocals l;
2372     bool crosspage;
2373     uint64_t ret;
2374 
2375     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2376     crosspage = mmu_lookup(cpu, addr, oi, ra, access_type, &l);
2377     if (likely(!crosspage)) {
2378         return do_ld_8(cpu, &l.page[0], l.mmu_idx, access_type, l.memop, ra);
2379     }
2380 
2381     ret = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx, access_type, l.memop, ra);
2382     ret = do_ld_beN(cpu, &l.page[1], ret, l.mmu_idx, access_type, l.memop, ra);
2383     if ((l.memop & MO_BSWAP) == MO_LE) {
2384         ret = bswap64(ret);
2385     }
2386     return ret;
2387 }
2388 
2389 static Int128 do_ld16_mmu(CPUState *cpu, vaddr addr,
2390                           MemOpIdx oi, uintptr_t ra)
2391 {
2392     MMULookupLocals l;
2393     bool crosspage;
2394     uint64_t a, b;
2395     Int128 ret;
2396     int first;
2397 
2398     cpu_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
2399     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_LOAD, &l);
2400     if (likely(!crosspage)) {
2401         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2402             ret = do_ld16_mmio_beN(cpu, l.page[0].full, 0, addr, 16,
2403                                    l.mmu_idx, ra);
2404             if ((l.memop & MO_BSWAP) == MO_LE) {
2405                 ret = bswap128(ret);
2406             }
2407         } else {
2408             /* Perform the load host endian. */
2409             ret = load_atom_16(cpu, ra, l.page[0].haddr, l.memop);
2410             if (l.memop & MO_BSWAP) {
2411                 ret = bswap128(ret);
2412             }
2413         }
2414         return ret;
2415     }
2416 
2417     first = l.page[0].size;
2418     if (first == 8) {
2419         MemOp mop8 = (l.memop & ~MO_SIZE) | MO_64;
2420 
2421         a = do_ld_8(cpu, &l.page[0], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
2422         b = do_ld_8(cpu, &l.page[1], l.mmu_idx, MMU_DATA_LOAD, mop8, ra);
2423         if ((mop8 & MO_BSWAP) == MO_LE) {
2424             ret = int128_make128(a, b);
2425         } else {
2426             ret = int128_make128(b, a);
2427         }
2428         return ret;
2429     }
2430 
2431     if (first < 8) {
2432         a = do_ld_beN(cpu, &l.page[0], 0, l.mmu_idx,
2433                       MMU_DATA_LOAD, l.memop, ra);
2434         ret = do_ld16_beN(cpu, &l.page[1], a, l.mmu_idx, l.memop, ra);
2435     } else {
2436         ret = do_ld16_beN(cpu, &l.page[0], 0, l.mmu_idx, l.memop, ra);
2437         b = int128_getlo(ret);
2438         ret = int128_lshift(ret, l.page[1].size * 8);
2439         a = int128_gethi(ret);
2440         b = do_ld_beN(cpu, &l.page[1], b, l.mmu_idx,
2441                       MMU_DATA_LOAD, l.memop, ra);
2442         ret = int128_make128(b, a);
2443     }
2444     if ((l.memop & MO_BSWAP) == MO_LE) {
2445         ret = bswap128(ret);
2446     }
2447     return ret;
2448 }
2449 
2450 /*
2451  * Store Helpers
2452  */
2453 
2454 /**
2455  * do_st_mmio_leN:
2456  * @cpu: generic cpu state
2457  * @full: page parameters
2458  * @val_le: data to store
2459  * @addr: virtual address
2460  * @size: number of bytes
2461  * @mmu_idx: virtual address context
2462  * @ra: return address into tcg generated code, or 0
2463  * Context: BQL held
2464  *
2465  * Store @size bytes at @addr, which is memory-mapped i/o.
2466  * The bytes to store are extracted in little-endian order from @val_le;
2467  * return the bytes of @val_le beyond @p->size that have not been stored.
2468  */
2469 static uint64_t int_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
2470                                 uint64_t val_le, vaddr addr, int size,
2471                                 int mmu_idx, uintptr_t ra,
2472                                 MemoryRegion *mr, hwaddr mr_offset)
2473 {
2474     do {
2475         MemOp this_mop;
2476         unsigned this_size;
2477         MemTxResult r;
2478 
2479         /* Store aligned pieces up to 8 bytes. */
2480         this_mop = ctz32(size | (int)addr | 8);
2481         this_size = 1 << this_mop;
2482         this_mop |= MO_LE;
2483 
2484         r = memory_region_dispatch_write(mr, mr_offset, val_le,
2485                                          this_mop, full->attrs);
2486         if (unlikely(r != MEMTX_OK)) {
2487             io_failed(cpu, full, addr, this_size, MMU_DATA_STORE,
2488                       mmu_idx, r, ra);
2489         }
2490         if (this_size == 8) {
2491             return 0;
2492         }
2493 
2494         val_le >>= this_size * 8;
2495         addr += this_size;
2496         mr_offset += this_size;
2497         size -= this_size;
2498     } while (size);
2499 
2500     return val_le;
2501 }
2502 
2503 static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
2504                                uint64_t val_le, vaddr addr, int size,
2505                                int mmu_idx, uintptr_t ra)
2506 {
2507     MemoryRegionSection *section;
2508     hwaddr mr_offset;
2509     MemoryRegion *mr;
2510     MemTxAttrs attrs;
2511 
2512     tcg_debug_assert(size > 0 && size <= 8);
2513 
2514     attrs = full->attrs;
2515     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
2516     mr = section->mr;
2517 
2518     BQL_LOCK_GUARD();
2519     return int_st_mmio_leN(cpu, full, val_le, addr, size, mmu_idx,
2520                            ra, mr, mr_offset);
2521 }
2522 
2523 static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,
2524                                  Int128 val_le, vaddr addr, int size,
2525                                  int mmu_idx, uintptr_t ra)
2526 {
2527     MemoryRegionSection *section;
2528     MemoryRegion *mr;
2529     hwaddr mr_offset;
2530     MemTxAttrs attrs;
2531 
2532     tcg_debug_assert(size > 8 && size <= 16);
2533 
2534     attrs = full->attrs;
2535     section = io_prepare(&mr_offset, cpu, full->xlat_section, attrs, addr, ra);
2536     mr = section->mr;
2537 
2538     BQL_LOCK_GUARD();
2539     int_st_mmio_leN(cpu, full, int128_getlo(val_le), addr, 8,
2540                     mmu_idx, ra, mr, mr_offset);
2541     return int_st_mmio_leN(cpu, full, int128_gethi(val_le), addr + 8,
2542                            size - 8, mmu_idx, ra, mr, mr_offset + 8);
2543 }
2544 
2545 /*
2546  * Wrapper for the above.
2547  */
2548 static uint64_t do_st_leN(CPUState *cpu, MMULookupPageData *p,
2549                           uint64_t val_le, int mmu_idx,
2550                           MemOp mop, uintptr_t ra)
2551 {
2552     MemOp atom;
2553     unsigned tmp, half_size;
2554 
2555     if (unlikely(p->flags & TLB_MMIO)) {
2556         return do_st_mmio_leN(cpu, p->full, val_le, p->addr,
2557                               p->size, mmu_idx, ra);
2558     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2559         return val_le >> (p->size * 8);
2560     }
2561 
2562     /*
2563      * It is a given that we cross a page and therefore there is no atomicity
2564      * for the store as a whole, but subobjects may need attention.
2565      */
2566     atom = mop & MO_ATOM_MASK;
2567     switch (atom) {
2568     case MO_ATOM_SUBALIGN:
2569         return store_parts_leN(p->haddr, p->size, val_le);
2570 
2571     case MO_ATOM_IFALIGN_PAIR:
2572     case MO_ATOM_WITHIN16_PAIR:
2573         tmp = mop & MO_SIZE;
2574         tmp = tmp ? tmp - 1 : 0;
2575         half_size = 1 << tmp;
2576         if (atom == MO_ATOM_IFALIGN_PAIR
2577             ? p->size == half_size
2578             : p->size >= half_size) {
2579             if (!HAVE_al8_fast && p->size <= 4) {
2580                 return store_whole_le4(p->haddr, p->size, val_le);
2581             } else if (HAVE_al8) {
2582                 return store_whole_le8(p->haddr, p->size, val_le);
2583             } else {
2584                 cpu_loop_exit_atomic(cpu, ra);
2585             }
2586         }
2587         /* fall through */
2588 
2589     case MO_ATOM_IFALIGN:
2590     case MO_ATOM_WITHIN16:
2591     case MO_ATOM_NONE:
2592         return store_bytes_leN(p->haddr, p->size, val_le);
2593 
2594     default:
2595         g_assert_not_reached();
2596     }
2597 }
2598 
2599 /*
2600  * Wrapper for the above, for 8 < size < 16.
2601  */
2602 static uint64_t do_st16_leN(CPUState *cpu, MMULookupPageData *p,
2603                             Int128 val_le, int mmu_idx,
2604                             MemOp mop, uintptr_t ra)
2605 {
2606     int size = p->size;
2607     MemOp atom;
2608 
2609     if (unlikely(p->flags & TLB_MMIO)) {
2610         return do_st16_mmio_leN(cpu, p->full, val_le, p->addr,
2611                                 size, mmu_idx, ra);
2612     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2613         return int128_gethi(val_le) >> ((size - 8) * 8);
2614     }
2615 
2616     /*
2617      * It is a given that we cross a page and therefore there is no atomicity
2618      * for the store as a whole, but subobjects may need attention.
2619      */
2620     atom = mop & MO_ATOM_MASK;
2621     switch (atom) {
2622     case MO_ATOM_SUBALIGN:
2623         store_parts_leN(p->haddr, 8, int128_getlo(val_le));
2624         return store_parts_leN(p->haddr + 8, p->size - 8,
2625                                int128_gethi(val_le));
2626 
2627     case MO_ATOM_WITHIN16_PAIR:
2628         /* Since size > 8, this is the half that must be atomic. */
2629         if (!HAVE_CMPXCHG128) {
2630             cpu_loop_exit_atomic(cpu, ra);
2631         }
2632         return store_whole_le16(p->haddr, p->size, val_le);
2633 
2634     case MO_ATOM_IFALIGN_PAIR:
2635         /*
2636          * Since size > 8, both halves are misaligned,
2637          * and so neither is atomic.
2638          */
2639     case MO_ATOM_IFALIGN:
2640     case MO_ATOM_WITHIN16:
2641     case MO_ATOM_NONE:
2642         stq_le_p(p->haddr, int128_getlo(val_le));
2643         return store_bytes_leN(p->haddr + 8, p->size - 8,
2644                                int128_gethi(val_le));
2645 
2646     default:
2647         g_assert_not_reached();
2648     }
2649 }
2650 
2651 static void do_st_1(CPUState *cpu, MMULookupPageData *p, uint8_t val,
2652                     int mmu_idx, uintptr_t ra)
2653 {
2654     if (unlikely(p->flags & TLB_MMIO)) {
2655         do_st_mmio_leN(cpu, p->full, val, p->addr, 1, mmu_idx, ra);
2656     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2657         /* nothing */
2658     } else {
2659         *(uint8_t *)p->haddr = val;
2660     }
2661 }
2662 
2663 static void do_st_2(CPUState *cpu, MMULookupPageData *p, uint16_t val,
2664                     int mmu_idx, MemOp memop, uintptr_t ra)
2665 {
2666     if (unlikely(p->flags & TLB_MMIO)) {
2667         if ((memop & MO_BSWAP) != MO_LE) {
2668             val = bswap16(val);
2669         }
2670         do_st_mmio_leN(cpu, p->full, val, p->addr, 2, mmu_idx, ra);
2671     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2672         /* nothing */
2673     } else {
2674         /* Swap to host endian if necessary, then store. */
2675         if (memop & MO_BSWAP) {
2676             val = bswap16(val);
2677         }
2678         store_atom_2(cpu, ra, p->haddr, memop, val);
2679     }
2680 }
2681 
2682 static void do_st_4(CPUState *cpu, MMULookupPageData *p, uint32_t val,
2683                     int mmu_idx, MemOp memop, uintptr_t ra)
2684 {
2685     if (unlikely(p->flags & TLB_MMIO)) {
2686         if ((memop & MO_BSWAP) != MO_LE) {
2687             val = bswap32(val);
2688         }
2689         do_st_mmio_leN(cpu, p->full, val, p->addr, 4, mmu_idx, ra);
2690     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2691         /* nothing */
2692     } else {
2693         /* Swap to host endian if necessary, then store. */
2694         if (memop & MO_BSWAP) {
2695             val = bswap32(val);
2696         }
2697         store_atom_4(cpu, ra, p->haddr, memop, val);
2698     }
2699 }
2700 
2701 static void do_st_8(CPUState *cpu, MMULookupPageData *p, uint64_t val,
2702                     int mmu_idx, MemOp memop, uintptr_t ra)
2703 {
2704     if (unlikely(p->flags & TLB_MMIO)) {
2705         if ((memop & MO_BSWAP) != MO_LE) {
2706             val = bswap64(val);
2707         }
2708         do_st_mmio_leN(cpu, p->full, val, p->addr, 8, mmu_idx, ra);
2709     } else if (unlikely(p->flags & TLB_DISCARD_WRITE)) {
2710         /* nothing */
2711     } else {
2712         /* Swap to host endian if necessary, then store. */
2713         if (memop & MO_BSWAP) {
2714             val = bswap64(val);
2715         }
2716         store_atom_8(cpu, ra, p->haddr, memop, val);
2717     }
2718 }
2719 
2720 static void do_st1_mmu(CPUState *cpu, vaddr addr, uint8_t val,
2721                        MemOpIdx oi, uintptr_t ra)
2722 {
2723     MMULookupLocals l;
2724     bool crosspage;
2725 
2726     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2727     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
2728     tcg_debug_assert(!crosspage);
2729 
2730     do_st_1(cpu, &l.page[0], val, l.mmu_idx, ra);
2731 }
2732 
2733 static void do_st2_mmu(CPUState *cpu, vaddr addr, uint16_t val,
2734                        MemOpIdx oi, uintptr_t ra)
2735 {
2736     MMULookupLocals l;
2737     bool crosspage;
2738     uint8_t a, b;
2739 
2740     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2741     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
2742     if (likely(!crosspage)) {
2743         do_st_2(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2744         return;
2745     }
2746 
2747     if ((l.memop & MO_BSWAP) == MO_LE) {
2748         a = val, b = val >> 8;
2749     } else {
2750         b = val, a = val >> 8;
2751     }
2752     do_st_1(cpu, &l.page[0], a, l.mmu_idx, ra);
2753     do_st_1(cpu, &l.page[1], b, l.mmu_idx, ra);
2754 }
2755 
2756 static void do_st4_mmu(CPUState *cpu, vaddr addr, uint32_t val,
2757                        MemOpIdx oi, uintptr_t ra)
2758 {
2759     MMULookupLocals l;
2760     bool crosspage;
2761 
2762     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2763     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
2764     if (likely(!crosspage)) {
2765         do_st_4(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2766         return;
2767     }
2768 
2769     /* Swap to little endian for simplicity, then store by bytes. */
2770     if ((l.memop & MO_BSWAP) != MO_LE) {
2771         val = bswap32(val);
2772     }
2773     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2774     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2775 }
2776 
2777 static void do_st8_mmu(CPUState *cpu, vaddr addr, uint64_t val,
2778                        MemOpIdx oi, uintptr_t ra)
2779 {
2780     MMULookupLocals l;
2781     bool crosspage;
2782 
2783     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2784     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
2785     if (likely(!crosspage)) {
2786         do_st_8(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2787         return;
2788     }
2789 
2790     /* Swap to little endian for simplicity, then store by bytes. */
2791     if ((l.memop & MO_BSWAP) != MO_LE) {
2792         val = bswap64(val);
2793     }
2794     val = do_st_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2795     (void) do_st_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2796 }
2797 
2798 static void do_st16_mmu(CPUState *cpu, vaddr addr, Int128 val,
2799                         MemOpIdx oi, uintptr_t ra)
2800 {
2801     MMULookupLocals l;
2802     bool crosspage;
2803     uint64_t a, b;
2804     int first;
2805 
2806     cpu_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
2807     crosspage = mmu_lookup(cpu, addr, oi, ra, MMU_DATA_STORE, &l);
2808     if (likely(!crosspage)) {
2809         if (unlikely(l.page[0].flags & TLB_MMIO)) {
2810             if ((l.memop & MO_BSWAP) != MO_LE) {
2811                 val = bswap128(val);
2812             }
2813             do_st16_mmio_leN(cpu, l.page[0].full, val, addr, 16, l.mmu_idx, ra);
2814         } else if (unlikely(l.page[0].flags & TLB_DISCARD_WRITE)) {
2815             /* nothing */
2816         } else {
2817             /* Swap to host endian if necessary, then store. */
2818             if (l.memop & MO_BSWAP) {
2819                 val = bswap128(val);
2820             }
2821             store_atom_16(cpu, ra, l.page[0].haddr, l.memop, val);
2822         }
2823         return;
2824     }
2825 
2826     first = l.page[0].size;
2827     if (first == 8) {
2828         MemOp mop8 = (l.memop & ~(MO_SIZE | MO_BSWAP)) | MO_64;
2829 
2830         if (l.memop & MO_BSWAP) {
2831             val = bswap128(val);
2832         }
2833         if (HOST_BIG_ENDIAN) {
2834             b = int128_getlo(val), a = int128_gethi(val);
2835         } else {
2836             a = int128_getlo(val), b = int128_gethi(val);
2837         }
2838         do_st_8(cpu, &l.page[0], a, l.mmu_idx, mop8, ra);
2839         do_st_8(cpu, &l.page[1], b, l.mmu_idx, mop8, ra);
2840         return;
2841     }
2842 
2843     if ((l.memop & MO_BSWAP) != MO_LE) {
2844         val = bswap128(val);
2845     }
2846     if (first < 8) {
2847         do_st_leN(cpu, &l.page[0], int128_getlo(val), l.mmu_idx, l.memop, ra);
2848         val = int128_urshift(val, first * 8);
2849         do_st16_leN(cpu, &l.page[1], val, l.mmu_idx, l.memop, ra);
2850     } else {
2851         b = do_st16_leN(cpu, &l.page[0], val, l.mmu_idx, l.memop, ra);
2852         do_st_leN(cpu, &l.page[1], b, l.mmu_idx, l.memop, ra);
2853     }
2854 }
2855 
2856 #include "ldst_common.c.inc"
2857 
2858 /*
2859  * First set of functions passes in OI and RETADDR.
2860  * This makes them callable from other helpers.
2861  */
2862 
2863 #define ATOMIC_NAME(X) \
2864     glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
2865 
2866 #define ATOMIC_MMU_CLEANUP
2867 
2868 #include "atomic_common.c.inc"
2869 
2870 #define DATA_SIZE 1
2871 #include "atomic_template.h"
2872 
2873 #define DATA_SIZE 2
2874 #include "atomic_template.h"
2875 
2876 #define DATA_SIZE 4
2877 #include "atomic_template.h"
2878 
2879 #ifdef CONFIG_ATOMIC64
2880 #define DATA_SIZE 8
2881 #include "atomic_template.h"
2882 #endif
2883 
2884 #if defined(CONFIG_ATOMIC128) || HAVE_CMPXCHG128
2885 #define DATA_SIZE 16
2886 #include "atomic_template.h"
2887 #endif
2888 
2889 /* Code access functions.  */
2890 
2891 uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr)
2892 {
2893     CPUState *cs = env_cpu(env);
2894     MemOpIdx oi = make_memop_idx(MO_UB, cpu_mmu_index(cs, true));
2895     return do_ld1_mmu(cs, addr, oi, 0, MMU_INST_FETCH);
2896 }
2897 
2898 uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr)
2899 {
2900     CPUState *cs = env_cpu(env);
2901     MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(cs, true));
2902     return do_ld2_mmu(cs, addr, oi, 0, MMU_INST_FETCH);
2903 }
2904 
2905 uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr)
2906 {
2907     CPUState *cs = env_cpu(env);
2908     MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(cs, true));
2909     return do_ld4_mmu(cs, addr, oi, 0, MMU_INST_FETCH);
2910 }
2911 
2912 uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr)
2913 {
2914     CPUState *cs = env_cpu(env);
2915     MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(cs, true));
2916     return do_ld8_mmu(cs, addr, oi, 0, MMU_INST_FETCH);
2917 }
2918 
2919 uint8_t cpu_ldb_code_mmu(CPUArchState *env, abi_ptr addr,
2920                          MemOpIdx oi, uintptr_t retaddr)
2921 {
2922     return do_ld1_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
2923 }
2924 
2925 uint16_t cpu_ldw_code_mmu(CPUArchState *env, abi_ptr addr,
2926                           MemOpIdx oi, uintptr_t retaddr)
2927 {
2928     return do_ld2_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
2929 }
2930 
2931 uint32_t cpu_ldl_code_mmu(CPUArchState *env, abi_ptr addr,
2932                           MemOpIdx oi, uintptr_t retaddr)
2933 {
2934     return do_ld4_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
2935 }
2936 
2937 uint64_t cpu_ldq_code_mmu(CPUArchState *env, abi_ptr addr,
2938                           MemOpIdx oi, uintptr_t retaddr)
2939 {
2940     return do_ld8_mmu(env_cpu(env), addr, oi, retaddr, MMU_INST_FETCH);
2941 }
2942