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 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/>.
27 #include "memory-internal.h"
30 //#define DEBUG_TLB_CHECK
35 static const CPUTLBEntry s_cputlb_empty_entry
= {
43 * If flush_global is true (the usual case), flush all tlb entries.
44 * If flush_global is false, flush (at least) all tlb entries not
47 * Since QEMU doesn't currently implement a global/not-global flag
48 * for tlb entries, at the moment tlb_flush() will also flush all
49 * tlb entries in the flush_global == false case. This is OK because
50 * CPU architectures generally permit an implementation to drop
51 * entries from the TLB at any time, so flushing more entries than
52 * required is only an efficiency issue, not a correctness issue.
54 void tlb_flush(CPUArchState
*env
, int flush_global
)
58 #if defined(DEBUG_TLB)
59 printf("tlb_flush:\n");
61 /* must reset current TB so that interrupts cannot modify the
62 links while we are modifying them */
63 env
->current_tb
= NULL
;
65 for (i
= 0; i
< CPU_TLB_SIZE
; i
++) {
68 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
69 env
->tlb_table
[mmu_idx
][i
] = s_cputlb_empty_entry
;
73 memset(env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
75 env
->tlb_flush_addr
= -1;
76 env
->tlb_flush_mask
= 0;
80 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, target_ulong addr
)
82 if (addr
== (tlb_entry
->addr_read
&
83 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
84 addr
== (tlb_entry
->addr_write
&
85 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
86 addr
== (tlb_entry
->addr_code
&
87 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
88 *tlb_entry
= s_cputlb_empty_entry
;
92 void tlb_flush_page(CPUArchState
*env
, target_ulong addr
)
97 #if defined(DEBUG_TLB)
98 printf("tlb_flush_page: " TARGET_FMT_lx
"\n", addr
);
100 /* Check if we need to flush due to large pages. */
101 if ((addr
& env
->tlb_flush_mask
) == env
->tlb_flush_addr
) {
102 #if defined(DEBUG_TLB)
103 printf("tlb_flush_page: forced full flush ("
104 TARGET_FMT_lx
"/" TARGET_FMT_lx
")\n",
105 env
->tlb_flush_addr
, env
->tlb_flush_mask
);
110 /* must reset current TB so that interrupts cannot modify the
111 links while we are modifying them */
112 env
->current_tb
= NULL
;
114 addr
&= TARGET_PAGE_MASK
;
115 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
116 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
117 tlb_flush_entry(&env
->tlb_table
[mmu_idx
][i
], addr
);
120 tb_flush_jmp_cache(env
, addr
);
123 /* update the TLBs so that writes to code in the virtual page 'addr'
125 void tlb_protect_code(ram_addr_t ram_addr
)
127 cpu_physical_memory_reset_dirty(ram_addr
,
128 ram_addr
+ TARGET_PAGE_SIZE
,
132 /* update the TLB so that writes in physical page 'phys_addr' are no longer
133 tested for self modifying code */
134 void tlb_unprotect_code_phys(CPUArchState
*env
, ram_addr_t ram_addr
,
137 cpu_physical_memory_set_dirty_flags(ram_addr
, CODE_DIRTY_FLAG
);
140 static bool tlb_is_dirty_ram(CPUTLBEntry
*tlbe
)
142 return (tlbe
->addr_write
& (TLB_INVALID_MASK
|TLB_MMIO
|TLB_NOTDIRTY
)) == 0;
145 void tlb_reset_dirty_range(CPUTLBEntry
*tlb_entry
, uintptr_t start
,
150 if (tlb_is_dirty_ram(tlb_entry
)) {
151 addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) + tlb_entry
->addend
;
152 if ((addr
- start
) < length
) {
153 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
158 static inline void tlb_update_dirty(CPUTLBEntry
*tlb_entry
)
163 if (tlb_is_dirty_ram(tlb_entry
)) {
164 p
= (void *)(uintptr_t)((tlb_entry
->addr_write
& TARGET_PAGE_MASK
)
165 + tlb_entry
->addend
);
166 ram_addr
= qemu_ram_addr_from_host_nofail(p
);
167 if (!cpu_physical_memory_is_dirty(ram_addr
)) {
168 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
173 void cpu_tlb_reset_dirty_all(ram_addr_t start1
, ram_addr_t length
)
177 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
180 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
183 for (i
= 0; i
< CPU_TLB_SIZE
; i
++) {
184 tlb_reset_dirty_range(&env
->tlb_table
[mmu_idx
][i
],
191 static inline void tlb_set_dirty1(CPUTLBEntry
*tlb_entry
, target_ulong vaddr
)
193 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
)) {
194 tlb_entry
->addr_write
= vaddr
;
198 /* update the TLB corresponding to virtual page vaddr
199 so that it is no longer dirty */
200 void tlb_set_dirty(CPUArchState
*env
, target_ulong vaddr
)
205 vaddr
&= TARGET_PAGE_MASK
;
206 i
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
207 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
208 tlb_set_dirty1(&env
->tlb_table
[mmu_idx
][i
], vaddr
);
212 /* Our TLB does not support large pages, so remember the area covered by
213 large pages and trigger a full TLB flush if these are invalidated. */
214 static void tlb_add_large_page(CPUArchState
*env
, target_ulong vaddr
,
217 target_ulong mask
= ~(size
- 1);
219 if (env
->tlb_flush_addr
== (target_ulong
)-1) {
220 env
->tlb_flush_addr
= vaddr
& mask
;
221 env
->tlb_flush_mask
= mask
;
224 /* Extend the existing region to include the new page.
225 This is a compromise between unnecessary flushes and the cost
226 of maintaining a full variable size TLB. */
227 mask
&= env
->tlb_flush_mask
;
228 while (((env
->tlb_flush_addr
^ vaddr
) & mask
) != 0) {
231 env
->tlb_flush_addr
&= mask
;
232 env
->tlb_flush_mask
= mask
;
235 /* Add a new TLB entry. At most one entry for a given virtual address
236 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
237 supplied size is only used by tlb_flush_page. */
238 void tlb_set_page(CPUArchState
*env
, target_ulong vaddr
,
239 target_phys_addr_t paddr
, int prot
,
240 int mmu_idx
, target_ulong size
)
242 MemoryRegionSection
*section
;
244 target_ulong address
;
245 target_ulong code_address
;
248 target_phys_addr_t iotlb
;
250 assert(size
>= TARGET_PAGE_SIZE
);
251 if (size
!= TARGET_PAGE_SIZE
) {
252 tlb_add_large_page(env
, vaddr
, size
);
254 section
= phys_page_find(paddr
>> TARGET_PAGE_BITS
);
255 #if defined(DEBUG_TLB)
256 printf("tlb_set_page: vaddr=" TARGET_FMT_lx
" paddr=0x" TARGET_FMT_plx
257 " prot=%x idx=%d pd=0x%08lx\n",
258 vaddr
, paddr
, prot
, mmu_idx
, pd
);
262 if (!(memory_region_is_ram(section
->mr
) ||
263 memory_region_is_romd(section
->mr
))) {
264 /* IO memory case (romd handled later) */
267 if (memory_region_is_ram(section
->mr
) ||
268 memory_region_is_romd(section
->mr
)) {
269 addend
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
)
270 + memory_region_section_addr(section
, paddr
);
275 code_address
= address
;
276 iotlb
= memory_region_section_get_iotlb(env
, section
, vaddr
, paddr
, prot
,
279 index
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
280 env
->iotlb
[mmu_idx
][index
] = iotlb
- vaddr
;
281 te
= &env
->tlb_table
[mmu_idx
][index
];
282 te
->addend
= addend
- vaddr
;
283 if (prot
& PAGE_READ
) {
284 te
->addr_read
= address
;
289 if (prot
& PAGE_EXEC
) {
290 te
->addr_code
= code_address
;
294 if (prot
& PAGE_WRITE
) {
295 if ((memory_region_is_ram(section
->mr
) && section
->readonly
)
296 || memory_region_is_romd(section
->mr
)) {
297 /* Write access calls the I/O callback. */
298 te
->addr_write
= address
| TLB_MMIO
;
299 } else if (memory_region_is_ram(section
->mr
)
300 && !cpu_physical_memory_is_dirty(
301 section
->mr
->ram_addr
302 + memory_region_section_addr(section
, paddr
))) {
303 te
->addr_write
= address
| TLB_NOTDIRTY
;
305 te
->addr_write
= address
;
312 /* NOTE: this function can trigger an exception */
313 /* NOTE2: the returned address is not exactly the physical address: it
314 * is actually a ram_addr_t (in system mode; the user mode emulation
315 * version of this function returns a guest virtual address).
317 tb_page_addr_t
get_page_addr_code(CPUArchState
*env1
, target_ulong addr
)
319 int mmu_idx
, page_index
, pd
;
323 page_index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
324 mmu_idx
= cpu_mmu_index(env1
);
325 if (unlikely(env1
->tlb_table
[mmu_idx
][page_index
].addr_code
!=
326 (addr
& TARGET_PAGE_MASK
))) {
327 cpu_ldub_code(env1
, addr
);
329 pd
= env1
->iotlb
[mmu_idx
][page_index
] & ~TARGET_PAGE_MASK
;
330 mr
= iotlb_to_region(pd
);
331 if (memory_region_is_unassigned(mr
)) {
332 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_SPARC)
333 cpu_unassigned_access(env1
, addr
, 0, 1, 0, 4);
335 cpu_abort(env1
, "Trying to execute code outside RAM or ROM at 0x"
336 TARGET_FMT_lx
"\n", addr
);
339 p
= (void *)((uintptr_t)addr
+ env1
->tlb_table
[mmu_idx
][page_index
].addend
);
340 return qemu_ram_addr_from_host_nofail(p
);
343 #define MMUSUFFIX _cmmu
345 #define GETPC() ((uintptr_t)0)
346 #define SOFTMMU_CODE_ACCESS
349 #include "softmmu_template.h"
352 #include "softmmu_template.h"
355 #include "softmmu_template.h"
358 #include "softmmu_template.h"