2 * Common CPU TLB handling
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
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"
30 #include "qemu/error-report.h"
32 #include "exec/helper-proto-common.h"
33 #include "qemu/atomic.h"
34 #include "qemu/atomic128.h"
35 #include "exec/translate-all.h"
40 #include "qemu/plugin-memory.h"
42 #include "tcg/tcg-ldst.h"
43 #include "tcg/oversized-guest.h"
45 /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
46 /* #define DEBUG_TLB */
47 /* #define DEBUG_TLB_LOG */
50 # define DEBUG_TLB_GATE 1
52 # define DEBUG_TLB_LOG_GATE 1
54 # define DEBUG_TLB_LOG_GATE 0
57 # define DEBUG_TLB_GATE 0
58 # define DEBUG_TLB_LOG_GATE 0
61 #define tlb_debug(fmt, ...) do { \
62 if (DEBUG_TLB_LOG_GATE) { \
63 qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
65 } else if (DEBUG_TLB_GATE) { \
66 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
70 #define assert_cpu_is_self(cpu) do { \
71 if (DEBUG_TLB_GATE) { \
72 g_assert(!(cpu)->created || qemu_cpu_is_self(cpu)); \
76 /* run_on_cpu_data.target_ptr should always be big enough for a
77 * vaddr even on 32 bit builds
79 QEMU_BUILD_BUG_ON(sizeof(vaddr
) > sizeof(run_on_cpu_data
));
81 /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
83 QEMU_BUILD_BUG_ON(NB_MMU_MODES
> 16);
84 #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
86 static inline size_t tlb_n_entries(CPUTLBDescFast
*fast
)
88 return (fast
->mask
>> CPU_TLB_ENTRY_BITS
) + 1;
91 static inline size_t sizeof_tlb(CPUTLBDescFast
*fast
)
93 return fast
->mask
+ (1 << CPU_TLB_ENTRY_BITS
);
96 static void tlb_window_reset(CPUTLBDesc
*desc
, int64_t ns
,
99 desc
->window_begin_ns
= ns
;
100 desc
->window_max_entries
= max_entries
;
103 static void tb_jmp_cache_clear_page(CPUState
*cpu
, vaddr page_addr
)
105 CPUJumpCache
*jc
= cpu
->tb_jmp_cache
;
112 i0
= tb_jmp_cache_hash_page(page_addr
);
113 for (i
= 0; i
< TB_JMP_PAGE_SIZE
; i
++) {
114 qatomic_set(&jc
->array
[i0
+ i
].tb
, NULL
);
119 * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
120 * @desc: The CPUTLBDesc portion of the TLB
121 * @fast: The CPUTLBDescFast portion of the same TLB
123 * Called with tlb_lock_held.
125 * We have two main constraints when resizing a TLB: (1) we only resize it
126 * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
127 * the array or unnecessarily flushing it), which means we do not control how
128 * frequently the resizing can occur; (2) we don't have access to the guest's
129 * future scheduling decisions, and therefore have to decide the magnitude of
130 * the resize based on past observations.
132 * In general, a memory-hungry process can benefit greatly from an appropriately
133 * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
134 * we just have to make the TLB as large as possible; while an oversized TLB
135 * results in minimal TLB miss rates, it also takes longer to be flushed
136 * (flushes can be _very_ frequent), and the reduced locality can also hurt
139 * To achieve near-optimal performance for all kinds of workloads, we:
141 * 1. Aggressively increase the size of the TLB when the use rate of the
142 * TLB being flushed is high, since it is likely that in the near future this
143 * memory-hungry process will execute again, and its memory hungriness will
144 * probably be similar.
146 * 2. Slowly reduce the size of the TLB as the use rate declines over a
147 * reasonably large time window. The rationale is that if in such a time window
148 * we have not observed a high TLB use rate, it is likely that we won't observe
149 * it in the near future. In that case, once a time window expires we downsize
150 * the TLB to match the maximum use rate observed in the window.
152 * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
153 * since in that range performance is likely near-optimal. Recall that the TLB
154 * is direct mapped, so we want the use rate to be low (or at least not too
155 * high), since otherwise we are likely to have a significant amount of
158 static void tlb_mmu_resize_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
,
161 size_t old_size
= tlb_n_entries(fast
);
163 size_t new_size
= old_size
;
164 int64_t window_len_ms
= 100;
165 int64_t window_len_ns
= window_len_ms
* 1000 * 1000;
166 bool window_expired
= now
> desc
->window_begin_ns
+ window_len_ns
;
168 if (desc
->n_used_entries
> desc
->window_max_entries
) {
169 desc
->window_max_entries
= desc
->n_used_entries
;
171 rate
= desc
->window_max_entries
* 100 / old_size
;
174 new_size
= MIN(old_size
<< 1, 1 << CPU_TLB_DYN_MAX_BITS
);
175 } else if (rate
< 30 && window_expired
) {
176 size_t ceil
= pow2ceil(desc
->window_max_entries
);
177 size_t expected_rate
= desc
->window_max_entries
* 100 / ceil
;
180 * Avoid undersizing when the max number of entries seen is just below
181 * a pow2. For instance, if max_entries == 1025, the expected use rate
182 * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
183 * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
184 * later. Thus, make sure that the expected use rate remains below 70%.
185 * (and since we double the size, that means the lowest rate we'd
186 * expect to get is 35%, which is still in the 30-70% range where
187 * we consider that the size is appropriate.)
189 if (expected_rate
> 70) {
192 new_size
= MAX(ceil
, 1 << CPU_TLB_DYN_MIN_BITS
);
195 if (new_size
== old_size
) {
196 if (window_expired
) {
197 tlb_window_reset(desc
, now
, desc
->n_used_entries
);
203 g_free(desc
->fulltlb
);
205 tlb_window_reset(desc
, now
, 0);
206 /* desc->n_used_entries is cleared by the caller */
207 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
208 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
209 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
212 * If the allocations fail, try smaller sizes. We just freed some
213 * memory, so going back to half of new_size has a good chance of working.
214 * Increased memory pressure elsewhere in the system might cause the
215 * allocations to fail though, so we progressively reduce the allocation
216 * size, aborting if we cannot even allocate the smallest TLB we support.
218 while (fast
->table
== NULL
|| desc
->fulltlb
== NULL
) {
219 if (new_size
== (1 << CPU_TLB_DYN_MIN_BITS
)) {
220 error_report("%s: %s", __func__
, strerror(errno
));
223 new_size
= MAX(new_size
>> 1, 1 << CPU_TLB_DYN_MIN_BITS
);
224 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
227 g_free(desc
->fulltlb
);
228 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
229 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
233 static void tlb_mmu_flush_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
)
235 desc
->n_used_entries
= 0;
236 desc
->large_page_addr
= -1;
237 desc
->large_page_mask
= -1;
239 memset(fast
->table
, -1, sizeof_tlb(fast
));
240 memset(desc
->vtable
, -1, sizeof(desc
->vtable
));
243 static void tlb_flush_one_mmuidx_locked(CPUArchState
*env
, int mmu_idx
,
246 CPUTLBDesc
*desc
= &env_tlb(env
)->d
[mmu_idx
];
247 CPUTLBDescFast
*fast
= &env_tlb(env
)->f
[mmu_idx
];
249 tlb_mmu_resize_locked(desc
, fast
, now
);
250 tlb_mmu_flush_locked(desc
, fast
);
253 static void tlb_mmu_init(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
, int64_t now
)
255 size_t n_entries
= 1 << CPU_TLB_DYN_DEFAULT_BITS
;
257 tlb_window_reset(desc
, now
, 0);
258 desc
->n_used_entries
= 0;
259 fast
->mask
= (n_entries
- 1) << CPU_TLB_ENTRY_BITS
;
260 fast
->table
= g_new(CPUTLBEntry
, n_entries
);
261 desc
->fulltlb
= g_new(CPUTLBEntryFull
, n_entries
);
262 tlb_mmu_flush_locked(desc
, fast
);
265 static inline void tlb_n_used_entries_inc(CPUArchState
*env
, uintptr_t mmu_idx
)
267 env_tlb(env
)->d
[mmu_idx
].n_used_entries
++;
270 static inline void tlb_n_used_entries_dec(CPUArchState
*env
, uintptr_t mmu_idx
)
272 env_tlb(env
)->d
[mmu_idx
].n_used_entries
--;
275 void tlb_init(CPUState
*cpu
)
277 CPUArchState
*env
= cpu
->env_ptr
;
278 int64_t now
= get_clock_realtime();
281 qemu_spin_init(&env_tlb(env
)->c
.lock
);
283 /* All tlbs are initialized flushed. */
284 env_tlb(env
)->c
.dirty
= 0;
286 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
287 tlb_mmu_init(&env_tlb(env
)->d
[i
], &env_tlb(env
)->f
[i
], now
);
291 void tlb_destroy(CPUState
*cpu
)
293 CPUArchState
*env
= cpu
->env_ptr
;
296 qemu_spin_destroy(&env_tlb(env
)->c
.lock
);
297 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
298 CPUTLBDesc
*desc
= &env_tlb(env
)->d
[i
];
299 CPUTLBDescFast
*fast
= &env_tlb(env
)->f
[i
];
302 g_free(desc
->fulltlb
);
306 /* flush_all_helper: run fn across all cpus
308 * If the wait flag is set then the src cpu's helper will be queued as
309 * "safe" work and the loop exited creating a synchronisation point
310 * where all queued work will be finished before execution starts
313 static void flush_all_helper(CPUState
*src
, run_on_cpu_func fn
,
320 async_run_on_cpu(cpu
, fn
, d
);
325 void tlb_flush_counts(size_t *pfull
, size_t *ppart
, size_t *pelide
)
328 size_t full
= 0, part
= 0, elide
= 0;
331 CPUArchState
*env
= cpu
->env_ptr
;
333 full
+= qatomic_read(&env_tlb(env
)->c
.full_flush_count
);
334 part
+= qatomic_read(&env_tlb(env
)->c
.part_flush_count
);
335 elide
+= qatomic_read(&env_tlb(env
)->c
.elide_flush_count
);
342 static void tlb_flush_by_mmuidx_async_work(CPUState
*cpu
, run_on_cpu_data data
)
344 CPUArchState
*env
= cpu
->env_ptr
;
345 uint16_t asked
= data
.host_int
;
346 uint16_t all_dirty
, work
, to_clean
;
347 int64_t now
= get_clock_realtime();
349 assert_cpu_is_self(cpu
);
351 tlb_debug("mmu_idx:0x%04" PRIx16
"\n", asked
);
353 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
355 all_dirty
= env_tlb(env
)->c
.dirty
;
356 to_clean
= asked
& all_dirty
;
357 all_dirty
&= ~to_clean
;
358 env_tlb(env
)->c
.dirty
= all_dirty
;
360 for (work
= to_clean
; work
!= 0; work
&= work
- 1) {
361 int mmu_idx
= ctz32(work
);
362 tlb_flush_one_mmuidx_locked(env
, mmu_idx
, now
);
365 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
367 tcg_flush_jmp_cache(cpu
);
369 if (to_clean
== ALL_MMUIDX_BITS
) {
370 qatomic_set(&env_tlb(env
)->c
.full_flush_count
,
371 env_tlb(env
)->c
.full_flush_count
+ 1);
373 qatomic_set(&env_tlb(env
)->c
.part_flush_count
,
374 env_tlb(env
)->c
.part_flush_count
+ ctpop16(to_clean
));
375 if (to_clean
!= asked
) {
376 qatomic_set(&env_tlb(env
)->c
.elide_flush_count
,
377 env_tlb(env
)->c
.elide_flush_count
+
378 ctpop16(asked
& ~to_clean
));
383 void tlb_flush_by_mmuidx(CPUState
*cpu
, uint16_t idxmap
)
385 tlb_debug("mmu_idx: 0x%" PRIx16
"\n", idxmap
);
387 if (cpu
->created
&& !qemu_cpu_is_self(cpu
)) {
388 async_run_on_cpu(cpu
, tlb_flush_by_mmuidx_async_work
,
389 RUN_ON_CPU_HOST_INT(idxmap
));
391 tlb_flush_by_mmuidx_async_work(cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
395 void tlb_flush(CPUState
*cpu
)
397 tlb_flush_by_mmuidx(cpu
, ALL_MMUIDX_BITS
);
400 void tlb_flush_by_mmuidx_all_cpus(CPUState
*src_cpu
, uint16_t idxmap
)
402 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
404 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
406 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
407 fn(src_cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
410 void tlb_flush_all_cpus(CPUState
*src_cpu
)
412 tlb_flush_by_mmuidx_all_cpus(src_cpu
, ALL_MMUIDX_BITS
);
415 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
, uint16_t idxmap
)
417 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
419 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
421 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
422 async_safe_run_on_cpu(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
425 void tlb_flush_all_cpus_synced(CPUState
*src_cpu
)
427 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, ALL_MMUIDX_BITS
);
430 static bool tlb_hit_page_mask_anyprot(CPUTLBEntry
*tlb_entry
,
431 vaddr page
, vaddr mask
)
434 mask
&= TARGET_PAGE_MASK
| TLB_INVALID_MASK
;
436 return (page
== (tlb_entry
->addr_read
& mask
) ||
437 page
== (tlb_addr_write(tlb_entry
) & mask
) ||
438 page
== (tlb_entry
->addr_code
& mask
));
441 static inline bool tlb_hit_page_anyprot(CPUTLBEntry
*tlb_entry
, vaddr page
)
443 return tlb_hit_page_mask_anyprot(tlb_entry
, page
, -1);
447 * tlb_entry_is_empty - return true if the entry is not in use
448 * @te: pointer to CPUTLBEntry
450 static inline bool tlb_entry_is_empty(const CPUTLBEntry
*te
)
452 return te
->addr_read
== -1 && te
->addr_write
== -1 && te
->addr_code
== -1;
455 /* Called with tlb_c.lock held */
456 static bool tlb_flush_entry_mask_locked(CPUTLBEntry
*tlb_entry
,
460 if (tlb_hit_page_mask_anyprot(tlb_entry
, page
, mask
)) {
461 memset(tlb_entry
, -1, sizeof(*tlb_entry
));
467 static inline bool tlb_flush_entry_locked(CPUTLBEntry
*tlb_entry
, vaddr page
)
469 return tlb_flush_entry_mask_locked(tlb_entry
, page
, -1);
472 /* Called with tlb_c.lock held */
473 static void tlb_flush_vtlb_page_mask_locked(CPUArchState
*env
, int mmu_idx
,
477 CPUTLBDesc
*d
= &env_tlb(env
)->d
[mmu_idx
];
480 assert_cpu_is_self(env_cpu(env
));
481 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
482 if (tlb_flush_entry_mask_locked(&d
->vtable
[k
], page
, mask
)) {
483 tlb_n_used_entries_dec(env
, mmu_idx
);
488 static inline void tlb_flush_vtlb_page_locked(CPUArchState
*env
, int mmu_idx
,
491 tlb_flush_vtlb_page_mask_locked(env
, mmu_idx
, page
, -1);
494 static void tlb_flush_page_locked(CPUArchState
*env
, int midx
, vaddr page
)
496 vaddr lp_addr
= env_tlb(env
)->d
[midx
].large_page_addr
;
497 vaddr lp_mask
= env_tlb(env
)->d
[midx
].large_page_mask
;
499 /* Check if we need to flush due to large pages. */
500 if ((page
& lp_mask
) == lp_addr
) {
501 tlb_debug("forcing full flush midx %d (%016"
502 VADDR_PRIx
"/%016" VADDR_PRIx
")\n",
503 midx
, lp_addr
, lp_mask
);
504 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
506 if (tlb_flush_entry_locked(tlb_entry(env
, midx
, page
), page
)) {
507 tlb_n_used_entries_dec(env
, midx
);
509 tlb_flush_vtlb_page_locked(env
, midx
, page
);
514 * tlb_flush_page_by_mmuidx_async_0:
515 * @cpu: cpu on which to flush
516 * @addr: page of virtual address to flush
517 * @idxmap: set of mmu_idx to flush
519 * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
520 * at @addr from the tlbs indicated by @idxmap from @cpu.
522 static void tlb_flush_page_by_mmuidx_async_0(CPUState
*cpu
,
526 CPUArchState
*env
= cpu
->env_ptr
;
529 assert_cpu_is_self(cpu
);
531 tlb_debug("page addr: %016" VADDR_PRIx
" mmu_map:0x%x\n", addr
, idxmap
);
533 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
534 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
535 if ((idxmap
>> mmu_idx
) & 1) {
536 tlb_flush_page_locked(env
, mmu_idx
, addr
);
539 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
542 * Discard jump cache entries for any tb which might potentially
543 * overlap the flushed page, which includes the previous.
545 tb_jmp_cache_clear_page(cpu
, addr
- TARGET_PAGE_SIZE
);
546 tb_jmp_cache_clear_page(cpu
, addr
);
550 * tlb_flush_page_by_mmuidx_async_1:
551 * @cpu: cpu on which to flush
552 * @data: encoded addr + idxmap
554 * Helper for tlb_flush_page_by_mmuidx and friends, called through
555 * async_run_on_cpu. The idxmap parameter is encoded in the page
556 * offset of the target_ptr field. This limits the set of mmu_idx
557 * that can be passed via this method.
559 static void tlb_flush_page_by_mmuidx_async_1(CPUState
*cpu
,
560 run_on_cpu_data data
)
562 vaddr addr_and_idxmap
= data
.target_ptr
;
563 vaddr addr
= addr_and_idxmap
& TARGET_PAGE_MASK
;
564 uint16_t idxmap
= addr_and_idxmap
& ~TARGET_PAGE_MASK
;
566 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
572 } TLBFlushPageByMMUIdxData
;
575 * tlb_flush_page_by_mmuidx_async_2:
576 * @cpu: cpu on which to flush
577 * @data: allocated addr + idxmap
579 * Helper for tlb_flush_page_by_mmuidx and friends, called through
580 * async_run_on_cpu. The addr+idxmap parameters are stored in a
581 * TLBFlushPageByMMUIdxData structure that has been allocated
582 * specifically for this helper. Free the structure when done.
584 static void tlb_flush_page_by_mmuidx_async_2(CPUState
*cpu
,
585 run_on_cpu_data data
)
587 TLBFlushPageByMMUIdxData
*d
= data
.host_ptr
;
589 tlb_flush_page_by_mmuidx_async_0(cpu
, d
->addr
, d
->idxmap
);
593 void tlb_flush_page_by_mmuidx(CPUState
*cpu
, vaddr addr
, uint16_t idxmap
)
595 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%" PRIx16
"\n", addr
, idxmap
);
597 /* This should already be page aligned */
598 addr
&= TARGET_PAGE_MASK
;
600 if (qemu_cpu_is_self(cpu
)) {
601 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
602 } else if (idxmap
< TARGET_PAGE_SIZE
) {
604 * Most targets have only a few mmu_idx. In the case where
605 * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid
606 * allocating memory for this operation.
608 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_1
,
609 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
611 TLBFlushPageByMMUIdxData
*d
= g_new(TLBFlushPageByMMUIdxData
, 1);
613 /* Otherwise allocate a structure, freed by the worker. */
616 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_2
,
617 RUN_ON_CPU_HOST_PTR(d
));
621 void tlb_flush_page(CPUState
*cpu
, vaddr addr
)
623 tlb_flush_page_by_mmuidx(cpu
, addr
, ALL_MMUIDX_BITS
);
626 void tlb_flush_page_by_mmuidx_all_cpus(CPUState
*src_cpu
, vaddr addr
,
629 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
631 /* This should already be page aligned */
632 addr
&= TARGET_PAGE_MASK
;
635 * Allocate memory to hold addr+idxmap only when needed.
636 * See tlb_flush_page_by_mmuidx for details.
638 if (idxmap
< TARGET_PAGE_SIZE
) {
639 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
640 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
644 /* Allocate a separate data block for each destination cpu. */
645 CPU_FOREACH(dst_cpu
) {
646 if (dst_cpu
!= src_cpu
) {
647 TLBFlushPageByMMUIdxData
*d
648 = g_new(TLBFlushPageByMMUIdxData
, 1);
652 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
653 RUN_ON_CPU_HOST_PTR(d
));
658 tlb_flush_page_by_mmuidx_async_0(src_cpu
, addr
, idxmap
);
661 void tlb_flush_page_all_cpus(CPUState
*src
, vaddr addr
)
663 tlb_flush_page_by_mmuidx_all_cpus(src
, addr
, ALL_MMUIDX_BITS
);
666 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
670 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
672 /* This should already be page aligned */
673 addr
&= TARGET_PAGE_MASK
;
676 * Allocate memory to hold addr+idxmap only when needed.
677 * See tlb_flush_page_by_mmuidx for details.
679 if (idxmap
< TARGET_PAGE_SIZE
) {
680 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
681 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
682 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
683 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
686 TLBFlushPageByMMUIdxData
*d
;
688 /* Allocate a separate data block for each destination cpu. */
689 CPU_FOREACH(dst_cpu
) {
690 if (dst_cpu
!= src_cpu
) {
691 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
694 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
695 RUN_ON_CPU_HOST_PTR(d
));
699 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
702 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_2
,
703 RUN_ON_CPU_HOST_PTR(d
));
707 void tlb_flush_page_all_cpus_synced(CPUState
*src
, vaddr addr
)
709 tlb_flush_page_by_mmuidx_all_cpus_synced(src
, addr
, ALL_MMUIDX_BITS
);
712 static void tlb_flush_range_locked(CPUArchState
*env
, int midx
,
713 vaddr addr
, vaddr len
,
716 CPUTLBDesc
*d
= &env_tlb(env
)->d
[midx
];
717 CPUTLBDescFast
*f
= &env_tlb(env
)->f
[midx
];
718 vaddr mask
= MAKE_64BIT_MASK(0, bits
);
721 * If @bits is smaller than the tlb size, there may be multiple entries
722 * within the TLB; otherwise all addresses that match under @mask hit
723 * the same TLB entry.
724 * TODO: Perhaps allow bits to be a few bits less than the size.
725 * For now, just flush the entire TLB.
727 * If @len is larger than the tlb size, then it will take longer to
728 * test all of the entries in the TLB than it will to flush it all.
730 if (mask
< f
->mask
|| len
> f
->mask
) {
731 tlb_debug("forcing full flush midx %d ("
732 "%016" VADDR_PRIx
"/%016" VADDR_PRIx
"+%016" VADDR_PRIx
")\n",
733 midx
, addr
, mask
, len
);
734 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
739 * Check if we need to flush due to large pages.
740 * Because large_page_mask contains all 1's from the msb,
741 * we only need to test the end of the range.
743 if (((addr
+ len
- 1) & d
->large_page_mask
) == d
->large_page_addr
) {
744 tlb_debug("forcing full flush midx %d ("
745 "%016" VADDR_PRIx
"/%016" VADDR_PRIx
")\n",
746 midx
, d
->large_page_addr
, d
->large_page_mask
);
747 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
751 for (vaddr i
= 0; i
< len
; i
+= TARGET_PAGE_SIZE
) {
752 vaddr page
= addr
+ i
;
753 CPUTLBEntry
*entry
= tlb_entry(env
, midx
, page
);
755 if (tlb_flush_entry_mask_locked(entry
, page
, mask
)) {
756 tlb_n_used_entries_dec(env
, midx
);
758 tlb_flush_vtlb_page_mask_locked(env
, midx
, page
, mask
);
769 static void tlb_flush_range_by_mmuidx_async_0(CPUState
*cpu
,
772 CPUArchState
*env
= cpu
->env_ptr
;
775 assert_cpu_is_self(cpu
);
777 tlb_debug("range: %016" VADDR_PRIx
"/%u+%016" VADDR_PRIx
" mmu_map:0x%x\n",
778 d
.addr
, d
.bits
, d
.len
, d
.idxmap
);
780 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
781 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
782 if ((d
.idxmap
>> mmu_idx
) & 1) {
783 tlb_flush_range_locked(env
, mmu_idx
, d
.addr
, d
.len
, d
.bits
);
786 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
789 * If the length is larger than the jump cache size, then it will take
790 * longer to clear each entry individually than it will to clear it all.
792 if (d
.len
>= (TARGET_PAGE_SIZE
* TB_JMP_CACHE_SIZE
)) {
793 tcg_flush_jmp_cache(cpu
);
798 * Discard jump cache entries for any tb which might potentially
799 * overlap the flushed pages, which includes the previous.
801 d
.addr
-= TARGET_PAGE_SIZE
;
802 for (vaddr i
= 0, n
= d
.len
/ TARGET_PAGE_SIZE
+ 1; i
< n
; i
++) {
803 tb_jmp_cache_clear_page(cpu
, d
.addr
);
804 d
.addr
+= TARGET_PAGE_SIZE
;
808 static void tlb_flush_range_by_mmuidx_async_1(CPUState
*cpu
,
809 run_on_cpu_data data
)
811 TLBFlushRangeData
*d
= data
.host_ptr
;
812 tlb_flush_range_by_mmuidx_async_0(cpu
, *d
);
816 void tlb_flush_range_by_mmuidx(CPUState
*cpu
, vaddr addr
,
817 vaddr len
, uint16_t idxmap
,
823 * If all bits are significant, and len is small,
824 * this devolves to tlb_flush_page.
826 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
827 tlb_flush_page_by_mmuidx(cpu
, addr
, idxmap
);
830 /* If no page bits are significant, this devolves to tlb_flush. */
831 if (bits
< TARGET_PAGE_BITS
) {
832 tlb_flush_by_mmuidx(cpu
, idxmap
);
836 /* This should already be page aligned */
837 d
.addr
= addr
& TARGET_PAGE_MASK
;
842 if (qemu_cpu_is_self(cpu
)) {
843 tlb_flush_range_by_mmuidx_async_0(cpu
, d
);
845 /* Otherwise allocate a structure, freed by the worker. */
846 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
847 async_run_on_cpu(cpu
, tlb_flush_range_by_mmuidx_async_1
,
848 RUN_ON_CPU_HOST_PTR(p
));
852 void tlb_flush_page_bits_by_mmuidx(CPUState
*cpu
, vaddr addr
,
853 uint16_t idxmap
, unsigned bits
)
855 tlb_flush_range_by_mmuidx(cpu
, addr
, TARGET_PAGE_SIZE
, idxmap
, bits
);
858 void tlb_flush_range_by_mmuidx_all_cpus(CPUState
*src_cpu
,
859 vaddr addr
, vaddr len
,
860 uint16_t idxmap
, unsigned bits
)
866 * If all bits are significant, and len is small,
867 * this devolves to tlb_flush_page.
869 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
870 tlb_flush_page_by_mmuidx_all_cpus(src_cpu
, addr
, idxmap
);
873 /* If no page bits are significant, this devolves to tlb_flush. */
874 if (bits
< TARGET_PAGE_BITS
) {
875 tlb_flush_by_mmuidx_all_cpus(src_cpu
, idxmap
);
879 /* This should already be page aligned */
880 d
.addr
= addr
& TARGET_PAGE_MASK
;
885 /* Allocate a separate data block for each destination cpu. */
886 CPU_FOREACH(dst_cpu
) {
887 if (dst_cpu
!= src_cpu
) {
888 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
889 async_run_on_cpu(dst_cpu
,
890 tlb_flush_range_by_mmuidx_async_1
,
891 RUN_ON_CPU_HOST_PTR(p
));
895 tlb_flush_range_by_mmuidx_async_0(src_cpu
, d
);
898 void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState
*src_cpu
,
899 vaddr addr
, uint16_t idxmap
,
902 tlb_flush_range_by_mmuidx_all_cpus(src_cpu
, addr
, TARGET_PAGE_SIZE
,
906 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
912 TLBFlushRangeData d
, *p
;
916 * If all bits are significant, and len is small,
917 * this devolves to tlb_flush_page.
919 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
920 tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu
, addr
, idxmap
);
923 /* If no page bits are significant, this devolves to tlb_flush. */
924 if (bits
< TARGET_PAGE_BITS
) {
925 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, idxmap
);
929 /* This should already be page aligned */
930 d
.addr
= addr
& TARGET_PAGE_MASK
;
935 /* Allocate a separate data block for each destination cpu. */
936 CPU_FOREACH(dst_cpu
) {
937 if (dst_cpu
!= src_cpu
) {
938 p
= g_memdup(&d
, sizeof(d
));
939 async_run_on_cpu(dst_cpu
, tlb_flush_range_by_mmuidx_async_1
,
940 RUN_ON_CPU_HOST_PTR(p
));
944 p
= g_memdup(&d
, sizeof(d
));
945 async_safe_run_on_cpu(src_cpu
, tlb_flush_range_by_mmuidx_async_1
,
946 RUN_ON_CPU_HOST_PTR(p
));
949 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
954 tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu
, addr
, TARGET_PAGE_SIZE
,
958 /* update the TLBs so that writes to code in the virtual page 'addr'
960 void tlb_protect_code(ram_addr_t ram_addr
)
962 cpu_physical_memory_test_and_clear_dirty(ram_addr
& TARGET_PAGE_MASK
,
967 /* update the TLB so that writes in physical page 'phys_addr' are no longer
968 tested for self modifying code */
969 void tlb_unprotect_code(ram_addr_t ram_addr
)
971 cpu_physical_memory_set_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
);
976 * Dirty write flag handling
978 * When the TCG code writes to a location it looks up the address in
979 * the TLB and uses that data to compute the final address. If any of
980 * the lower bits of the address are set then the slow path is forced.
981 * There are a number of reasons to do this but for normal RAM the
982 * most usual is detecting writes to code regions which may invalidate
985 * Other vCPUs might be reading their TLBs during guest execution, so we update
986 * te->addr_write with qatomic_set. We don't need to worry about this for
987 * oversized guests as MTTCG is disabled for them.
989 * Called with tlb_c.lock held.
991 static void tlb_reset_dirty_range_locked(CPUTLBEntry
*tlb_entry
,
992 uintptr_t start
, uintptr_t length
)
994 uintptr_t addr
= tlb_entry
->addr_write
;
996 if ((addr
& (TLB_INVALID_MASK
| TLB_MMIO
|
997 TLB_DISCARD_WRITE
| TLB_NOTDIRTY
)) == 0) {
998 addr
&= TARGET_PAGE_MASK
;
999 addr
+= tlb_entry
->addend
;
1000 if ((addr
- start
) < length
) {
1001 #if TARGET_LONG_BITS == 32
1002 uint32_t *ptr_write
= (uint32_t *)&tlb_entry
->addr_write
;
1003 ptr_write
+= HOST_BIG_ENDIAN
;
1004 qatomic_set(ptr_write
, *ptr_write
| TLB_NOTDIRTY
);
1005 #elif TCG_OVERSIZED_GUEST
1006 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
1008 qatomic_set(&tlb_entry
->addr_write
,
1009 tlb_entry
->addr_write
| TLB_NOTDIRTY
);
1016 * Called with tlb_c.lock held.
1017 * Called only from the vCPU context, i.e. the TLB's owner thread.
1019 static inline void copy_tlb_helper_locked(CPUTLBEntry
*d
, const CPUTLBEntry
*s
)
1024 /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
1026 * We must take tlb_c.lock to avoid racing with another vCPU update. The only
1027 * thing actually updated is the target TLB entry ->addr_write flags.
1029 void tlb_reset_dirty(CPUState
*cpu
, ram_addr_t start1
, ram_addr_t length
)
1036 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
1037 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1039 unsigned int n
= tlb_n_entries(&env_tlb(env
)->f
[mmu_idx
]);
1041 for (i
= 0; i
< n
; i
++) {
1042 tlb_reset_dirty_range_locked(&env_tlb(env
)->f
[mmu_idx
].table
[i
],
1046 for (i
= 0; i
< CPU_VTLB_SIZE
; i
++) {
1047 tlb_reset_dirty_range_locked(&env_tlb(env
)->d
[mmu_idx
].vtable
[i
],
1051 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
1054 /* Called with tlb_c.lock held */
1055 static inline void tlb_set_dirty1_locked(CPUTLBEntry
*tlb_entry
,
1058 if (tlb_entry
->addr_write
== (addr
| TLB_NOTDIRTY
)) {
1059 tlb_entry
->addr_write
= addr
;
1063 /* update the TLB corresponding to virtual page vaddr
1064 so that it is no longer dirty */
1065 void tlb_set_dirty(CPUState
*cpu
, vaddr addr
)
1067 CPUArchState
*env
= cpu
->env_ptr
;
1070 assert_cpu_is_self(cpu
);
1072 addr
&= TARGET_PAGE_MASK
;
1073 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
1074 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1075 tlb_set_dirty1_locked(tlb_entry(env
, mmu_idx
, addr
), addr
);
1078 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1080 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
1081 tlb_set_dirty1_locked(&env_tlb(env
)->d
[mmu_idx
].vtable
[k
], addr
);
1084 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
1087 /* Our TLB does not support large pages, so remember the area covered by
1088 large pages and trigger a full TLB flush if these are invalidated. */
1089 static void tlb_add_large_page(CPUArchState
*env
, int mmu_idx
,
1090 vaddr addr
, uint64_t size
)
1092 vaddr lp_addr
= env_tlb(env
)->d
[mmu_idx
].large_page_addr
;
1093 vaddr lp_mask
= ~(size
- 1);
1095 if (lp_addr
== (vaddr
)-1) {
1096 /* No previous large page. */
1099 /* Extend the existing region to include the new page.
1100 This is a compromise between unnecessary flushes and
1101 the cost of maintaining a full variable size TLB. */
1102 lp_mask
&= env_tlb(env
)->d
[mmu_idx
].large_page_mask
;
1103 while (((lp_addr
^ addr
) & lp_mask
) != 0) {
1107 env_tlb(env
)->d
[mmu_idx
].large_page_addr
= lp_addr
& lp_mask
;
1108 env_tlb(env
)->d
[mmu_idx
].large_page_mask
= lp_mask
;
1111 static inline void tlb_set_compare(CPUTLBEntryFull
*full
, CPUTLBEntry
*ent
,
1112 vaddr address
, int flags
,
1113 MMUAccessType access_type
, bool enable
)
1116 address
|= flags
& TLB_FLAGS_MASK
;
1117 flags
&= TLB_SLOW_FLAGS_MASK
;
1119 address
|= TLB_FORCE_SLOW
;
1125 ent
->addr_idx
[access_type
] = address
;
1126 full
->slow_flags
[access_type
] = flags
;
1130 * Add a new TLB entry. At most one entry for a given virtual address
1131 * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
1132 * supplied size is only used by tlb_flush_page.
1134 * Called from TCG-generated code, which is under an RCU read-side
1137 void tlb_set_page_full(CPUState
*cpu
, int mmu_idx
,
1138 vaddr addr
, CPUTLBEntryFull
*full
)
1140 CPUArchState
*env
= cpu
->env_ptr
;
1141 CPUTLB
*tlb
= env_tlb(env
);
1142 CPUTLBDesc
*desc
= &tlb
->d
[mmu_idx
];
1143 MemoryRegionSection
*section
;
1144 unsigned int index
, read_flags
, write_flags
;
1146 CPUTLBEntry
*te
, tn
;
1147 hwaddr iotlb
, xlat
, sz
, paddr_page
;
1149 int asidx
, wp_flags
, prot
;
1150 bool is_ram
, is_romd
;
1152 assert_cpu_is_self(cpu
);
1154 if (full
->lg_page_size
<= TARGET_PAGE_BITS
) {
1155 sz
= TARGET_PAGE_SIZE
;
1157 sz
= (hwaddr
)1 << full
->lg_page_size
;
1158 tlb_add_large_page(env
, mmu_idx
, addr
, sz
);
1160 addr_page
= addr
& TARGET_PAGE_MASK
;
1161 paddr_page
= full
->phys_addr
& TARGET_PAGE_MASK
;
1164 asidx
= cpu_asidx_from_attrs(cpu
, full
->attrs
);
1165 section
= address_space_translate_for_iotlb(cpu
, asidx
, paddr_page
,
1166 &xlat
, &sz
, full
->attrs
, &prot
);
1167 assert(sz
>= TARGET_PAGE_SIZE
);
1169 tlb_debug("vaddr=%016" VADDR_PRIx
" paddr=0x" HWADDR_FMT_plx
1170 " prot=%x idx=%d\n",
1171 addr
, full
->phys_addr
, prot
, mmu_idx
);
1174 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1175 /* Repeat the MMU check and TLB fill on every access. */
1176 read_flags
|= TLB_INVALID_MASK
;
1178 if (full
->attrs
.byte_swap
) {
1179 read_flags
|= TLB_BSWAP
;
1182 is_ram
= memory_region_is_ram(section
->mr
);
1183 is_romd
= memory_region_is_romd(section
->mr
);
1185 if (is_ram
|| is_romd
) {
1186 /* RAM and ROMD both have associated host memory. */
1187 addend
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) + xlat
;
1189 /* I/O does not; force the host address to NULL. */
1193 write_flags
= read_flags
;
1195 iotlb
= memory_region_get_ram_addr(section
->mr
) + xlat
;
1196 assert(!(iotlb
& ~TARGET_PAGE_MASK
));
1198 * Computing is_clean is expensive; avoid all that unless
1199 * the page is actually writable.
1201 if (prot
& PAGE_WRITE
) {
1202 if (section
->readonly
) {
1203 write_flags
|= TLB_DISCARD_WRITE
;
1204 } else if (cpu_physical_memory_is_clean(iotlb
)) {
1205 write_flags
|= TLB_NOTDIRTY
;
1210 iotlb
= memory_region_section_get_iotlb(cpu
, section
) + xlat
;
1212 * Writes to romd devices must go through MMIO to enable write.
1213 * Reads to romd devices go through the ram_ptr found above,
1214 * but of course reads to I/O must go through MMIO.
1216 write_flags
|= TLB_MMIO
;
1218 read_flags
= write_flags
;
1222 wp_flags
= cpu_watchpoint_address_matches(cpu
, addr_page
,
1225 index
= tlb_index(env
, mmu_idx
, addr_page
);
1226 te
= tlb_entry(env
, mmu_idx
, addr_page
);
1229 * Hold the TLB lock for the rest of the function. We could acquire/release
1230 * the lock several times in the function, but it is faster to amortize the
1231 * acquisition cost by acquiring it just once. Note that this leads to
1232 * a longer critical section, but this is not a concern since the TLB lock
1233 * is unlikely to be contended.
1235 qemu_spin_lock(&tlb
->c
.lock
);
1237 /* Note that the tlb is no longer clean. */
1238 tlb
->c
.dirty
|= 1 << mmu_idx
;
1240 /* Make sure there's no cached translation for the new page. */
1241 tlb_flush_vtlb_page_locked(env
, mmu_idx
, addr_page
);
1244 * Only evict the old entry to the victim tlb if it's for a
1245 * different page; otherwise just overwrite the stale data.
1247 if (!tlb_hit_page_anyprot(te
, addr_page
) && !tlb_entry_is_empty(te
)) {
1248 unsigned vidx
= desc
->vindex
++ % CPU_VTLB_SIZE
;
1249 CPUTLBEntry
*tv
= &desc
->vtable
[vidx
];
1251 /* Evict the old entry into the victim tlb. */
1252 copy_tlb_helper_locked(tv
, te
);
1253 desc
->vfulltlb
[vidx
] = desc
->fulltlb
[index
];
1254 tlb_n_used_entries_dec(env
, mmu_idx
);
1257 /* refill the tlb */
1259 * When memory region is ram, iotlb contains a TARGET_PAGE_BITS
1260 * aligned ram_addr_t of the page base of the target RAM.
1261 * Otherwise, iotlb contains
1262 * - a physical section number in the lower TARGET_PAGE_BITS
1263 * - the offset within section->mr of the page base (I/O, ROMD) with the
1264 * TARGET_PAGE_BITS masked off.
1265 * We subtract addr_page (which is page aligned and thus won't
1266 * disturb the low bits) to give an offset which can be added to the
1267 * (non-page-aligned) vaddr of the eventual memory access to get
1268 * the MemoryRegion offset for the access. Note that the vaddr we
1269 * subtract here is that of the page base, and not the same as the
1270 * vaddr we add back in io_readx()/io_writex()/get_page_addr_code().
1272 desc
->fulltlb
[index
] = *full
;
1273 full
= &desc
->fulltlb
[index
];
1274 full
->xlat_section
= iotlb
- addr_page
;
1275 full
->phys_addr
= paddr_page
;
1277 /* Now calculate the new entry */
1278 tn
.addend
= addend
- addr_page
;
1280 tlb_set_compare(full
, &tn
, addr_page
, read_flags
,
1281 MMU_INST_FETCH
, prot
& PAGE_EXEC
);
1283 if (wp_flags
& BP_MEM_READ
) {
1284 read_flags
|= TLB_WATCHPOINT
;
1286 tlb_set_compare(full
, &tn
, addr_page
, read_flags
,
1287 MMU_DATA_LOAD
, prot
& PAGE_READ
);
1289 if (prot
& PAGE_WRITE_INV
) {
1290 write_flags
|= TLB_INVALID_MASK
;
1292 if (wp_flags
& BP_MEM_WRITE
) {
1293 write_flags
|= TLB_WATCHPOINT
;
1295 tlb_set_compare(full
, &tn
, addr_page
, write_flags
,
1296 MMU_DATA_STORE
, prot
& PAGE_WRITE
);
1298 copy_tlb_helper_locked(te
, &tn
);
1299 tlb_n_used_entries_inc(env
, mmu_idx
);
1300 qemu_spin_unlock(&tlb
->c
.lock
);
1303 void tlb_set_page_with_attrs(CPUState
*cpu
, vaddr addr
,
1304 hwaddr paddr
, MemTxAttrs attrs
, int prot
,
1305 int mmu_idx
, uint64_t size
)
1307 CPUTLBEntryFull full
= {
1311 .lg_page_size
= ctz64(size
)
1314 assert(is_power_of_2(size
));
1315 tlb_set_page_full(cpu
, mmu_idx
, addr
, &full
);
1318 void tlb_set_page(CPUState
*cpu
, vaddr addr
,
1319 hwaddr paddr
, int prot
,
1320 int mmu_idx
, uint64_t size
)
1322 tlb_set_page_with_attrs(cpu
, addr
, paddr
, MEMTXATTRS_UNSPECIFIED
,
1323 prot
, mmu_idx
, size
);
1327 * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1328 * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1329 * be discarded and looked up again (e.g. via tlb_entry()).
1331 static void tlb_fill(CPUState
*cpu
, vaddr addr
, int size
,
1332 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1337 * This is not a probe, so only valid return is success; failure
1338 * should result in exception + longjmp to the cpu loop.
1340 ok
= cpu
->cc
->tcg_ops
->tlb_fill(cpu
, addr
, size
,
1341 access_type
, mmu_idx
, false, retaddr
);
1345 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
1346 MMUAccessType access_type
,
1347 int mmu_idx
, uintptr_t retaddr
)
1349 cpu
->cc
->tcg_ops
->do_unaligned_access(cpu
, addr
, access_type
,
1353 static inline void cpu_transaction_failed(CPUState
*cpu
, hwaddr physaddr
,
1354 vaddr addr
, unsigned size
,
1355 MMUAccessType access_type
,
1356 int mmu_idx
, MemTxAttrs attrs
,
1357 MemTxResult response
,
1360 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1362 if (!cpu
->ignore_memory_transaction_failures
&&
1363 cc
->tcg_ops
->do_transaction_failed
) {
1364 cc
->tcg_ops
->do_transaction_failed(cpu
, physaddr
, addr
, size
,
1365 access_type
, mmu_idx
, attrs
,
1371 * Save a potentially trashed CPUTLBEntryFull for later lookup by plugin.
1372 * This is read by tlb_plugin_lookup if the fulltlb entry doesn't match
1373 * because of the side effect of io_writex changing memory layout.
1375 static void save_iotlb_data(CPUState
*cs
, MemoryRegionSection
*section
,
1378 #ifdef CONFIG_PLUGIN
1379 SavedIOTLB
*saved
= &cs
->saved_iotlb
;
1380 saved
->section
= section
;
1381 saved
->mr_offset
= mr_offset
;
1385 static uint64_t io_readx(CPUArchState
*env
, CPUTLBEntryFull
*full
,
1386 int mmu_idx
, vaddr addr
, uintptr_t retaddr
,
1387 MMUAccessType access_type
, MemOp op
)
1389 CPUState
*cpu
= env_cpu(env
);
1391 MemoryRegionSection
*section
;
1396 section
= iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1398 mr_offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1399 cpu
->mem_io_pc
= retaddr
;
1400 if (!cpu
->can_do_io
) {
1401 cpu_io_recompile(cpu
, retaddr
);
1405 * The memory_region_dispatch may trigger a flush/resize
1406 * so for plugins we save the iotlb_data just in case.
1408 save_iotlb_data(cpu
, section
, mr_offset
);
1411 QEMU_IOTHREAD_LOCK_GUARD();
1412 r
= memory_region_dispatch_read(mr
, mr_offset
, &val
, op
, full
->attrs
);
1415 if (r
!= MEMTX_OK
) {
1416 hwaddr physaddr
= mr_offset
+
1417 section
->offset_within_address_space
-
1418 section
->offset_within_region
;
1420 cpu_transaction_failed(cpu
, physaddr
, addr
, memop_size(op
), access_type
,
1421 mmu_idx
, full
->attrs
, r
, retaddr
);
1426 static void io_writex(CPUArchState
*env
, CPUTLBEntryFull
*full
,
1427 int mmu_idx
, uint64_t val
, vaddr addr
,
1428 uintptr_t retaddr
, MemOp op
)
1430 CPUState
*cpu
= env_cpu(env
);
1432 MemoryRegionSection
*section
;
1436 section
= iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1438 mr_offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1439 if (!cpu
->can_do_io
) {
1440 cpu_io_recompile(cpu
, retaddr
);
1442 cpu
->mem_io_pc
= retaddr
;
1445 * The memory_region_dispatch may trigger a flush/resize
1446 * so for plugins we save the iotlb_data just in case.
1448 save_iotlb_data(cpu
, section
, mr_offset
);
1451 QEMU_IOTHREAD_LOCK_GUARD();
1452 r
= memory_region_dispatch_write(mr
, mr_offset
, val
, op
, full
->attrs
);
1455 if (r
!= MEMTX_OK
) {
1456 hwaddr physaddr
= mr_offset
+
1457 section
->offset_within_address_space
-
1458 section
->offset_within_region
;
1460 cpu_transaction_failed(cpu
, physaddr
, addr
, memop_size(op
),
1461 MMU_DATA_STORE
, mmu_idx
, full
->attrs
, r
,
1466 /* Return true if ADDR is present in the victim tlb, and has been copied
1467 back to the main tlb. */
1468 static bool victim_tlb_hit(CPUArchState
*env
, size_t mmu_idx
, size_t index
,
1469 MMUAccessType access_type
, vaddr page
)
1473 assert_cpu_is_self(env_cpu(env
));
1474 for (vidx
= 0; vidx
< CPU_VTLB_SIZE
; ++vidx
) {
1475 CPUTLBEntry
*vtlb
= &env_tlb(env
)->d
[mmu_idx
].vtable
[vidx
];
1476 uint64_t cmp
= tlb_read_idx(vtlb
, access_type
);
1479 /* Found entry in victim tlb, swap tlb and iotlb. */
1480 CPUTLBEntry tmptlb
, *tlb
= &env_tlb(env
)->f
[mmu_idx
].table
[index
];
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
);
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
;
1498 static void notdirty_write(CPUState
*cpu
, vaddr mem_vaddr
, unsigned size
,
1499 CPUTLBEntryFull
*full
, uintptr_t retaddr
)
1501 ram_addr_t ram_addr
= mem_vaddr
+ full
->xlat_section
;
1503 trace_memory_notdirty_write_access(mem_vaddr
, ram_addr
, size
);
1505 if (!cpu_physical_memory_get_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
)) {
1506 tb_invalidate_phys_range_fast(ram_addr
, size
, retaddr
);
1510 * Set both VGA and migration bits for simplicity and to remove
1511 * the notdirty callback faster.
1513 cpu_physical_memory_set_dirty_range(ram_addr
, size
, DIRTY_CLIENTS_NOCODE
);
1515 /* We remove the notdirty callback only if the code has been flushed. */
1516 if (!cpu_physical_memory_is_clean(ram_addr
)) {
1517 trace_memory_notdirty_set_dirty(mem_vaddr
);
1518 tlb_set_dirty(cpu
, mem_vaddr
);
1522 static int probe_access_internal(CPUArchState
*env
, vaddr addr
,
1523 int fault_size
, MMUAccessType access_type
,
1524 int mmu_idx
, bool nonfault
,
1525 void **phost
, CPUTLBEntryFull
**pfull
,
1526 uintptr_t retaddr
, bool check_mem_cbs
)
1528 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1529 CPUTLBEntry
*entry
= tlb_entry(env
, mmu_idx
, addr
);
1530 uint64_t tlb_addr
= tlb_read_idx(entry
, access_type
);
1531 vaddr page_addr
= addr
& TARGET_PAGE_MASK
;
1532 int flags
= TLB_FLAGS_MASK
& ~TLB_FORCE_SLOW
;
1533 bool force_mmio
= check_mem_cbs
&& cpu_plugin_mem_cbs_enabled(env_cpu(env
));
1534 CPUTLBEntryFull
*full
;
1536 if (!tlb_hit_page(tlb_addr
, page_addr
)) {
1537 if (!victim_tlb_hit(env
, mmu_idx
, index
, access_type
, page_addr
)) {
1538 CPUState
*cs
= env_cpu(env
);
1540 if (!cs
->cc
->tcg_ops
->tlb_fill(cs
, addr
, fault_size
, access_type
,
1541 mmu_idx
, nonfault
, retaddr
)) {
1542 /* Non-faulting page table read failed. */
1545 return TLB_INVALID_MASK
;
1548 /* TLB resize via tlb_fill may have moved the entry. */
1549 index
= tlb_index(env
, mmu_idx
, addr
);
1550 entry
= tlb_entry(env
, mmu_idx
, addr
);
1553 * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately,
1554 * to force the next access through tlb_fill. We've just
1555 * called tlb_fill, so we know that this entry *is* valid.
1557 flags
&= ~TLB_INVALID_MASK
;
1559 tlb_addr
= tlb_read_idx(entry
, access_type
);
1563 *pfull
= full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1564 flags
|= full
->slow_flags
[access_type
];
1566 /* Fold all "mmio-like" bits into TLB_MMIO. This is not RAM. */
1567 if (unlikely(flags
& ~(TLB_WATCHPOINT
| TLB_NOTDIRTY
))
1569 (access_type
!= MMU_INST_FETCH
&& force_mmio
)) {
1574 /* Everything else is RAM. */
1575 *phost
= (void *)((uintptr_t)addr
+ entry
->addend
);
1579 int probe_access_full(CPUArchState
*env
, vaddr addr
, int size
,
1580 MMUAccessType access_type
, int mmu_idx
,
1581 bool nonfault
, void **phost
, CPUTLBEntryFull
**pfull
,
1584 int flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1585 nonfault
, phost
, pfull
, retaddr
, true);
1587 /* Handle clean RAM pages. */
1588 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1589 notdirty_write(env_cpu(env
), addr
, 1, *pfull
, retaddr
);
1590 flags
&= ~TLB_NOTDIRTY
;
1596 int probe_access_full_mmu(CPUArchState
*env
, vaddr addr
, int size
,
1597 MMUAccessType access_type
, int mmu_idx
,
1598 void **phost
, CPUTLBEntryFull
**pfull
)
1600 void *discard_phost
;
1601 CPUTLBEntryFull
*discard_tlb
;
1603 /* privately handle users that don't need full results */
1604 phost
= phost
? phost
: &discard_phost
;
1605 pfull
= pfull
? pfull
: &discard_tlb
;
1607 int flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1608 true, phost
, pfull
, 0, false);
1610 /* Handle clean RAM pages. */
1611 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1612 notdirty_write(env_cpu(env
), addr
, 1, *pfull
, 0);
1613 flags
&= ~TLB_NOTDIRTY
;
1619 int probe_access_flags(CPUArchState
*env
, vaddr addr
, int size
,
1620 MMUAccessType access_type
, int mmu_idx
,
1621 bool nonfault
, void **phost
, uintptr_t retaddr
)
1623 CPUTLBEntryFull
*full
;
1626 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1628 flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1629 nonfault
, phost
, &full
, retaddr
, true);
1631 /* Handle clean RAM pages. */
1632 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1633 notdirty_write(env_cpu(env
), addr
, 1, full
, retaddr
);
1634 flags
&= ~TLB_NOTDIRTY
;
1640 void *probe_access(CPUArchState
*env
, vaddr addr
, int size
,
1641 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1643 CPUTLBEntryFull
*full
;
1647 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1649 flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1650 false, &host
, &full
, retaddr
, true);
1652 /* Per the interface, size == 0 merely faults the access. */
1657 if (unlikely(flags
& (TLB_NOTDIRTY
| TLB_WATCHPOINT
))) {
1658 /* Handle watchpoints. */
1659 if (flags
& TLB_WATCHPOINT
) {
1660 int wp_access
= (access_type
== MMU_DATA_STORE
1661 ? BP_MEM_WRITE
: BP_MEM_READ
);
1662 cpu_check_watchpoint(env_cpu(env
), addr
, size
,
1663 full
->attrs
, wp_access
, retaddr
);
1666 /* Handle clean RAM pages. */
1667 if (flags
& TLB_NOTDIRTY
) {
1668 notdirty_write(env_cpu(env
), addr
, 1, full
, retaddr
);
1675 void *tlb_vaddr_to_host(CPUArchState
*env
, abi_ptr addr
,
1676 MMUAccessType access_type
, int mmu_idx
)
1678 CPUTLBEntryFull
*full
;
1682 flags
= probe_access_internal(env
, addr
, 0, access_type
,
1683 mmu_idx
, true, &host
, &full
, 0, false);
1685 /* No combination of flags are expected by the caller. */
1686 return flags
? NULL
: host
;
1690 * Return a ram_addr_t for the virtual address for execution.
1692 * Return -1 if we can't translate and execute from an entire page
1693 * of RAM. This will force us to execute by loading and translating
1694 * one insn at a time, without caching.
1696 * NOTE: This function will trigger an exception if the page is
1699 tb_page_addr_t
get_page_addr_code_hostp(CPUArchState
*env
, vaddr addr
,
1702 CPUTLBEntryFull
*full
;
1705 (void)probe_access_internal(env
, addr
, 1, MMU_INST_FETCH
,
1706 cpu_mmu_index(env
, true), false,
1707 &p
, &full
, 0, false);
1712 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1719 return qemu_ram_addr_from_host_nofail(p
);
1722 /* Load/store with atomicity primitives. */
1723 #include "ldst_atomicity.c.inc"
1725 #ifdef CONFIG_PLUGIN
1727 * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1728 * This should be a hot path as we will have just looked this path up
1729 * in the softmmu lookup code (or helper). We don't handle re-fills or
1730 * checking the victim table. This is purely informational.
1732 * This almost never fails as the memory access being instrumented
1733 * should have just filled the TLB. The one corner case is io_writex
1734 * which can cause TLB flushes and potential resizing of the TLBs
1735 * losing the information we need. In those cases we need to recover
1736 * data from a copy of the CPUTLBEntryFull. As long as this always occurs
1737 * from the same thread (which a mem callback will be) this is safe.
1740 bool tlb_plugin_lookup(CPUState
*cpu
, vaddr addr
, int mmu_idx
,
1741 bool is_store
, struct qemu_plugin_hwaddr
*data
)
1743 CPUArchState
*env
= cpu
->env_ptr
;
1744 CPUTLBEntry
*tlbe
= tlb_entry(env
, mmu_idx
, addr
);
1745 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1746 uint64_t tlb_addr
= is_store
? tlb_addr_write(tlbe
) : tlbe
->addr_read
;
1748 if (likely(tlb_hit(tlb_addr
, addr
))) {
1749 /* We must have an iotlb entry for MMIO */
1750 if (tlb_addr
& TLB_MMIO
) {
1751 CPUTLBEntryFull
*full
;
1752 full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1754 data
->v
.io
.section
=
1755 iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1756 data
->v
.io
.offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1758 data
->is_io
= false;
1759 data
->v
.ram
.hostaddr
= (void *)((uintptr_t)addr
+ tlbe
->addend
);
1763 SavedIOTLB
*saved
= &cpu
->saved_iotlb
;
1765 data
->v
.io
.section
= saved
->section
;
1766 data
->v
.io
.offset
= saved
->mr_offset
;
1774 * Probe for a load/store operation.
1775 * Return the host address and into @flags.
1778 typedef struct MMULookupPageData
{
1779 CPUTLBEntryFull
*full
;
1784 } MMULookupPageData
;
1786 typedef struct MMULookupLocals
{
1787 MMULookupPageData page
[2];
1793 * mmu_lookup1: translate one page
1795 * @data: lookup parameters
1796 * @mmu_idx: virtual address context
1797 * @access_type: load/store/code
1798 * @ra: return address into tcg generated code, or 0
1800 * Resolve the translation for the one page at @data.addr, filling in
1801 * the rest of @data with the results. If the translation fails,
1802 * tlb_fill will longjmp out. Return true if the softmmu tlb for
1803 * @mmu_idx may have resized.
1805 static bool mmu_lookup1(CPUArchState
*env
, MMULookupPageData
*data
,
1806 int mmu_idx
, MMUAccessType access_type
, uintptr_t ra
)
1808 vaddr addr
= data
->addr
;
1809 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1810 CPUTLBEntry
*entry
= tlb_entry(env
, mmu_idx
, addr
);
1811 uint64_t tlb_addr
= tlb_read_idx(entry
, access_type
);
1812 bool maybe_resized
= false;
1813 CPUTLBEntryFull
*full
;
1816 /* If the TLB entry is for a different page, reload and try again. */
1817 if (!tlb_hit(tlb_addr
, addr
)) {
1818 if (!victim_tlb_hit(env
, mmu_idx
, index
, access_type
,
1819 addr
& TARGET_PAGE_MASK
)) {
1820 tlb_fill(env_cpu(env
), addr
, data
->size
, access_type
, mmu_idx
, ra
);
1821 maybe_resized
= true;
1822 index
= tlb_index(env
, mmu_idx
, addr
);
1823 entry
= tlb_entry(env
, mmu_idx
, addr
);
1825 tlb_addr
= tlb_read_idx(entry
, access_type
) & ~TLB_INVALID_MASK
;
1828 full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1829 flags
= tlb_addr
& (TLB_FLAGS_MASK
& ~TLB_FORCE_SLOW
);
1830 flags
|= full
->slow_flags
[access_type
];
1833 data
->flags
= flags
;
1834 /* Compute haddr speculatively; depending on flags it might be invalid. */
1835 data
->haddr
= (void *)((uintptr_t)addr
+ entry
->addend
);
1837 return maybe_resized
;
1841 * mmu_watch_or_dirty
1843 * @data: lookup parameters
1844 * @access_type: load/store/code
1845 * @ra: return address into tcg generated code, or 0
1847 * Trigger watchpoints for @data.addr:@data.size;
1848 * record writes to protected clean pages.
1850 static void mmu_watch_or_dirty(CPUArchState
*env
, MMULookupPageData
*data
,
1851 MMUAccessType access_type
, uintptr_t ra
)
1853 CPUTLBEntryFull
*full
= data
->full
;
1854 vaddr addr
= data
->addr
;
1855 int flags
= data
->flags
;
1856 int size
= data
->size
;
1858 /* On watchpoint hit, this will longjmp out. */
1859 if (flags
& TLB_WATCHPOINT
) {
1860 int wp
= access_type
== MMU_DATA_STORE
? BP_MEM_WRITE
: BP_MEM_READ
;
1861 cpu_check_watchpoint(env_cpu(env
), addr
, size
, full
->attrs
, wp
, ra
);
1862 flags
&= ~TLB_WATCHPOINT
;
1865 /* Note that notdirty is only set for writes. */
1866 if (flags
& TLB_NOTDIRTY
) {
1867 notdirty_write(env_cpu(env
), addr
, size
, full
, ra
);
1868 flags
&= ~TLB_NOTDIRTY
;
1870 data
->flags
= flags
;
1874 * mmu_lookup: translate page(s)
1876 * @addr: virtual address
1877 * @oi: combined mmu_idx and MemOp
1878 * @ra: return address into tcg generated code, or 0
1879 * @access_type: load/store/code
1882 * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
1883 * bytes. Return true if the lookup crosses a page boundary.
1885 static bool mmu_lookup(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
1886 uintptr_t ra
, MMUAccessType type
, MMULookupLocals
*l
)
1892 l
->memop
= get_memop(oi
);
1893 l
->mmu_idx
= get_mmuidx(oi
);
1895 tcg_debug_assert(l
->mmu_idx
< NB_MMU_MODES
);
1897 /* Handle CPU specific unaligned behaviour */
1898 a_bits
= get_alignment_bits(l
->memop
);
1899 if (addr
& ((1 << a_bits
) - 1)) {
1900 cpu_unaligned_access(env_cpu(env
), addr
, type
, l
->mmu_idx
, ra
);
1903 l
->page
[0].addr
= addr
;
1904 l
->page
[0].size
= memop_size(l
->memop
);
1905 l
->page
[1].addr
= (addr
+ l
->page
[0].size
- 1) & TARGET_PAGE_MASK
;
1906 l
->page
[1].size
= 0;
1907 crosspage
= (addr
^ l
->page
[1].addr
) & TARGET_PAGE_MASK
;
1909 if (likely(!crosspage
)) {
1910 mmu_lookup1(env
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1912 flags
= l
->page
[0].flags
;
1913 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1914 mmu_watch_or_dirty(env
, &l
->page
[0], type
, ra
);
1916 if (unlikely(flags
& TLB_BSWAP
)) {
1917 l
->memop
^= MO_BSWAP
;
1920 /* Finish compute of page crossing. */
1921 int size0
= l
->page
[1].addr
- addr
;
1922 l
->page
[1].size
= l
->page
[0].size
- size0
;
1923 l
->page
[0].size
= size0
;
1926 * Lookup both pages, recognizing exceptions from either. If the
1927 * second lookup potentially resized, refresh first CPUTLBEntryFull.
1929 mmu_lookup1(env
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1930 if (mmu_lookup1(env
, &l
->page
[1], l
->mmu_idx
, type
, ra
)) {
1931 uintptr_t index
= tlb_index(env
, l
->mmu_idx
, addr
);
1932 l
->page
[0].full
= &env_tlb(env
)->d
[l
->mmu_idx
].fulltlb
[index
];
1935 flags
= l
->page
[0].flags
| l
->page
[1].flags
;
1936 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1937 mmu_watch_or_dirty(env
, &l
->page
[0], type
, ra
);
1938 mmu_watch_or_dirty(env
, &l
->page
[1], type
, ra
);
1942 * Since target/sparc is the only user of TLB_BSWAP, and all
1943 * Sparc accesses are aligned, any treatment across two pages
1944 * would be arbitrary. Refuse it until there's a use.
1946 tcg_debug_assert((flags
& TLB_BSWAP
) == 0);
1953 * Probe for an atomic operation. Do not allow unaligned operations,
1954 * or io operations to proceed. Return the host address.
1956 static void *atomic_mmu_lookup(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
1957 int size
, uintptr_t retaddr
)
1959 uintptr_t mmu_idx
= get_mmuidx(oi
);
1960 MemOp mop
= get_memop(oi
);
1961 int a_bits
= get_alignment_bits(mop
);
1966 CPUTLBEntryFull
*full
;
1968 tcg_debug_assert(mmu_idx
< NB_MMU_MODES
);
1970 /* Adjust the given return address. */
1971 retaddr
-= GETPC_ADJ
;
1973 /* Enforce guest required alignment. */
1974 if (unlikely(a_bits
> 0 && (addr
& ((1 << a_bits
) - 1)))) {
1975 /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1976 cpu_unaligned_access(env_cpu(env
), addr
, MMU_DATA_STORE
,
1980 /* Enforce qemu required alignment. */
1981 if (unlikely(addr
& (size
- 1))) {
1982 /* We get here if guest alignment was not requested,
1983 or was not enforced by cpu_unaligned_access above.
1984 We might widen the access and emulate, but for now
1985 mark an exception and exit the cpu loop. */
1986 goto stop_the_world
;
1989 index
= tlb_index(env
, mmu_idx
, addr
);
1990 tlbe
= tlb_entry(env
, mmu_idx
, addr
);
1992 /* Check TLB entry and enforce page permissions. */
1993 tlb_addr
= tlb_addr_write(tlbe
);
1994 if (!tlb_hit(tlb_addr
, addr
)) {
1995 if (!victim_tlb_hit(env
, mmu_idx
, index
, MMU_DATA_STORE
,
1996 addr
& TARGET_PAGE_MASK
)) {
1997 tlb_fill(env_cpu(env
), addr
, size
,
1998 MMU_DATA_STORE
, mmu_idx
, retaddr
);
1999 index
= tlb_index(env
, mmu_idx
, addr
);
2000 tlbe
= tlb_entry(env
, mmu_idx
, addr
);
2002 tlb_addr
= tlb_addr_write(tlbe
) & ~TLB_INVALID_MASK
;
2006 * Let the guest notice RMW on a write-only page.
2007 * We have just verified that the page is writable.
2008 * Subpage lookups may have left TLB_INVALID_MASK set,
2009 * but addr_read will only be -1 if PAGE_READ was unset.
2011 if (unlikely(tlbe
->addr_read
== -1)) {
2012 tlb_fill(env_cpu(env
), addr
, size
, MMU_DATA_LOAD
, mmu_idx
, retaddr
);
2014 * Since we don't support reads and writes to different
2015 * addresses, and we do have the proper page loaded for
2016 * write, this shouldn't ever return. But just in case,
2017 * handle via stop-the-world.
2019 goto stop_the_world
;
2021 /* Collect tlb flags for read. */
2022 tlb_addr
|= tlbe
->addr_read
;
2024 /* Notice an IO access or a needs-MMU-lookup access */
2025 if (unlikely(tlb_addr
& (TLB_MMIO
| TLB_DISCARD_WRITE
))) {
2026 /* There's really nothing that can be done to
2027 support this apart from stop-the-world. */
2028 goto stop_the_world
;
2031 hostaddr
= (void *)((uintptr_t)addr
+ tlbe
->addend
);
2032 full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
2034 if (unlikely(tlb_addr
& TLB_NOTDIRTY
)) {
2035 notdirty_write(env_cpu(env
), addr
, size
, full
, retaddr
);
2038 if (unlikely(tlb_addr
& TLB_FORCE_SLOW
)) {
2041 if (full
->slow_flags
[MMU_DATA_STORE
] & TLB_WATCHPOINT
) {
2042 wp_flags
|= BP_MEM_WRITE
;
2044 if (full
->slow_flags
[MMU_DATA_LOAD
] & TLB_WATCHPOINT
) {
2045 wp_flags
|= BP_MEM_READ
;
2048 cpu_check_watchpoint(env_cpu(env
), addr
, size
,
2049 full
->attrs
, wp_flags
, retaddr
);
2056 cpu_loop_exit_atomic(env_cpu(env
), retaddr
);
2062 * We support two different access types. SOFTMMU_CODE_ACCESS is
2063 * specifically for reading instructions from system memory. It is
2064 * called by the translation loop and in some helpers where the code
2065 * is disassembled. It shouldn't be called directly by guest code.
2067 * For the benefit of TCG generated code, we want to avoid the
2068 * complication of ABI-specific return type promotion and always
2069 * return a value extended to the register size of the host. This is
2070 * tcg_target_long, except in the case of a 32-bit host and 64-bit
2071 * data, and for that we always have uint64_t.
2073 * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
2079 * @full: page parameters
2080 * @ret_be: accumulated data
2081 * @addr: virtual address
2082 * @size: number of bytes
2083 * @mmu_idx: virtual address context
2084 * @ra: return address into tcg generated code, or 0
2085 * Context: iothread lock held
2087 * Load @size bytes from @addr, which is memory-mapped i/o.
2088 * The bytes are concatenated in big-endian order with @ret_be.
2090 static uint64_t do_ld_mmio_beN(CPUArchState
*env
, CPUTLBEntryFull
*full
,
2091 uint64_t ret_be
, vaddr addr
, int size
,
2092 int mmu_idx
, MMUAccessType type
, uintptr_t ra
)
2096 tcg_debug_assert(size
> 0 && size
<= 8);
2098 /* Read aligned pieces up to 8 bytes. */
2099 switch ((size
| (int)addr
) & 7) {
2104 t
= io_readx(env
, full
, mmu_idx
, addr
, ra
, type
, MO_UB
);
2105 ret_be
= (ret_be
<< 8) | t
;
2111 t
= io_readx(env
, full
, mmu_idx
, addr
, ra
, type
, MO_BEUW
);
2112 ret_be
= (ret_be
<< 16) | t
;
2117 t
= io_readx(env
, full
, mmu_idx
, addr
, ra
, type
, MO_BEUL
);
2118 ret_be
= (ret_be
<< 32) | t
;
2123 return io_readx(env
, full
, mmu_idx
, addr
, ra
, type
, MO_BEUQ
);
2125 qemu_build_not_reached();
2133 * @p: translation parameters
2134 * @ret_be: accumulated data
2136 * Load @p->size bytes from @p->haddr, which is RAM.
2137 * The bytes to concatenated in big-endian order with @ret_be.
2139 static uint64_t do_ld_bytes_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2141 uint8_t *haddr
= p
->haddr
;
2142 int i
, size
= p
->size
;
2144 for (i
= 0; i
< size
; i
++) {
2145 ret_be
= (ret_be
<< 8) | haddr
[i
];
2152 * @p: translation parameters
2153 * @ret_be: accumulated data
2155 * As do_ld_bytes_beN, but atomically on each aligned part.
2157 static uint64_t do_ld_parts_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2159 void *haddr
= p
->haddr
;
2167 * Find minimum of alignment and size.
2168 * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2169 * would have only checked the low bits of addr|size once at the start,
2170 * but is just as easy.
2172 switch (((uintptr_t)haddr
| size
) & 7) {
2174 x
= cpu_to_be32(load_atomic4(haddr
));
2175 ret_be
= (ret_be
<< 32) | x
;
2180 x
= cpu_to_be16(load_atomic2(haddr
));
2181 ret_be
= (ret_be
<< 16) | x
;
2185 x
= *(uint8_t *)haddr
;
2186 ret_be
= (ret_be
<< 8) | x
;
2190 g_assert_not_reached();
2194 } while (size
!= 0);
2200 * @p: translation parameters
2201 * @ret_be: accumulated data
2203 * As do_ld_bytes_beN, but with one atomic load.
2204 * Four aligned bytes are guaranteed to cover the load.
2206 static uint64_t do_ld_whole_be4(MMULookupPageData
*p
, uint64_t ret_be
)
2208 int o
= p
->addr
& 3;
2209 uint32_t x
= load_atomic4(p
->haddr
- o
);
2213 x
>>= (4 - p
->size
) * 8;
2214 return (ret_be
<< (p
->size
* 8)) | x
;
2219 * @p: translation parameters
2220 * @ret_be: accumulated data
2222 * As do_ld_bytes_beN, but with one atomic load.
2223 * Eight aligned bytes are guaranteed to cover the load.
2225 static uint64_t do_ld_whole_be8(CPUArchState
*env
, uintptr_t ra
,
2226 MMULookupPageData
*p
, uint64_t ret_be
)
2228 int o
= p
->addr
& 7;
2229 uint64_t x
= load_atomic8_or_exit(env
, ra
, p
->haddr
- o
);
2233 x
>>= (8 - p
->size
) * 8;
2234 return (ret_be
<< (p
->size
* 8)) | x
;
2239 * @p: translation parameters
2240 * @ret_be: accumulated data
2242 * As do_ld_bytes_beN, but with one atomic load.
2243 * 16 aligned bytes are guaranteed to cover the load.
2245 static Int128
do_ld_whole_be16(CPUArchState
*env
, uintptr_t ra
,
2246 MMULookupPageData
*p
, uint64_t ret_be
)
2248 int o
= p
->addr
& 15;
2249 Int128 x
, y
= load_atomic16_or_exit(env
, ra
, p
->haddr
- o
);
2252 if (!HOST_BIG_ENDIAN
) {
2255 y
= int128_lshift(y
, o
* 8);
2256 y
= int128_urshift(y
, (16 - size
) * 8);
2257 x
= int128_make64(ret_be
);
2258 x
= int128_lshift(x
, size
* 8);
2259 return int128_or(x
, y
);
2263 * Wrapper for the above.
2265 static uint64_t do_ld_beN(CPUArchState
*env
, MMULookupPageData
*p
,
2266 uint64_t ret_be
, int mmu_idx
, MMUAccessType type
,
2267 MemOp mop
, uintptr_t ra
)
2270 unsigned tmp
, half_size
;
2272 if (unlikely(p
->flags
& TLB_MMIO
)) {
2273 QEMU_IOTHREAD_LOCK_GUARD();
2274 return do_ld_mmio_beN(env
, p
->full
, ret_be
, p
->addr
, p
->size
,
2279 * It is a given that we cross a page and therefore there is no
2280 * atomicity for the load as a whole, but subobjects may need attention.
2282 atom
= mop
& MO_ATOM_MASK
;
2284 case MO_ATOM_SUBALIGN
:
2285 return do_ld_parts_beN(p
, ret_be
);
2287 case MO_ATOM_IFALIGN_PAIR
:
2288 case MO_ATOM_WITHIN16_PAIR
:
2289 tmp
= mop
& MO_SIZE
;
2290 tmp
= tmp
? tmp
- 1 : 0;
2291 half_size
= 1 << tmp
;
2292 if (atom
== MO_ATOM_IFALIGN_PAIR
2293 ? p
->size
== half_size
2294 : p
->size
>= half_size
) {
2295 if (!HAVE_al8_fast
&& p
->size
< 4) {
2296 return do_ld_whole_be4(p
, ret_be
);
2298 return do_ld_whole_be8(env
, ra
, p
, ret_be
);
2303 case MO_ATOM_IFALIGN
:
2304 case MO_ATOM_WITHIN16
:
2306 return do_ld_bytes_beN(p
, ret_be
);
2309 g_assert_not_reached();
2314 * Wrapper for the above, for 8 < size < 16.
2316 static Int128
do_ld16_beN(CPUArchState
*env
, MMULookupPageData
*p
,
2317 uint64_t a
, int mmu_idx
, MemOp mop
, uintptr_t ra
)
2323 if (unlikely(p
->flags
& TLB_MMIO
)) {
2324 QEMU_IOTHREAD_LOCK_GUARD();
2325 a
= do_ld_mmio_beN(env
, p
->full
, a
, p
->addr
, size
- 8,
2326 mmu_idx
, MMU_DATA_LOAD
, ra
);
2327 b
= do_ld_mmio_beN(env
, p
->full
, 0, p
->addr
+ 8, 8,
2328 mmu_idx
, MMU_DATA_LOAD
, ra
);
2329 return int128_make128(b
, a
);
2333 * It is a given that we cross a page and therefore there is no
2334 * atomicity for the load as a whole, but subobjects may need attention.
2336 atom
= mop
& MO_ATOM_MASK
;
2338 case MO_ATOM_SUBALIGN
:
2340 a
= do_ld_parts_beN(p
, a
);
2341 p
->haddr
+= size
- 8;
2343 b
= do_ld_parts_beN(p
, 0);
2346 case MO_ATOM_WITHIN16_PAIR
:
2347 /* Since size > 8, this is the half that must be atomic. */
2348 return do_ld_whole_be16(env
, ra
, p
, a
);
2350 case MO_ATOM_IFALIGN_PAIR
:
2352 * Since size > 8, both halves are misaligned,
2353 * and so neither is atomic.
2355 case MO_ATOM_IFALIGN
:
2356 case MO_ATOM_WITHIN16
:
2359 a
= do_ld_bytes_beN(p
, a
);
2360 b
= ldq_be_p(p
->haddr
+ size
- 8);
2364 g_assert_not_reached();
2367 return int128_make128(b
, a
);
2370 static uint8_t do_ld_1(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2371 MMUAccessType type
, uintptr_t ra
)
2373 if (unlikely(p
->flags
& TLB_MMIO
)) {
2374 return io_readx(env
, p
->full
, mmu_idx
, p
->addr
, ra
, type
, MO_UB
);
2376 return *(uint8_t *)p
->haddr
;
2380 static uint16_t do_ld_2(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2381 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2385 if (unlikely(p
->flags
& TLB_MMIO
)) {
2386 QEMU_IOTHREAD_LOCK_GUARD();
2387 ret
= do_ld_mmio_beN(env
, p
->full
, 0, p
->addr
, 2, mmu_idx
, type
, ra
);
2388 if ((memop
& MO_BSWAP
) == MO_LE
) {
2392 /* Perform the load host endian, then swap if necessary. */
2393 ret
= load_atom_2(env
, ra
, p
->haddr
, memop
);
2394 if (memop
& MO_BSWAP
) {
2401 static uint32_t do_ld_4(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2402 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2406 if (unlikely(p
->flags
& TLB_MMIO
)) {
2407 QEMU_IOTHREAD_LOCK_GUARD();
2408 ret
= do_ld_mmio_beN(env
, p
->full
, 0, p
->addr
, 4, mmu_idx
, type
, ra
);
2409 if ((memop
& MO_BSWAP
) == MO_LE
) {
2413 /* Perform the load host endian. */
2414 ret
= load_atom_4(env
, ra
, p
->haddr
, memop
);
2415 if (memop
& MO_BSWAP
) {
2422 static uint64_t do_ld_8(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2423 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2427 if (unlikely(p
->flags
& TLB_MMIO
)) {
2428 QEMU_IOTHREAD_LOCK_GUARD();
2429 ret
= do_ld_mmio_beN(env
, p
->full
, 0, p
->addr
, 8, mmu_idx
, type
, ra
);
2430 if ((memop
& MO_BSWAP
) == MO_LE
) {
2434 /* Perform the load host endian. */
2435 ret
= load_atom_8(env
, ra
, p
->haddr
, memop
);
2436 if (memop
& MO_BSWAP
) {
2443 static uint8_t do_ld1_mmu(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
2444 uintptr_t ra
, MMUAccessType access_type
)
2449 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2450 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2451 tcg_debug_assert(!crosspage
);
2453 return do_ld_1(env
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2456 tcg_target_ulong
helper_ldub_mmu(CPUArchState
*env
, uint64_t addr
,
2457 MemOpIdx oi
, uintptr_t retaddr
)
2459 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_8
);
2460 return do_ld1_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2463 static uint16_t do_ld2_mmu(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
2464 uintptr_t ra
, MMUAccessType access_type
)
2471 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2472 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2473 if (likely(!crosspage
)) {
2474 return do_ld_2(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2477 a
= do_ld_1(env
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2478 b
= do_ld_1(env
, &l
.page
[1], l
.mmu_idx
, access_type
, ra
);
2480 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2488 tcg_target_ulong
helper_lduw_mmu(CPUArchState
*env
, uint64_t addr
,
2489 MemOpIdx oi
, uintptr_t retaddr
)
2491 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2492 return do_ld2_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2495 static uint32_t do_ld4_mmu(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
2496 uintptr_t ra
, MMUAccessType access_type
)
2502 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2503 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2504 if (likely(!crosspage
)) {
2505 return do_ld_4(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2508 ret
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2509 ret
= do_ld_beN(env
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2510 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2516 tcg_target_ulong
helper_ldul_mmu(CPUArchState
*env
, uint64_t addr
,
2517 MemOpIdx oi
, uintptr_t retaddr
)
2519 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2520 return do_ld4_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2523 static uint64_t do_ld8_mmu(CPUArchState
*env
, vaddr addr
, MemOpIdx oi
,
2524 uintptr_t ra
, MMUAccessType access_type
)
2530 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2531 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2532 if (likely(!crosspage
)) {
2533 return do_ld_8(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2536 ret
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2537 ret
= do_ld_beN(env
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2538 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2544 uint64_t helper_ldq_mmu(CPUArchState
*env
, uint64_t addr
,
2545 MemOpIdx oi
, uintptr_t retaddr
)
2547 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
2548 return do_ld8_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2552 * Provide signed versions of the load routines as well. We can of course
2553 * avoid this for 64-bit data, or for 32-bit data on 32-bit host.
2556 tcg_target_ulong
helper_ldsb_mmu(CPUArchState
*env
, uint64_t addr
,
2557 MemOpIdx oi
, uintptr_t retaddr
)
2559 return (int8_t)helper_ldub_mmu(env
, addr
, oi
, retaddr
);
2562 tcg_target_ulong
helper_ldsw_mmu(CPUArchState
*env
, uint64_t addr
,
2563 MemOpIdx oi
, uintptr_t retaddr
)
2565 return (int16_t)helper_lduw_mmu(env
, addr
, oi
, retaddr
);
2568 tcg_target_ulong
helper_ldsl_mmu(CPUArchState
*env
, uint64_t addr
,
2569 MemOpIdx oi
, uintptr_t retaddr
)
2571 return (int32_t)helper_ldul_mmu(env
, addr
, oi
, retaddr
);
2574 static Int128
do_ld16_mmu(CPUArchState
*env
, vaddr addr
,
2575 MemOpIdx oi
, uintptr_t ra
)
2583 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2584 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_LOAD
, &l
);
2585 if (likely(!crosspage
)) {
2586 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
2587 QEMU_IOTHREAD_LOCK_GUARD();
2588 a
= do_ld_mmio_beN(env
, l
.page
[0].full
, 0, addr
, 8,
2589 l
.mmu_idx
, MMU_DATA_LOAD
, ra
);
2590 b
= do_ld_mmio_beN(env
, l
.page
[0].full
, 0, addr
+ 8, 8,
2591 l
.mmu_idx
, MMU_DATA_LOAD
, ra
);
2592 ret
= int128_make128(b
, a
);
2593 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2594 ret
= bswap128(ret
);
2597 /* Perform the load host endian. */
2598 ret
= load_atom_16(env
, ra
, l
.page
[0].haddr
, l
.memop
);
2599 if (l
.memop
& MO_BSWAP
) {
2600 ret
= bswap128(ret
);
2606 first
= l
.page
[0].size
;
2608 MemOp mop8
= (l
.memop
& ~MO_SIZE
) | MO_64
;
2610 a
= do_ld_8(env
, &l
.page
[0], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2611 b
= do_ld_8(env
, &l
.page
[1], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2612 if ((mop8
& MO_BSWAP
) == MO_LE
) {
2613 ret
= int128_make128(a
, b
);
2615 ret
= int128_make128(b
, a
);
2621 a
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
,
2622 MMU_DATA_LOAD
, l
.memop
, ra
);
2623 ret
= do_ld16_beN(env
, &l
.page
[1], a
, l
.mmu_idx
, l
.memop
, ra
);
2625 ret
= do_ld16_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, l
.memop
, ra
);
2626 b
= int128_getlo(ret
);
2627 ret
= int128_lshift(ret
, l
.page
[1].size
* 8);
2628 a
= int128_gethi(ret
);
2629 b
= do_ld_beN(env
, &l
.page
[1], b
, l
.mmu_idx
,
2630 MMU_DATA_LOAD
, l
.memop
, ra
);
2631 ret
= int128_make128(b
, a
);
2633 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2634 ret
= bswap128(ret
);
2639 Int128
helper_ld16_mmu(CPUArchState
*env
, uint64_t addr
,
2640 uint32_t oi
, uintptr_t retaddr
)
2642 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
2643 return do_ld16_mmu(env
, addr
, oi
, retaddr
);
2646 Int128
helper_ld_i128(CPUArchState
*env
, uint64_t addr
, uint32_t oi
)
2648 return helper_ld16_mmu(env
, addr
, oi
, GETPC());
2652 * Load helpers for cpu_ldst.h.
2655 static void plugin_load_cb(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
)
2657 qemu_plugin_vcpu_mem_cb(env_cpu(env
), addr
, oi
, QEMU_PLUGIN_MEM_R
);
2660 uint8_t cpu_ldb_mmu(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
, uintptr_t ra
)
2664 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_UB
);
2665 ret
= do_ld1_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2666 plugin_load_cb(env
, addr
, oi
);
2670 uint16_t cpu_ldw_mmu(CPUArchState
*env
, abi_ptr addr
,
2671 MemOpIdx oi
, uintptr_t ra
)
2675 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2676 ret
= do_ld2_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2677 plugin_load_cb(env
, addr
, oi
);
2681 uint32_t cpu_ldl_mmu(CPUArchState
*env
, abi_ptr addr
,
2682 MemOpIdx oi
, uintptr_t ra
)
2686 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2687 ret
= do_ld4_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2688 plugin_load_cb(env
, addr
, oi
);
2692 uint64_t cpu_ldq_mmu(CPUArchState
*env
, abi_ptr addr
,
2693 MemOpIdx oi
, uintptr_t ra
)
2697 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
2698 ret
= do_ld8_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2699 plugin_load_cb(env
, addr
, oi
);
2703 Int128
cpu_ld16_mmu(CPUArchState
*env
, abi_ptr addr
,
2704 MemOpIdx oi
, uintptr_t ra
)
2708 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
2709 ret
= do_ld16_mmu(env
, addr
, oi
, ra
);
2710 plugin_load_cb(env
, addr
, oi
);
2721 * @full: page parameters
2722 * @val_le: data to store
2723 * @addr: virtual address
2724 * @size: number of bytes
2725 * @mmu_idx: virtual address context
2726 * @ra: return address into tcg generated code, or 0
2727 * Context: iothread lock held
2729 * Store @size bytes at @addr, which is memory-mapped i/o.
2730 * The bytes to store are extracted in little-endian order from @val_le;
2731 * return the bytes of @val_le beyond @p->size that have not been stored.
2733 static uint64_t do_st_mmio_leN(CPUArchState
*env
, CPUTLBEntryFull
*full
,
2734 uint64_t val_le
, vaddr addr
, int size
,
2735 int mmu_idx
, uintptr_t ra
)
2737 tcg_debug_assert(size
> 0 && size
<= 8);
2740 /* Store aligned pieces up to 8 bytes. */
2741 switch ((size
| (int)addr
) & 7) {
2746 io_writex(env
, full
, mmu_idx
, val_le
, addr
, ra
, MO_UB
);
2753 io_writex(env
, full
, mmu_idx
, val_le
, addr
, ra
, MO_LEUW
);
2759 io_writex(env
, full
, mmu_idx
, val_le
, addr
, ra
, MO_LEUL
);
2765 io_writex(env
, full
, mmu_idx
, val_le
, addr
, ra
, MO_LEUQ
);
2768 qemu_build_not_reached();
2776 * Wrapper for the above.
2778 static uint64_t do_st_leN(CPUArchState
*env
, MMULookupPageData
*p
,
2779 uint64_t val_le
, int mmu_idx
,
2780 MemOp mop
, uintptr_t ra
)
2783 unsigned tmp
, half_size
;
2785 if (unlikely(p
->flags
& TLB_MMIO
)) {
2786 QEMU_IOTHREAD_LOCK_GUARD();
2787 return do_st_mmio_leN(env
, p
->full
, val_le
, p
->addr
,
2788 p
->size
, mmu_idx
, ra
);
2789 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2790 return val_le
>> (p
->size
* 8);
2794 * It is a given that we cross a page and therefore there is no atomicity
2795 * for the store as a whole, but subobjects may need attention.
2797 atom
= mop
& MO_ATOM_MASK
;
2799 case MO_ATOM_SUBALIGN
:
2800 return store_parts_leN(p
->haddr
, p
->size
, val_le
);
2802 case MO_ATOM_IFALIGN_PAIR
:
2803 case MO_ATOM_WITHIN16_PAIR
:
2804 tmp
= mop
& MO_SIZE
;
2805 tmp
= tmp
? tmp
- 1 : 0;
2806 half_size
= 1 << tmp
;
2807 if (atom
== MO_ATOM_IFALIGN_PAIR
2808 ? p
->size
== half_size
2809 : p
->size
>= half_size
) {
2810 if (!HAVE_al8_fast
&& p
->size
<= 4) {
2811 return store_whole_le4(p
->haddr
, p
->size
, val_le
);
2812 } else if (HAVE_al8
) {
2813 return store_whole_le8(p
->haddr
, p
->size
, val_le
);
2815 cpu_loop_exit_atomic(env_cpu(env
), ra
);
2820 case MO_ATOM_IFALIGN
:
2821 case MO_ATOM_WITHIN16
:
2823 return store_bytes_leN(p
->haddr
, p
->size
, val_le
);
2826 g_assert_not_reached();
2831 * Wrapper for the above, for 8 < size < 16.
2833 static uint64_t do_st16_leN(CPUArchState
*env
, MMULookupPageData
*p
,
2834 Int128 val_le
, int mmu_idx
,
2835 MemOp mop
, uintptr_t ra
)
2840 if (unlikely(p
->flags
& TLB_MMIO
)) {
2841 QEMU_IOTHREAD_LOCK_GUARD();
2842 do_st_mmio_leN(env
, p
->full
, int128_getlo(val_le
),
2843 p
->addr
, 8, mmu_idx
, ra
);
2844 return do_st_mmio_leN(env
, p
->full
, int128_gethi(val_le
),
2845 p
->addr
+ 8, size
- 8, mmu_idx
, ra
);
2846 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2847 return int128_gethi(val_le
) >> ((size
- 8) * 8);
2851 * It is a given that we cross a page and therefore there is no atomicity
2852 * for the store as a whole, but subobjects may need attention.
2854 atom
= mop
& MO_ATOM_MASK
;
2856 case MO_ATOM_SUBALIGN
:
2857 store_parts_leN(p
->haddr
, 8, int128_getlo(val_le
));
2858 return store_parts_leN(p
->haddr
+ 8, p
->size
- 8,
2859 int128_gethi(val_le
));
2861 case MO_ATOM_WITHIN16_PAIR
:
2862 /* Since size > 8, this is the half that must be atomic. */
2863 if (!HAVE_ATOMIC128_RW
) {
2864 cpu_loop_exit_atomic(env_cpu(env
), ra
);
2866 return store_whole_le16(p
->haddr
, p
->size
, val_le
);
2868 case MO_ATOM_IFALIGN_PAIR
:
2870 * Since size > 8, both halves are misaligned,
2871 * and so neither is atomic.
2873 case MO_ATOM_IFALIGN
:
2874 case MO_ATOM_WITHIN16
:
2876 stq_le_p(p
->haddr
, int128_getlo(val_le
));
2877 return store_bytes_leN(p
->haddr
+ 8, p
->size
- 8,
2878 int128_gethi(val_le
));
2881 g_assert_not_reached();
2885 static void do_st_1(CPUArchState
*env
, MMULookupPageData
*p
, uint8_t val
,
2886 int mmu_idx
, uintptr_t ra
)
2888 if (unlikely(p
->flags
& TLB_MMIO
)) {
2889 io_writex(env
, p
->full
, mmu_idx
, val
, p
->addr
, ra
, MO_UB
);
2890 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2893 *(uint8_t *)p
->haddr
= val
;
2897 static void do_st_2(CPUArchState
*env
, MMULookupPageData
*p
, uint16_t val
,
2898 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2900 if (unlikely(p
->flags
& TLB_MMIO
)) {
2901 if ((memop
& MO_BSWAP
) != MO_LE
) {
2904 QEMU_IOTHREAD_LOCK_GUARD();
2905 do_st_mmio_leN(env
, p
->full
, val
, p
->addr
, 2, mmu_idx
, ra
);
2906 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2909 /* Swap to host endian if necessary, then store. */
2910 if (memop
& MO_BSWAP
) {
2913 store_atom_2(env
, ra
, p
->haddr
, memop
, val
);
2917 static void do_st_4(CPUArchState
*env
, MMULookupPageData
*p
, uint32_t val
,
2918 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2920 if (unlikely(p
->flags
& TLB_MMIO
)) {
2921 if ((memop
& MO_BSWAP
) != MO_LE
) {
2924 QEMU_IOTHREAD_LOCK_GUARD();
2925 do_st_mmio_leN(env
, p
->full
, val
, p
->addr
, 4, mmu_idx
, ra
);
2926 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2929 /* Swap to host endian if necessary, then store. */
2930 if (memop
& MO_BSWAP
) {
2933 store_atom_4(env
, ra
, p
->haddr
, memop
, val
);
2937 static void do_st_8(CPUArchState
*env
, MMULookupPageData
*p
, uint64_t val
,
2938 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2940 if (unlikely(p
->flags
& TLB_MMIO
)) {
2941 if ((memop
& MO_BSWAP
) != MO_LE
) {
2944 QEMU_IOTHREAD_LOCK_GUARD();
2945 do_st_mmio_leN(env
, p
->full
, val
, p
->addr
, 8, mmu_idx
, ra
);
2946 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2949 /* Swap to host endian if necessary, then store. */
2950 if (memop
& MO_BSWAP
) {
2953 store_atom_8(env
, ra
, p
->haddr
, memop
, val
);
2957 void helper_stb_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
2958 MemOpIdx oi
, uintptr_t ra
)
2963 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_8
);
2964 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2965 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2966 tcg_debug_assert(!crosspage
);
2968 do_st_1(env
, &l
.page
[0], val
, l
.mmu_idx
, ra
);
2971 static void do_st2_mmu(CPUArchState
*env
, vaddr addr
, uint16_t val
,
2972 MemOpIdx oi
, uintptr_t ra
)
2978 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2979 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2980 if (likely(!crosspage
)) {
2981 do_st_2(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2985 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2986 a
= val
, b
= val
>> 8;
2988 b
= val
, a
= val
>> 8;
2990 do_st_1(env
, &l
.page
[0], a
, l
.mmu_idx
, ra
);
2991 do_st_1(env
, &l
.page
[1], b
, l
.mmu_idx
, ra
);
2994 void helper_stw_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
2995 MemOpIdx oi
, uintptr_t retaddr
)
2997 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2998 do_st2_mmu(env
, addr
, val
, oi
, retaddr
);
3001 static void do_st4_mmu(CPUArchState
*env
, vaddr addr
, uint32_t val
,
3002 MemOpIdx oi
, uintptr_t ra
)
3007 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
3008 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
3009 if (likely(!crosspage
)) {
3010 do_st_4(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
3014 /* Swap to little endian for simplicity, then store by bytes. */
3015 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
3018 val
= do_st_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
3019 (void) do_st_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
3022 void helper_stl_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
3023 MemOpIdx oi
, uintptr_t retaddr
)
3025 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
3026 do_st4_mmu(env
, addr
, val
, oi
, retaddr
);
3029 static void do_st8_mmu(CPUArchState
*env
, vaddr addr
, uint64_t val
,
3030 MemOpIdx oi
, uintptr_t ra
)
3035 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
3036 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
3037 if (likely(!crosspage
)) {
3038 do_st_8(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
3042 /* Swap to little endian for simplicity, then store by bytes. */
3043 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
3046 val
= do_st_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
3047 (void) do_st_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
3050 void helper_stq_mmu(CPUArchState
*env
, uint64_t addr
, uint64_t val
,
3051 MemOpIdx oi
, uintptr_t retaddr
)
3053 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
3054 do_st8_mmu(env
, addr
, val
, oi
, retaddr
);
3057 static void do_st16_mmu(CPUArchState
*env
, vaddr addr
, Int128 val
,
3058 MemOpIdx oi
, uintptr_t ra
)
3065 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
3066 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
3067 if (likely(!crosspage
)) {
3068 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
3069 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
3070 val
= bswap128(val
);
3072 a
= int128_getlo(val
);
3073 b
= int128_gethi(val
);
3074 QEMU_IOTHREAD_LOCK_GUARD();
3075 do_st_mmio_leN(env
, l
.page
[0].full
, a
, addr
, 8, l
.mmu_idx
, ra
);
3076 do_st_mmio_leN(env
, l
.page
[0].full
, b
, addr
+ 8, 8, l
.mmu_idx
, ra
);
3077 } else if (unlikely(l
.page
[0].flags
& TLB_DISCARD_WRITE
)) {
3080 /* Swap to host endian if necessary, then store. */
3081 if (l
.memop
& MO_BSWAP
) {
3082 val
= bswap128(val
);
3084 store_atom_16(env
, ra
, l
.page
[0].haddr
, l
.memop
, val
);
3089 first
= l
.page
[0].size
;
3091 MemOp mop8
= (l
.memop
& ~(MO_SIZE
| MO_BSWAP
)) | MO_64
;
3093 if (l
.memop
& MO_BSWAP
) {
3094 val
= bswap128(val
);
3096 if (HOST_BIG_ENDIAN
) {
3097 b
= int128_getlo(val
), a
= int128_gethi(val
);
3099 a
= int128_getlo(val
), b
= int128_gethi(val
);
3101 do_st_8(env
, &l
.page
[0], a
, l
.mmu_idx
, mop8
, ra
);
3102 do_st_8(env
, &l
.page
[1], b
, l
.mmu_idx
, mop8
, ra
);
3106 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
3107 val
= bswap128(val
);
3110 do_st_leN(env
, &l
.page
[0], int128_getlo(val
), l
.mmu_idx
, l
.memop
, ra
);
3111 val
= int128_urshift(val
, first
* 8);
3112 do_st16_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
3114 b
= do_st16_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
3115 do_st_leN(env
, &l
.page
[1], b
, l
.mmu_idx
, l
.memop
, ra
);
3119 void helper_st16_mmu(CPUArchState
*env
, uint64_t addr
, Int128 val
,
3120 MemOpIdx oi
, uintptr_t retaddr
)
3122 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
3123 do_st16_mmu(env
, addr
, val
, oi
, retaddr
);
3126 void helper_st_i128(CPUArchState
*env
, uint64_t addr
, Int128 val
, MemOpIdx oi
)
3128 helper_st16_mmu(env
, addr
, val
, oi
, GETPC());
3132 * Store Helpers for cpu_ldst.h
3135 static void plugin_store_cb(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
)
3137 qemu_plugin_vcpu_mem_cb(env_cpu(env
), addr
, oi
, QEMU_PLUGIN_MEM_W
);
3140 void cpu_stb_mmu(CPUArchState
*env
, abi_ptr addr
, uint8_t val
,
3141 MemOpIdx oi
, uintptr_t retaddr
)
3143 helper_stb_mmu(env
, addr
, val
, oi
, retaddr
);
3144 plugin_store_cb(env
, addr
, oi
);
3147 void cpu_stw_mmu(CPUArchState
*env
, abi_ptr addr
, uint16_t val
,
3148 MemOpIdx oi
, uintptr_t retaddr
)
3150 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
3151 do_st2_mmu(env
, addr
, val
, oi
, retaddr
);
3152 plugin_store_cb(env
, addr
, oi
);
3155 void cpu_stl_mmu(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
3156 MemOpIdx oi
, uintptr_t retaddr
)
3158 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
3159 do_st4_mmu(env
, addr
, val
, oi
, retaddr
);
3160 plugin_store_cb(env
, addr
, oi
);
3163 void cpu_stq_mmu(CPUArchState
*env
, abi_ptr addr
, uint64_t val
,
3164 MemOpIdx oi
, uintptr_t retaddr
)
3166 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
3167 do_st8_mmu(env
, addr
, val
, oi
, retaddr
);
3168 plugin_store_cb(env
, addr
, oi
);
3171 void cpu_st16_mmu(CPUArchState
*env
, abi_ptr addr
, Int128 val
,
3172 MemOpIdx oi
, uintptr_t retaddr
)
3174 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
3175 do_st16_mmu(env
, addr
, val
, oi
, retaddr
);
3176 plugin_store_cb(env
, addr
, oi
);
3179 #include "ldst_common.c.inc"
3182 * First set of functions passes in OI and RETADDR.
3183 * This makes them callable from other helpers.
3186 #define ATOMIC_NAME(X) \
3187 glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
3189 #define ATOMIC_MMU_CLEANUP
3191 #include "atomic_common.c.inc"
3194 #include "atomic_template.h"
3197 #include "atomic_template.h"
3200 #include "atomic_template.h"
3202 #ifdef CONFIG_ATOMIC64
3204 #include "atomic_template.h"
3207 #if defined(CONFIG_ATOMIC128) || HAVE_CMPXCHG128
3208 #define DATA_SIZE 16
3209 #include "atomic_template.h"
3212 /* Code access functions. */
3214 uint32_t cpu_ldub_code(CPUArchState
*env
, abi_ptr addr
)
3216 MemOpIdx oi
= make_memop_idx(MO_UB
, cpu_mmu_index(env
, true));
3217 return do_ld1_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3220 uint32_t cpu_lduw_code(CPUArchState
*env
, abi_ptr addr
)
3222 MemOpIdx oi
= make_memop_idx(MO_TEUW
, cpu_mmu_index(env
, true));
3223 return do_ld2_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3226 uint32_t cpu_ldl_code(CPUArchState
*env
, abi_ptr addr
)
3228 MemOpIdx oi
= make_memop_idx(MO_TEUL
, cpu_mmu_index(env
, true));
3229 return do_ld4_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3232 uint64_t cpu_ldq_code(CPUArchState
*env
, abi_ptr addr
)
3234 MemOpIdx oi
= make_memop_idx(MO_TEUQ
, cpu_mmu_index(env
, true));
3235 return do_ld8_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3238 uint8_t cpu_ldb_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3239 MemOpIdx oi
, uintptr_t retaddr
)
3241 return do_ld1_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3244 uint16_t cpu_ldw_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3245 MemOpIdx oi
, uintptr_t retaddr
)
3247 return do_ld2_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3250 uint32_t cpu_ldl_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3251 MemOpIdx oi
, uintptr_t retaddr
)
3253 return do_ld4_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3256 uint64_t cpu_ldq_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3257 MemOpIdx oi
, uintptr_t retaddr
)
3259 return do_ld8_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);