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/tb-flush.h"
28 #include "exec/memory-internal.h"
29 #include "exec/ram_addr.h"
31 #include "qemu/error-report.h"
33 #include "exec/helper-proto-common.h"
34 #include "qemu/atomic.h"
35 #include "qemu/atomic128.h"
36 #include "exec/translate-all.h"
39 #include "internal-common.h"
40 #include "internal-target.h"
42 #include "qemu/plugin-memory.h"
44 #include "tcg/tcg-ldst.h"
45 #include "tcg/oversized-guest.h"
47 /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
48 /* #define DEBUG_TLB */
49 /* #define DEBUG_TLB_LOG */
52 # define DEBUG_TLB_GATE 1
54 # define DEBUG_TLB_LOG_GATE 1
56 # define DEBUG_TLB_LOG_GATE 0
59 # define DEBUG_TLB_GATE 0
60 # define DEBUG_TLB_LOG_GATE 0
63 #define tlb_debug(fmt, ...) do { \
64 if (DEBUG_TLB_LOG_GATE) { \
65 qemu_log_mask(CPU_LOG_MMU, "%s: " fmt, __func__, \
67 } else if (DEBUG_TLB_GATE) { \
68 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
72 #define assert_cpu_is_self(cpu) do { \
73 if (DEBUG_TLB_GATE) { \
74 g_assert(!(cpu)->created || qemu_cpu_is_self(cpu)); \
78 /* run_on_cpu_data.target_ptr should always be big enough for a
79 * vaddr even on 32 bit builds
81 QEMU_BUILD_BUG_ON(sizeof(vaddr
) > sizeof(run_on_cpu_data
));
83 /* We currently can't handle more than 16 bits in the MMUIDX bitmask.
85 QEMU_BUILD_BUG_ON(NB_MMU_MODES
> 16);
86 #define ALL_MMUIDX_BITS ((1 << NB_MMU_MODES) - 1)
88 static inline size_t tlb_n_entries(CPUTLBDescFast
*fast
)
90 return (fast
->mask
>> CPU_TLB_ENTRY_BITS
) + 1;
93 static inline size_t sizeof_tlb(CPUTLBDescFast
*fast
)
95 return fast
->mask
+ (1 << CPU_TLB_ENTRY_BITS
);
98 static void tlb_window_reset(CPUTLBDesc
*desc
, int64_t ns
,
101 desc
->window_begin_ns
= ns
;
102 desc
->window_max_entries
= max_entries
;
105 static void tb_jmp_cache_clear_page(CPUState
*cpu
, vaddr page_addr
)
107 CPUJumpCache
*jc
= cpu
->tb_jmp_cache
;
114 i0
= tb_jmp_cache_hash_page(page_addr
);
115 for (i
= 0; i
< TB_JMP_PAGE_SIZE
; i
++) {
116 qatomic_set(&jc
->array
[i0
+ i
].tb
, NULL
);
121 * tlb_mmu_resize_locked() - perform TLB resize bookkeeping; resize if necessary
122 * @desc: The CPUTLBDesc portion of the TLB
123 * @fast: The CPUTLBDescFast portion of the same TLB
125 * Called with tlb_lock_held.
127 * We have two main constraints when resizing a TLB: (1) we only resize it
128 * on a TLB flush (otherwise we'd have to take a perf hit by either rehashing
129 * the array or unnecessarily flushing it), which means we do not control how
130 * frequently the resizing can occur; (2) we don't have access to the guest's
131 * future scheduling decisions, and therefore have to decide the magnitude of
132 * the resize based on past observations.
134 * In general, a memory-hungry process can benefit greatly from an appropriately
135 * sized TLB, since a guest TLB miss is very expensive. This doesn't mean that
136 * we just have to make the TLB as large as possible; while an oversized TLB
137 * results in minimal TLB miss rates, it also takes longer to be flushed
138 * (flushes can be _very_ frequent), and the reduced locality can also hurt
141 * To achieve near-optimal performance for all kinds of workloads, we:
143 * 1. Aggressively increase the size of the TLB when the use rate of the
144 * TLB being flushed is high, since it is likely that in the near future this
145 * memory-hungry process will execute again, and its memory hungriness will
146 * probably be similar.
148 * 2. Slowly reduce the size of the TLB as the use rate declines over a
149 * reasonably large time window. The rationale is that if in such a time window
150 * we have not observed a high TLB use rate, it is likely that we won't observe
151 * it in the near future. In that case, once a time window expires we downsize
152 * the TLB to match the maximum use rate observed in the window.
154 * 3. Try to keep the maximum use rate in a time window in the 30-70% range,
155 * since in that range performance is likely near-optimal. Recall that the TLB
156 * is direct mapped, so we want the use rate to be low (or at least not too
157 * high), since otherwise we are likely to have a significant amount of
160 static void tlb_mmu_resize_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
,
163 size_t old_size
= tlb_n_entries(fast
);
165 size_t new_size
= old_size
;
166 int64_t window_len_ms
= 100;
167 int64_t window_len_ns
= window_len_ms
* 1000 * 1000;
168 bool window_expired
= now
> desc
->window_begin_ns
+ window_len_ns
;
170 if (desc
->n_used_entries
> desc
->window_max_entries
) {
171 desc
->window_max_entries
= desc
->n_used_entries
;
173 rate
= desc
->window_max_entries
* 100 / old_size
;
176 new_size
= MIN(old_size
<< 1, 1 << CPU_TLB_DYN_MAX_BITS
);
177 } else if (rate
< 30 && window_expired
) {
178 size_t ceil
= pow2ceil(desc
->window_max_entries
);
179 size_t expected_rate
= desc
->window_max_entries
* 100 / ceil
;
182 * Avoid undersizing when the max number of entries seen is just below
183 * a pow2. For instance, if max_entries == 1025, the expected use rate
184 * would be 1025/2048==50%. However, if max_entries == 1023, we'd get
185 * 1023/1024==99.9% use rate, so we'd likely end up doubling the size
186 * later. Thus, make sure that the expected use rate remains below 70%.
187 * (and since we double the size, that means the lowest rate we'd
188 * expect to get is 35%, which is still in the 30-70% range where
189 * we consider that the size is appropriate.)
191 if (expected_rate
> 70) {
194 new_size
= MAX(ceil
, 1 << CPU_TLB_DYN_MIN_BITS
);
197 if (new_size
== old_size
) {
198 if (window_expired
) {
199 tlb_window_reset(desc
, now
, desc
->n_used_entries
);
205 g_free(desc
->fulltlb
);
207 tlb_window_reset(desc
, now
, 0);
208 /* desc->n_used_entries is cleared by the caller */
209 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
210 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
211 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
214 * If the allocations fail, try smaller sizes. We just freed some
215 * memory, so going back to half of new_size has a good chance of working.
216 * Increased memory pressure elsewhere in the system might cause the
217 * allocations to fail though, so we progressively reduce the allocation
218 * size, aborting if we cannot even allocate the smallest TLB we support.
220 while (fast
->table
== NULL
|| desc
->fulltlb
== NULL
) {
221 if (new_size
== (1 << CPU_TLB_DYN_MIN_BITS
)) {
222 error_report("%s: %s", __func__
, strerror(errno
));
225 new_size
= MAX(new_size
>> 1, 1 << CPU_TLB_DYN_MIN_BITS
);
226 fast
->mask
= (new_size
- 1) << CPU_TLB_ENTRY_BITS
;
229 g_free(desc
->fulltlb
);
230 fast
->table
= g_try_new(CPUTLBEntry
, new_size
);
231 desc
->fulltlb
= g_try_new(CPUTLBEntryFull
, new_size
);
235 static void tlb_mmu_flush_locked(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
)
237 desc
->n_used_entries
= 0;
238 desc
->large_page_addr
= -1;
239 desc
->large_page_mask
= -1;
241 memset(fast
->table
, -1, sizeof_tlb(fast
));
242 memset(desc
->vtable
, -1, sizeof(desc
->vtable
));
245 static void tlb_flush_one_mmuidx_locked(CPUState
*cpu
, int mmu_idx
,
248 CPUTLBDesc
*desc
= &cpu
->neg
.tlb
.d
[mmu_idx
];
249 CPUTLBDescFast
*fast
= &cpu
->neg
.tlb
.f
[mmu_idx
];
251 tlb_mmu_resize_locked(desc
, fast
, now
);
252 tlb_mmu_flush_locked(desc
, fast
);
255 static void tlb_mmu_init(CPUTLBDesc
*desc
, CPUTLBDescFast
*fast
, int64_t now
)
257 size_t n_entries
= 1 << CPU_TLB_DYN_DEFAULT_BITS
;
259 tlb_window_reset(desc
, now
, 0);
260 desc
->n_used_entries
= 0;
261 fast
->mask
= (n_entries
- 1) << CPU_TLB_ENTRY_BITS
;
262 fast
->table
= g_new(CPUTLBEntry
, n_entries
);
263 desc
->fulltlb
= g_new(CPUTLBEntryFull
, n_entries
);
264 tlb_mmu_flush_locked(desc
, fast
);
267 static inline void tlb_n_used_entries_inc(CPUState
*cpu
, uintptr_t mmu_idx
)
269 cpu
->neg
.tlb
.d
[mmu_idx
].n_used_entries
++;
272 static inline void tlb_n_used_entries_dec(CPUState
*cpu
, uintptr_t mmu_idx
)
274 cpu
->neg
.tlb
.d
[mmu_idx
].n_used_entries
--;
277 void tlb_init(CPUState
*cpu
)
279 int64_t now
= get_clock_realtime();
282 qemu_spin_init(&cpu
->neg
.tlb
.c
.lock
);
284 /* All tlbs are initialized flushed. */
285 cpu
->neg
.tlb
.c
.dirty
= 0;
287 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
288 tlb_mmu_init(&cpu
->neg
.tlb
.d
[i
], &cpu
->neg
.tlb
.f
[i
], now
);
292 void tlb_destroy(CPUState
*cpu
)
296 qemu_spin_destroy(&cpu
->neg
.tlb
.c
.lock
);
297 for (i
= 0; i
< NB_MMU_MODES
; i
++) {
298 CPUTLBDesc
*desc
= &cpu
->neg
.tlb
.d
[i
];
299 CPUTLBDescFast
*fast
= &cpu
->neg
.tlb
.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 static void tlb_flush_by_mmuidx_async_work(CPUState
*cpu
, run_on_cpu_data data
)
327 uint16_t asked
= data
.host_int
;
328 uint16_t all_dirty
, work
, to_clean
;
329 int64_t now
= get_clock_realtime();
331 assert_cpu_is_self(cpu
);
333 tlb_debug("mmu_idx:0x%04" PRIx16
"\n", asked
);
335 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
337 all_dirty
= cpu
->neg
.tlb
.c
.dirty
;
338 to_clean
= asked
& all_dirty
;
339 all_dirty
&= ~to_clean
;
340 cpu
->neg
.tlb
.c
.dirty
= all_dirty
;
342 for (work
= to_clean
; work
!= 0; work
&= work
- 1) {
343 int mmu_idx
= ctz32(work
);
344 tlb_flush_one_mmuidx_locked(cpu
, mmu_idx
, now
);
347 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
349 tcg_flush_jmp_cache(cpu
);
351 if (to_clean
== ALL_MMUIDX_BITS
) {
352 qatomic_set(&cpu
->neg
.tlb
.c
.full_flush_count
,
353 cpu
->neg
.tlb
.c
.full_flush_count
+ 1);
355 qatomic_set(&cpu
->neg
.tlb
.c
.part_flush_count
,
356 cpu
->neg
.tlb
.c
.part_flush_count
+ ctpop16(to_clean
));
357 if (to_clean
!= asked
) {
358 qatomic_set(&cpu
->neg
.tlb
.c
.elide_flush_count
,
359 cpu
->neg
.tlb
.c
.elide_flush_count
+
360 ctpop16(asked
& ~to_clean
));
365 void tlb_flush_by_mmuidx(CPUState
*cpu
, uint16_t idxmap
)
367 tlb_debug("mmu_idx: 0x%" PRIx16
"\n", idxmap
);
369 if (cpu
->created
&& !qemu_cpu_is_self(cpu
)) {
370 async_run_on_cpu(cpu
, tlb_flush_by_mmuidx_async_work
,
371 RUN_ON_CPU_HOST_INT(idxmap
));
373 tlb_flush_by_mmuidx_async_work(cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
377 void tlb_flush(CPUState
*cpu
)
379 tlb_flush_by_mmuidx(cpu
, ALL_MMUIDX_BITS
);
382 void tlb_flush_by_mmuidx_all_cpus(CPUState
*src_cpu
, uint16_t idxmap
)
384 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
386 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
388 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
389 fn(src_cpu
, RUN_ON_CPU_HOST_INT(idxmap
));
392 void tlb_flush_all_cpus(CPUState
*src_cpu
)
394 tlb_flush_by_mmuidx_all_cpus(src_cpu
, ALL_MMUIDX_BITS
);
397 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
, uint16_t idxmap
)
399 const run_on_cpu_func fn
= tlb_flush_by_mmuidx_async_work
;
401 tlb_debug("mmu_idx: 0x%"PRIx16
"\n", idxmap
);
403 flush_all_helper(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
404 async_safe_run_on_cpu(src_cpu
, fn
, RUN_ON_CPU_HOST_INT(idxmap
));
407 void tlb_flush_all_cpus_synced(CPUState
*src_cpu
)
409 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, ALL_MMUIDX_BITS
);
412 static bool tlb_hit_page_mask_anyprot(CPUTLBEntry
*tlb_entry
,
413 vaddr page
, vaddr mask
)
416 mask
&= TARGET_PAGE_MASK
| TLB_INVALID_MASK
;
418 return (page
== (tlb_entry
->addr_read
& mask
) ||
419 page
== (tlb_addr_write(tlb_entry
) & mask
) ||
420 page
== (tlb_entry
->addr_code
& mask
));
423 static inline bool tlb_hit_page_anyprot(CPUTLBEntry
*tlb_entry
, vaddr page
)
425 return tlb_hit_page_mask_anyprot(tlb_entry
, page
, -1);
429 * tlb_entry_is_empty - return true if the entry is not in use
430 * @te: pointer to CPUTLBEntry
432 static inline bool tlb_entry_is_empty(const CPUTLBEntry
*te
)
434 return te
->addr_read
== -1 && te
->addr_write
== -1 && te
->addr_code
== -1;
437 /* Called with tlb_c.lock held */
438 static bool tlb_flush_entry_mask_locked(CPUTLBEntry
*tlb_entry
,
442 if (tlb_hit_page_mask_anyprot(tlb_entry
, page
, mask
)) {
443 memset(tlb_entry
, -1, sizeof(*tlb_entry
));
449 static inline bool tlb_flush_entry_locked(CPUTLBEntry
*tlb_entry
, vaddr page
)
451 return tlb_flush_entry_mask_locked(tlb_entry
, page
, -1);
454 /* Called with tlb_c.lock held */
455 static void tlb_flush_vtlb_page_mask_locked(CPUState
*cpu
, int mmu_idx
,
459 CPUTLBDesc
*d
= &cpu
->neg
.tlb
.d
[mmu_idx
];
462 assert_cpu_is_self(cpu
);
463 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
464 if (tlb_flush_entry_mask_locked(&d
->vtable
[k
], page
, mask
)) {
465 tlb_n_used_entries_dec(cpu
, mmu_idx
);
470 static inline void tlb_flush_vtlb_page_locked(CPUState
*cpu
, int mmu_idx
,
473 tlb_flush_vtlb_page_mask_locked(cpu
, mmu_idx
, page
, -1);
476 static void tlb_flush_page_locked(CPUState
*cpu
, int midx
, vaddr page
)
478 vaddr lp_addr
= cpu
->neg
.tlb
.d
[midx
].large_page_addr
;
479 vaddr lp_mask
= cpu
->neg
.tlb
.d
[midx
].large_page_mask
;
481 /* Check if we need to flush due to large pages. */
482 if ((page
& lp_mask
) == lp_addr
) {
483 tlb_debug("forcing full flush midx %d (%016"
484 VADDR_PRIx
"/%016" VADDR_PRIx
")\n",
485 midx
, lp_addr
, lp_mask
);
486 tlb_flush_one_mmuidx_locked(cpu
, midx
, get_clock_realtime());
488 if (tlb_flush_entry_locked(tlb_entry(cpu
, midx
, page
), page
)) {
489 tlb_n_used_entries_dec(cpu
, midx
);
491 tlb_flush_vtlb_page_locked(cpu
, midx
, page
);
496 * tlb_flush_page_by_mmuidx_async_0:
497 * @cpu: cpu on which to flush
498 * @addr: page of virtual address to flush
499 * @idxmap: set of mmu_idx to flush
501 * Helper for tlb_flush_page_by_mmuidx and friends, flush one page
502 * at @addr from the tlbs indicated by @idxmap from @cpu.
504 static void tlb_flush_page_by_mmuidx_async_0(CPUState
*cpu
,
510 assert_cpu_is_self(cpu
);
512 tlb_debug("page addr: %016" VADDR_PRIx
" mmu_map:0x%x\n", addr
, idxmap
);
514 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
515 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
516 if ((idxmap
>> mmu_idx
) & 1) {
517 tlb_flush_page_locked(cpu
, mmu_idx
, addr
);
520 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
523 * Discard jump cache entries for any tb which might potentially
524 * overlap the flushed page, which includes the previous.
526 tb_jmp_cache_clear_page(cpu
, addr
- TARGET_PAGE_SIZE
);
527 tb_jmp_cache_clear_page(cpu
, addr
);
531 * tlb_flush_page_by_mmuidx_async_1:
532 * @cpu: cpu on which to flush
533 * @data: encoded addr + idxmap
535 * Helper for tlb_flush_page_by_mmuidx and friends, called through
536 * async_run_on_cpu. The idxmap parameter is encoded in the page
537 * offset of the target_ptr field. This limits the set of mmu_idx
538 * that can be passed via this method.
540 static void tlb_flush_page_by_mmuidx_async_1(CPUState
*cpu
,
541 run_on_cpu_data data
)
543 vaddr addr_and_idxmap
= data
.target_ptr
;
544 vaddr addr
= addr_and_idxmap
& TARGET_PAGE_MASK
;
545 uint16_t idxmap
= addr_and_idxmap
& ~TARGET_PAGE_MASK
;
547 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
553 } TLBFlushPageByMMUIdxData
;
556 * tlb_flush_page_by_mmuidx_async_2:
557 * @cpu: cpu on which to flush
558 * @data: allocated addr + idxmap
560 * Helper for tlb_flush_page_by_mmuidx and friends, called through
561 * async_run_on_cpu. The addr+idxmap parameters are stored in a
562 * TLBFlushPageByMMUIdxData structure that has been allocated
563 * specifically for this helper. Free the structure when done.
565 static void tlb_flush_page_by_mmuidx_async_2(CPUState
*cpu
,
566 run_on_cpu_data data
)
568 TLBFlushPageByMMUIdxData
*d
= data
.host_ptr
;
570 tlb_flush_page_by_mmuidx_async_0(cpu
, d
->addr
, d
->idxmap
);
574 void tlb_flush_page_by_mmuidx(CPUState
*cpu
, vaddr addr
, uint16_t idxmap
)
576 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%" PRIx16
"\n", addr
, idxmap
);
578 /* This should already be page aligned */
579 addr
&= TARGET_PAGE_MASK
;
581 if (qemu_cpu_is_self(cpu
)) {
582 tlb_flush_page_by_mmuidx_async_0(cpu
, addr
, idxmap
);
583 } else if (idxmap
< TARGET_PAGE_SIZE
) {
585 * Most targets have only a few mmu_idx. In the case where
586 * we can stuff idxmap into the low TARGET_PAGE_BITS, avoid
587 * allocating memory for this operation.
589 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_1
,
590 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
592 TLBFlushPageByMMUIdxData
*d
= g_new(TLBFlushPageByMMUIdxData
, 1);
594 /* Otherwise allocate a structure, freed by the worker. */
597 async_run_on_cpu(cpu
, tlb_flush_page_by_mmuidx_async_2
,
598 RUN_ON_CPU_HOST_PTR(d
));
602 void tlb_flush_page(CPUState
*cpu
, vaddr addr
)
604 tlb_flush_page_by_mmuidx(cpu
, addr
, ALL_MMUIDX_BITS
);
607 void tlb_flush_page_by_mmuidx_all_cpus(CPUState
*src_cpu
, vaddr addr
,
610 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
612 /* This should already be page aligned */
613 addr
&= TARGET_PAGE_MASK
;
616 * Allocate memory to hold addr+idxmap only when needed.
617 * See tlb_flush_page_by_mmuidx for details.
619 if (idxmap
< TARGET_PAGE_SIZE
) {
620 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
621 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
625 /* Allocate a separate data block for each destination cpu. */
626 CPU_FOREACH(dst_cpu
) {
627 if (dst_cpu
!= src_cpu
) {
628 TLBFlushPageByMMUIdxData
*d
629 = g_new(TLBFlushPageByMMUIdxData
, 1);
633 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
634 RUN_ON_CPU_HOST_PTR(d
));
639 tlb_flush_page_by_mmuidx_async_0(src_cpu
, addr
, idxmap
);
642 void tlb_flush_page_all_cpus(CPUState
*src
, vaddr addr
)
644 tlb_flush_page_by_mmuidx_all_cpus(src
, addr
, ALL_MMUIDX_BITS
);
647 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
651 tlb_debug("addr: %016" VADDR_PRIx
" mmu_idx:%"PRIx16
"\n", addr
, idxmap
);
653 /* This should already be page aligned */
654 addr
&= TARGET_PAGE_MASK
;
657 * Allocate memory to hold addr+idxmap only when needed.
658 * See tlb_flush_page_by_mmuidx for details.
660 if (idxmap
< TARGET_PAGE_SIZE
) {
661 flush_all_helper(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
662 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
663 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_1
,
664 RUN_ON_CPU_TARGET_PTR(addr
| idxmap
));
667 TLBFlushPageByMMUIdxData
*d
;
669 /* Allocate a separate data block for each destination cpu. */
670 CPU_FOREACH(dst_cpu
) {
671 if (dst_cpu
!= src_cpu
) {
672 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
675 async_run_on_cpu(dst_cpu
, tlb_flush_page_by_mmuidx_async_2
,
676 RUN_ON_CPU_HOST_PTR(d
));
680 d
= g_new(TLBFlushPageByMMUIdxData
, 1);
683 async_safe_run_on_cpu(src_cpu
, tlb_flush_page_by_mmuidx_async_2
,
684 RUN_ON_CPU_HOST_PTR(d
));
688 void tlb_flush_page_all_cpus_synced(CPUState
*src
, vaddr addr
)
690 tlb_flush_page_by_mmuidx_all_cpus_synced(src
, addr
, ALL_MMUIDX_BITS
);
693 static void tlb_flush_range_locked(CPUState
*cpu
, int midx
,
694 vaddr addr
, vaddr len
,
697 CPUTLBDesc
*d
= &cpu
->neg
.tlb
.d
[midx
];
698 CPUTLBDescFast
*f
= &cpu
->neg
.tlb
.f
[midx
];
699 vaddr mask
= MAKE_64BIT_MASK(0, bits
);
702 * If @bits is smaller than the tlb size, there may be multiple entries
703 * within the TLB; otherwise all addresses that match under @mask hit
704 * the same TLB entry.
705 * TODO: Perhaps allow bits to be a few bits less than the size.
706 * For now, just flush the entire TLB.
708 * If @len is larger than the tlb size, then it will take longer to
709 * test all of the entries in the TLB than it will to flush it all.
711 if (mask
< f
->mask
|| len
> f
->mask
) {
712 tlb_debug("forcing full flush midx %d ("
713 "%016" VADDR_PRIx
"/%016" VADDR_PRIx
"+%016" VADDR_PRIx
")\n",
714 midx
, addr
, mask
, len
);
715 tlb_flush_one_mmuidx_locked(cpu
, midx
, get_clock_realtime());
720 * Check if we need to flush due to large pages.
721 * Because large_page_mask contains all 1's from the msb,
722 * we only need to test the end of the range.
724 if (((addr
+ len
- 1) & d
->large_page_mask
) == d
->large_page_addr
) {
725 tlb_debug("forcing full flush midx %d ("
726 "%016" VADDR_PRIx
"/%016" VADDR_PRIx
")\n",
727 midx
, d
->large_page_addr
, d
->large_page_mask
);
728 tlb_flush_one_mmuidx_locked(cpu
, midx
, get_clock_realtime());
732 for (vaddr i
= 0; i
< len
; i
+= TARGET_PAGE_SIZE
) {
733 vaddr page
= addr
+ i
;
734 CPUTLBEntry
*entry
= tlb_entry(cpu
, midx
, page
);
736 if (tlb_flush_entry_mask_locked(entry
, page
, mask
)) {
737 tlb_n_used_entries_dec(cpu
, midx
);
739 tlb_flush_vtlb_page_mask_locked(cpu
, midx
, page
, mask
);
750 static void tlb_flush_range_by_mmuidx_async_0(CPUState
*cpu
,
755 assert_cpu_is_self(cpu
);
757 tlb_debug("range: %016" VADDR_PRIx
"/%u+%016" VADDR_PRIx
" mmu_map:0x%x\n",
758 d
.addr
, d
.bits
, d
.len
, d
.idxmap
);
760 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
761 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
762 if ((d
.idxmap
>> mmu_idx
) & 1) {
763 tlb_flush_range_locked(cpu
, mmu_idx
, d
.addr
, d
.len
, d
.bits
);
766 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
769 * If the length is larger than the jump cache size, then it will take
770 * longer to clear each entry individually than it will to clear it all.
772 if (d
.len
>= (TARGET_PAGE_SIZE
* TB_JMP_CACHE_SIZE
)) {
773 tcg_flush_jmp_cache(cpu
);
778 * Discard jump cache entries for any tb which might potentially
779 * overlap the flushed pages, which includes the previous.
781 d
.addr
-= TARGET_PAGE_SIZE
;
782 for (vaddr i
= 0, n
= d
.len
/ TARGET_PAGE_SIZE
+ 1; i
< n
; i
++) {
783 tb_jmp_cache_clear_page(cpu
, d
.addr
);
784 d
.addr
+= TARGET_PAGE_SIZE
;
788 static void tlb_flush_range_by_mmuidx_async_1(CPUState
*cpu
,
789 run_on_cpu_data data
)
791 TLBFlushRangeData
*d
= data
.host_ptr
;
792 tlb_flush_range_by_mmuidx_async_0(cpu
, *d
);
796 void tlb_flush_range_by_mmuidx(CPUState
*cpu
, vaddr addr
,
797 vaddr len
, uint16_t idxmap
,
803 * If all bits are significant, and len is small,
804 * this devolves to tlb_flush_page.
806 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
807 tlb_flush_page_by_mmuidx(cpu
, addr
, idxmap
);
810 /* If no page bits are significant, this devolves to tlb_flush. */
811 if (bits
< TARGET_PAGE_BITS
) {
812 tlb_flush_by_mmuidx(cpu
, idxmap
);
816 /* This should already be page aligned */
817 d
.addr
= addr
& TARGET_PAGE_MASK
;
822 if (qemu_cpu_is_self(cpu
)) {
823 tlb_flush_range_by_mmuidx_async_0(cpu
, d
);
825 /* Otherwise allocate a structure, freed by the worker. */
826 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
827 async_run_on_cpu(cpu
, tlb_flush_range_by_mmuidx_async_1
,
828 RUN_ON_CPU_HOST_PTR(p
));
832 void tlb_flush_page_bits_by_mmuidx(CPUState
*cpu
, vaddr addr
,
833 uint16_t idxmap
, unsigned bits
)
835 tlb_flush_range_by_mmuidx(cpu
, addr
, TARGET_PAGE_SIZE
, idxmap
, bits
);
838 void tlb_flush_range_by_mmuidx_all_cpus(CPUState
*src_cpu
,
839 vaddr addr
, vaddr len
,
840 uint16_t idxmap
, unsigned bits
)
846 * If all bits are significant, and len is small,
847 * this devolves to tlb_flush_page.
849 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
850 tlb_flush_page_by_mmuidx_all_cpus(src_cpu
, addr
, idxmap
);
853 /* If no page bits are significant, this devolves to tlb_flush. */
854 if (bits
< TARGET_PAGE_BITS
) {
855 tlb_flush_by_mmuidx_all_cpus(src_cpu
, idxmap
);
859 /* This should already be page aligned */
860 d
.addr
= addr
& TARGET_PAGE_MASK
;
865 /* Allocate a separate data block for each destination cpu. */
866 CPU_FOREACH(dst_cpu
) {
867 if (dst_cpu
!= src_cpu
) {
868 TLBFlushRangeData
*p
= g_memdup(&d
, sizeof(d
));
869 async_run_on_cpu(dst_cpu
,
870 tlb_flush_range_by_mmuidx_async_1
,
871 RUN_ON_CPU_HOST_PTR(p
));
875 tlb_flush_range_by_mmuidx_async_0(src_cpu
, d
);
878 void tlb_flush_page_bits_by_mmuidx_all_cpus(CPUState
*src_cpu
,
879 vaddr addr
, uint16_t idxmap
,
882 tlb_flush_range_by_mmuidx_all_cpus(src_cpu
, addr
, TARGET_PAGE_SIZE
,
886 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
892 TLBFlushRangeData d
, *p
;
896 * If all bits are significant, and len is small,
897 * this devolves to tlb_flush_page.
899 if (bits
>= TARGET_LONG_BITS
&& len
<= TARGET_PAGE_SIZE
) {
900 tlb_flush_page_by_mmuidx_all_cpus_synced(src_cpu
, addr
, idxmap
);
903 /* If no page bits are significant, this devolves to tlb_flush. */
904 if (bits
< TARGET_PAGE_BITS
) {
905 tlb_flush_by_mmuidx_all_cpus_synced(src_cpu
, idxmap
);
909 /* This should already be page aligned */
910 d
.addr
= addr
& TARGET_PAGE_MASK
;
915 /* Allocate a separate data block for each destination cpu. */
916 CPU_FOREACH(dst_cpu
) {
917 if (dst_cpu
!= src_cpu
) {
918 p
= g_memdup(&d
, sizeof(d
));
919 async_run_on_cpu(dst_cpu
, tlb_flush_range_by_mmuidx_async_1
,
920 RUN_ON_CPU_HOST_PTR(p
));
924 p
= g_memdup(&d
, sizeof(d
));
925 async_safe_run_on_cpu(src_cpu
, tlb_flush_range_by_mmuidx_async_1
,
926 RUN_ON_CPU_HOST_PTR(p
));
929 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState
*src_cpu
,
934 tlb_flush_range_by_mmuidx_all_cpus_synced(src_cpu
, addr
, TARGET_PAGE_SIZE
,
938 /* update the TLBs so that writes to code in the virtual page 'addr'
940 void tlb_protect_code(ram_addr_t ram_addr
)
942 cpu_physical_memory_test_and_clear_dirty(ram_addr
& TARGET_PAGE_MASK
,
947 /* update the TLB so that writes in physical page 'phys_addr' are no longer
948 tested for self modifying code */
949 void tlb_unprotect_code(ram_addr_t ram_addr
)
951 cpu_physical_memory_set_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
);
956 * Dirty write flag handling
958 * When the TCG code writes to a location it looks up the address in
959 * the TLB and uses that data to compute the final address. If any of
960 * the lower bits of the address are set then the slow path is forced.
961 * There are a number of reasons to do this but for normal RAM the
962 * most usual is detecting writes to code regions which may invalidate
965 * Other vCPUs might be reading their TLBs during guest execution, so we update
966 * te->addr_write with qatomic_set. We don't need to worry about this for
967 * oversized guests as MTTCG is disabled for them.
969 * Called with tlb_c.lock held.
971 static void tlb_reset_dirty_range_locked(CPUTLBEntry
*tlb_entry
,
972 uintptr_t start
, uintptr_t length
)
974 uintptr_t addr
= tlb_entry
->addr_write
;
976 if ((addr
& (TLB_INVALID_MASK
| TLB_MMIO
|
977 TLB_DISCARD_WRITE
| TLB_NOTDIRTY
)) == 0) {
978 addr
&= TARGET_PAGE_MASK
;
979 addr
+= tlb_entry
->addend
;
980 if ((addr
- start
) < length
) {
981 #if TARGET_LONG_BITS == 32
982 uint32_t *ptr_write
= (uint32_t *)&tlb_entry
->addr_write
;
983 ptr_write
+= HOST_BIG_ENDIAN
;
984 qatomic_set(ptr_write
, *ptr_write
| TLB_NOTDIRTY
);
985 #elif TCG_OVERSIZED_GUEST
986 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
988 qatomic_set(&tlb_entry
->addr_write
,
989 tlb_entry
->addr_write
| TLB_NOTDIRTY
);
996 * Called with tlb_c.lock held.
997 * Called only from the vCPU context, i.e. the TLB's owner thread.
999 static inline void copy_tlb_helper_locked(CPUTLBEntry
*d
, const CPUTLBEntry
*s
)
1004 /* This is a cross vCPU call (i.e. another vCPU resetting the flags of
1006 * We must take tlb_c.lock to avoid racing with another vCPU update. The only
1007 * thing actually updated is the target TLB entry ->addr_write flags.
1009 void tlb_reset_dirty(CPUState
*cpu
, ram_addr_t start1
, ram_addr_t length
)
1013 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
1014 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1016 unsigned int n
= tlb_n_entries(&cpu
->neg
.tlb
.f
[mmu_idx
]);
1018 for (i
= 0; i
< n
; i
++) {
1019 tlb_reset_dirty_range_locked(&cpu
->neg
.tlb
.f
[mmu_idx
].table
[i
],
1023 for (i
= 0; i
< CPU_VTLB_SIZE
; i
++) {
1024 tlb_reset_dirty_range_locked(&cpu
->neg
.tlb
.d
[mmu_idx
].vtable
[i
],
1028 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
1031 /* Called with tlb_c.lock held */
1032 static inline void tlb_set_dirty1_locked(CPUTLBEntry
*tlb_entry
,
1035 if (tlb_entry
->addr_write
== (addr
| TLB_NOTDIRTY
)) {
1036 tlb_entry
->addr_write
= addr
;
1040 /* update the TLB corresponding to virtual page vaddr
1041 so that it is no longer dirty */
1042 void tlb_set_dirty(CPUState
*cpu
, vaddr addr
)
1046 assert_cpu_is_self(cpu
);
1048 addr
&= TARGET_PAGE_MASK
;
1049 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
1050 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1051 tlb_set_dirty1_locked(tlb_entry(cpu
, mmu_idx
, addr
), addr
);
1054 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1056 for (k
= 0; k
< CPU_VTLB_SIZE
; k
++) {
1057 tlb_set_dirty1_locked(&cpu
->neg
.tlb
.d
[mmu_idx
].vtable
[k
], addr
);
1060 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
1063 /* Our TLB does not support large pages, so remember the area covered by
1064 large pages and trigger a full TLB flush if these are invalidated. */
1065 static void tlb_add_large_page(CPUState
*cpu
, int mmu_idx
,
1066 vaddr addr
, uint64_t size
)
1068 vaddr lp_addr
= cpu
->neg
.tlb
.d
[mmu_idx
].large_page_addr
;
1069 vaddr lp_mask
= ~(size
- 1);
1071 if (lp_addr
== (vaddr
)-1) {
1072 /* No previous large page. */
1075 /* Extend the existing region to include the new page.
1076 This is a compromise between unnecessary flushes and
1077 the cost of maintaining a full variable size TLB. */
1078 lp_mask
&= cpu
->neg
.tlb
.d
[mmu_idx
].large_page_mask
;
1079 while (((lp_addr
^ addr
) & lp_mask
) != 0) {
1083 cpu
->neg
.tlb
.d
[mmu_idx
].large_page_addr
= lp_addr
& lp_mask
;
1084 cpu
->neg
.tlb
.d
[mmu_idx
].large_page_mask
= lp_mask
;
1087 static inline void tlb_set_compare(CPUTLBEntryFull
*full
, CPUTLBEntry
*ent
,
1088 vaddr address
, int flags
,
1089 MMUAccessType access_type
, bool enable
)
1092 address
|= flags
& TLB_FLAGS_MASK
;
1093 flags
&= TLB_SLOW_FLAGS_MASK
;
1095 address
|= TLB_FORCE_SLOW
;
1101 ent
->addr_idx
[access_type
] = address
;
1102 full
->slow_flags
[access_type
] = flags
;
1106 * Add a new TLB entry. At most one entry for a given virtual address
1107 * is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
1108 * supplied size is only used by tlb_flush_page.
1110 * Called from TCG-generated code, which is under an RCU read-side
1113 void tlb_set_page_full(CPUState
*cpu
, int mmu_idx
,
1114 vaddr addr
, CPUTLBEntryFull
*full
)
1116 CPUTLB
*tlb
= &cpu
->neg
.tlb
;
1117 CPUTLBDesc
*desc
= &tlb
->d
[mmu_idx
];
1118 MemoryRegionSection
*section
;
1119 unsigned int index
, read_flags
, write_flags
;
1121 CPUTLBEntry
*te
, tn
;
1122 hwaddr iotlb
, xlat
, sz
, paddr_page
;
1124 int asidx
, wp_flags
, prot
;
1125 bool is_ram
, is_romd
;
1127 assert_cpu_is_self(cpu
);
1129 if (full
->lg_page_size
<= TARGET_PAGE_BITS
) {
1130 sz
= TARGET_PAGE_SIZE
;
1132 sz
= (hwaddr
)1 << full
->lg_page_size
;
1133 tlb_add_large_page(cpu
, mmu_idx
, addr
, sz
);
1135 addr_page
= addr
& TARGET_PAGE_MASK
;
1136 paddr_page
= full
->phys_addr
& TARGET_PAGE_MASK
;
1139 asidx
= cpu_asidx_from_attrs(cpu
, full
->attrs
);
1140 section
= address_space_translate_for_iotlb(cpu
, asidx
, paddr_page
,
1141 &xlat
, &sz
, full
->attrs
, &prot
);
1142 assert(sz
>= TARGET_PAGE_SIZE
);
1144 tlb_debug("vaddr=%016" VADDR_PRIx
" paddr=0x" HWADDR_FMT_plx
1145 " prot=%x idx=%d\n",
1146 addr
, full
->phys_addr
, prot
, mmu_idx
);
1149 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1150 /* Repeat the MMU check and TLB fill on every access. */
1151 read_flags
|= TLB_INVALID_MASK
;
1153 if (full
->attrs
.byte_swap
) {
1154 read_flags
|= TLB_BSWAP
;
1157 is_ram
= memory_region_is_ram(section
->mr
);
1158 is_romd
= memory_region_is_romd(section
->mr
);
1160 if (is_ram
|| is_romd
) {
1161 /* RAM and ROMD both have associated host memory. */
1162 addend
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) + xlat
;
1164 /* I/O does not; force the host address to NULL. */
1168 write_flags
= read_flags
;
1170 iotlb
= memory_region_get_ram_addr(section
->mr
) + xlat
;
1171 assert(!(iotlb
& ~TARGET_PAGE_MASK
));
1173 * Computing is_clean is expensive; avoid all that unless
1174 * the page is actually writable.
1176 if (prot
& PAGE_WRITE
) {
1177 if (section
->readonly
) {
1178 write_flags
|= TLB_DISCARD_WRITE
;
1179 } else if (cpu_physical_memory_is_clean(iotlb
)) {
1180 write_flags
|= TLB_NOTDIRTY
;
1185 iotlb
= memory_region_section_get_iotlb(cpu
, section
) + xlat
;
1187 * Writes to romd devices must go through MMIO to enable write.
1188 * Reads to romd devices go through the ram_ptr found above,
1189 * but of course reads to I/O must go through MMIO.
1191 write_flags
|= TLB_MMIO
;
1193 read_flags
= write_flags
;
1197 wp_flags
= cpu_watchpoint_address_matches(cpu
, addr_page
,
1200 index
= tlb_index(cpu
, mmu_idx
, addr_page
);
1201 te
= tlb_entry(cpu
, mmu_idx
, addr_page
);
1204 * Hold the TLB lock for the rest of the function. We could acquire/release
1205 * the lock several times in the function, but it is faster to amortize the
1206 * acquisition cost by acquiring it just once. Note that this leads to
1207 * a longer critical section, but this is not a concern since the TLB lock
1208 * is unlikely to be contended.
1210 qemu_spin_lock(&tlb
->c
.lock
);
1212 /* Note that the tlb is no longer clean. */
1213 tlb
->c
.dirty
|= 1 << mmu_idx
;
1215 /* Make sure there's no cached translation for the new page. */
1216 tlb_flush_vtlb_page_locked(cpu
, mmu_idx
, addr_page
);
1219 * Only evict the old entry to the victim tlb if it's for a
1220 * different page; otherwise just overwrite the stale data.
1222 if (!tlb_hit_page_anyprot(te
, addr_page
) && !tlb_entry_is_empty(te
)) {
1223 unsigned vidx
= desc
->vindex
++ % CPU_VTLB_SIZE
;
1224 CPUTLBEntry
*tv
= &desc
->vtable
[vidx
];
1226 /* Evict the old entry into the victim tlb. */
1227 copy_tlb_helper_locked(tv
, te
);
1228 desc
->vfulltlb
[vidx
] = desc
->fulltlb
[index
];
1229 tlb_n_used_entries_dec(cpu
, mmu_idx
);
1232 /* refill the tlb */
1234 * When memory region is ram, iotlb contains a TARGET_PAGE_BITS
1235 * aligned ram_addr_t of the page base of the target RAM.
1236 * Otherwise, iotlb contains
1237 * - a physical section number in the lower TARGET_PAGE_BITS
1238 * - the offset within section->mr of the page base (I/O, ROMD) with the
1239 * TARGET_PAGE_BITS masked off.
1240 * We subtract addr_page (which is page aligned and thus won't
1241 * disturb the low bits) to give an offset which can be added to the
1242 * (non-page-aligned) vaddr of the eventual memory access to get
1243 * the MemoryRegion offset for the access. Note that the vaddr we
1244 * subtract here is that of the page base, and not the same as the
1245 * vaddr we add back in io_prepare()/get_page_addr_code().
1247 desc
->fulltlb
[index
] = *full
;
1248 full
= &desc
->fulltlb
[index
];
1249 full
->xlat_section
= iotlb
- addr_page
;
1250 full
->phys_addr
= paddr_page
;
1252 /* Now calculate the new entry */
1253 tn
.addend
= addend
- addr_page
;
1255 tlb_set_compare(full
, &tn
, addr_page
, read_flags
,
1256 MMU_INST_FETCH
, prot
& PAGE_EXEC
);
1258 if (wp_flags
& BP_MEM_READ
) {
1259 read_flags
|= TLB_WATCHPOINT
;
1261 tlb_set_compare(full
, &tn
, addr_page
, read_flags
,
1262 MMU_DATA_LOAD
, prot
& PAGE_READ
);
1264 if (prot
& PAGE_WRITE_INV
) {
1265 write_flags
|= TLB_INVALID_MASK
;
1267 if (wp_flags
& BP_MEM_WRITE
) {
1268 write_flags
|= TLB_WATCHPOINT
;
1270 tlb_set_compare(full
, &tn
, addr_page
, write_flags
,
1271 MMU_DATA_STORE
, prot
& PAGE_WRITE
);
1273 copy_tlb_helper_locked(te
, &tn
);
1274 tlb_n_used_entries_inc(cpu
, mmu_idx
);
1275 qemu_spin_unlock(&tlb
->c
.lock
);
1278 void tlb_set_page_with_attrs(CPUState
*cpu
, vaddr addr
,
1279 hwaddr paddr
, MemTxAttrs attrs
, int prot
,
1280 int mmu_idx
, uint64_t size
)
1282 CPUTLBEntryFull full
= {
1286 .lg_page_size
= ctz64(size
)
1289 assert(is_power_of_2(size
));
1290 tlb_set_page_full(cpu
, mmu_idx
, addr
, &full
);
1293 void tlb_set_page(CPUState
*cpu
, vaddr addr
,
1294 hwaddr paddr
, int prot
,
1295 int mmu_idx
, uint64_t size
)
1297 tlb_set_page_with_attrs(cpu
, addr
, paddr
, MEMTXATTRS_UNSPECIFIED
,
1298 prot
, mmu_idx
, size
);
1302 * Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
1303 * caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
1304 * be discarded and looked up again (e.g. via tlb_entry()).
1306 static void tlb_fill(CPUState
*cpu
, vaddr addr
, int size
,
1307 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1312 * This is not a probe, so only valid return is success; failure
1313 * should result in exception + longjmp to the cpu loop.
1315 ok
= cpu
->cc
->tcg_ops
->tlb_fill(cpu
, addr
, size
,
1316 access_type
, mmu_idx
, false, retaddr
);
1320 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
1321 MMUAccessType access_type
,
1322 int mmu_idx
, uintptr_t retaddr
)
1324 cpu
->cc
->tcg_ops
->do_unaligned_access(cpu
, addr
, access_type
,
1328 static MemoryRegionSection
*
1329 io_prepare(hwaddr
*out_offset
, CPUState
*cpu
, hwaddr xlat
,
1330 MemTxAttrs attrs
, vaddr addr
, uintptr_t retaddr
)
1332 MemoryRegionSection
*section
;
1335 section
= iotlb_to_section(cpu
, xlat
, attrs
);
1336 mr_offset
= (xlat
& TARGET_PAGE_MASK
) + addr
;
1337 cpu
->mem_io_pc
= retaddr
;
1338 if (!cpu
->neg
.can_do_io
) {
1339 cpu_io_recompile(cpu
, retaddr
);
1342 *out_offset
= mr_offset
;
1346 static void io_failed(CPUState
*cpu
, CPUTLBEntryFull
*full
, vaddr addr
,
1347 unsigned size
, MMUAccessType access_type
, int mmu_idx
,
1348 MemTxResult response
, uintptr_t retaddr
)
1350 if (!cpu
->ignore_memory_transaction_failures
1351 && cpu
->cc
->tcg_ops
->do_transaction_failed
) {
1352 hwaddr physaddr
= full
->phys_addr
| (addr
& ~TARGET_PAGE_MASK
);
1354 cpu
->cc
->tcg_ops
->do_transaction_failed(cpu
, physaddr
, addr
, size
,
1355 access_type
, mmu_idx
,
1356 full
->attrs
, response
, retaddr
);
1360 /* Return true if ADDR is present in the victim tlb, and has been copied
1361 back to the main tlb. */
1362 static bool victim_tlb_hit(CPUState
*cpu
, size_t mmu_idx
, size_t index
,
1363 MMUAccessType access_type
, vaddr page
)
1367 assert_cpu_is_self(cpu
);
1368 for (vidx
= 0; vidx
< CPU_VTLB_SIZE
; ++vidx
) {
1369 CPUTLBEntry
*vtlb
= &cpu
->neg
.tlb
.d
[mmu_idx
].vtable
[vidx
];
1370 uint64_t cmp
= tlb_read_idx(vtlb
, access_type
);
1373 /* Found entry in victim tlb, swap tlb and iotlb. */
1374 CPUTLBEntry tmptlb
, *tlb
= &cpu
->neg
.tlb
.f
[mmu_idx
].table
[index
];
1376 qemu_spin_lock(&cpu
->neg
.tlb
.c
.lock
);
1377 copy_tlb_helper_locked(&tmptlb
, tlb
);
1378 copy_tlb_helper_locked(tlb
, vtlb
);
1379 copy_tlb_helper_locked(vtlb
, &tmptlb
);
1380 qemu_spin_unlock(&cpu
->neg
.tlb
.c
.lock
);
1382 CPUTLBEntryFull
*f1
= &cpu
->neg
.tlb
.d
[mmu_idx
].fulltlb
[index
];
1383 CPUTLBEntryFull
*f2
= &cpu
->neg
.tlb
.d
[mmu_idx
].vfulltlb
[vidx
];
1384 CPUTLBEntryFull tmpf
;
1385 tmpf
= *f1
; *f1
= *f2
; *f2
= tmpf
;
1392 static void notdirty_write(CPUState
*cpu
, vaddr mem_vaddr
, unsigned size
,
1393 CPUTLBEntryFull
*full
, uintptr_t retaddr
)
1395 ram_addr_t ram_addr
= mem_vaddr
+ full
->xlat_section
;
1397 trace_memory_notdirty_write_access(mem_vaddr
, ram_addr
, size
);
1399 if (!cpu_physical_memory_get_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
)) {
1400 tb_invalidate_phys_range_fast(ram_addr
, size
, retaddr
);
1404 * Set both VGA and migration bits for simplicity and to remove
1405 * the notdirty callback faster.
1407 cpu_physical_memory_set_dirty_range(ram_addr
, size
, DIRTY_CLIENTS_NOCODE
);
1409 /* We remove the notdirty callback only if the code has been flushed. */
1410 if (!cpu_physical_memory_is_clean(ram_addr
)) {
1411 trace_memory_notdirty_set_dirty(mem_vaddr
);
1412 tlb_set_dirty(cpu
, mem_vaddr
);
1416 static int probe_access_internal(CPUState
*cpu
, vaddr addr
,
1417 int fault_size
, MMUAccessType access_type
,
1418 int mmu_idx
, bool nonfault
,
1419 void **phost
, CPUTLBEntryFull
**pfull
,
1420 uintptr_t retaddr
, bool check_mem_cbs
)
1422 uintptr_t index
= tlb_index(cpu
, mmu_idx
, addr
);
1423 CPUTLBEntry
*entry
= tlb_entry(cpu
, mmu_idx
, addr
);
1424 uint64_t tlb_addr
= tlb_read_idx(entry
, access_type
);
1425 vaddr page_addr
= addr
& TARGET_PAGE_MASK
;
1426 int flags
= TLB_FLAGS_MASK
& ~TLB_FORCE_SLOW
;
1427 bool force_mmio
= check_mem_cbs
&& cpu_plugin_mem_cbs_enabled(cpu
);
1428 CPUTLBEntryFull
*full
;
1430 if (!tlb_hit_page(tlb_addr
, page_addr
)) {
1431 if (!victim_tlb_hit(cpu
, mmu_idx
, index
, access_type
, page_addr
)) {
1432 if (!cpu
->cc
->tcg_ops
->tlb_fill(cpu
, addr
, fault_size
, access_type
,
1433 mmu_idx
, nonfault
, retaddr
)) {
1434 /* Non-faulting page table read failed. */
1437 return TLB_INVALID_MASK
;
1440 /* TLB resize via tlb_fill may have moved the entry. */
1441 index
= tlb_index(cpu
, mmu_idx
, addr
);
1442 entry
= tlb_entry(cpu
, mmu_idx
, addr
);
1445 * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately,
1446 * to force the next access through tlb_fill. We've just
1447 * called tlb_fill, so we know that this entry *is* valid.
1449 flags
&= ~TLB_INVALID_MASK
;
1451 tlb_addr
= tlb_read_idx(entry
, access_type
);
1455 *pfull
= full
= &cpu
->neg
.tlb
.d
[mmu_idx
].fulltlb
[index
];
1456 flags
|= full
->slow_flags
[access_type
];
1458 /* Fold all "mmio-like" bits into TLB_MMIO. This is not RAM. */
1459 if (unlikely(flags
& ~(TLB_WATCHPOINT
| TLB_NOTDIRTY
))
1461 (access_type
!= MMU_INST_FETCH
&& force_mmio
)) {
1466 /* Everything else is RAM. */
1467 *phost
= (void *)((uintptr_t)addr
+ entry
->addend
);
1471 int probe_access_full(CPUArchState
*env
, vaddr addr
, int size
,
1472 MMUAccessType access_type
, int mmu_idx
,
1473 bool nonfault
, void **phost
, CPUTLBEntryFull
**pfull
,
1476 int flags
= probe_access_internal(env_cpu(env
), addr
, size
, access_type
,
1477 mmu_idx
, nonfault
, phost
, pfull
, retaddr
,
1480 /* Handle clean RAM pages. */
1481 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1482 int dirtysize
= size
== 0 ? 1 : size
;
1483 notdirty_write(env_cpu(env
), addr
, dirtysize
, *pfull
, retaddr
);
1484 flags
&= ~TLB_NOTDIRTY
;
1490 int probe_access_full_mmu(CPUArchState
*env
, vaddr addr
, int size
,
1491 MMUAccessType access_type
, int mmu_idx
,
1492 void **phost
, CPUTLBEntryFull
**pfull
)
1494 void *discard_phost
;
1495 CPUTLBEntryFull
*discard_tlb
;
1497 /* privately handle users that don't need full results */
1498 phost
= phost
? phost
: &discard_phost
;
1499 pfull
= pfull
? pfull
: &discard_tlb
;
1501 int flags
= probe_access_internal(env_cpu(env
), addr
, size
, access_type
,
1502 mmu_idx
, true, phost
, pfull
, 0, false);
1504 /* Handle clean RAM pages. */
1505 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1506 int dirtysize
= size
== 0 ? 1 : size
;
1507 notdirty_write(env_cpu(env
), addr
, dirtysize
, *pfull
, 0);
1508 flags
&= ~TLB_NOTDIRTY
;
1514 int probe_access_flags(CPUArchState
*env
, vaddr addr
, int size
,
1515 MMUAccessType access_type
, int mmu_idx
,
1516 bool nonfault
, void **phost
, uintptr_t retaddr
)
1518 CPUTLBEntryFull
*full
;
1521 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1523 flags
= probe_access_internal(env_cpu(env
), addr
, size
, access_type
,
1524 mmu_idx
, nonfault
, phost
, &full
, retaddr
,
1527 /* Handle clean RAM pages. */
1528 if (unlikely(flags
& TLB_NOTDIRTY
)) {
1529 int dirtysize
= size
== 0 ? 1 : size
;
1530 notdirty_write(env_cpu(env
), addr
, dirtysize
, full
, retaddr
);
1531 flags
&= ~TLB_NOTDIRTY
;
1537 void *probe_access(CPUArchState
*env
, vaddr addr
, int size
,
1538 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
1540 CPUTLBEntryFull
*full
;
1544 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
1546 flags
= probe_access_internal(env_cpu(env
), addr
, size
, access_type
,
1547 mmu_idx
, false, &host
, &full
, retaddr
,
1550 /* Per the interface, size == 0 merely faults the access. */
1555 if (unlikely(flags
& (TLB_NOTDIRTY
| TLB_WATCHPOINT
))) {
1556 /* Handle watchpoints. */
1557 if (flags
& TLB_WATCHPOINT
) {
1558 int wp_access
= (access_type
== MMU_DATA_STORE
1559 ? BP_MEM_WRITE
: BP_MEM_READ
);
1560 cpu_check_watchpoint(env_cpu(env
), addr
, size
,
1561 full
->attrs
, wp_access
, retaddr
);
1564 /* Handle clean RAM pages. */
1565 if (flags
& TLB_NOTDIRTY
) {
1566 notdirty_write(env_cpu(env
), addr
, size
, full
, retaddr
);
1573 void *tlb_vaddr_to_host(CPUArchState
*env
, abi_ptr addr
,
1574 MMUAccessType access_type
, int mmu_idx
)
1576 CPUTLBEntryFull
*full
;
1580 flags
= probe_access_internal(env_cpu(env
), addr
, 0, access_type
,
1581 mmu_idx
, true, &host
, &full
, 0, false);
1583 /* No combination of flags are expected by the caller. */
1584 return flags
? NULL
: host
;
1588 * Return a ram_addr_t for the virtual address for execution.
1590 * Return -1 if we can't translate and execute from an entire page
1591 * of RAM. This will force us to execute by loading and translating
1592 * one insn at a time, without caching.
1594 * NOTE: This function will trigger an exception if the page is
1597 tb_page_addr_t
get_page_addr_code_hostp(CPUArchState
*env
, vaddr addr
,
1600 CPUTLBEntryFull
*full
;
1603 (void)probe_access_internal(env_cpu(env
), addr
, 1, MMU_INST_FETCH
,
1604 cpu_mmu_index(env_cpu(env
), true), false,
1605 &p
, &full
, 0, false);
1610 if (full
->lg_page_size
< TARGET_PAGE_BITS
) {
1617 return qemu_ram_addr_from_host_nofail(p
);
1620 /* Load/store with atomicity primitives. */
1621 #include "ldst_atomicity.c.inc"
1623 #ifdef CONFIG_PLUGIN
1625 * Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
1626 * This should be a hot path as we will have just looked this path up
1627 * in the softmmu lookup code (or helper). We don't handle re-fills or
1628 * checking the victim table. This is purely informational.
1630 * The one corner case is i/o write, which can cause changes to the
1631 * address space. Those changes, and the corresponding tlb flush,
1632 * should be delayed until the next TB, so even then this ought not fail.
1633 * But check, Just in Case.
1635 bool tlb_plugin_lookup(CPUState
*cpu
, vaddr addr
, int mmu_idx
,
1636 bool is_store
, struct qemu_plugin_hwaddr
*data
)
1638 CPUTLBEntry
*tlbe
= tlb_entry(cpu
, mmu_idx
, addr
);
1639 uintptr_t index
= tlb_index(cpu
, mmu_idx
, addr
);
1640 MMUAccessType access_type
= is_store
? MMU_DATA_STORE
: MMU_DATA_LOAD
;
1641 uint64_t tlb_addr
= tlb_read_idx(tlbe
, access_type
);
1642 CPUTLBEntryFull
*full
;
1644 if (unlikely(!tlb_hit(tlb_addr
, addr
))) {
1648 full
= &cpu
->neg
.tlb
.d
[mmu_idx
].fulltlb
[index
];
1649 data
->phys_addr
= full
->phys_addr
| (addr
& ~TARGET_PAGE_MASK
);
1651 /* We must have an iotlb entry for MMIO */
1652 if (tlb_addr
& TLB_MMIO
) {
1653 MemoryRegionSection
*section
=
1654 iotlb_to_section(cpu
, full
->xlat_section
& ~TARGET_PAGE_MASK
,
1657 data
->mr
= section
->mr
;
1659 data
->is_io
= false;
1667 * Probe for a load/store operation.
1668 * Return the host address and into @flags.
1671 typedef struct MMULookupPageData
{
1672 CPUTLBEntryFull
*full
;
1677 } MMULookupPageData
;
1679 typedef struct MMULookupLocals
{
1680 MMULookupPageData page
[2];
1686 * mmu_lookup1: translate one page
1687 * @cpu: generic cpu state
1688 * @data: lookup parameters
1689 * @mmu_idx: virtual address context
1690 * @access_type: load/store/code
1691 * @ra: return address into tcg generated code, or 0
1693 * Resolve the translation for the one page at @data.addr, filling in
1694 * the rest of @data with the results. If the translation fails,
1695 * tlb_fill will longjmp out. Return true if the softmmu tlb for
1696 * @mmu_idx may have resized.
1698 static bool mmu_lookup1(CPUState
*cpu
, MMULookupPageData
*data
,
1699 int mmu_idx
, MMUAccessType access_type
, uintptr_t ra
)
1701 vaddr addr
= data
->addr
;
1702 uintptr_t index
= tlb_index(cpu
, mmu_idx
, addr
);
1703 CPUTLBEntry
*entry
= tlb_entry(cpu
, mmu_idx
, addr
);
1704 uint64_t tlb_addr
= tlb_read_idx(entry
, access_type
);
1705 bool maybe_resized
= false;
1706 CPUTLBEntryFull
*full
;
1709 /* If the TLB entry is for a different page, reload and try again. */
1710 if (!tlb_hit(tlb_addr
, addr
)) {
1711 if (!victim_tlb_hit(cpu
, mmu_idx
, index
, access_type
,
1712 addr
& TARGET_PAGE_MASK
)) {
1713 tlb_fill(cpu
, addr
, data
->size
, access_type
, mmu_idx
, ra
);
1714 maybe_resized
= true;
1715 index
= tlb_index(cpu
, mmu_idx
, addr
);
1716 entry
= tlb_entry(cpu
, mmu_idx
, addr
);
1718 tlb_addr
= tlb_read_idx(entry
, access_type
) & ~TLB_INVALID_MASK
;
1721 full
= &cpu
->neg
.tlb
.d
[mmu_idx
].fulltlb
[index
];
1722 flags
= tlb_addr
& (TLB_FLAGS_MASK
& ~TLB_FORCE_SLOW
);
1723 flags
|= full
->slow_flags
[access_type
];
1726 data
->flags
= flags
;
1727 /* Compute haddr speculatively; depending on flags it might be invalid. */
1728 data
->haddr
= (void *)((uintptr_t)addr
+ entry
->addend
);
1730 return maybe_resized
;
1734 * mmu_watch_or_dirty
1735 * @cpu: generic cpu state
1736 * @data: lookup parameters
1737 * @access_type: load/store/code
1738 * @ra: return address into tcg generated code, or 0
1740 * Trigger watchpoints for @data.addr:@data.size;
1741 * record writes to protected clean pages.
1743 static void mmu_watch_or_dirty(CPUState
*cpu
, MMULookupPageData
*data
,
1744 MMUAccessType access_type
, uintptr_t ra
)
1746 CPUTLBEntryFull
*full
= data
->full
;
1747 vaddr addr
= data
->addr
;
1748 int flags
= data
->flags
;
1749 int size
= data
->size
;
1751 /* On watchpoint hit, this will longjmp out. */
1752 if (flags
& TLB_WATCHPOINT
) {
1753 int wp
= access_type
== MMU_DATA_STORE
? BP_MEM_WRITE
: BP_MEM_READ
;
1754 cpu_check_watchpoint(cpu
, addr
, size
, full
->attrs
, wp
, ra
);
1755 flags
&= ~TLB_WATCHPOINT
;
1758 /* Note that notdirty is only set for writes. */
1759 if (flags
& TLB_NOTDIRTY
) {
1760 notdirty_write(cpu
, addr
, size
, full
, ra
);
1761 flags
&= ~TLB_NOTDIRTY
;
1763 data
->flags
= flags
;
1767 * mmu_lookup: translate page(s)
1768 * @cpu: generic cpu state
1769 * @addr: virtual address
1770 * @oi: combined mmu_idx and MemOp
1771 * @ra: return address into tcg generated code, or 0
1772 * @access_type: load/store/code
1775 * Resolve the translation for the page(s) beginning at @addr, for MemOp.size
1776 * bytes. Return true if the lookup crosses a page boundary.
1778 static bool mmu_lookup(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
1779 uintptr_t ra
, MMUAccessType type
, MMULookupLocals
*l
)
1785 l
->memop
= get_memop(oi
);
1786 l
->mmu_idx
= get_mmuidx(oi
);
1788 tcg_debug_assert(l
->mmu_idx
< NB_MMU_MODES
);
1790 /* Handle CPU specific unaligned behaviour */
1791 a_bits
= get_alignment_bits(l
->memop
);
1792 if (addr
& ((1 << a_bits
) - 1)) {
1793 cpu_unaligned_access(cpu
, addr
, type
, l
->mmu_idx
, ra
);
1796 l
->page
[0].addr
= addr
;
1797 l
->page
[0].size
= memop_size(l
->memop
);
1798 l
->page
[1].addr
= (addr
+ l
->page
[0].size
- 1) & TARGET_PAGE_MASK
;
1799 l
->page
[1].size
= 0;
1800 crosspage
= (addr
^ l
->page
[1].addr
) & TARGET_PAGE_MASK
;
1802 if (likely(!crosspage
)) {
1803 mmu_lookup1(cpu
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1805 flags
= l
->page
[0].flags
;
1806 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1807 mmu_watch_or_dirty(cpu
, &l
->page
[0], type
, ra
);
1809 if (unlikely(flags
& TLB_BSWAP
)) {
1810 l
->memop
^= MO_BSWAP
;
1813 /* Finish compute of page crossing. */
1814 int size0
= l
->page
[1].addr
- addr
;
1815 l
->page
[1].size
= l
->page
[0].size
- size0
;
1816 l
->page
[0].size
= size0
;
1819 * Lookup both pages, recognizing exceptions from either. If the
1820 * second lookup potentially resized, refresh first CPUTLBEntryFull.
1822 mmu_lookup1(cpu
, &l
->page
[0], l
->mmu_idx
, type
, ra
);
1823 if (mmu_lookup1(cpu
, &l
->page
[1], l
->mmu_idx
, type
, ra
)) {
1824 uintptr_t index
= tlb_index(cpu
, l
->mmu_idx
, addr
);
1825 l
->page
[0].full
= &cpu
->neg
.tlb
.d
[l
->mmu_idx
].fulltlb
[index
];
1828 flags
= l
->page
[0].flags
| l
->page
[1].flags
;
1829 if (unlikely(flags
& (TLB_WATCHPOINT
| TLB_NOTDIRTY
))) {
1830 mmu_watch_or_dirty(cpu
, &l
->page
[0], type
, ra
);
1831 mmu_watch_or_dirty(cpu
, &l
->page
[1], type
, ra
);
1835 * Since target/sparc is the only user of TLB_BSWAP, and all
1836 * Sparc accesses are aligned, any treatment across two pages
1837 * would be arbitrary. Refuse it until there's a use.
1839 tcg_debug_assert((flags
& TLB_BSWAP
) == 0);
1846 * Probe for an atomic operation. Do not allow unaligned operations,
1847 * or io operations to proceed. Return the host address.
1849 static void *atomic_mmu_lookup(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
1850 int size
, uintptr_t retaddr
)
1852 uintptr_t mmu_idx
= get_mmuidx(oi
);
1853 MemOp mop
= get_memop(oi
);
1854 int a_bits
= get_alignment_bits(mop
);
1859 CPUTLBEntryFull
*full
;
1861 tcg_debug_assert(mmu_idx
< NB_MMU_MODES
);
1863 /* Adjust the given return address. */
1864 retaddr
-= GETPC_ADJ
;
1866 /* Enforce guest required alignment. */
1867 if (unlikely(a_bits
> 0 && (addr
& ((1 << a_bits
) - 1)))) {
1868 /* ??? Maybe indicate atomic op to cpu_unaligned_access */
1869 cpu_unaligned_access(cpu
, addr
, MMU_DATA_STORE
,
1873 /* Enforce qemu required alignment. */
1874 if (unlikely(addr
& (size
- 1))) {
1875 /* We get here if guest alignment was not requested,
1876 or was not enforced by cpu_unaligned_access above.
1877 We might widen the access and emulate, but for now
1878 mark an exception and exit the cpu loop. */
1879 goto stop_the_world
;
1882 index
= tlb_index(cpu
, mmu_idx
, addr
);
1883 tlbe
= tlb_entry(cpu
, mmu_idx
, addr
);
1885 /* Check TLB entry and enforce page permissions. */
1886 tlb_addr
= tlb_addr_write(tlbe
);
1887 if (!tlb_hit(tlb_addr
, addr
)) {
1888 if (!victim_tlb_hit(cpu
, mmu_idx
, index
, MMU_DATA_STORE
,
1889 addr
& TARGET_PAGE_MASK
)) {
1890 tlb_fill(cpu
, addr
, size
,
1891 MMU_DATA_STORE
, mmu_idx
, retaddr
);
1892 index
= tlb_index(cpu
, mmu_idx
, addr
);
1893 tlbe
= tlb_entry(cpu
, mmu_idx
, addr
);
1895 tlb_addr
= tlb_addr_write(tlbe
) & ~TLB_INVALID_MASK
;
1899 * Let the guest notice RMW on a write-only page.
1900 * We have just verified that the page is writable.
1901 * Subpage lookups may have left TLB_INVALID_MASK set,
1902 * but addr_read will only be -1 if PAGE_READ was unset.
1904 if (unlikely(tlbe
->addr_read
== -1)) {
1905 tlb_fill(cpu
, addr
, size
, MMU_DATA_LOAD
, mmu_idx
, retaddr
);
1907 * Since we don't support reads and writes to different
1908 * addresses, and we do have the proper page loaded for
1909 * write, this shouldn't ever return. But just in case,
1910 * handle via stop-the-world.
1912 goto stop_the_world
;
1914 /* Collect tlb flags for read. */
1915 tlb_addr
|= tlbe
->addr_read
;
1917 /* Notice an IO access or a needs-MMU-lookup access */
1918 if (unlikely(tlb_addr
& (TLB_MMIO
| TLB_DISCARD_WRITE
))) {
1919 /* There's really nothing that can be done to
1920 support this apart from stop-the-world. */
1921 goto stop_the_world
;
1924 hostaddr
= (void *)((uintptr_t)addr
+ tlbe
->addend
);
1925 full
= &cpu
->neg
.tlb
.d
[mmu_idx
].fulltlb
[index
];
1927 if (unlikely(tlb_addr
& TLB_NOTDIRTY
)) {
1928 notdirty_write(cpu
, addr
, size
, full
, retaddr
);
1931 if (unlikely(tlb_addr
& TLB_FORCE_SLOW
)) {
1934 if (full
->slow_flags
[MMU_DATA_STORE
] & TLB_WATCHPOINT
) {
1935 wp_flags
|= BP_MEM_WRITE
;
1937 if (full
->slow_flags
[MMU_DATA_LOAD
] & TLB_WATCHPOINT
) {
1938 wp_flags
|= BP_MEM_READ
;
1941 cpu_check_watchpoint(cpu
, addr
, size
,
1942 full
->attrs
, wp_flags
, retaddr
);
1949 cpu_loop_exit_atomic(cpu
, retaddr
);
1955 * We support two different access types. SOFTMMU_CODE_ACCESS is
1956 * specifically for reading instructions from system memory. It is
1957 * called by the translation loop and in some helpers where the code
1958 * is disassembled. It shouldn't be called directly by guest code.
1960 * For the benefit of TCG generated code, we want to avoid the
1961 * complication of ABI-specific return type promotion and always
1962 * return a value extended to the register size of the host. This is
1963 * tcg_target_long, except in the case of a 32-bit host and 64-bit
1964 * data, and for that we always have uint64_t.
1966 * We don't bother with this widened value for SOFTMMU_CODE_ACCESS.
1971 * @cpu: generic cpu state
1972 * @full: page parameters
1973 * @ret_be: accumulated data
1974 * @addr: virtual address
1975 * @size: number of bytes
1976 * @mmu_idx: virtual address context
1977 * @ra: return address into tcg generated code, or 0
1980 * Load @size bytes from @addr, which is memory-mapped i/o.
1981 * The bytes are concatenated in big-endian order with @ret_be.
1983 static uint64_t int_ld_mmio_beN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
1984 uint64_t ret_be
, vaddr addr
, int size
,
1985 int mmu_idx
, MMUAccessType type
, uintptr_t ra
,
1986 MemoryRegion
*mr
, hwaddr mr_offset
)
1994 /* Read aligned pieces up to 8 bytes. */
1995 this_mop
= ctz32(size
| (int)addr
| 8);
1996 this_size
= 1 << this_mop
;
1999 r
= memory_region_dispatch_read(mr
, mr_offset
, &val
,
2000 this_mop
, full
->attrs
);
2001 if (unlikely(r
!= MEMTX_OK
)) {
2002 io_failed(cpu
, full
, addr
, this_size
, type
, mmu_idx
, r
, ra
);
2004 if (this_size
== 8) {
2008 ret_be
= (ret_be
<< (this_size
* 8)) | val
;
2010 mr_offset
+= this_size
;
2017 static uint64_t do_ld_mmio_beN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
2018 uint64_t ret_be
, vaddr addr
, int size
,
2019 int mmu_idx
, MMUAccessType type
, uintptr_t ra
)
2021 MemoryRegionSection
*section
;
2027 tcg_debug_assert(size
> 0 && size
<= 8);
2029 attrs
= full
->attrs
;
2030 section
= io_prepare(&mr_offset
, cpu
, full
->xlat_section
, attrs
, addr
, ra
);
2034 ret
= int_ld_mmio_beN(cpu
, full
, ret_be
, addr
, size
, mmu_idx
,
2035 type
, ra
, mr
, mr_offset
);
2041 static Int128
do_ld16_mmio_beN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
2042 uint64_t ret_be
, vaddr addr
, int size
,
2043 int mmu_idx
, uintptr_t ra
)
2045 MemoryRegionSection
*section
;
2051 tcg_debug_assert(size
> 8 && size
<= 16);
2053 attrs
= full
->attrs
;
2054 section
= io_prepare(&mr_offset
, cpu
, full
->xlat_section
, attrs
, addr
, ra
);
2058 a
= int_ld_mmio_beN(cpu
, full
, ret_be
, addr
, size
- 8, mmu_idx
,
2059 MMU_DATA_LOAD
, ra
, mr
, mr_offset
);
2060 b
= int_ld_mmio_beN(cpu
, full
, ret_be
, addr
+ size
- 8, 8, mmu_idx
,
2061 MMU_DATA_LOAD
, ra
, mr
, mr_offset
+ size
- 8);
2064 return int128_make128(b
, a
);
2069 * @p: translation parameters
2070 * @ret_be: accumulated data
2072 * Load @p->size bytes from @p->haddr, which is RAM.
2073 * The bytes to concatenated in big-endian order with @ret_be.
2075 static uint64_t do_ld_bytes_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2077 uint8_t *haddr
= p
->haddr
;
2078 int i
, size
= p
->size
;
2080 for (i
= 0; i
< size
; i
++) {
2081 ret_be
= (ret_be
<< 8) | haddr
[i
];
2088 * @p: translation parameters
2089 * @ret_be: accumulated data
2091 * As do_ld_bytes_beN, but atomically on each aligned part.
2093 static uint64_t do_ld_parts_beN(MMULookupPageData
*p
, uint64_t ret_be
)
2095 void *haddr
= p
->haddr
;
2103 * Find minimum of alignment and size.
2104 * This is slightly stronger than required by MO_ATOM_SUBALIGN, which
2105 * would have only checked the low bits of addr|size once at the start,
2106 * but is just as easy.
2108 switch (((uintptr_t)haddr
| size
) & 7) {
2110 x
= cpu_to_be32(load_atomic4(haddr
));
2111 ret_be
= (ret_be
<< 32) | x
;
2116 x
= cpu_to_be16(load_atomic2(haddr
));
2117 ret_be
= (ret_be
<< 16) | x
;
2121 x
= *(uint8_t *)haddr
;
2122 ret_be
= (ret_be
<< 8) | x
;
2126 g_assert_not_reached();
2130 } while (size
!= 0);
2136 * @p: translation parameters
2137 * @ret_be: accumulated data
2139 * As do_ld_bytes_beN, but with one atomic load.
2140 * Four aligned bytes are guaranteed to cover the load.
2142 static uint64_t do_ld_whole_be4(MMULookupPageData
*p
, uint64_t ret_be
)
2144 int o
= p
->addr
& 3;
2145 uint32_t x
= load_atomic4(p
->haddr
- o
);
2149 x
>>= (4 - p
->size
) * 8;
2150 return (ret_be
<< (p
->size
* 8)) | x
;
2155 * @p: translation parameters
2156 * @ret_be: accumulated data
2158 * As do_ld_bytes_beN, but with one atomic load.
2159 * Eight aligned bytes are guaranteed to cover the load.
2161 static uint64_t do_ld_whole_be8(CPUState
*cpu
, uintptr_t ra
,
2162 MMULookupPageData
*p
, uint64_t ret_be
)
2164 int o
= p
->addr
& 7;
2165 uint64_t x
= load_atomic8_or_exit(cpu
, ra
, p
->haddr
- o
);
2169 x
>>= (8 - p
->size
) * 8;
2170 return (ret_be
<< (p
->size
* 8)) | x
;
2175 * @p: translation parameters
2176 * @ret_be: accumulated data
2178 * As do_ld_bytes_beN, but with one atomic load.
2179 * 16 aligned bytes are guaranteed to cover the load.
2181 static Int128
do_ld_whole_be16(CPUState
*cpu
, uintptr_t ra
,
2182 MMULookupPageData
*p
, uint64_t ret_be
)
2184 int o
= p
->addr
& 15;
2185 Int128 x
, y
= load_atomic16_or_exit(cpu
, ra
, p
->haddr
- o
);
2188 if (!HOST_BIG_ENDIAN
) {
2191 y
= int128_lshift(y
, o
* 8);
2192 y
= int128_urshift(y
, (16 - size
) * 8);
2193 x
= int128_make64(ret_be
);
2194 x
= int128_lshift(x
, size
* 8);
2195 return int128_or(x
, y
);
2199 * Wrapper for the above.
2201 static uint64_t do_ld_beN(CPUState
*cpu
, MMULookupPageData
*p
,
2202 uint64_t ret_be
, int mmu_idx
, MMUAccessType type
,
2203 MemOp mop
, uintptr_t ra
)
2206 unsigned tmp
, half_size
;
2208 if (unlikely(p
->flags
& TLB_MMIO
)) {
2209 return do_ld_mmio_beN(cpu
, p
->full
, ret_be
, p
->addr
, p
->size
,
2214 * It is a given that we cross a page and therefore there is no
2215 * atomicity for the load as a whole, but subobjects may need attention.
2217 atom
= mop
& MO_ATOM_MASK
;
2219 case MO_ATOM_SUBALIGN
:
2220 return do_ld_parts_beN(p
, ret_be
);
2222 case MO_ATOM_IFALIGN_PAIR
:
2223 case MO_ATOM_WITHIN16_PAIR
:
2224 tmp
= mop
& MO_SIZE
;
2225 tmp
= tmp
? tmp
- 1 : 0;
2226 half_size
= 1 << tmp
;
2227 if (atom
== MO_ATOM_IFALIGN_PAIR
2228 ? p
->size
== half_size
2229 : p
->size
>= half_size
) {
2230 if (!HAVE_al8_fast
&& p
->size
< 4) {
2231 return do_ld_whole_be4(p
, ret_be
);
2233 return do_ld_whole_be8(cpu
, ra
, p
, ret_be
);
2238 case MO_ATOM_IFALIGN
:
2239 case MO_ATOM_WITHIN16
:
2241 return do_ld_bytes_beN(p
, ret_be
);
2244 g_assert_not_reached();
2249 * Wrapper for the above, for 8 < size < 16.
2251 static Int128
do_ld16_beN(CPUState
*cpu
, MMULookupPageData
*p
,
2252 uint64_t a
, int mmu_idx
, MemOp mop
, uintptr_t ra
)
2258 if (unlikely(p
->flags
& TLB_MMIO
)) {
2259 return do_ld16_mmio_beN(cpu
, p
->full
, a
, p
->addr
, size
, mmu_idx
, ra
);
2263 * It is a given that we cross a page and therefore there is no
2264 * atomicity for the load as a whole, but subobjects may need attention.
2266 atom
= mop
& MO_ATOM_MASK
;
2268 case MO_ATOM_SUBALIGN
:
2270 a
= do_ld_parts_beN(p
, a
);
2271 p
->haddr
+= size
- 8;
2273 b
= do_ld_parts_beN(p
, 0);
2276 case MO_ATOM_WITHIN16_PAIR
:
2277 /* Since size > 8, this is the half that must be atomic. */
2278 return do_ld_whole_be16(cpu
, ra
, p
, a
);
2280 case MO_ATOM_IFALIGN_PAIR
:
2282 * Since size > 8, both halves are misaligned,
2283 * and so neither is atomic.
2285 case MO_ATOM_IFALIGN
:
2286 case MO_ATOM_WITHIN16
:
2289 a
= do_ld_bytes_beN(p
, a
);
2290 b
= ldq_be_p(p
->haddr
+ size
- 8);
2294 g_assert_not_reached();
2297 return int128_make128(b
, a
);
2300 static uint8_t do_ld_1(CPUState
*cpu
, MMULookupPageData
*p
, int mmu_idx
,
2301 MMUAccessType type
, uintptr_t ra
)
2303 if (unlikely(p
->flags
& TLB_MMIO
)) {
2304 return do_ld_mmio_beN(cpu
, p
->full
, 0, p
->addr
, 1, mmu_idx
, type
, ra
);
2306 return *(uint8_t *)p
->haddr
;
2310 static uint16_t do_ld_2(CPUState
*cpu
, MMULookupPageData
*p
, int mmu_idx
,
2311 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2315 if (unlikely(p
->flags
& TLB_MMIO
)) {
2316 ret
= do_ld_mmio_beN(cpu
, p
->full
, 0, p
->addr
, 2, mmu_idx
, type
, ra
);
2317 if ((memop
& MO_BSWAP
) == MO_LE
) {
2321 /* Perform the load host endian, then swap if necessary. */
2322 ret
= load_atom_2(cpu
, ra
, p
->haddr
, memop
);
2323 if (memop
& MO_BSWAP
) {
2330 static uint32_t do_ld_4(CPUState
*cpu
, MMULookupPageData
*p
, int mmu_idx
,
2331 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2335 if (unlikely(p
->flags
& TLB_MMIO
)) {
2336 ret
= do_ld_mmio_beN(cpu
, p
->full
, 0, p
->addr
, 4, mmu_idx
, type
, ra
);
2337 if ((memop
& MO_BSWAP
) == MO_LE
) {
2341 /* Perform the load host endian. */
2342 ret
= load_atom_4(cpu
, ra
, p
->haddr
, memop
);
2343 if (memop
& MO_BSWAP
) {
2350 static uint64_t do_ld_8(CPUState
*cpu
, MMULookupPageData
*p
, int mmu_idx
,
2351 MMUAccessType type
, MemOp memop
, uintptr_t ra
)
2355 if (unlikely(p
->flags
& TLB_MMIO
)) {
2356 ret
= do_ld_mmio_beN(cpu
, p
->full
, 0, p
->addr
, 8, mmu_idx
, type
, ra
);
2357 if ((memop
& MO_BSWAP
) == MO_LE
) {
2361 /* Perform the load host endian. */
2362 ret
= load_atom_8(cpu
, ra
, p
->haddr
, memop
);
2363 if (memop
& MO_BSWAP
) {
2370 static uint8_t do_ld1_mmu(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
2371 uintptr_t ra
, MMUAccessType access_type
)
2376 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2377 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, access_type
, &l
);
2378 tcg_debug_assert(!crosspage
);
2380 return do_ld_1(cpu
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2383 static uint16_t do_ld2_mmu(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
2384 uintptr_t ra
, MMUAccessType access_type
)
2391 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2392 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, access_type
, &l
);
2393 if (likely(!crosspage
)) {
2394 return do_ld_2(cpu
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2397 a
= do_ld_1(cpu
, &l
.page
[0], l
.mmu_idx
, access_type
, ra
);
2398 b
= do_ld_1(cpu
, &l
.page
[1], l
.mmu_idx
, access_type
, ra
);
2400 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2408 static uint32_t do_ld4_mmu(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
2409 uintptr_t ra
, MMUAccessType access_type
)
2415 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2416 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, access_type
, &l
);
2417 if (likely(!crosspage
)) {
2418 return do_ld_4(cpu
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2421 ret
= do_ld_beN(cpu
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2422 ret
= do_ld_beN(cpu
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2423 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2429 static uint64_t do_ld8_mmu(CPUState
*cpu
, vaddr addr
, MemOpIdx oi
,
2430 uintptr_t ra
, MMUAccessType access_type
)
2436 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2437 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, access_type
, &l
);
2438 if (likely(!crosspage
)) {
2439 return do_ld_8(cpu
, &l
.page
[0], l
.mmu_idx
, access_type
, l
.memop
, ra
);
2442 ret
= do_ld_beN(cpu
, &l
.page
[0], 0, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2443 ret
= do_ld_beN(cpu
, &l
.page
[1], ret
, l
.mmu_idx
, access_type
, l
.memop
, ra
);
2444 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2450 static Int128
do_ld16_mmu(CPUState
*cpu
, vaddr addr
,
2451 MemOpIdx oi
, uintptr_t ra
)
2459 cpu_req_mo(TCG_MO_LD_LD
| TCG_MO_ST_LD
);
2460 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_LOAD
, &l
);
2461 if (likely(!crosspage
)) {
2462 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
2463 ret
= do_ld16_mmio_beN(cpu
, l
.page
[0].full
, 0, addr
, 16,
2465 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2466 ret
= bswap128(ret
);
2469 /* Perform the load host endian. */
2470 ret
= load_atom_16(cpu
, ra
, l
.page
[0].haddr
, l
.memop
);
2471 if (l
.memop
& MO_BSWAP
) {
2472 ret
= bswap128(ret
);
2478 first
= l
.page
[0].size
;
2480 MemOp mop8
= (l
.memop
& ~MO_SIZE
) | MO_64
;
2482 a
= do_ld_8(cpu
, &l
.page
[0], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2483 b
= do_ld_8(cpu
, &l
.page
[1], l
.mmu_idx
, MMU_DATA_LOAD
, mop8
, ra
);
2484 if ((mop8
& MO_BSWAP
) == MO_LE
) {
2485 ret
= int128_make128(a
, b
);
2487 ret
= int128_make128(b
, a
);
2493 a
= do_ld_beN(cpu
, &l
.page
[0], 0, l
.mmu_idx
,
2494 MMU_DATA_LOAD
, l
.memop
, ra
);
2495 ret
= do_ld16_beN(cpu
, &l
.page
[1], a
, l
.mmu_idx
, l
.memop
, ra
);
2497 ret
= do_ld16_beN(cpu
, &l
.page
[0], 0, l
.mmu_idx
, l
.memop
, ra
);
2498 b
= int128_getlo(ret
);
2499 ret
= int128_lshift(ret
, l
.page
[1].size
* 8);
2500 a
= int128_gethi(ret
);
2501 b
= do_ld_beN(cpu
, &l
.page
[1], b
, l
.mmu_idx
,
2502 MMU_DATA_LOAD
, l
.memop
, ra
);
2503 ret
= int128_make128(b
, a
);
2505 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2506 ret
= bswap128(ret
);
2517 * @cpu: generic cpu state
2518 * @full: page parameters
2519 * @val_le: data to store
2520 * @addr: virtual address
2521 * @size: number of bytes
2522 * @mmu_idx: virtual address context
2523 * @ra: return address into tcg generated code, or 0
2526 * Store @size bytes at @addr, which is memory-mapped i/o.
2527 * The bytes to store are extracted in little-endian order from @val_le;
2528 * return the bytes of @val_le beyond @p->size that have not been stored.
2530 static uint64_t int_st_mmio_leN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
2531 uint64_t val_le
, vaddr addr
, int size
,
2532 int mmu_idx
, uintptr_t ra
,
2533 MemoryRegion
*mr
, hwaddr mr_offset
)
2540 /* Store aligned pieces up to 8 bytes. */
2541 this_mop
= ctz32(size
| (int)addr
| 8);
2542 this_size
= 1 << this_mop
;
2545 r
= memory_region_dispatch_write(mr
, mr_offset
, val_le
,
2546 this_mop
, full
->attrs
);
2547 if (unlikely(r
!= MEMTX_OK
)) {
2548 io_failed(cpu
, full
, addr
, this_size
, MMU_DATA_STORE
,
2551 if (this_size
== 8) {
2555 val_le
>>= this_size
* 8;
2557 mr_offset
+= this_size
;
2564 static uint64_t do_st_mmio_leN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
2565 uint64_t val_le
, vaddr addr
, int size
,
2566 int mmu_idx
, uintptr_t ra
)
2568 MemoryRegionSection
*section
;
2574 tcg_debug_assert(size
> 0 && size
<= 8);
2576 attrs
= full
->attrs
;
2577 section
= io_prepare(&mr_offset
, cpu
, full
->xlat_section
, attrs
, addr
, ra
);
2581 ret
= int_st_mmio_leN(cpu
, full
, val_le
, addr
, size
, mmu_idx
,
2588 static uint64_t do_st16_mmio_leN(CPUState
*cpu
, CPUTLBEntryFull
*full
,
2589 Int128 val_le
, vaddr addr
, int size
,
2590 int mmu_idx
, uintptr_t ra
)
2592 MemoryRegionSection
*section
;
2598 tcg_debug_assert(size
> 8 && size
<= 16);
2600 attrs
= full
->attrs
;
2601 section
= io_prepare(&mr_offset
, cpu
, full
->xlat_section
, attrs
, addr
, ra
);
2605 int_st_mmio_leN(cpu
, full
, int128_getlo(val_le
), addr
, 8,
2606 mmu_idx
, ra
, mr
, mr_offset
);
2607 ret
= int_st_mmio_leN(cpu
, full
, int128_gethi(val_le
), addr
+ 8,
2608 size
- 8, mmu_idx
, ra
, mr
, mr_offset
+ 8);
2615 * Wrapper for the above.
2617 static uint64_t do_st_leN(CPUState
*cpu
, MMULookupPageData
*p
,
2618 uint64_t val_le
, int mmu_idx
,
2619 MemOp mop
, uintptr_t ra
)
2622 unsigned tmp
, half_size
;
2624 if (unlikely(p
->flags
& TLB_MMIO
)) {
2625 return do_st_mmio_leN(cpu
, p
->full
, val_le
, p
->addr
,
2626 p
->size
, mmu_idx
, ra
);
2627 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2628 return val_le
>> (p
->size
* 8);
2632 * It is a given that we cross a page and therefore there is no atomicity
2633 * for the store as a whole, but subobjects may need attention.
2635 atom
= mop
& MO_ATOM_MASK
;
2637 case MO_ATOM_SUBALIGN
:
2638 return store_parts_leN(p
->haddr
, p
->size
, val_le
);
2640 case MO_ATOM_IFALIGN_PAIR
:
2641 case MO_ATOM_WITHIN16_PAIR
:
2642 tmp
= mop
& MO_SIZE
;
2643 tmp
= tmp
? tmp
- 1 : 0;
2644 half_size
= 1 << tmp
;
2645 if (atom
== MO_ATOM_IFALIGN_PAIR
2646 ? p
->size
== half_size
2647 : p
->size
>= half_size
) {
2648 if (!HAVE_al8_fast
&& p
->size
<= 4) {
2649 return store_whole_le4(p
->haddr
, p
->size
, val_le
);
2650 } else if (HAVE_al8
) {
2651 return store_whole_le8(p
->haddr
, p
->size
, val_le
);
2653 cpu_loop_exit_atomic(cpu
, ra
);
2658 case MO_ATOM_IFALIGN
:
2659 case MO_ATOM_WITHIN16
:
2661 return store_bytes_leN(p
->haddr
, p
->size
, val_le
);
2664 g_assert_not_reached();
2669 * Wrapper for the above, for 8 < size < 16.
2671 static uint64_t do_st16_leN(CPUState
*cpu
, MMULookupPageData
*p
,
2672 Int128 val_le
, int mmu_idx
,
2673 MemOp mop
, uintptr_t ra
)
2678 if (unlikely(p
->flags
& TLB_MMIO
)) {
2679 return do_st16_mmio_leN(cpu
, p
->full
, val_le
, p
->addr
,
2681 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2682 return int128_gethi(val_le
) >> ((size
- 8) * 8);
2686 * It is a given that we cross a page and therefore there is no atomicity
2687 * for the store as a whole, but subobjects may need attention.
2689 atom
= mop
& MO_ATOM_MASK
;
2691 case MO_ATOM_SUBALIGN
:
2692 store_parts_leN(p
->haddr
, 8, int128_getlo(val_le
));
2693 return store_parts_leN(p
->haddr
+ 8, p
->size
- 8,
2694 int128_gethi(val_le
));
2696 case MO_ATOM_WITHIN16_PAIR
:
2697 /* Since size > 8, this is the half that must be atomic. */
2698 if (!HAVE_CMPXCHG128
) {
2699 cpu_loop_exit_atomic(cpu
, ra
);
2701 return store_whole_le16(p
->haddr
, p
->size
, val_le
);
2703 case MO_ATOM_IFALIGN_PAIR
:
2705 * Since size > 8, both halves are misaligned,
2706 * and so neither is atomic.
2708 case MO_ATOM_IFALIGN
:
2709 case MO_ATOM_WITHIN16
:
2711 stq_le_p(p
->haddr
, int128_getlo(val_le
));
2712 return store_bytes_leN(p
->haddr
+ 8, p
->size
- 8,
2713 int128_gethi(val_le
));
2716 g_assert_not_reached();
2720 static void do_st_1(CPUState
*cpu
, MMULookupPageData
*p
, uint8_t val
,
2721 int mmu_idx
, uintptr_t ra
)
2723 if (unlikely(p
->flags
& TLB_MMIO
)) {
2724 do_st_mmio_leN(cpu
, p
->full
, val
, p
->addr
, 1, mmu_idx
, ra
);
2725 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2728 *(uint8_t *)p
->haddr
= val
;
2732 static void do_st_2(CPUState
*cpu
, MMULookupPageData
*p
, uint16_t val
,
2733 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2735 if (unlikely(p
->flags
& TLB_MMIO
)) {
2736 if ((memop
& MO_BSWAP
) != MO_LE
) {
2739 do_st_mmio_leN(cpu
, p
->full
, val
, p
->addr
, 2, mmu_idx
, ra
);
2740 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2743 /* Swap to host endian if necessary, then store. */
2744 if (memop
& MO_BSWAP
) {
2747 store_atom_2(cpu
, ra
, p
->haddr
, memop
, val
);
2751 static void do_st_4(CPUState
*cpu
, MMULookupPageData
*p
, uint32_t val
,
2752 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2754 if (unlikely(p
->flags
& TLB_MMIO
)) {
2755 if ((memop
& MO_BSWAP
) != MO_LE
) {
2758 do_st_mmio_leN(cpu
, p
->full
, val
, p
->addr
, 4, mmu_idx
, ra
);
2759 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2762 /* Swap to host endian if necessary, then store. */
2763 if (memop
& MO_BSWAP
) {
2766 store_atom_4(cpu
, ra
, p
->haddr
, memop
, val
);
2770 static void do_st_8(CPUState
*cpu
, MMULookupPageData
*p
, uint64_t val
,
2771 int mmu_idx
, MemOp memop
, uintptr_t ra
)
2773 if (unlikely(p
->flags
& TLB_MMIO
)) {
2774 if ((memop
& MO_BSWAP
) != MO_LE
) {
2777 do_st_mmio_leN(cpu
, p
->full
, val
, p
->addr
, 8, mmu_idx
, ra
);
2778 } else if (unlikely(p
->flags
& TLB_DISCARD_WRITE
)) {
2781 /* Swap to host endian if necessary, then store. */
2782 if (memop
& MO_BSWAP
) {
2785 store_atom_8(cpu
, ra
, p
->haddr
, memop
, val
);
2789 static void do_st1_mmu(CPUState
*cpu
, vaddr addr
, uint8_t val
,
2790 MemOpIdx oi
, uintptr_t ra
)
2795 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2796 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2797 tcg_debug_assert(!crosspage
);
2799 do_st_1(cpu
, &l
.page
[0], val
, l
.mmu_idx
, ra
);
2802 static void do_st2_mmu(CPUState
*cpu
, vaddr addr
, uint16_t val
,
2803 MemOpIdx oi
, uintptr_t ra
)
2809 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2810 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2811 if (likely(!crosspage
)) {
2812 do_st_2(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2816 if ((l
.memop
& MO_BSWAP
) == MO_LE
) {
2817 a
= val
, b
= val
>> 8;
2819 b
= val
, a
= val
>> 8;
2821 do_st_1(cpu
, &l
.page
[0], a
, l
.mmu_idx
, ra
);
2822 do_st_1(cpu
, &l
.page
[1], b
, l
.mmu_idx
, ra
);
2825 static void do_st4_mmu(CPUState
*cpu
, vaddr addr
, uint32_t val
,
2826 MemOpIdx oi
, uintptr_t ra
)
2831 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2832 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2833 if (likely(!crosspage
)) {
2834 do_st_4(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2838 /* Swap to little endian for simplicity, then store by bytes. */
2839 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2842 val
= do_st_leN(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2843 (void) do_st_leN(cpu
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2846 static void do_st8_mmu(CPUState
*cpu
, vaddr addr
, uint64_t val
,
2847 MemOpIdx oi
, uintptr_t ra
)
2852 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2853 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2854 if (likely(!crosspage
)) {
2855 do_st_8(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2859 /* Swap to little endian for simplicity, then store by bytes. */
2860 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2863 val
= do_st_leN(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2864 (void) do_st_leN(cpu
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2867 static void do_st16_mmu(CPUState
*cpu
, vaddr addr
, Int128 val
,
2868 MemOpIdx oi
, uintptr_t ra
)
2875 cpu_req_mo(TCG_MO_LD_ST
| TCG_MO_ST_ST
);
2876 crosspage
= mmu_lookup(cpu
, addr
, oi
, ra
, MMU_DATA_STORE
, &l
);
2877 if (likely(!crosspage
)) {
2878 if (unlikely(l
.page
[0].flags
& TLB_MMIO
)) {
2879 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2880 val
= bswap128(val
);
2882 do_st16_mmio_leN(cpu
, l
.page
[0].full
, val
, addr
, 16, l
.mmu_idx
, ra
);
2883 } else if (unlikely(l
.page
[0].flags
& TLB_DISCARD_WRITE
)) {
2886 /* Swap to host endian if necessary, then store. */
2887 if (l
.memop
& MO_BSWAP
) {
2888 val
= bswap128(val
);
2890 store_atom_16(cpu
, ra
, l
.page
[0].haddr
, l
.memop
, val
);
2895 first
= l
.page
[0].size
;
2897 MemOp mop8
= (l
.memop
& ~(MO_SIZE
| MO_BSWAP
)) | MO_64
;
2899 if (l
.memop
& MO_BSWAP
) {
2900 val
= bswap128(val
);
2902 if (HOST_BIG_ENDIAN
) {
2903 b
= int128_getlo(val
), a
= int128_gethi(val
);
2905 a
= int128_getlo(val
), b
= int128_gethi(val
);
2907 do_st_8(cpu
, &l
.page
[0], a
, l
.mmu_idx
, mop8
, ra
);
2908 do_st_8(cpu
, &l
.page
[1], b
, l
.mmu_idx
, mop8
, ra
);
2912 if ((l
.memop
& MO_BSWAP
) != MO_LE
) {
2913 val
= bswap128(val
);
2916 do_st_leN(cpu
, &l
.page
[0], int128_getlo(val
), l
.mmu_idx
, l
.memop
, ra
);
2917 val
= int128_urshift(val
, first
* 8);
2918 do_st16_leN(cpu
, &l
.page
[1], val
, l
.mmu_idx
, l
.memop
, ra
);
2920 b
= do_st16_leN(cpu
, &l
.page
[0], val
, l
.mmu_idx
, l
.memop
, ra
);
2921 do_st_leN(cpu
, &l
.page
[1], b
, l
.mmu_idx
, l
.memop
, ra
);
2925 #include "ldst_common.c.inc"
2928 * First set of functions passes in OI and RETADDR.
2929 * This makes them callable from other helpers.
2932 #define ATOMIC_NAME(X) \
2933 glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
2935 #define ATOMIC_MMU_CLEANUP
2937 #include "atomic_common.c.inc"
2940 #include "atomic_template.h"
2943 #include "atomic_template.h"
2946 #include "atomic_template.h"
2948 #ifdef CONFIG_ATOMIC64
2950 #include "atomic_template.h"
2953 #if defined(CONFIG_ATOMIC128) || HAVE_CMPXCHG128
2954 #define DATA_SIZE 16
2955 #include "atomic_template.h"
2958 /* Code access functions. */
2960 uint32_t cpu_ldub_code(CPUArchState
*env
, abi_ptr addr
)
2962 CPUState
*cs
= env_cpu(env
);
2963 MemOpIdx oi
= make_memop_idx(MO_UB
, cpu_mmu_index(cs
, true));
2964 return do_ld1_mmu(cs
, addr
, oi
, 0, MMU_INST_FETCH
);
2967 uint32_t cpu_lduw_code(CPUArchState
*env
, abi_ptr addr
)
2969 CPUState
*cs
= env_cpu(env
);
2970 MemOpIdx oi
= make_memop_idx(MO_TEUW
, cpu_mmu_index(cs
, true));
2971 return do_ld2_mmu(cs
, addr
, oi
, 0, MMU_INST_FETCH
);
2974 uint32_t cpu_ldl_code(CPUArchState
*env
, abi_ptr addr
)
2976 CPUState
*cs
= env_cpu(env
);
2977 MemOpIdx oi
= make_memop_idx(MO_TEUL
, cpu_mmu_index(cs
, true));
2978 return do_ld4_mmu(cs
, addr
, oi
, 0, MMU_INST_FETCH
);
2981 uint64_t cpu_ldq_code(CPUArchState
*env
, abi_ptr addr
)
2983 CPUState
*cs
= env_cpu(env
);
2984 MemOpIdx oi
= make_memop_idx(MO_TEUQ
, cpu_mmu_index(cs
, true));
2985 return do_ld8_mmu(cs
, addr
, oi
, 0, MMU_INST_FETCH
);
2988 uint8_t cpu_ldb_code_mmu(CPUArchState
*env
, abi_ptr addr
,
2989 MemOpIdx oi
, uintptr_t retaddr
)
2991 return do_ld1_mmu(env_cpu(env
), addr
, oi
, retaddr
, MMU_INST_FETCH
);
2994 uint16_t cpu_ldw_code_mmu(CPUArchState
*env
, abi_ptr addr
,
2995 MemOpIdx oi
, uintptr_t retaddr
)
2997 return do_ld2_mmu(env_cpu(env
), addr
, oi
, retaddr
, MMU_INST_FETCH
);
3000 uint32_t cpu_ldl_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3001 MemOpIdx oi
, uintptr_t retaddr
)
3003 return do_ld4_mmu(env_cpu(env
), addr
, oi
, retaddr
, MMU_INST_FETCH
);
3006 uint64_t cpu_ldq_code_mmu(CPUArchState
*env
, abi_ptr addr
,
3007 MemOpIdx oi
, uintptr_t retaddr
)
3009 return do_ld8_mmu(env_cpu(env
), addr
, oi
, retaddr
, MMU_INST_FETCH
);