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