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.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 "exec/helper-proto.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 * target_ulong even on 32 bit builds */
78 QEMU_BUILD_BUG_ON(sizeof(target_ulong
) > sizeof(run_on_cpu_data
));
80 /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
82 QEMU_BUILD_BUG_ON(NB_MMU_MODES
> 16);
83 #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
85 static inline size_t tlb_n_entries(CPUTLBDescFast
*fast
)
87 return (fast
->mask
>> CPU_TLB_ENTRY_BITS
) + 1;
90 static inline size_t sizeof_tlb(CPUTLBDescFast
*fast
)
92 return fast
->mask
+ (1 << CPU_TLB_ENTRY_BITS
);
95 static void tlb_window_reset(CPUTLBDesc
*desc
, int64_t ns
,
98 desc
->window_begin_ns
= ns
;
99 desc
->window_max_entries
= max_entries
;
102 static void tb_jmp_cache_clear_page(CPUState
*cpu
, target_ulong page_addr
)
104 CPUJumpCache
*jc
= cpu
->tb_jmp_cache
;
111 i0
= tb_jmp_cache_hash_page(page_addr
);
112 for (i
= 0; i
< TB_JMP_PAGE_SIZE
; i
++) {
113 qatomic_set(&jc
->array
[i0
+ i
].tb
, NULL
);
118 * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
119 * @desc: The CPUTLBDesc portion of the TLB
120 * @fast: The CPUTLBDescFast portion of the same TLB
122 * Called with tlb_lock_held.
124 * We have two main constraints when resizing a TLB: (1) we only resize it
125 * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
126 * the array or unnecessarily flushing it), which means we do not control how
127 * frequently the resizing can occur; (2) we don't have access to the guest's
128 * future scheduling decisions, and therefore have to decide the magnitude of
129 * the resize based on past observations.
131 * In general, a memory-hungry process can benefit greatly from an appropriately
132 * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
133 * we just have to make the TLB as large as possible; while an oversized TLB
134 * results in minimal TLB miss rates, it also takes longer to be flushed
135 * (flushes can be _very_ frequent), and the reduced locality can also hurt
138 * To achieve near-optimal performance for all kinds of workloads, we:
140 * 1. Aggressively increase the size of the TLB when the use rate of the
141 * TLB being flushed is high, since it is likely that in the near future this
142 * memory-hungry process will execute again, and its memory hungriness will
143 * probably be similar.
145 * 2. Slowly reduce the size of the TLB as the use rate declines over a
146 * reasonably large time window. The rationale is that if in such a time window
147 * we have not observed a high TLB use rate, it is likely that we won't observe
148 * it in the near future. In that case, once a time window expires we downsize
149 * the TLB to match the maximum use rate observed in the window.
151 * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
152 * since in that range performance is likely near-optimal. Recall that the TLB
153 * is direct mapped, so we want the use rate to be low (or at least not too
154 * high), since otherwise we are likely to have a significant amount of
157 static void tlb_mmu_resize_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
,
160 size_t old_size
= tlb_n_entries(fast
);
162 size_t new_size
= old_size
;
163 int64_t window_len_ms
= 100;
164 int64_t window_len_ns
= window_len_ms
* 1000 * 1000;
165 bool window_expired
= now
> desc
->window_begin_ns
+ window_len_ns
;
167 if (desc
->n_used_entries
> desc
->window_max_entries
) {
168 desc
->window_max_entries
= desc
->n_used_entries
;
170 rate
= desc
->window_max_entries
* 100 / old_size
;
173 new_size
= MIN(old_size
<< 1, 1 << CPU_TLB_DYN_MAX_BITS
);
174 } else if (rate
< 30 && window_expired
) {
175 size_t ceil
= pow2ceil(desc
->window_max_entries
);
176 size_t expected_rate
= desc
->window_max_entries
* 100 / ceil
;
179 * Avoid undersizing when the max number of entries seen is just below
180 * a pow2. For instance, if max_entries == 1025, the expected use rate
181 * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
182 * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
183 * later. Thus, make sure that the expected use rate remains below 70%.
184 * (and since we double the size, that means the lowest rate we'd
185 * expect to get is 35%, which is still in the 30-70% range where
186 * we consider that the size is appropriate.)
188 if (expected_rate
> 70) {
191 new_size
= MAX(ceil
, 1 << CPU_TLB_DYN_MIN_BITS
);
194 if (new_size
== old_size
) {
195 if (window_expired
) {
196 tlb_window_reset(desc
, now
, desc
->n_used_entries
);
202 g_free(desc
->fulltlb
);
204 tlb_window_reset(desc
, now
, 0);
205 /* desc->n_used_entries is cleared by the caller */
206 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
207 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
208 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
211 * If the allocations fail, try smaller sizes. We just freed some
212 * memory, so going back to half of new_size has a good chance of working.
213 * Increased memory pressure elsewhere in the system might cause the
214 * allocations to fail though, so we progressively reduce the allocation
215 * size, aborting if we cannot even allocate the smallest TLB we support.
217 while (fast
->table
== NULL
|| desc
->fulltlb
== NULL
) {
218 if (new_size
== (1 << CPU_TLB_DYN_MIN_BITS
)) {
219 error_report("%s: %s", __func__
, strerror(errno
));
222 new_size
= MAX(new_size
>> 1, 1 << CPU_TLB_DYN_MIN_BITS
);
223 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
226 g_free(desc
->fulltlb
);
227 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
228 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
232 static void tlb_mmu_flush_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
)
234 desc
->n_used_entries
= 0;
235 desc
->large_page_addr
= -1;
236 desc
->large_page_mask
= -1;
238 memset(fast
->table
, -1, sizeof_tlb(fast
));
239 memset(desc
->vtable
, -1, sizeof(desc
->vtable
));
242 static void tlb_flush_one_mmuidx_locked(CPUArchState
*env
, int mmu_idx
,
245 CPUTLBDesc
*desc
= &env_tlb(env
)->d
[mmu_idx
];
246 CPUTLBDescFast
*fast
= &env_tlb(env
)->f
[mmu_idx
];
248 tlb_mmu_resize_locked(desc
, fast
, now
);
249 tlb_mmu_flush_locked(desc
, fast
);
252 static void tlb_mmu_init(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
, int64_t now
)
254 size_t n_entries
= 1 << CPU_TLB_DYN_DEFAULT_BITS
;
256 tlb_window_reset(desc
, now
, 0);
257 desc
->n_used_entries
= 0;
258 fast
->mask
= (n_entries
- 1) << CPU_TLB_ENTRY_BITS
;
259 fast
->table
= g_new(CPUTLBEntry
, n_entries
);
260 desc
->fulltlb
= g_new(CPUTLBEntryFull
, n_entries
);
261 tlb_mmu_flush_locked(desc
, fast
);
264 static inline void tlb_n_used_entries_inc(CPUArchState
*env
, uintptr_t mmu_idx
)
266 env_tlb(env
)->d
[mmu_idx
].n_used_entries
++;
269 static inline void tlb_n_used_entries_dec(CPUArchState
*env
, uintptr_t mmu_idx
)
271 env_tlb(env
)->d
[mmu_idx
].n_used_entries
--;
274 void tlb_init(CPUState
*cpu
)
276 CPUArchState
*env
= cpu
->env_ptr
;
277 int64_t now
= get_clock_realtime();
280 qemu_spin_init(&env_tlb(env
)->c
.lock
);
282 /* All tlbs are initialized flushed. */
283 env_tlb(env
)->c
.dirty
= 0;
285 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
286 tlb_mmu_init(&env_tlb(env
)->d
[i
], &env_tlb(env
)->f
[i
], now
);
290 void tlb_destroy(CPUState
*cpu
)
292 CPUArchState
*env
= cpu
->env_ptr
;
295 qemu_spin_destroy(&env_tlb(env
)->c
.lock
);
296 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
297 CPUTLBDesc
*desc
= &env_tlb(env
)->d
[i
];
298 CPUTLBDescFast
*fast
= &env_tlb(env
)->f
[i
];
301 g_free(desc
->fulltlb
);
305 /* flush_all_helper: run fn across all cpus
307 * If the wait flag is set then the src cpu's helper will be queued as
308 * "safe" work and the loop exited creating a synchronisation point
309 * where all queued work will be finished before execution starts
312 static void flush_all_helper(CPUState
*src
, run_on_cpu_func fn
,
319 async_run_on_cpu(cpu
, fn
, d
);
324 void tlb_flush_counts(size_t *pfull
, size_t *ppart
, size_t *pelide
)
327 size_t full
= 0, part
= 0, elide
= 0;
330 CPUArchState
*env
= cpu
->env_ptr
;
332 full
+= qatomic_read(&env_tlb(env
)->c
.full_flush_count
);
333 part
+= qatomic_read(&env_tlb(env
)->c
.part_flush_count
);
334 elide
+= qatomic_read(&env_tlb(env
)->c
.elide_flush_count
);
341 static void tlb_flush_by_mmuidx_async_work(CPUState
*cpu
, run_on_cpu_data data
)
343 CPUArchState
*env
= cpu
->env_ptr
;
344 uint16_t asked
= data
.host_int
;
345 uint16_t all_dirty
, work
, to_clean
;
346 int64_t now
= get_clock_realtime();
348 assert_cpu_is_self(cpu
);
350 tlb_debug("mmu_idx:0x%04" PRIx16
"\n", asked
);
352 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
354 all_dirty
= env_tlb(env
)->c
.dirty
;
355 to_clean
= asked
& all_dirty
;
356 all_dirty
&= ~to_clean
;
357 env_tlb(env
)->c
.dirty
= all_dirty
;
359 for (work
= to_clean
; work
!= 0; work
&= work
- 1) {
360 int mmu_idx
= ctz32(work
);
361 tlb_flush_one_mmuidx_locked(env
, mmu_idx
, now
);
364 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
366 tcg_flush_jmp_cache(cpu
);
368 if (to_clean
== ALL_MMUIDX_BITS
) {
369 qatomic_set(&env_tlb(env
)->c
.full_flush_count
,
370 env_tlb(env
)->c
.full_flush_count
+ 1);
372 qatomic_set(&env_tlb(env
)->c
.part_flush_count
,
373 env_tlb(env
)->c
.part_flush_count
+ ctpop16(to_clean
));
374 if (to_clean
!= asked
) {
375 qatomic_set(&env_tlb(env
)->c
.elide_flush_count
,
376 env_tlb(env
)->c
.elide_flush_count
+
377 ctpop16(asked
& ~to_clean
));
382 void tlb_flush_by_mmuidx(CPUState
*cpu
, uint16_t idxmap
)
384 tlb_debug("mmu_idx: 0x%" PRIx16
"\n", idxmap
);
386 if (cpu
->created
&& !qemu_cpu_is_self(cpu
)) {
387 async_run_on_cpu(cpu
, tlb_flush_by_mmuidx_async_work
,
388 RUN_ON_CPU_HOST_INT(idxmap
));
390 tlb_flush_by_mmuidx_async_work(cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
394 void tlb_flush(CPUState
*cpu
)
396 tlb_flush_by_mmuidx(cpu
, ALL_MMUIDX_BITS
);
399 void tlb_flush_by_mmuidx_all_cpus(CPUState
*src_cpu
, uint16_t idxmap
)
401 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
403 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
405 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
406 fn(src_cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
409 void tlb_flush_all_cpus(CPUState
*src_cpu
)
411 tlb_flush_by_mmuidx_all_cpus(src_cpu
, ALL_MMUIDX_BITS
);
414 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
, uint16_t idxmap
)
416 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
418 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
420 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
421 async_safe_run_on_cpu(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
424 void tlb_flush_all_cpus_synced(CPUState
*src_cpu
)
426 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, ALL_MMUIDX_BITS
);
429 static bool tlb_hit_page_mask_anyprot(CPUTLBEntry
*tlb_entry
,
430 target_ulong page
, target_ulong mask
)
433 mask
&= TARGET_PAGE_MASK
| TLB_INVALID_MASK
;
435 return (page
== (tlb_entry
->addr_read
& mask
) ||
436 page
== (tlb_addr_write(tlb_entry
) & mask
) ||
437 page
== (tlb_entry
->addr_code
& mask
));
440 static inline bool tlb_hit_page_anyprot(CPUTLBEntry
*tlb_entry
,
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
,
470 return tlb_flush_entry_mask_locked(tlb_entry
, page
, -1);
473 /* Called with tlb_c.lock held */
474 static void tlb_flush_vtlb_page_mask_locked(CPUArchState
*env
, int mmu_idx
,
478 CPUTLBDesc
*d
= &env_tlb(env
)->d
[mmu_idx
];
481 assert_cpu_is_self(env_cpu(env
));
482 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
483 if (tlb_flush_entry_mask_locked(&d
->vtable
[k
], page
, mask
)) {
484 tlb_n_used_entries_dec(env
, mmu_idx
);
489 static inline void tlb_flush_vtlb_page_locked(CPUArchState
*env
, int mmu_idx
,
492 tlb_flush_vtlb_page_mask_locked(env
, mmu_idx
, page
, -1);
495 static void tlb_flush_page_locked(CPUArchState
*env
, int midx
,
498 target_ulong lp_addr
= env_tlb(env
)->d
[midx
].large_page_addr
;
499 target_ulong lp_mask
= env_tlb(env
)->d
[midx
].large_page_mask
;
501 /* Check if we need to flush due to large pages. */
502 if ((page
& lp_mask
) == lp_addr
) {
503 tlb_debug("forcing full flush midx %d ("
504 TARGET_FMT_lx
"/" TARGET_FMT_lx
")\n",
505 midx
, lp_addr
, lp_mask
);
506 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
508 if (tlb_flush_entry_locked(tlb_entry(env
, midx
, page
), page
)) {
509 tlb_n_used_entries_dec(env
, midx
);
511 tlb_flush_vtlb_page_locked(env
, midx
, page
);
516 * tlb_flush_page_by_mmuidx_async_0:
517 * @cpu: cpu on which to flush
518 * @addr: page of virtual address to flush
519 * @idxmap: set of mmu_idx to flush
521 * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
522 * at @addr from the tlbs indicated by @idxmap from @cpu.
524 static void tlb_flush_page_by_mmuidx_async_0(CPUState
*cpu
,
528 CPUArchState
*env
= cpu
->env_ptr
;
531 assert_cpu_is_self(cpu
);
533 tlb_debug("page addr:" TARGET_FMT_lx
" mmu_map:0x%x\n", addr
, idxmap
);
535 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
536 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
537 if ((idxmap
>> mmu_idx
) & 1) {
538 tlb_flush_page_locked(env
, mmu_idx
, addr
);
541 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
544 * Discard jump cache entries for any tb which might potentially
545 * overlap the flushed page, which includes the previous.
547 tb_jmp_cache_clear_page(cpu
, addr
- TARGET_PAGE_SIZE
);
548 tb_jmp_cache_clear_page(cpu
, addr
);
552 * tlb_flush_page_by_mmuidx_async_1:
553 * @cpu: cpu on which to flush
554 * @data: encoded addr + idxmap
556 * Helper for tlb_flush_page_by_mmuidx and friends, called through
557 * async_run_on_cpu. The idxmap parameter is encoded in the page
558 * offset of the target_ptr field. This limits the set of mmu_idx
559 * that can be passed via this method.
561 static void tlb_flush_page_by_mmuidx_async_1(CPUState
*cpu
,
562 run_on_cpu_data data
)
564 target_ulong addr_and_idxmap
= (target_ulong
) data
.target_ptr
;
565 target_ulong addr
= addr_and_idxmap
& TARGET_PAGE_MASK
;
566 uint16_t idxmap
= addr_and_idxmap
& ~TARGET_PAGE_MASK
;
568 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
574 } TLBFlushPageByMMUIdxData
;
577 * tlb_flush_page_by_mmuidx_async_2:
578 * @cpu: cpu on which to flush
579 * @data: allocated addr + idxmap
581 * Helper for tlb_flush_page_by_mmuidx and friends, called through
582 * async_run_on_cpu. The addr+idxmap parameters are stored in a
583 * TLBFlushPageByMMUIdxData structure that has been allocated
584 * specifically for this helper. Free the structure when done.
586 static void tlb_flush_page_by_mmuidx_async_2(CPUState
*cpu
,
587 run_on_cpu_data data
)
589 TLBFlushPageByMMUIdxData
*d
= data
.host_ptr
;
591 tlb_flush_page_by_mmuidx_async_0(cpu
, d
->addr
, d
->idxmap
);
595 void tlb_flush_page_by_mmuidx(CPUState
*cpu
, target_ulong addr
, uint16_t idxmap
)
597 tlb_debug("addr: "TARGET_FMT_lx
" mmu_idx:%" PRIx16
"\n", addr
, idxmap
);
599 /* This should already be page aligned */
600 addr
&= TARGET_PAGE_MASK
;
602 if (qemu_cpu_is_self(cpu
)) {
603 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
604 } else if (idxmap
< TARGET_PAGE_SIZE
) {
606 * Most targets have only a few mmu_idx. In the case where
607 * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid
608 * allocating memory for this operation.
610 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_1
,
611 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
613 TLBFlushPageByMMUIdxData
*d
= g_new(TLBFlushPageByMMUIdxData
, 1);
615 /* Otherwise allocate a structure, freed by the worker. */
618 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_2
,
619 RUN_ON_CPU_HOST_PTR(d
));
623 void tlb_flush_page(CPUState
*cpu
, target_ulong addr
)
625 tlb_flush_page_by_mmuidx(cpu
, addr
, ALL_MMUIDX_BITS
);
628 void tlb_flush_page_by_mmuidx_all_cpus(CPUState
*src_cpu
, target_ulong addr
,
631 tlb_debug("addr: "TARGET_FMT_lx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
633 /* This should already be page aligned */
634 addr
&= TARGET_PAGE_MASK
;
637 * Allocate memory to hold addr+idxmap only when needed.
638 * See tlb_flush_page_by_mmuidx for details.
640 if (idxmap
< TARGET_PAGE_SIZE
) {
641 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
642 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
646 /* Allocate a separate data block for each destination cpu. */
647 CPU_FOREACH(dst_cpu
) {
648 if (dst_cpu
!= src_cpu
) {
649 TLBFlushPageByMMUIdxData
*d
650 = g_new(TLBFlushPageByMMUIdxData
, 1);
654 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
655 RUN_ON_CPU_HOST_PTR(d
));
660 tlb_flush_page_by_mmuidx_async_0(src_cpu
, addr
, idxmap
);
663 void tlb_flush_page_all_cpus(CPUState
*src
, target_ulong addr
)
665 tlb_flush_page_by_mmuidx_all_cpus(src
, addr
, ALL_MMUIDX_BITS
);
668 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
672 tlb_debug("addr: "TARGET_FMT_lx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
674 /* This should already be page aligned */
675 addr
&= TARGET_PAGE_MASK
;
678 * Allocate memory to hold addr+idxmap only when needed.
679 * See tlb_flush_page_by_mmuidx for details.
681 if (idxmap
< TARGET_PAGE_SIZE
) {
682 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
683 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
684 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
685 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
688 TLBFlushPageByMMUIdxData
*d
;
690 /* Allocate a separate data block for each destination cpu. */
691 CPU_FOREACH(dst_cpu
) {
692 if (dst_cpu
!= src_cpu
) {
693 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
696 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
697 RUN_ON_CPU_HOST_PTR(d
));
701 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
704 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_2
,
705 RUN_ON_CPU_HOST_PTR(d
));
709 void tlb_flush_page_all_cpus_synced(CPUState
*src
, target_ulong addr
)
711 tlb_flush_page_by_mmuidx_all_cpus_synced(src
, addr
, ALL_MMUIDX_BITS
);
714 static void tlb_flush_range_locked(CPUArchState
*env
, int midx
,
715 target_ulong addr
, target_ulong len
,
718 CPUTLBDesc
*d
= &env_tlb(env
)->d
[midx
];
719 CPUTLBDescFast
*f
= &env_tlb(env
)->f
[midx
];
720 target_ulong mask
= MAKE_64BIT_MASK(0, bits
);
723 * If @bits is smaller than the tlb size, there may be multiple entries
724 * within the TLB; otherwise all addresses that match under @mask hit
725 * the same TLB entry.
726 * TODO: Perhaps allow bits to be a few bits less than the size.
727 * For now, just flush the entire TLB.
729 * If @len is larger than the tlb size, then it will take longer to
730 * test all of the entries in the TLB than it will to flush it all.
732 if (mask
< f
->mask
|| len
> f
->mask
) {
733 tlb_debug("forcing full flush midx %d ("
734 TARGET_FMT_lx
"/" TARGET_FMT_lx
"+" TARGET_FMT_lx
")\n",
735 midx
, addr
, mask
, len
);
736 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
741 * Check if we need to flush due to large pages.
742 * Because large_page_mask contains all 1's from the msb,
743 * we only need to test the end of the range.
745 if (((addr
+ len
- 1) & d
->large_page_mask
) == d
->large_page_addr
) {
746 tlb_debug("forcing full flush midx %d ("
747 TARGET_FMT_lx
"/" TARGET_FMT_lx
")\n",
748 midx
, d
->large_page_addr
, d
->large_page_mask
);
749 tlb_flush_one_mmuidx_locked(env
, midx
, get_clock_realtime());
753 for (target_ulong i
= 0; i
< len
; i
+= TARGET_PAGE_SIZE
) {
754 target_ulong page
= addr
+ i
;
755 CPUTLBEntry
*entry
= tlb_entry(env
, midx
, page
);
757 if (tlb_flush_entry_mask_locked(entry
, page
, mask
)) {
758 tlb_n_used_entries_dec(env
, midx
);
760 tlb_flush_vtlb_page_mask_locked(env
, midx
, page
, mask
);
771 static void tlb_flush_range_by_mmuidx_async_0(CPUState
*cpu
,
774 CPUArchState
*env
= cpu
->env_ptr
;
777 assert_cpu_is_self(cpu
);
779 tlb_debug("range:" TARGET_FMT_lx
"/%u+" TARGET_FMT_lx
" mmu_map:0x%x\n",
780 d
.addr
, d
.bits
, d
.len
, d
.idxmap
);
782 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
783 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
784 if ((d
.idxmap
>> mmu_idx
) & 1) {
785 tlb_flush_range_locked(env
, mmu_idx
, d
.addr
, d
.len
, d
.bits
);
788 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
791 * If the length is larger than the jump cache size, then it will take
792 * longer to clear each entry individually than it will to clear it all.
794 if (d
.len
>= (TARGET_PAGE_SIZE
* TB_JMP_CACHE_SIZE
)) {
795 tcg_flush_jmp_cache(cpu
);
800 * Discard jump cache entries for any tb which might potentially
801 * overlap the flushed pages, which includes the previous.
803 d
.addr
-= TARGET_PAGE_SIZE
;
804 for (target_ulong i
= 0, n
= d
.len
/ TARGET_PAGE_SIZE
+ 1; i
< n
; i
++) {
805 tb_jmp_cache_clear_page(cpu
, d
.addr
);
806 d
.addr
+= TARGET_PAGE_SIZE
;
810 static void tlb_flush_range_by_mmuidx_async_1(CPUState
*cpu
,
811 run_on_cpu_data data
)
813 TLBFlushRangeData
*d
= data
.host_ptr
;
814 tlb_flush_range_by_mmuidx_async_0(cpu
, *d
);
818 void tlb_flush_range_by_mmuidx(CPUState
*cpu
, target_ulong addr
,
819 target_ulong len
, uint16_t idxmap
,
825 * If all bits are significant, and len is small,
826 * this devolves to tlb_flush_page.
828 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
829 tlb_flush_page_by_mmuidx(cpu
, addr
, idxmap
);
832 /* If no page bits are significant, this devolves to tlb_flush. */
833 if (bits
< TARGET_PAGE_BITS
) {
834 tlb_flush_by_mmuidx(cpu
, idxmap
);
838 /* This should already be page aligned */
839 d
.addr
= addr
& TARGET_PAGE_MASK
;
844 if (qemu_cpu_is_self(cpu
)) {
845 tlb_flush_range_by_mmuidx_async_0(cpu
, d
);
847 /* Otherwise allocate a structure, freed by the worker. */
848 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
849 async_run_on_cpu(cpu
, tlb_flush_range_by_mmuidx_async_1
,
850 RUN_ON_CPU_HOST_PTR(p
));
854 void tlb_flush_page_bits_by_mmuidx(CPUState
*cpu
, target_ulong addr
,
855 uint16_t idxmap
, unsigned bits
)
857 tlb_flush_range_by_mmuidx(cpu
, addr
, TARGET_PAGE_SIZE
, idxmap
, bits
);
860 void tlb_flush_range_by_mmuidx_all_cpus(CPUState
*src_cpu
,
861 target_ulong addr
, target_ulong len
,
862 uint16_t idxmap
, unsigned bits
)
868 * If all bits are significant, and len is small,
869 * this devolves to tlb_flush_page.
871 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
872 tlb_flush_page_by_mmuidx_all_cpus(src_cpu
, addr
, idxmap
);
875 /* If no page bits are significant, this devolves to tlb_flush. */
876 if (bits
< TARGET_PAGE_BITS
) {
877 tlb_flush_by_mmuidx_all_cpus(src_cpu
, idxmap
);
881 /* This should already be page aligned */
882 d
.addr
= addr
& TARGET_PAGE_MASK
;
887 /* Allocate a separate data block for each destination cpu. */
888 CPU_FOREACH(dst_cpu
) {
889 if (dst_cpu
!= src_cpu
) {
890 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
891 async_run_on_cpu(dst_cpu
,
892 tlb_flush_range_by_mmuidx_async_1
,
893 RUN_ON_CPU_HOST_PTR(p
));
897 tlb_flush_range_by_mmuidx_async_0(src_cpu
, d
);
900 void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState
*src_cpu
,
902 uint16_t idxmap
, unsigned bits
)
904 tlb_flush_range_by_mmuidx_all_cpus(src_cpu
, addr
, TARGET_PAGE_SIZE
,
908 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
914 TLBFlushRangeData d
, *p
;
918 * If all bits are significant, and len is small,
919 * this devolves to tlb_flush_page.
921 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
922 tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu
, addr
, idxmap
);
925 /* If no page bits are significant, this devolves to tlb_flush. */
926 if (bits
< TARGET_PAGE_BITS
) {
927 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, idxmap
);
931 /* This should already be page aligned */
932 d
.addr
= addr
& TARGET_PAGE_MASK
;
937 /* Allocate a separate data block for each destination cpu. */
938 CPU_FOREACH(dst_cpu
) {
939 if (dst_cpu
!= src_cpu
) {
940 p
= g_memdup(&d
, sizeof(d
));
941 async_run_on_cpu(dst_cpu
, tlb_flush_range_by_mmuidx_async_1
,
942 RUN_ON_CPU_HOST_PTR(p
));
946 p
= g_memdup(&d
, sizeof(d
));
947 async_safe_run_on_cpu(src_cpu
, tlb_flush_range_by_mmuidx_async_1
,
948 RUN_ON_CPU_HOST_PTR(p
));
951 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
956 tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu
, addr
, TARGET_PAGE_SIZE
,
960 /* update the TLBs so that writes to code in the virtual page 'addr'
962 void tlb_protect_code(ram_addr_t ram_addr
)
964 cpu_physical_memory_test_and_clear_dirty(ram_addr
& TARGET_PAGE_MASK
,
969 /* update the TLB so that writes in physical page 'phys_addr' are no longer
970 tested for self modifying code */
971 void tlb_unprotect_code(ram_addr_t ram_addr
)
973 cpu_physical_memory_set_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
);
978 * Dirty write flag handling
980 * When the TCG code writes to a location it looks up the address in
981 * the TLB and uses that data to compute the final address. If any of
982 * the lower bits of the address are set then the slow path is forced.
983 * There are a number of reasons to do this but for normal RAM the
984 * most usual is detecting writes to code regions which may invalidate
987 * Other vCPUs might be reading their TLBs during guest execution, so we update
988 * te->addr_write with qatomic_set. We don't need to worry about this for
989 * oversized guests as MTTCG is disabled for them.
991 * Called with tlb_c.lock held.
993 static void tlb_reset_dirty_range_locked(CPUTLBEntry
*tlb_entry
,
994 uintptr_t start
, uintptr_t length
)
996 uintptr_t addr
= tlb_entry
->addr_write
;
998 if ((addr
& (TLB_INVALID_MASK
| TLB_MMIO
|
999 TLB_DISCARD_WRITE
| TLB_NOTDIRTY
)) == 0) {
1000 addr
&= TARGET_PAGE_MASK
;
1001 addr
+= tlb_entry
->addend
;
1002 if ((addr
- start
) < length
) {
1003 #if TCG_OVERSIZED_GUEST
1004 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
1006 qatomic_set(&tlb_entry
->addr_write
,
1007 tlb_entry
->addr_write
| TLB_NOTDIRTY
);
1014 * Called with tlb_c.lock held.
1015 * Called only from the vCPU context, i.e. the TLB's owner thread.
1017 static inline void copy_tlb_helper_locked(CPUTLBEntry
*d
, const CPUTLBEntry
*s
)
1022 /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
1024 * We must take tlb_c.lock to avoid racing with another vCPU update. The only
1025 * thing actually updated is the target TLB entry ->addr_write flags.
1027 void tlb_reset_dirty(CPUState
*cpu
, ram_addr_t start1
, ram_addr_t length
)
1034 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
1035 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1037 unsigned int n
= tlb_n_entries(&env_tlb(env
)->f
[mmu_idx
]);
1039 for (i
= 0; i
< n
; i
++) {
1040 tlb_reset_dirty_range_locked(&env_tlb(env
)->f
[mmu_idx
].table
[i
],
1044 for (i
= 0; i
< CPU_VTLB_SIZE
; i
++) {
1045 tlb_reset_dirty_range_locked(&env_tlb(env
)->d
[mmu_idx
].vtable
[i
],
1049 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
1052 /* Called with tlb_c.lock held */
1053 static inline void tlb_set_dirty1_locked(CPUTLBEntry
*tlb_entry
,
1056 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
)) {
1057 tlb_entry
->addr_write
= vaddr
;
1061 /* update the TLB corresponding to virtual page vaddr
1062 so that it is no longer dirty */
1063 void tlb_set_dirty(CPUState
*cpu
, target_ulong vaddr
)
1065 CPUArchState
*env
= cpu
->env_ptr
;
1068 assert_cpu_is_self(cpu
);
1070 vaddr
&= TARGET_PAGE_MASK
;
1071 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
1072 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1073 tlb_set_dirty1_locked(tlb_entry(env
, mmu_idx
, vaddr
), vaddr
);
1076 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1078 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
1079 tlb_set_dirty1_locked(&env_tlb(env
)->d
[mmu_idx
].vtable
[k
], vaddr
);
1082 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
1085 /* Our TLB does not support large pages, so remember the area covered by
1086 large pages and trigger a full TLB flush if these are invalidated. */
1087 static void tlb_add_large_page(CPUArchState
*env
, int mmu_idx
,
1088 target_ulong vaddr
, target_ulong size
)
1090 target_ulong lp_addr
= env_tlb(env
)->d
[mmu_idx
].large_page_addr
;
1091 target_ulong lp_mask
= ~(size
- 1);
1093 if (lp_addr
== (target_ulong
)-1) {
1094 /* No previous large page. */
1097 /* Extend the existing region to include the new page.
1098 This is a compromise between unnecessary flushes and
1099 the cost of maintaining a full variable size TLB. */
1100 lp_mask
&= env_tlb(env
)->d
[mmu_idx
].large_page_mask
;
1101 while (((lp_addr
^ vaddr
) & lp_mask
) != 0) {
1105 env_tlb(env
)->d
[mmu_idx
].large_page_addr
= lp_addr
& lp_mask
;
1106 env_tlb(env
)->d
[mmu_idx
].large_page_mask
= lp_mask
;
1110 * Add a new TLB entry. At most one entry for a given virtual address
1111 * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
1112 * supplied size is only used by tlb_flush_page.
1114 * Called from TCG-generated code, which is under an RCU read-side
1117 void tlb_set_page_full(CPUState
*cpu
, int mmu_idx
,
1118 target_ulong vaddr
, CPUTLBEntryFull
*full
)
1120 CPUArchState
*env
= cpu
->env_ptr
;
1121 CPUTLB
*tlb
= env_tlb(env
);
1122 CPUTLBDesc
*desc
= &tlb
->d
[mmu_idx
];
1123 MemoryRegionSection
*section
;
1125 target_ulong address
;
1126 target_ulong write_address
;
1128 CPUTLBEntry
*te
, tn
;
1129 hwaddr iotlb
, xlat
, sz
, paddr_page
;
1130 target_ulong vaddr_page
;
1131 int asidx
, wp_flags
, prot
;
1132 bool is_ram
, is_romd
;
1134 assert_cpu_is_self(cpu
);
1136 if (full
->lg_page_size
<= TARGET_PAGE_BITS
) {
1137 sz
= TARGET_PAGE_SIZE
;
1139 sz
= (hwaddr
)1 << full
->lg_page_size
;
1140 tlb_add_large_page(env
, mmu_idx
, vaddr
, sz
);
1142 vaddr_page
= vaddr
& TARGET_PAGE_MASK
;
1143 paddr_page
= full
->phys_addr
& TARGET_PAGE_MASK
;
1146 asidx
= cpu_asidx_from_attrs(cpu
, full
->attrs
);
1147 section
= address_space_translate_for_iotlb(cpu
, asidx
, paddr_page
,
1148 &xlat
, &sz
, full
->attrs
, &prot
);
1149 assert(sz
>= TARGET_PAGE_SIZE
);
1151 tlb_debug("vaddr=" TARGET_FMT_lx
" paddr=0x" HWADDR_FMT_plx
1152 " prot=%x idx=%d\n",
1153 vaddr
, full
->phys_addr
, prot
, mmu_idx
);
1155 address
= vaddr_page
;
1156 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1157 /* Repeat the MMU check and TLB fill on every access. */
1158 address
|= TLB_INVALID_MASK
;
1160 if (full
->attrs
.byte_swap
) {
1161 address
|= TLB_BSWAP
;
1164 is_ram
= memory_region_is_ram(section
->mr
);
1165 is_romd
= memory_region_is_romd(section
->mr
);
1167 if (is_ram
|| is_romd
) {
1168 /* RAM and ROMD both have associated host memory. */
1169 addend
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) + xlat
;
1171 /* I/O does not; force the host address to NULL. */
1175 write_address
= address
;
1177 iotlb
= memory_region_get_ram_addr(section
->mr
) + xlat
;
1179 * Computing is_clean is expensive; avoid all that unless
1180 * the page is actually writable.
1182 if (prot
& PAGE_WRITE
) {
1183 if (section
->readonly
) {
1184 write_address
|= TLB_DISCARD_WRITE
;
1185 } else if (cpu_physical_memory_is_clean(iotlb
)) {
1186 write_address
|= TLB_NOTDIRTY
;
1191 iotlb
= memory_region_section_get_iotlb(cpu
, section
) + xlat
;
1193 * Writes to romd devices must go through MMIO to enable write.
1194 * Reads to romd devices go through the ram_ptr found above,
1195 * but of course reads to I/O must go through MMIO.
1197 write_address
|= TLB_MMIO
;
1199 address
= write_address
;
1203 wp_flags
= cpu_watchpoint_address_matches(cpu
, vaddr_page
,
1206 index
= tlb_index(env
, mmu_idx
, vaddr_page
);
1207 te
= tlb_entry(env
, mmu_idx
, vaddr_page
);
1210 * Hold the TLB lock for the rest of the function. We could acquire/release
1211 * the lock several times in the function, but it is faster to amortize the
1212 * acquisition cost by acquiring it just once. Note that this leads to
1213 * a longer critical section, but this is not a concern since the TLB lock
1214 * is unlikely to be contended.
1216 qemu_spin_lock(&tlb
->c
.lock
);
1218 /* Note that the tlb is no longer clean. */
1219 tlb
->c
.dirty
|= 1 << mmu_idx
;
1221 /* Make sure there's no cached translation for the new page. */
1222 tlb_flush_vtlb_page_locked(env
, mmu_idx
, vaddr_page
);
1225 * Only evict the old entry to the victim tlb if it's for a
1226 * different page; otherwise just overwrite the stale data.
1228 if (!tlb_hit_page_anyprot(te
, vaddr_page
) && !tlb_entry_is_empty(te
)) {
1229 unsigned vidx
= desc
->vindex
++ % CPU_VTLB_SIZE
;
1230 CPUTLBEntry
*tv
= &desc
->vtable
[vidx
];
1232 /* Evict the old entry into the victim tlb. */
1233 copy_tlb_helper_locked(tv
, te
);
1234 desc
->vfulltlb
[vidx
] = desc
->fulltlb
[index
];
1235 tlb_n_used_entries_dec(env
, mmu_idx
);
1238 /* refill the tlb */
1240 * At this point iotlb contains a physical section number in the lower
1241 * TARGET_PAGE_BITS, and either
1242 * + the ram_addr_t of the page base of the target RAM (RAM)
1243 * + the offset within section->mr of the page base (I/O, ROMD)
1244 * We subtract the vaddr_page (which is page aligned and thus won't
1245 * disturb the low bits) to give an offset which can be added to the
1246 * (non-page-aligned) vaddr of the eventual memory access to get
1247 * the MemoryRegion offset for the access. Note that the vaddr we
1248 * subtract here is that of the page base, and not the same as the
1249 * vaddr we add back in io_readx()/io_writex()/get_page_addr_code().
1251 desc
->fulltlb
[index
] = *full
;
1252 desc
->fulltlb
[index
].xlat_section
= iotlb
- vaddr_page
;
1253 desc
->fulltlb
[index
].phys_addr
= paddr_page
;
1255 /* Now calculate the new entry */
1256 tn
.addend
= addend
- vaddr_page
;
1257 if (prot
& PAGE_READ
) {
1258 tn
.addr_read
= address
;
1259 if (wp_flags
& BP_MEM_READ
) {
1260 tn
.addr_read
|= TLB_WATCHPOINT
;
1266 if (prot
& PAGE_EXEC
) {
1267 tn
.addr_code
= address
;
1273 if (prot
& PAGE_WRITE
) {
1274 tn
.addr_write
= write_address
;
1275 if (prot
& PAGE_WRITE_INV
) {
1276 tn
.addr_write
|= TLB_INVALID_MASK
;
1278 if (wp_flags
& BP_MEM_WRITE
) {
1279 tn
.addr_write
|= TLB_WATCHPOINT
;
1283 copy_tlb_helper_locked(te
, &tn
);
1284 tlb_n_used_entries_inc(env
, mmu_idx
);
1285 qemu_spin_unlock(&tlb
->c
.lock
);
1288 void tlb_set_page_with_attrs(CPUState
*cpu
, target_ulong vaddr
,
1289 hwaddr paddr
, MemTxAttrs attrs
, int prot
,
1290 int mmu_idx
, target_ulong size
)
1292 CPUTLBEntryFull full
= {
1296 .lg_page_size
= ctz64(size
)
1299 assert(is_power_of_2(size
));
1300 tlb_set_page_full(cpu
, mmu_idx
, vaddr
, &full
);
1303 void tlb_set_page(CPUState
*cpu
, target_ulong vaddr
,
1304 hwaddr paddr
, int prot
,
1305 int mmu_idx
, target_ulong size
)
1307 tlb_set_page_with_attrs(cpu
, vaddr
, paddr
, MEMTXATTRS_UNSPECIFIED
,
1308 prot
, mmu_idx
, size
);
1312 * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1313 * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1314 * be discarded and looked up again (e.g. via tlb_entry()).
1316 static void tlb_fill(CPUState
*cpu
, target_ulong addr
, int size
,
1317 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1322 * This is not a probe, so only valid return is success; failure
1323 * should result in exception + longjmp to the cpu loop.
1325 ok
= cpu
->cc
->tcg_ops
->tlb_fill(cpu
, addr
, size
,
1326 access_type
, mmu_idx
, false, retaddr
);
1330 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
1331 MMUAccessType access_type
,
1332 int mmu_idx
, uintptr_t retaddr
)
1334 cpu
->cc
->tcg_ops
->do_unaligned_access(cpu
, addr
, access_type
,
1338 static inline void cpu_transaction_failed(CPUState
*cpu
, hwaddr physaddr
,
1339 vaddr addr
, unsigned size
,
1340 MMUAccessType access_type
,
1341 int mmu_idx
, MemTxAttrs attrs
,
1342 MemTxResult response
,
1345 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1347 if (!cpu
->ignore_memory_transaction_failures
&&
1348 cc
->tcg_ops
->do_transaction_failed
) {
1349 cc
->tcg_ops
->do_transaction_failed(cpu
, physaddr
, addr
, size
,
1350 access_type
, mmu_idx
, attrs
,
1355 static uint64_t io_readx(CPUArchState
*env
, CPUTLBEntryFull
*full
,
1356 int mmu_idx
, target_ulong addr
, uintptr_t retaddr
,
1357 MMUAccessType access_type
, MemOp op
)
1359 CPUState
*cpu
= env_cpu(env
);
1361 MemoryRegionSection
*section
;
1366 section
= iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1368 mr_offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1369 cpu
->mem_io_pc
= retaddr
;
1370 if (!cpu
->can_do_io
) {
1371 cpu_io_recompile(cpu
, retaddr
);
1375 QEMU_IOTHREAD_LOCK_GUARD();
1376 r
= memory_region_dispatch_read(mr
, mr_offset
, &val
, op
, full
->attrs
);
1379 if (r
!= MEMTX_OK
) {
1380 hwaddr physaddr
= mr_offset
+
1381 section
->offset_within_address_space
-
1382 section
->offset_within_region
;
1384 cpu_transaction_failed(cpu
, physaddr
, addr
, memop_size(op
), access_type
,
1385 mmu_idx
, full
->attrs
, r
, retaddr
);
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.
1395 static void save_iotlb_data(CPUState
*cs
, MemoryRegionSection
*section
,
1398 #ifdef CONFIG_PLUGIN
1399 SavedIOTLB
*saved
= &cs
->saved_iotlb
;
1400 saved
->section
= section
;
1401 saved
->mr_offset
= mr_offset
;
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
)
1409 CPUState
*cpu
= env_cpu(env
);
1411 MemoryRegionSection
*section
;
1415 section
= iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1417 mr_offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1418 if (!cpu
->can_do_io
) {
1419 cpu_io_recompile(cpu
, retaddr
);
1421 cpu
->mem_io_pc
= retaddr
;
1424 * The memory_region_dispatch may trigger a flush/resize
1425 * so for plugins we save the iotlb_data just in case.
1427 save_iotlb_data(cpu
, section
, mr_offset
);
1430 QEMU_IOTHREAD_LOCK_GUARD();
1431 r
= memory_region_dispatch_write(mr
, mr_offset
, val
, op
, full
->attrs
);
1434 if (r
!= MEMTX_OK
) {
1435 hwaddr physaddr
= mr_offset
+
1436 section
->offset_within_address_space
-
1437 section
->offset_within_region
;
1439 cpu_transaction_failed(cpu
, physaddr
, addr
, memop_size(op
),
1440 MMU_DATA_STORE
, mmu_idx
, full
->attrs
, r
,
1445 /* Return true if ADDR is present in the victim tlb, and has been copied
1446 back to the main tlb. */
1447 static bool victim_tlb_hit(CPUArchState
*env
, size_t mmu_idx
, size_t index
,
1448 MMUAccessType access_type
, target_ulong page
)
1452 assert_cpu_is_self(env_cpu(env
));
1453 for (vidx
= 0; vidx
< CPU_VTLB_SIZE
; ++vidx
) {
1454 CPUTLBEntry
*vtlb
= &env_tlb(env
)->d
[mmu_idx
].vtable
[vidx
];
1455 target_ulong cmp
= tlb_read_idx(vtlb
, access_type
);
1458 /* Found entry in victim tlb, swap tlb and iotlb. */
1459 CPUTLBEntry tmptlb
, *tlb
= &env_tlb(env
)->f
[mmu_idx
].table
[index
];
1461 qemu_spin_lock(&env_tlb(env
)->c
.lock
);
1462 copy_tlb_helper_locked(&tmptlb
, tlb
);
1463 copy_tlb_helper_locked(tlb
, vtlb
);
1464 copy_tlb_helper_locked(vtlb
, &tmptlb
);
1465 qemu_spin_unlock(&env_tlb(env
)->c
.lock
);
1467 CPUTLBEntryFull
*f1
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1468 CPUTLBEntryFull
*f2
= &env_tlb(env
)->d
[mmu_idx
].vfulltlb
[vidx
];
1469 CPUTLBEntryFull tmpf
;
1470 tmpf
= *f1
; *f1
= *f2
; *f2
= tmpf
;
1477 static void notdirty_write(CPUState
*cpu
, vaddr mem_vaddr
, unsigned size
,
1478 CPUTLBEntryFull
*full
, uintptr_t retaddr
)
1480 ram_addr_t ram_addr
= mem_vaddr
+ full
->xlat_section
;
1482 trace_memory_notdirty_write_access(mem_vaddr
, ram_addr
, size
);
1484 if (!cpu_physical_memory_get_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
)) {
1485 tb_invalidate_phys_range_fast(ram_addr
, size
, retaddr
);
1489 * Set both VGA and migration bits for simplicity and to remove
1490 * the notdirty callback faster.
1492 cpu_physical_memory_set_dirty_range(ram_addr
, size
, DIRTY_CLIENTS_NOCODE
);
1494 /* We remove the notdirty callback only if the code has been flushed. */
1495 if (!cpu_physical_memory_is_clean(ram_addr
)) {
1496 trace_memory_notdirty_set_dirty(mem_vaddr
);
1497 tlb_set_dirty(cpu
, mem_vaddr
);
1501 static int probe_access_internal(CPUArchState
*env
, target_ulong addr
,
1502 int fault_size
, MMUAccessType access_type
,
1503 int mmu_idx
, bool nonfault
,
1504 void **phost
, CPUTLBEntryFull
**pfull
,
1507 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1508 CPUTLBEntry
*entry
= tlb_entry(env
, mmu_idx
, addr
);
1509 target_ulong tlb_addr
= tlb_read_idx(entry
, access_type
);
1510 target_ulong page_addr
= addr
& TARGET_PAGE_MASK
;
1511 int flags
= TLB_FLAGS_MASK
;
1513 if (!tlb_hit_page(tlb_addr
, page_addr
)) {
1514 if (!victim_tlb_hit(env
, mmu_idx
, index
, access_type
, page_addr
)) {
1515 CPUState
*cs
= env_cpu(env
);
1517 if (!cs
->cc
->tcg_ops
->tlb_fill(cs
, addr
, fault_size
, access_type
,
1518 mmu_idx
, nonfault
, retaddr
)) {
1519 /* Non-faulting page table read failed. */
1522 return TLB_INVALID_MASK
;
1525 /* TLB resize via tlb_fill may have moved the entry. */
1526 index
= tlb_index(env
, mmu_idx
, addr
);
1527 entry
= tlb_entry(env
, mmu_idx
, addr
);
1530 * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately,
1531 * to force the next access through tlb_fill. We've just
1532 * called tlb_fill, so we know that this entry *is* valid.
1534 flags
&= ~TLB_INVALID_MASK
;
1536 tlb_addr
= tlb_read_idx(entry
, access_type
);
1540 *pfull
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1542 /* Fold all "mmio-like" bits into TLB_MMIO. This is not RAM. */
1543 if (unlikely(flags
& ~(TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1548 /* Everything else is RAM. */
1549 *phost
= (void *)((uintptr_t)addr
+ entry
->addend
);
1553 int probe_access_full(CPUArchState
*env
, target_ulong addr
, int size
,
1554 MMUAccessType access_type
, int mmu_idx
,
1555 bool nonfault
, void **phost
, CPUTLBEntryFull
**pfull
,
1558 int flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1559 nonfault
, phost
, pfull
, retaddr
);
1561 /* Handle clean RAM pages. */
1562 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1563 notdirty_write(env_cpu(env
), addr
, 1, *pfull
, retaddr
);
1564 flags
&= ~TLB_NOTDIRTY
;
1570 int probe_access_flags(CPUArchState
*env
, target_ulong addr
, int size
,
1571 MMUAccessType access_type
, int mmu_idx
,
1572 bool nonfault
, void **phost
, uintptr_t retaddr
)
1574 CPUTLBEntryFull
*full
;
1577 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1579 flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1580 nonfault
, phost
, &full
, retaddr
);
1582 /* Handle clean RAM pages. */
1583 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1584 notdirty_write(env_cpu(env
), addr
, 1, full
, retaddr
);
1585 flags
&= ~TLB_NOTDIRTY
;
1591 void *probe_access(CPUArchState
*env
, target_ulong addr
, int size
,
1592 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1594 CPUTLBEntryFull
*full
;
1598 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1600 flags
= probe_access_internal(env
, addr
, size
, access_type
, mmu_idx
,
1601 false, &host
, &full
, retaddr
);
1603 /* Per the interface, size == 0 merely faults the access. */
1608 if (unlikely(flags
& (TLB_NOTDIRTY
| TLB_WATCHPOINT
))) {
1609 /* Handle watchpoints. */
1610 if (flags
& TLB_WATCHPOINT
) {
1611 int wp_access
= (access_type
== MMU_DATA_STORE
1612 ? BP_MEM_WRITE
: BP_MEM_READ
);
1613 cpu_check_watchpoint(env_cpu(env
), addr
, size
,
1614 full
->attrs
, wp_access
, retaddr
);
1617 /* Handle clean RAM pages. */
1618 if (flags
& TLB_NOTDIRTY
) {
1619 notdirty_write(env_cpu(env
), addr
, 1, full
, retaddr
);
1626 void *tlb_vaddr_to_host(CPUArchState
*env
, abi_ptr addr
,
1627 MMUAccessType access_type
, int mmu_idx
)
1629 CPUTLBEntryFull
*full
;
1633 flags
= probe_access_internal(env
, addr
, 0, access_type
,
1634 mmu_idx
, true, &host
, &full
, 0);
1636 /* No combination of flags are expected by the caller. */
1637 return flags
? NULL
: host
;
1641 * Return a ram_addr_t for the virtual address for execution.
1643 * Return -1 if we can't translate and execute from an entire page
1644 * of RAM. This will force us to execute by loading and translating
1645 * one insn at a time, without caching.
1647 * NOTE: This function will trigger an exception if the page is
1650 tb_page_addr_t
get_page_addr_code_hostp(CPUArchState
*env
, target_ulong addr
,
1653 CPUTLBEntryFull
*full
;
1656 (void)probe_access_internal(env
, addr
, 1, MMU_INST_FETCH
,
1657 cpu_mmu_index(env
, true), false, &p
, &full
, 0);
1662 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1669 return qemu_ram_addr_from_host_nofail(p
);
1672 /* Load/store with atomicity primitives. */
1673 #include "ldst_atomicity.c.inc"
1675 #ifdef CONFIG_PLUGIN
1677 * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1678 * This should be a hot path as we will have just looked this path up
1679 * in the softmmu lookup code (or helper). We don't handle re-fills or
1680 * checking the victim table. This is purely informational.
1682 * This almost never fails as the memory access being instrumented
1683 * should have just filled the TLB. The one corner case is io_writex
1684 * which can cause TLB flushes and potential resizing of the TLBs
1685 * losing the information we need. In those cases we need to recover
1686 * data from a copy of the CPUTLBEntryFull. As long as this always occurs
1687 * from the same thread (which a mem callback will be) this is safe.
1690 bool tlb_plugin_lookup(CPUState
*cpu
, target_ulong addr
, int mmu_idx
,
1691 bool is_store
, struct qemu_plugin_hwaddr
*data
)
1693 CPUArchState
*env
= cpu
->env_ptr
;
1694 CPUTLBEntry
*tlbe
= tlb_entry(env
, mmu_idx
, addr
);
1695 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1696 target_ulong tlb_addr
= is_store
? tlb_addr_write(tlbe
) : tlbe
->addr_read
;
1698 if (likely(tlb_hit(tlb_addr
, addr
))) {
1699 /* We must have an iotlb entry for MMIO */
1700 if (tlb_addr
& TLB_MMIO
) {
1701 CPUTLBEntryFull
*full
;
1702 full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1704 data
->v
.io
.section
=
1705 iotlb_to_section(cpu
, full
->xlat_section
, full
->attrs
);
1706 data
->v
.io
.offset
= (full
->xlat_section
& TARGET_PAGE_MASK
) + addr
;
1708 data
->is_io
= false;
1709 data
->v
.ram
.hostaddr
= (void *)((uintptr_t)addr
+ tlbe
->addend
);
1713 SavedIOTLB
*saved
= &cpu
->saved_iotlb
;
1715 data
->v
.io
.section
= saved
->section
;
1716 data
->v
.io
.offset
= saved
->mr_offset
;
1724 * Probe for a load/store operation.
1725 * Return the host address and into @flags.
1728 typedef struct MMULookupPageData
{
1729 CPUTLBEntryFull
*full
;
1734 } MMULookupPageData
;
1736 typedef struct MMULookupLocals
{
1737 MMULookupPageData page
[2];
1743 * mmu_lookup1: translate one page
1745 * @data: lookup parameters
1746 * @mmu_idx: virtual address context
1747 * @access_type: load/store/code
1748 * @ra: return address into tcg generated code, or 0
1750 * Resolve the translation for the one page at @data.addr, filling in
1751 * the rest of @data with the results. If the translation fails,
1752 * tlb_fill will longjmp out. Return true if the softmmu tlb for
1753 * @mmu_idx may have resized.
1755 static bool mmu_lookup1(CPUArchState
*env
, MMULookupPageData
*data
,
1756 int mmu_idx
, MMUAccessType access_type
, uintptr_t ra
)
1758 target_ulong addr
= data
->addr
;
1759 uintptr_t index
= tlb_index(env
, mmu_idx
, addr
);
1760 CPUTLBEntry
*entry
= tlb_entry(env
, mmu_idx
, addr
);
1761 target_ulong tlb_addr
= tlb_read_idx(entry
, access_type
);
1762 bool maybe_resized
= false;
1764 /* If the TLB entry is for a different page, reload and try again. */
1765 if (!tlb_hit(tlb_addr
, addr
)) {
1766 if (!victim_tlb_hit(env
, mmu_idx
, index
, access_type
,
1767 addr
& TARGET_PAGE_MASK
)) {
1768 tlb_fill(env_cpu(env
), addr
, data
->size
, access_type
, mmu_idx
, ra
);
1769 maybe_resized
= true;
1770 index
= tlb_index(env
, mmu_idx
, addr
);
1771 entry
= tlb_entry(env
, mmu_idx
, addr
);
1773 tlb_addr
= tlb_read_idx(entry
, access_type
) & ~TLB_INVALID_MASK
;
1776 data
->flags
= tlb_addr
& TLB_FLAGS_MASK
;
1777 data
->full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1778 /* Compute haddr speculatively; depending on flags it might be invalid. */
1779 data
->haddr
= (void *)((uintptr_t)addr
+ entry
->addend
);
1781 return maybe_resized
;
1785 * mmu_watch_or_dirty
1787 * @data: lookup parameters
1788 * @access_type: load/store/code
1789 * @ra: return address into tcg generated code, or 0
1791 * Trigger watchpoints for @data.addr:@data.size;
1792 * record writes to protected clean pages.
1794 static void mmu_watch_or_dirty(CPUArchState
*env
, MMULookupPageData
*data
,
1795 MMUAccessType access_type
, uintptr_t ra
)
1797 CPUTLBEntryFull
*full
= data
->full
;
1798 target_ulong addr
= data
->addr
;
1799 int flags
= data
->flags
;
1800 int size
= data
->size
;
1802 /* On watchpoint hit, this will longjmp out. */
1803 if (flags
& TLB_WATCHPOINT
) {
1804 int wp
= access_type
== MMU_DATA_STORE
? BP_MEM_WRITE
: BP_MEM_READ
;
1805 cpu_check_watchpoint(env_cpu(env
), addr
, size
, full
->attrs
, wp
, ra
);
1806 flags
&= ~TLB_WATCHPOINT
;
1809 /* Note that notdirty is only set for writes. */
1810 if (flags
& TLB_NOTDIRTY
) {
1811 notdirty_write(env_cpu(env
), addr
, size
, full
, ra
);
1812 flags
&= ~TLB_NOTDIRTY
;
1814 data
->flags
= flags
;
1818 * mmu_lookup: translate page(s)
1820 * @addr: virtual address
1821 * @oi: combined mmu_idx and MemOp
1822 * @ra: return address into tcg generated code, or 0
1823 * @access_type: load/store/code
1826 * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
1827 * bytes. Return true if the lookup crosses a page boundary.
1829 static bool mmu_lookup(CPUArchState
*env
, target_ulong addr
, MemOpIdx oi
,
1830 uintptr_t ra
, MMUAccessType type
, MMULookupLocals
*l
)
1836 l
->memop
= get_memop(oi
);
1837 l
->mmu_idx
= get_mmuidx(oi
);
1839 tcg_debug_assert(l
->mmu_idx
< NB_MMU_MODES
);
1841 /* Handle CPU specific unaligned behaviour */
1842 a_bits
= get_alignment_bits(l
->memop
);
1843 if (addr
& ((1 << a_bits
) - 1)) {
1844 cpu_unaligned_access(env_cpu(env
), addr
, type
, l
->mmu_idx
, ra
);
1847 l
->page
[0].addr
= addr
;
1848 l
->page
[0].size
= memop_size(l
->memop
);
1849 l
->page
[1].addr
= (addr
+ l
->page
[0].size
- 1) & TARGET_PAGE_MASK
;
1850 l
->page
[1].size
= 0;
1851 crosspage
= (addr
^ l
->page
[1].addr
) & TARGET_PAGE_MASK
;
1853 if (likely(!crosspage
)) {
1854 mmu_lookup1(env
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1856 flags
= l
->page
[0].flags
;
1857 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1858 mmu_watch_or_dirty(env
, &l
->page
[0], type
, ra
);
1860 if (unlikely(flags
& TLB_BSWAP
)) {
1861 l
->memop
^= MO_BSWAP
;
1864 /* Finish compute of page crossing. */
1865 int size0
= l
->page
[1].addr
- addr
;
1866 l
->page
[1].size
= l
->page
[0].size
- size0
;
1867 l
->page
[0].size
= size0
;
1870 * Lookup both pages, recognizing exceptions from either. If the
1871 * second lookup potentially resized, refresh first CPUTLBEntryFull.
1873 mmu_lookup1(env
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1874 if (mmu_lookup1(env
, &l
->page
[1], l
->mmu_idx
, type
, ra
)) {
1875 uintptr_t index
= tlb_index(env
, l
->mmu_idx
, addr
);
1876 l
->page
[0].full
= &env_tlb(env
)->d
[l
->mmu_idx
].fulltlb
[index
];
1879 flags
= l
->page
[0].flags
| l
->page
[1].flags
;
1880 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1881 mmu_watch_or_dirty(env
, &l
->page
[0], type
, ra
);
1882 mmu_watch_or_dirty(env
, &l
->page
[1], type
, ra
);
1886 * Since target/sparc is the only user of TLB_BSWAP, and all
1887 * Sparc accesses are aligned, any treatment across two pages
1888 * would be arbitrary. Refuse it until there's a use.
1890 tcg_debug_assert((flags
& TLB_BSWAP
) == 0);
1897 * Probe for an atomic operation. Do not allow unaligned operations,
1898 * or io operations to proceed. Return the host address.
1900 static void *atomic_mmu_lookup(CPUArchState
*env
, target_ulong addr
,
1901 MemOpIdx oi
, int size
, uintptr_t retaddr
)
1903 uintptr_t mmu_idx
= get_mmuidx(oi
);
1904 MemOp mop
= get_memop(oi
);
1905 int a_bits
= get_alignment_bits(mop
);
1908 target_ulong tlb_addr
;
1910 CPUTLBEntryFull
*full
;
1912 tcg_debug_assert(mmu_idx
< NB_MMU_MODES
);
1914 /* Adjust the given return address. */
1915 retaddr
-= GETPC_ADJ
;
1917 /* Enforce guest required alignment. */
1918 if (unlikely(a_bits
> 0 && (addr
& ((1 << a_bits
) - 1)))) {
1919 /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1920 cpu_unaligned_access(env_cpu(env
), addr
, MMU_DATA_STORE
,
1924 /* Enforce qemu required alignment. */
1925 if (unlikely(addr
& (size
- 1))) {
1926 /* We get here if guest alignment was not requested,
1927 or was not enforced by cpu_unaligned_access above.
1928 We might widen the access and emulate, but for now
1929 mark an exception and exit the cpu loop. */
1930 goto stop_the_world
;
1933 index
= tlb_index(env
, mmu_idx
, addr
);
1934 tlbe
= tlb_entry(env
, mmu_idx
, addr
);
1936 /* Check TLB entry and enforce page permissions. */
1937 tlb_addr
= tlb_addr_write(tlbe
);
1938 if (!tlb_hit(tlb_addr
, addr
)) {
1939 if (!victim_tlb_hit(env
, mmu_idx
, index
, MMU_DATA_STORE
,
1940 addr
& TARGET_PAGE_MASK
)) {
1941 tlb_fill(env_cpu(env
), addr
, size
,
1942 MMU_DATA_STORE
, mmu_idx
, retaddr
);
1943 index
= tlb_index(env
, mmu_idx
, addr
);
1944 tlbe
= tlb_entry(env
, mmu_idx
, addr
);
1946 tlb_addr
= tlb_addr_write(tlbe
) & ~TLB_INVALID_MASK
;
1950 * Let the guest notice RMW on a write-only page.
1951 * We have just verified that the page is writable.
1952 * Subpage lookups may have left TLB_INVALID_MASK set,
1953 * but addr_read will only be -1 if PAGE_READ was unset.
1955 if (unlikely(tlbe
->addr_read
== -1)) {
1956 tlb_fill(env_cpu(env
), addr
, size
, MMU_DATA_LOAD
, mmu_idx
, retaddr
);
1958 * Since we don't support reads and writes to different
1959 * addresses, and we do have the proper page loaded for
1960 * write, this shouldn't ever return. But just in case,
1961 * handle via stop-the-world.
1963 goto stop_the_world
;
1965 /* Collect TLB_WATCHPOINT for read. */
1966 tlb_addr
|= tlbe
->addr_read
;
1968 /* Notice an IO access or a needs-MMU-lookup access */
1969 if (unlikely(tlb_addr
& (TLB_MMIO
| TLB_DISCARD_WRITE
))) {
1970 /* There's really nothing that can be done to
1971 support this apart from stop-the-world. */
1972 goto stop_the_world
;
1975 hostaddr
= (void *)((uintptr_t)addr
+ tlbe
->addend
);
1976 full
= &env_tlb(env
)->d
[mmu_idx
].fulltlb
[index
];
1978 if (unlikely(tlb_addr
& TLB_NOTDIRTY
)) {
1979 notdirty_write(env_cpu(env
), addr
, size
, full
, retaddr
);
1982 if (unlikely(tlb_addr
& TLB_WATCHPOINT
)) {
1983 cpu_check_watchpoint(env_cpu(env
), addr
, size
, full
->attrs
,
1984 BP_MEM_READ
| BP_MEM_WRITE
, retaddr
);
1990 cpu_loop_exit_atomic(env_cpu(env
), retaddr
);
1996 * We support two different access types. SOFTMMU_CODE_ACCESS is
1997 * specifically for reading instructions from system memory. It is
1998 * called by the translation loop and in some helpers where the code
1999 * is disassembled. It shouldn't be called directly by guest code.
2001 * For the benefit of TCG generated code, we want to avoid the
2002 * complication of ABI-specific return type promotion and always
2003 * return a value extended to the register size of the host. This is
2004 * tcg_target_long, except in the case of a 32-bit host and 64-bit
2005 * data, and for that we always have uint64_t.
2007 * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
2013 * @p: translation parameters
2014 * @ret_be: accumulated data
2015 * @mmu_idx: virtual address context
2016 * @ra: return address into tcg generated code, or 0
2018 * Load @p->size bytes from @p->addr, which is memory-mapped i/o.
2019 * The bytes are concatenated in big-endian order with @ret_be.
2021 static uint64_t do_ld_mmio_beN(CPUArchState
*env
, MMULookupPageData
*p
,
2022 uint64_t ret_be
, int mmu_idx
,
2023 MMUAccessType type
, uintptr_t ra
)
2025 CPUTLBEntryFull
*full
= p
->full
;
2026 target_ulong addr
= p
->addr
;
2027 int i
, size
= p
->size
;
2029 QEMU_IOTHREAD_LOCK_GUARD();
2030 for (i
= 0; i
< size
; i
++) {
2031 uint8_t x
= io_readx(env
, full
, mmu_idx
, addr
+ i
, ra
, type
, MO_UB
);
2032 ret_be
= (ret_be
<< 8) | x
;
2039 * @p: translation parameters
2040 * @ret_be: accumulated data
2042 * Load @p->size bytes from @p->haddr, which is RAM.
2043 * The bytes to concatenated in big-endian order with @ret_be.
2045 static uint64_t do_ld_bytes_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2047 uint8_t *haddr
= p
->haddr
;
2048 int i
, size
= p
->size
;
2050 for (i
= 0; i
< size
; i
++) {
2051 ret_be
= (ret_be
<< 8) | haddr
[i
];
2058 * @p: translation parameters
2059 * @ret_be: accumulated data
2061 * As do_ld_bytes_beN, but atomically on each aligned part.
2063 static uint64_t do_ld_parts_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2065 void *haddr
= p
->haddr
;
2073 * Find minimum of alignment and size.
2074 * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2075 * would have only checked the low bits of addr|size once at the start,
2076 * but is just as easy.
2078 switch (((uintptr_t)haddr
| size
) & 7) {
2080 x
= cpu_to_be32(load_atomic4(haddr
));
2081 ret_be
= (ret_be
<< 32) | x
;
2086 x
= cpu_to_be16(load_atomic2(haddr
));
2087 ret_be
= (ret_be
<< 16) | x
;
2091 x
= *(uint8_t *)haddr
;
2092 ret_be
= (ret_be
<< 8) | x
;
2096 g_assert_not_reached();
2100 } while (size
!= 0);
2106 * @p: translation parameters
2107 * @ret_be: accumulated data
2109 * As do_ld_bytes_beN, but with one atomic load.
2110 * Four aligned bytes are guaranteed to cover the load.
2112 static uint64_t do_ld_whole_be4(MMULookupPageData
*p
, uint64_t ret_be
)
2114 int o
= p
->addr
& 3;
2115 uint32_t x
= load_atomic4(p
->haddr
- o
);
2119 x
>>= (4 - p
->size
) * 8;
2120 return (ret_be
<< (p
->size
* 8)) | x
;
2125 * @p: translation parameters
2126 * @ret_be: accumulated data
2128 * As do_ld_bytes_beN, but with one atomic load.
2129 * Eight aligned bytes are guaranteed to cover the load.
2131 static uint64_t do_ld_whole_be8(CPUArchState
*env
, uintptr_t ra
,
2132 MMULookupPageData
*p
, uint64_t ret_be
)
2134 int o
= p
->addr
& 7;
2135 uint64_t x
= load_atomic8_or_exit(env
, ra
, p
->haddr
- o
);
2139 x
>>= (8 - p
->size
) * 8;
2140 return (ret_be
<< (p
->size
* 8)) | x
;
2145 * @p: translation parameters
2146 * @ret_be: accumulated data
2148 * As do_ld_bytes_beN, but with one atomic load.
2149 * 16 aligned bytes are guaranteed to cover the load.
2151 static Int128
do_ld_whole_be16(CPUArchState
*env
, uintptr_t ra
,
2152 MMULookupPageData
*p
, uint64_t ret_be
)
2154 int o
= p
->addr
& 15;
2155 Int128 x
, y
= load_atomic16_or_exit(env
, ra
, p
->haddr
- o
);
2158 if (!HOST_BIG_ENDIAN
) {
2161 y
= int128_lshift(y
, o
* 8);
2162 y
= int128_urshift(y
, (16 - size
) * 8);
2163 x
= int128_make64(ret_be
);
2164 x
= int128_lshift(x
, size
* 8);
2165 return int128_or(x
, y
);
2169 * Wrapper for the above.
2171 static uint64_t do_ld_beN(CPUArchState
*env
, MMULookupPageData
*p
,
2172 uint64_t ret_be
, int mmu_idx
, MMUAccessType type
,
2173 MemOp mop
, uintptr_t ra
)
2176 unsigned tmp
, half_size
;
2178 if (unlikely(p
->flags
& TLB_MMIO
)) {
2179 return do_ld_mmio_beN(env
, p
, ret_be
, mmu_idx
, type
, ra
);
2183 * It is a given that we cross a page and therefore there is no
2184 * atomicity for the load as a whole, but subobjects may need attention.
2186 atom
= mop
& MO_ATOM_MASK
;
2188 case MO_ATOM_SUBALIGN
:
2189 return do_ld_parts_beN(p
, ret_be
);
2191 case MO_ATOM_IFALIGN_PAIR
:
2192 case MO_ATOM_WITHIN16_PAIR
:
2193 tmp
= mop
& MO_SIZE
;
2194 tmp
= tmp
? tmp
- 1 : 0;
2195 half_size
= 1 << tmp
;
2196 if (atom
== MO_ATOM_IFALIGN_PAIR
2197 ? p
->size
== half_size
2198 : p
->size
>= half_size
) {
2199 if (!HAVE_al8_fast
&& p
->size
< 4) {
2200 return do_ld_whole_be4(p
, ret_be
);
2202 return do_ld_whole_be8(env
, ra
, p
, ret_be
);
2207 case MO_ATOM_IFALIGN
:
2208 case MO_ATOM_WITHIN16
:
2210 return do_ld_bytes_beN(p
, ret_be
);
2213 g_assert_not_reached();
2218 * Wrapper for the above, for 8 < size < 16.
2220 static Int128
do_ld16_beN(CPUArchState
*env
, MMULookupPageData
*p
,
2221 uint64_t a
, int mmu_idx
, MemOp mop
, uintptr_t ra
)
2227 if (unlikely(p
->flags
& TLB_MMIO
)) {
2229 a
= do_ld_mmio_beN(env
, p
, a
, mmu_idx
, MMU_DATA_LOAD
, ra
);
2232 b
= do_ld_mmio_beN(env
, p
, 0, mmu_idx
, MMU_DATA_LOAD
, ra
);
2233 return int128_make128(b
, a
);
2237 * It is a given that we cross a page and therefore there is no
2238 * atomicity for the load as a whole, but subobjects may need attention.
2240 atom
= mop
& MO_ATOM_MASK
;
2242 case MO_ATOM_SUBALIGN
:
2244 a
= do_ld_parts_beN(p
, a
);
2245 p
->haddr
+= size
- 8;
2247 b
= do_ld_parts_beN(p
, 0);
2250 case MO_ATOM_WITHIN16_PAIR
:
2251 /* Since size > 8, this is the half that must be atomic. */
2252 return do_ld_whole_be16(env
, ra
, p
, a
);
2254 case MO_ATOM_IFALIGN_PAIR
:
2256 * Since size > 8, both halves are misaligned,
2257 * and so neither is atomic.
2259 case MO_ATOM_IFALIGN
:
2260 case MO_ATOM_WITHIN16
:
2263 a
= do_ld_bytes_beN(p
, a
);
2264 b
= ldq_be_p(p
->haddr
+ size
- 8);
2268 g_assert_not_reached();
2271 return int128_make128(b
, a
);
2274 static uint8_t do_ld_1(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2275 MMUAccessType type
, uintptr_t ra
)
2277 if (unlikely(p
->flags
& TLB_MMIO
)) {
2278 return io_readx(env
, p
->full
, mmu_idx
, p
->addr
, ra
, type
, MO_UB
);
2280 return *(uint8_t *)p
->haddr
;
2284 static uint16_t do_ld_2(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2285 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2289 if (unlikely(p
->flags
& TLB_MMIO
)) {
2290 return io_readx(env
, p
->full
, mmu_idx
, p
->addr
, ra
, type
, memop
);
2293 /* Perform the load host endian, then swap if necessary. */
2294 ret
= load_atom_2(env
, ra
, p
->haddr
, memop
);
2295 if (memop
& MO_BSWAP
) {
2301 static uint32_t do_ld_4(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2302 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2306 if (unlikely(p
->flags
& TLB_MMIO
)) {
2307 return io_readx(env
, p
->full
, mmu_idx
, p
->addr
, ra
, type
, memop
);
2310 /* Perform the load host endian. */
2311 ret
= load_atom_4(env
, ra
, p
->haddr
, memop
);
2312 if (memop
& MO_BSWAP
) {
2318 static uint64_t do_ld_8(CPUArchState
*env
, MMULookupPageData
*p
, int mmu_idx
,
2319 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2323 if (unlikely(p
->flags
& TLB_MMIO
)) {
2324 return io_readx(env
, p
->full
, mmu_idx
, p
->addr
, ra
, type
, memop
);
2327 /* Perform the load host endian. */
2328 ret
= load_atom_8(env
, ra
, p
->haddr
, memop
);
2329 if (memop
& MO_BSWAP
) {
2335 static uint8_t do_ld1_mmu(CPUArchState
*env
, target_ulong addr
, MemOpIdx oi
,
2336 uintptr_t ra
, MMUAccessType access_type
)
2341 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2342 tcg_debug_assert(!crosspage
);
2344 return do_ld_1(env
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2347 tcg_target_ulong
helper_ldub_mmu(CPUArchState
*env
, uint64_t addr
,
2348 MemOpIdx oi
, uintptr_t retaddr
)
2350 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_8
);
2351 return do_ld1_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2354 static uint16_t do_ld2_mmu(CPUArchState
*env
, target_ulong addr
, MemOpIdx oi
,
2355 uintptr_t ra
, MMUAccessType access_type
)
2362 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2363 if (likely(!crosspage
)) {
2364 return do_ld_2(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2367 a
= do_ld_1(env
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2368 b
= do_ld_1(env
, &l
.page
[1], l
.mmu_idx
, access_type
, ra
);
2370 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2378 tcg_target_ulong
helper_lduw_mmu(CPUArchState
*env
, uint64_t addr
,
2379 MemOpIdx oi
, uintptr_t retaddr
)
2381 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2382 return do_ld2_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2385 static uint32_t do_ld4_mmu(CPUArchState
*env
, target_ulong addr
, MemOpIdx oi
,
2386 uintptr_t ra
, MMUAccessType access_type
)
2392 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2393 if (likely(!crosspage
)) {
2394 return do_ld_4(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2397 ret
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2398 ret
= do_ld_beN(env
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2399 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2405 tcg_target_ulong
helper_ldul_mmu(CPUArchState
*env
, uint64_t addr
,
2406 MemOpIdx oi
, uintptr_t retaddr
)
2408 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2409 return do_ld4_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2412 static uint64_t do_ld8_mmu(CPUArchState
*env
, target_ulong addr
, MemOpIdx oi
,
2413 uintptr_t ra
, MMUAccessType access_type
)
2419 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, access_type
, &l
);
2420 if (likely(!crosspage
)) {
2421 return do_ld_8(env
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2424 ret
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2425 ret
= do_ld_beN(env
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2426 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2432 uint64_t helper_ldq_mmu(CPUArchState
*env
, uint64_t addr
,
2433 MemOpIdx oi
, uintptr_t retaddr
)
2435 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
2436 return do_ld8_mmu(env
, addr
, oi
, retaddr
, MMU_DATA_LOAD
);
2440 * Provide signed versions of the load routines as well. We can of course
2441 * avoid this for 64-bit data, or for 32-bit data on 32-bit host.
2444 tcg_target_ulong
helper_ldsb_mmu(CPUArchState
*env
, uint64_t addr
,
2445 MemOpIdx oi
, uintptr_t retaddr
)
2447 return (int8_t)helper_ldub_mmu(env
, addr
, oi
, retaddr
);
2450 tcg_target_ulong
helper_ldsw_mmu(CPUArchState
*env
, uint64_t addr
,
2451 MemOpIdx oi
, uintptr_t retaddr
)
2453 return (int16_t)helper_lduw_mmu(env
, addr
, oi
, retaddr
);
2456 tcg_target_ulong
helper_ldsl_mmu(CPUArchState
*env
, uint64_t addr
,
2457 MemOpIdx oi
, uintptr_t retaddr
)
2459 return (int32_t)helper_ldul_mmu(env
, addr
, oi
, retaddr
);
2462 static Int128
do_ld16_mmu(CPUArchState
*env
, target_ulong addr
,
2463 MemOpIdx oi
, uintptr_t ra
)
2471 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_LOAD
, &l
);
2472 if (likely(!crosspage
)) {
2473 /* Perform the load host endian. */
2474 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
2475 QEMU_IOTHREAD_LOCK_GUARD();
2476 a
= io_readx(env
, l
.page
[0].full
, l
.mmu_idx
, addr
,
2477 ra
, MMU_DATA_LOAD
, MO_64
);
2478 b
= io_readx(env
, l
.page
[0].full
, l
.mmu_idx
, addr
+ 8,
2479 ra
, MMU_DATA_LOAD
, MO_64
);
2480 ret
= int128_make128(HOST_BIG_ENDIAN
? b
: a
,
2481 HOST_BIG_ENDIAN
? a
: b
);
2483 ret
= load_atom_16(env
, ra
, l
.page
[0].haddr
, l
.memop
);
2485 if (l
.memop
& MO_BSWAP
) {
2486 ret
= bswap128(ret
);
2491 first
= l
.page
[0].size
;
2493 MemOp mop8
= (l
.memop
& ~MO_SIZE
) | MO_64
;
2495 a
= do_ld_8(env
, &l
.page
[0], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2496 b
= do_ld_8(env
, &l
.page
[1], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2497 if ((mop8
& MO_BSWAP
) == MO_LE
) {
2498 ret
= int128_make128(a
, b
);
2500 ret
= int128_make128(b
, a
);
2506 a
= do_ld_beN(env
, &l
.page
[0], 0, l
.mmu_idx
,
2507 MMU_DATA_LOAD
, l
.memop
, ra
);
2508 ret
= do_ld16_beN(env
, &l
.page
[1], a
, l
.mmu_idx
, l
.memop
, ra
);
2510 ret
= do_ld16_beN(env
, &l
.page
[0], 0, l
.mmu_idx
, l
.memop
, ra
);
2511 b
= int128_getlo(ret
);
2512 ret
= int128_lshift(ret
, l
.page
[1].size
* 8);
2513 a
= int128_gethi(ret
);
2514 b
= do_ld_beN(env
, &l
.page
[1], b
, l
.mmu_idx
,
2515 MMU_DATA_LOAD
, l
.memop
, ra
);
2516 ret
= int128_make128(b
, a
);
2518 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2519 ret
= bswap128(ret
);
2524 Int128
helper_ld16_mmu(CPUArchState
*env
, uint64_t addr
,
2525 uint32_t oi
, uintptr_t retaddr
)
2527 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
2528 return do_ld16_mmu(env
, addr
, oi
, retaddr
);
2531 Int128
helper_ld_i128(CPUArchState
*env
, uint64_t addr
, uint32_t oi
)
2533 return helper_ld16_mmu(env
, addr
, oi
, GETPC());
2537 * Load helpers for cpu_ldst.h.
2540 static void plugin_load_cb(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
)
2542 qemu_plugin_vcpu_mem_cb(env_cpu(env
), addr
, oi
, QEMU_PLUGIN_MEM_R
);
2545 uint8_t cpu_ldb_mmu(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
, uintptr_t ra
)
2549 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_UB
);
2550 ret
= do_ld1_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2551 plugin_load_cb(env
, addr
, oi
);
2555 uint16_t cpu_ldw_mmu(CPUArchState
*env
, abi_ptr addr
,
2556 MemOpIdx oi
, uintptr_t ra
)
2560 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2561 ret
= do_ld2_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2562 plugin_load_cb(env
, addr
, oi
);
2566 uint32_t cpu_ldl_mmu(CPUArchState
*env
, abi_ptr addr
,
2567 MemOpIdx oi
, uintptr_t ra
)
2571 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2572 ret
= do_ld4_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2573 plugin_load_cb(env
, addr
, oi
);
2577 uint64_t cpu_ldq_mmu(CPUArchState
*env
, abi_ptr addr
,
2578 MemOpIdx oi
, uintptr_t ra
)
2582 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
2583 ret
= do_ld8_mmu(env
, addr
, oi
, ra
, MMU_DATA_LOAD
);
2584 plugin_load_cb(env
, addr
, oi
);
2588 Int128
cpu_ld16_mmu(CPUArchState
*env
, abi_ptr addr
,
2589 MemOpIdx oi
, uintptr_t ra
)
2593 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
2594 ret
= do_ld16_mmu(env
, addr
, oi
, ra
);
2595 plugin_load_cb(env
, addr
, oi
);
2606 * @p: translation parameters
2607 * @val_le: data to store
2608 * @mmu_idx: virtual address context
2609 * @ra: return address into tcg generated code, or 0
2611 * Store @p->size bytes at @p->addr, which is memory-mapped i/o.
2612 * The bytes to store are extracted in little-endian order from @val_le;
2613 * return the bytes of @val_le beyond @p->size that have not been stored.
2615 static uint64_t do_st_mmio_leN(CPUArchState
*env
, MMULookupPageData
*p
,
2616 uint64_t val_le
, int mmu_idx
, uintptr_t ra
)
2618 CPUTLBEntryFull
*full
= p
->full
;
2619 target_ulong addr
= p
->addr
;
2620 int i
, size
= p
->size
;
2622 QEMU_IOTHREAD_LOCK_GUARD();
2623 for (i
= 0; i
< size
; i
++, val_le
>>= 8) {
2624 io_writex(env
, full
, mmu_idx
, val_le
, addr
+ i
, ra
, MO_UB
);
2630 * Wrapper for the above.
2632 static uint64_t do_st_leN(CPUArchState
*env
, MMULookupPageData
*p
,
2633 uint64_t val_le
, int mmu_idx
,
2634 MemOp mop
, uintptr_t ra
)
2637 unsigned tmp
, half_size
;
2639 if (unlikely(p
->flags
& TLB_MMIO
)) {
2640 return do_st_mmio_leN(env
, p
, val_le
, mmu_idx
, ra
);
2641 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2642 return val_le
>> (p
->size
* 8);
2646 * It is a given that we cross a page and therefore there is no atomicity
2647 * for the store as a whole, but subobjects may need attention.
2649 atom
= mop
& MO_ATOM_MASK
;
2651 case MO_ATOM_SUBALIGN
:
2652 return store_parts_leN(p
->haddr
, p
->size
, val_le
);
2654 case MO_ATOM_IFALIGN_PAIR
:
2655 case MO_ATOM_WITHIN16_PAIR
:
2656 tmp
= mop
& MO_SIZE
;
2657 tmp
= tmp
? tmp
- 1 : 0;
2658 half_size
= 1 << tmp
;
2659 if (atom
== MO_ATOM_IFALIGN_PAIR
2660 ? p
->size
== half_size
2661 : p
->size
>= half_size
) {
2662 if (!HAVE_al8_fast
&& p
->size
<= 4) {
2663 return store_whole_le4(p
->haddr
, p
->size
, val_le
);
2664 } else if (HAVE_al8
) {
2665 return store_whole_le8(p
->haddr
, p
->size
, val_le
);
2667 cpu_loop_exit_atomic(env_cpu(env
), ra
);
2672 case MO_ATOM_IFALIGN
:
2673 case MO_ATOM_WITHIN16
:
2675 return store_bytes_leN(p
->haddr
, p
->size
, val_le
);
2678 g_assert_not_reached();
2683 * Wrapper for the above, for 8 < size < 16.
2685 static uint64_t do_st16_leN(CPUArchState
*env
, MMULookupPageData
*p
,
2686 Int128 val_le
, int mmu_idx
,
2687 MemOp mop
, uintptr_t ra
)
2692 if (unlikely(p
->flags
& TLB_MMIO
)) {
2694 do_st_mmio_leN(env
, p
, int128_getlo(val_le
), mmu_idx
, ra
);
2697 return do_st_mmio_leN(env
, p
, int128_gethi(val_le
), mmu_idx
, ra
);
2698 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2699 return int128_gethi(val_le
) >> ((size
- 8) * 8);
2703 * It is a given that we cross a page and therefore there is no atomicity
2704 * for the store as a whole, but subobjects may need attention.
2706 atom
= mop
& MO_ATOM_MASK
;
2708 case MO_ATOM_SUBALIGN
:
2709 store_parts_leN(p
->haddr
, 8, int128_getlo(val_le
));
2710 return store_parts_leN(p
->haddr
+ 8, p
->size
- 8,
2711 int128_gethi(val_le
));
2713 case MO_ATOM_WITHIN16_PAIR
:
2714 /* Since size > 8, this is the half that must be atomic. */
2716 cpu_loop_exit_atomic(env_cpu(env
), ra
);
2718 return store_whole_le16(p
->haddr
, p
->size
, val_le
);
2720 case MO_ATOM_IFALIGN_PAIR
:
2722 * Since size > 8, both halves are misaligned,
2723 * and so neither is atomic.
2725 case MO_ATOM_IFALIGN
:
2727 stq_le_p(p
->haddr
, int128_getlo(val_le
));
2728 return store_bytes_leN(p
->haddr
+ 8, p
->size
- 8,
2729 int128_gethi(val_le
));
2732 g_assert_not_reached();
2736 static void do_st_1(CPUArchState
*env
, MMULookupPageData
*p
, uint8_t val
,
2737 int mmu_idx
, uintptr_t ra
)
2739 if (unlikely(p
->flags
& TLB_MMIO
)) {
2740 io_writex(env
, p
->full
, mmu_idx
, val
, p
->addr
, ra
, MO_UB
);
2741 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2744 *(uint8_t *)p
->haddr
= val
;
2748 static void do_st_2(CPUArchState
*env
, MMULookupPageData
*p
, uint16_t val
,
2749 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2751 if (unlikely(p
->flags
& TLB_MMIO
)) {
2752 io_writex(env
, p
->full
, mmu_idx
, val
, p
->addr
, ra
, memop
);
2753 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2756 /* Swap to host endian if necessary, then store. */
2757 if (memop
& MO_BSWAP
) {
2760 store_atom_2(env
, ra
, p
->haddr
, memop
, val
);
2764 static void do_st_4(CPUArchState
*env
, MMULookupPageData
*p
, uint32_t val
,
2765 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2767 if (unlikely(p
->flags
& TLB_MMIO
)) {
2768 io_writex(env
, p
->full
, mmu_idx
, val
, p
->addr
, ra
, memop
);
2769 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2772 /* Swap to host endian if necessary, then store. */
2773 if (memop
& MO_BSWAP
) {
2776 store_atom_4(env
, ra
, p
->haddr
, memop
, val
);
2780 static void do_st_8(CPUArchState
*env
, MMULookupPageData
*p
, uint64_t val
,
2781 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2783 if (unlikely(p
->flags
& TLB_MMIO
)) {
2784 io_writex(env
, p
->full
, mmu_idx
, val
, p
->addr
, ra
, memop
);
2785 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2788 /* Swap to host endian if necessary, then store. */
2789 if (memop
& MO_BSWAP
) {
2792 store_atom_8(env
, ra
, p
->haddr
, memop
, val
);
2796 void helper_stb_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
2797 MemOpIdx oi
, uintptr_t ra
)
2802 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_8
);
2803 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2804 tcg_debug_assert(!crosspage
);
2806 do_st_1(env
, &l
.page
[0], val
, l
.mmu_idx
, ra
);
2809 static void do_st2_mmu(CPUArchState
*env
, target_ulong addr
, uint16_t val
,
2810 MemOpIdx oi
, uintptr_t ra
)
2816 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2817 if (likely(!crosspage
)) {
2818 do_st_2(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2822 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2823 a
= val
, b
= val
>> 8;
2825 b
= val
, a
= val
>> 8;
2827 do_st_1(env
, &l
.page
[0], a
, l
.mmu_idx
, ra
);
2828 do_st_1(env
, &l
.page
[1], b
, l
.mmu_idx
, ra
);
2831 void helper_stw_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
2832 MemOpIdx oi
, uintptr_t retaddr
)
2834 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2835 do_st2_mmu(env
, addr
, val
, oi
, retaddr
);
2838 static void do_st4_mmu(CPUArchState
*env
, target_ulong addr
, uint32_t val
,
2839 MemOpIdx oi
, uintptr_t ra
)
2844 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2845 if (likely(!crosspage
)) {
2846 do_st_4(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2850 /* Swap to little endian for simplicity, then store by bytes. */
2851 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2854 val
= do_st_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2855 (void) do_st_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2858 void helper_stl_mmu(CPUArchState
*env
, uint64_t addr
, uint32_t val
,
2859 MemOpIdx oi
, uintptr_t retaddr
)
2861 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2862 do_st4_mmu(env
, addr
, val
, oi
, retaddr
);
2865 static void do_st8_mmu(CPUArchState
*env
, target_ulong addr
, uint64_t val
,
2866 MemOpIdx oi
, uintptr_t ra
)
2871 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2872 if (likely(!crosspage
)) {
2873 do_st_8(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2877 /* Swap to little endian for simplicity, then store by bytes. */
2878 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2881 val
= do_st_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2882 (void) do_st_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2885 void helper_stq_mmu(CPUArchState
*env
, uint64_t addr
, uint64_t val
,
2886 MemOpIdx oi
, uintptr_t retaddr
)
2888 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
2889 do_st8_mmu(env
, addr
, val
, oi
, retaddr
);
2892 static void do_st16_mmu(CPUArchState
*env
, target_ulong addr
, Int128 val
,
2893 MemOpIdx oi
, uintptr_t ra
)
2900 crosspage
= mmu_lookup(env
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2901 if (likely(!crosspage
)) {
2902 /* Swap to host endian if necessary, then store. */
2903 if (l
.memop
& MO_BSWAP
) {
2904 val
= bswap128(val
);
2906 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
2907 QEMU_IOTHREAD_LOCK_GUARD();
2908 if (HOST_BIG_ENDIAN
) {
2909 b
= int128_getlo(val
), a
= int128_gethi(val
);
2911 a
= int128_getlo(val
), b
= int128_gethi(val
);
2913 io_writex(env
, l
.page
[0].full
, l
.mmu_idx
, a
, addr
, ra
, MO_64
);
2914 io_writex(env
, l
.page
[0].full
, l
.mmu_idx
, b
, addr
+ 8, ra
, MO_64
);
2915 } else if (unlikely(l
.page
[0].flags
& TLB_DISCARD_WRITE
)) {
2918 store_atom_16(env
, ra
, l
.page
[0].haddr
, l
.memop
, val
);
2923 first
= l
.page
[0].size
;
2925 MemOp mop8
= (l
.memop
& ~(MO_SIZE
| MO_BSWAP
)) | MO_64
;
2927 if (l
.memop
& MO_BSWAP
) {
2928 val
= bswap128(val
);
2930 if (HOST_BIG_ENDIAN
) {
2931 b
= int128_getlo(val
), a
= int128_gethi(val
);
2933 a
= int128_getlo(val
), b
= int128_gethi(val
);
2935 do_st_8(env
, &l
.page
[0], a
, l
.mmu_idx
, mop8
, ra
);
2936 do_st_8(env
, &l
.page
[1], b
, l
.mmu_idx
, mop8
, ra
);
2940 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2941 val
= bswap128(val
);
2944 do_st_leN(env
, &l
.page
[0], int128_getlo(val
), l
.mmu_idx
, l
.memop
, ra
);
2945 val
= int128_urshift(val
, first
* 8);
2946 do_st16_leN(env
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2948 b
= do_st16_leN(env
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2949 do_st_leN(env
, &l
.page
[1], b
, l
.mmu_idx
, l
.memop
, ra
);
2953 void helper_st16_mmu(CPUArchState
*env
, uint64_t addr
, Int128 val
,
2954 MemOpIdx oi
, uintptr_t retaddr
)
2956 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
2957 do_st16_mmu(env
, addr
, val
, oi
, retaddr
);
2960 void helper_st_i128(CPUArchState
*env
, uint64_t addr
, Int128 val
, MemOpIdx oi
)
2962 helper_st16_mmu(env
, addr
, val
, oi
, GETPC());
2966 * Store Helpers for cpu_ldst.h
2969 static void plugin_store_cb(CPUArchState
*env
, abi_ptr addr
, MemOpIdx oi
)
2971 qemu_plugin_vcpu_mem_cb(env_cpu(env
), addr
, oi
, QEMU_PLUGIN_MEM_W
);
2974 void cpu_stb_mmu(CPUArchState
*env
, target_ulong addr
, uint8_t val
,
2975 MemOpIdx oi
, uintptr_t retaddr
)
2977 helper_stb_mmu(env
, addr
, val
, oi
, retaddr
);
2978 plugin_store_cb(env
, addr
, oi
);
2981 void cpu_stw_mmu(CPUArchState
*env
, target_ulong addr
, uint16_t val
,
2982 MemOpIdx oi
, uintptr_t retaddr
)
2984 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_16
);
2985 do_st2_mmu(env
, addr
, val
, oi
, retaddr
);
2986 plugin_store_cb(env
, addr
, oi
);
2989 void cpu_stl_mmu(CPUArchState
*env
, target_ulong addr
, uint32_t val
,
2990 MemOpIdx oi
, uintptr_t retaddr
)
2992 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_32
);
2993 do_st4_mmu(env
, addr
, val
, oi
, retaddr
);
2994 plugin_store_cb(env
, addr
, oi
);
2997 void cpu_stq_mmu(CPUArchState
*env
, target_ulong addr
, uint64_t val
,
2998 MemOpIdx oi
, uintptr_t retaddr
)
3000 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_64
);
3001 do_st8_mmu(env
, addr
, val
, oi
, retaddr
);
3002 plugin_store_cb(env
, addr
, oi
);
3005 void cpu_st16_mmu(CPUArchState
*env
, target_ulong addr
, Int128 val
,
3006 MemOpIdx oi
, uintptr_t retaddr
)
3008 tcg_debug_assert((get_memop(oi
) & MO_SIZE
) == MO_128
);
3009 do_st16_mmu(env
, addr
, val
, oi
, retaddr
);
3010 plugin_store_cb(env
, addr
, oi
);
3013 #include "ldst_common.c.inc"
3016 * First set of functions passes in OI and RETADDR.
3017 * This makes them callable from other helpers.
3020 #define ATOMIC_NAME(X) \
3021 glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
3023 #define ATOMIC_MMU_CLEANUP
3025 #include "atomic_common.c.inc"
3028 #include "atomic_template.h"
3031 #include "atomic_template.h"
3034 #include "atomic_template.h"
3036 #ifdef CONFIG_ATOMIC64
3038 #include "atomic_template.h"
3041 #if HAVE_CMPXCHG128 || HAVE_ATOMIC128
3042 #define DATA_SIZE 16
3043 #include "atomic_template.h"
3046 /* Code access functions. */
3048 uint32_t cpu_ldub_code(CPUArchState
*env
, abi_ptr addr
)
3050 MemOpIdx oi
= make_memop_idx(MO_UB
, cpu_mmu_index(env
, true));
3051 return do_ld1_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3054 uint32_t cpu_lduw_code(CPUArchState
*env
, abi_ptr addr
)
3056 MemOpIdx oi
= make_memop_idx(MO_TEUW
, cpu_mmu_index(env
, true));
3057 return do_ld2_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3060 uint32_t cpu_ldl_code(CPUArchState
*env
, abi_ptr addr
)
3062 MemOpIdx oi
= make_memop_idx(MO_TEUL
, cpu_mmu_index(env
, true));
3063 return do_ld4_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3066 uint64_t cpu_ldq_code(CPUArchState
*env
, abi_ptr addr
)
3068 MemOpIdx oi
= make_memop_idx(MO_TEUQ
, cpu_mmu_index(env
, true));
3069 return do_ld8_mmu(env
, addr
, oi
, 0, MMU_INST_FETCH
);
3072 uint8_t cpu_ldb_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3073 MemOpIdx oi
, uintptr_t retaddr
)
3075 return do_ld1_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3078 uint16_t cpu_ldw_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3079 MemOpIdx oi
, uintptr_t retaddr
)
3081 return do_ld2_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3084 uint32_t cpu_ldl_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3085 MemOpIdx oi
, uintptr_t retaddr
)
3087 return do_ld4_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);
3090 uint64_t cpu_ldq_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3091 MemOpIdx oi
, uintptr_t retaddr
)
3093 return do_ld8_mmu(env
, addr
, oi
, retaddr
, MMU_INST_FETCH
);