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