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