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