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