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