]>
Commit | Line | Data |
---|---|---|
54936004 | 1 | /* |
fd6ce8f6 | 2 | * virtual page mapping and translated block handling |
5fafdf24 | 3 | * |
54936004 FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
fad6cb1a | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA |
54936004 | 19 | */ |
67b915a5 | 20 | #include "config.h" |
d5a8f07c FB |
21 | #ifdef _WIN32 |
22 | #include <windows.h> | |
23 | #else | |
a98d49b1 | 24 | #include <sys/types.h> |
d5a8f07c FB |
25 | #include <sys/mman.h> |
26 | #endif | |
54936004 FB |
27 | #include <stdlib.h> |
28 | #include <stdio.h> | |
29 | #include <stdarg.h> | |
30 | #include <string.h> | |
31 | #include <errno.h> | |
32 | #include <unistd.h> | |
33 | #include <inttypes.h> | |
34 | ||
6180a181 FB |
35 | #include "cpu.h" |
36 | #include "exec-all.h" | |
ca10f867 | 37 | #include "qemu-common.h" |
b67d9a52 | 38 | #include "tcg.h" |
b3c7724c | 39 | #include "hw/hw.h" |
74576198 | 40 | #include "osdep.h" |
7ba1e619 | 41 | #include "kvm.h" |
53a5960a PB |
42 | #if defined(CONFIG_USER_ONLY) |
43 | #include <qemu.h> | |
44 | #endif | |
54936004 | 45 | |
fd6ce8f6 | 46 | //#define DEBUG_TB_INVALIDATE |
66e85a21 | 47 | //#define DEBUG_FLUSH |
9fa3e853 | 48 | //#define DEBUG_TLB |
67d3b957 | 49 | //#define DEBUG_UNASSIGNED |
fd6ce8f6 FB |
50 | |
51 | /* make various TB consistency checks */ | |
5fafdf24 TS |
52 | //#define DEBUG_TB_CHECK |
53 | //#define DEBUG_TLB_CHECK | |
fd6ce8f6 | 54 | |
1196be37 | 55 | //#define DEBUG_IOPORT |
db7b5426 | 56 | //#define DEBUG_SUBPAGE |
1196be37 | 57 | |
99773bd4 PB |
58 | #if !defined(CONFIG_USER_ONLY) |
59 | /* TB consistency checks only implemented for usermode emulation. */ | |
60 | #undef DEBUG_TB_CHECK | |
61 | #endif | |
62 | ||
9fa3e853 FB |
63 | #define SMC_BITMAP_USE_THRESHOLD 10 |
64 | ||
108c49b8 FB |
65 | #if defined(TARGET_SPARC64) |
66 | #define TARGET_PHYS_ADDR_SPACE_BITS 41 | |
5dcb6b91 BS |
67 | #elif defined(TARGET_SPARC) |
68 | #define TARGET_PHYS_ADDR_SPACE_BITS 36 | |
bedb69ea JM |
69 | #elif defined(TARGET_ALPHA) |
70 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 | |
71 | #define TARGET_VIRT_ADDR_SPACE_BITS 42 | |
108c49b8 FB |
72 | #elif defined(TARGET_PPC64) |
73 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 | |
640f42e4 | 74 | #elif defined(TARGET_X86_64) && !defined(CONFIG_KQEMU) |
00f82b8a | 75 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 |
640f42e4 | 76 | #elif defined(TARGET_I386) && !defined(CONFIG_KQEMU) |
00f82b8a | 77 | #define TARGET_PHYS_ADDR_SPACE_BITS 36 |
108c49b8 FB |
78 | #else |
79 | /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */ | |
80 | #define TARGET_PHYS_ADDR_SPACE_BITS 32 | |
81 | #endif | |
82 | ||
bdaf78e0 | 83 | static TranslationBlock *tbs; |
26a5f13b | 84 | int code_gen_max_blocks; |
9fa3e853 | 85 | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
bdaf78e0 | 86 | static int nb_tbs; |
eb51d102 FB |
87 | /* any access to the tbs or the page table must use this lock */ |
88 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; | |
fd6ce8f6 | 89 | |
141ac468 BS |
90 | #if defined(__arm__) || defined(__sparc_v9__) |
91 | /* The prologue must be reachable with a direct jump. ARM and Sparc64 | |
92 | have limited branch ranges (possibly also PPC) so place it in a | |
d03d860b BS |
93 | section close to code segment. */ |
94 | #define code_gen_section \ | |
95 | __attribute__((__section__(".gen_code"))) \ | |
96 | __attribute__((aligned (32))) | |
f8e2af11 SW |
97 | #elif defined(_WIN32) |
98 | /* Maximum alignment for Win32 is 16. */ | |
99 | #define code_gen_section \ | |
100 | __attribute__((aligned (16))) | |
d03d860b BS |
101 | #else |
102 | #define code_gen_section \ | |
103 | __attribute__((aligned (32))) | |
104 | #endif | |
105 | ||
106 | uint8_t code_gen_prologue[1024] code_gen_section; | |
bdaf78e0 BS |
107 | static uint8_t *code_gen_buffer; |
108 | static unsigned long code_gen_buffer_size; | |
26a5f13b | 109 | /* threshold to flush the translated code buffer */ |
bdaf78e0 | 110 | static unsigned long code_gen_buffer_max_size; |
fd6ce8f6 FB |
111 | uint8_t *code_gen_ptr; |
112 | ||
e2eef170 | 113 | #if !defined(CONFIG_USER_ONLY) |
9fa3e853 | 114 | int phys_ram_fd; |
1ccde1cb | 115 | uint8_t *phys_ram_dirty; |
74576198 | 116 | static int in_migration; |
94a6b54f PB |
117 | |
118 | typedef struct RAMBlock { | |
119 | uint8_t *host; | |
120 | ram_addr_t offset; | |
121 | ram_addr_t length; | |
122 | struct RAMBlock *next; | |
123 | } RAMBlock; | |
124 | ||
125 | static RAMBlock *ram_blocks; | |
126 | /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug) | |
ccbb4d44 | 127 | then we can no longer assume contiguous ram offsets, and external uses |
94a6b54f PB |
128 | of this variable will break. */ |
129 | ram_addr_t last_ram_offset; | |
e2eef170 | 130 | #endif |
9fa3e853 | 131 | |
6a00d601 FB |
132 | CPUState *first_cpu; |
133 | /* current CPU in the current thread. It is only valid inside | |
134 | cpu_exec() */ | |
5fafdf24 | 135 | CPUState *cpu_single_env; |
2e70f6ef | 136 | /* 0 = Do not count executed instructions. |
bf20dc07 | 137 | 1 = Precise instruction counting. |
2e70f6ef PB |
138 | 2 = Adaptive rate instruction counting. */ |
139 | int use_icount = 0; | |
140 | /* Current instruction counter. While executing translated code this may | |
141 | include some instructions that have not yet been executed. */ | |
142 | int64_t qemu_icount; | |
6a00d601 | 143 | |
54936004 | 144 | typedef struct PageDesc { |
92e873b9 | 145 | /* list of TBs intersecting this ram page */ |
fd6ce8f6 | 146 | TranslationBlock *first_tb; |
9fa3e853 FB |
147 | /* in order to optimize self modifying code, we count the number |
148 | of lookups we do to a given page to use a bitmap */ | |
149 | unsigned int code_write_count; | |
150 | uint8_t *code_bitmap; | |
151 | #if defined(CONFIG_USER_ONLY) | |
152 | unsigned long flags; | |
153 | #endif | |
54936004 FB |
154 | } PageDesc; |
155 | ||
92e873b9 | 156 | typedef struct PhysPageDesc { |
0f459d16 | 157 | /* offset in host memory of the page + io_index in the low bits */ |
00f82b8a | 158 | ram_addr_t phys_offset; |
8da3ff18 | 159 | ram_addr_t region_offset; |
92e873b9 FB |
160 | } PhysPageDesc; |
161 | ||
54936004 | 162 | #define L2_BITS 10 |
bedb69ea JM |
163 | #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS) |
164 | /* XXX: this is a temporary hack for alpha target. | |
165 | * In the future, this is to be replaced by a multi-level table | |
166 | * to actually be able to handle the complete 64 bits address space. | |
167 | */ | |
168 | #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS) | |
169 | #else | |
03875444 | 170 | #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS) |
bedb69ea | 171 | #endif |
54936004 FB |
172 | |
173 | #define L1_SIZE (1 << L1_BITS) | |
174 | #define L2_SIZE (1 << L2_BITS) | |
175 | ||
83fb7adf FB |
176 | unsigned long qemu_real_host_page_size; |
177 | unsigned long qemu_host_page_bits; | |
178 | unsigned long qemu_host_page_size; | |
179 | unsigned long qemu_host_page_mask; | |
54936004 | 180 | |
92e873b9 | 181 | /* XXX: for system emulation, it could just be an array */ |
54936004 | 182 | static PageDesc *l1_map[L1_SIZE]; |
bdaf78e0 | 183 | static PhysPageDesc **l1_phys_map; |
54936004 | 184 | |
e2eef170 PB |
185 | #if !defined(CONFIG_USER_ONLY) |
186 | static void io_mem_init(void); | |
187 | ||
33417e70 | 188 | /* io memory support */ |
33417e70 FB |
189 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; |
190 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; | |
a4193c8a | 191 | void *io_mem_opaque[IO_MEM_NB_ENTRIES]; |
511d2b14 | 192 | static char io_mem_used[IO_MEM_NB_ENTRIES]; |
6658ffb8 PB |
193 | static int io_mem_watch; |
194 | #endif | |
33417e70 | 195 | |
34865134 | 196 | /* log support */ |
d9b630fd | 197 | static const char *logfilename = "/tmp/qemu.log"; |
34865134 FB |
198 | FILE *logfile; |
199 | int loglevel; | |
e735b91c | 200 | static int log_append = 0; |
34865134 | 201 | |
e3db7226 FB |
202 | /* statistics */ |
203 | static int tlb_flush_count; | |
204 | static int tb_flush_count; | |
205 | static int tb_phys_invalidate_count; | |
206 | ||
db7b5426 BS |
207 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
208 | typedef struct subpage_t { | |
209 | target_phys_addr_t base; | |
3ee89922 BS |
210 | CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4]; |
211 | CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4]; | |
212 | void *opaque[TARGET_PAGE_SIZE][2][4]; | |
8da3ff18 | 213 | ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4]; |
db7b5426 BS |
214 | } subpage_t; |
215 | ||
7cb69cae FB |
216 | #ifdef _WIN32 |
217 | static void map_exec(void *addr, long size) | |
218 | { | |
219 | DWORD old_protect; | |
220 | VirtualProtect(addr, size, | |
221 | PAGE_EXECUTE_READWRITE, &old_protect); | |
222 | ||
223 | } | |
224 | #else | |
225 | static void map_exec(void *addr, long size) | |
226 | { | |
4369415f | 227 | unsigned long start, end, page_size; |
7cb69cae | 228 | |
4369415f | 229 | page_size = getpagesize(); |
7cb69cae | 230 | start = (unsigned long)addr; |
4369415f | 231 | start &= ~(page_size - 1); |
7cb69cae FB |
232 | |
233 | end = (unsigned long)addr + size; | |
4369415f FB |
234 | end += page_size - 1; |
235 | end &= ~(page_size - 1); | |
7cb69cae FB |
236 | |
237 | mprotect((void *)start, end - start, | |
238 | PROT_READ | PROT_WRITE | PROT_EXEC); | |
239 | } | |
240 | #endif | |
241 | ||
b346ff46 | 242 | static void page_init(void) |
54936004 | 243 | { |
83fb7adf | 244 | /* NOTE: we can always suppose that qemu_host_page_size >= |
54936004 | 245 | TARGET_PAGE_SIZE */ |
c2b48b69 AL |
246 | #ifdef _WIN32 |
247 | { | |
248 | SYSTEM_INFO system_info; | |
249 | ||
250 | GetSystemInfo(&system_info); | |
251 | qemu_real_host_page_size = system_info.dwPageSize; | |
252 | } | |
253 | #else | |
254 | qemu_real_host_page_size = getpagesize(); | |
255 | #endif | |
83fb7adf FB |
256 | if (qemu_host_page_size == 0) |
257 | qemu_host_page_size = qemu_real_host_page_size; | |
258 | if (qemu_host_page_size < TARGET_PAGE_SIZE) | |
259 | qemu_host_page_size = TARGET_PAGE_SIZE; | |
260 | qemu_host_page_bits = 0; | |
261 | while ((1 << qemu_host_page_bits) < qemu_host_page_size) | |
262 | qemu_host_page_bits++; | |
263 | qemu_host_page_mask = ~(qemu_host_page_size - 1); | |
108c49b8 FB |
264 | l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *)); |
265 | memset(l1_phys_map, 0, L1_SIZE * sizeof(void *)); | |
50a9569b AZ |
266 | |
267 | #if !defined(_WIN32) && defined(CONFIG_USER_ONLY) | |
268 | { | |
269 | long long startaddr, endaddr; | |
270 | FILE *f; | |
271 | int n; | |
272 | ||
c8a706fe | 273 | mmap_lock(); |
0776590d | 274 | last_brk = (unsigned long)sbrk(0); |
50a9569b AZ |
275 | f = fopen("/proc/self/maps", "r"); |
276 | if (f) { | |
277 | do { | |
278 | n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr); | |
279 | if (n == 2) { | |
e0b8d65a BS |
280 | startaddr = MIN(startaddr, |
281 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); | |
282 | endaddr = MIN(endaddr, | |
283 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); | |
b5fc909e | 284 | page_set_flags(startaddr & TARGET_PAGE_MASK, |
50a9569b AZ |
285 | TARGET_PAGE_ALIGN(endaddr), |
286 | PAGE_RESERVED); | |
287 | } | |
288 | } while (!feof(f)); | |
289 | fclose(f); | |
290 | } | |
c8a706fe | 291 | mmap_unlock(); |
50a9569b AZ |
292 | } |
293 | #endif | |
54936004 FB |
294 | } |
295 | ||
434929bf | 296 | static inline PageDesc **page_l1_map(target_ulong index) |
54936004 | 297 | { |
17e2377a PB |
298 | #if TARGET_LONG_BITS > 32 |
299 | /* Host memory outside guest VM. For 32-bit targets we have already | |
300 | excluded high addresses. */ | |
d8173e0f | 301 | if (index > ((target_ulong)L2_SIZE * L1_SIZE)) |
17e2377a PB |
302 | return NULL; |
303 | #endif | |
434929bf AL |
304 | return &l1_map[index >> L2_BITS]; |
305 | } | |
306 | ||
307 | static inline PageDesc *page_find_alloc(target_ulong index) | |
308 | { | |
309 | PageDesc **lp, *p; | |
310 | lp = page_l1_map(index); | |
311 | if (!lp) | |
312 | return NULL; | |
313 | ||
54936004 FB |
314 | p = *lp; |
315 | if (!p) { | |
316 | /* allocate if not found */ | |
17e2377a | 317 | #if defined(CONFIG_USER_ONLY) |
17e2377a PB |
318 | size_t len = sizeof(PageDesc) * L2_SIZE; |
319 | /* Don't use qemu_malloc because it may recurse. */ | |
320 | p = mmap(0, len, PROT_READ | PROT_WRITE, | |
321 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
54936004 | 322 | *lp = p; |
fb1c2cd7 AJ |
323 | if (h2g_valid(p)) { |
324 | unsigned long addr = h2g(p); | |
17e2377a PB |
325 | page_set_flags(addr & TARGET_PAGE_MASK, |
326 | TARGET_PAGE_ALIGN(addr + len), | |
327 | PAGE_RESERVED); | |
328 | } | |
329 | #else | |
330 | p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE); | |
331 | *lp = p; | |
332 | #endif | |
54936004 FB |
333 | } |
334 | return p + (index & (L2_SIZE - 1)); | |
335 | } | |
336 | ||
00f82b8a | 337 | static inline PageDesc *page_find(target_ulong index) |
54936004 | 338 | { |
434929bf AL |
339 | PageDesc **lp, *p; |
340 | lp = page_l1_map(index); | |
341 | if (!lp) | |
342 | return NULL; | |
54936004 | 343 | |
434929bf | 344 | p = *lp; |
54936004 FB |
345 | if (!p) |
346 | return 0; | |
fd6ce8f6 FB |
347 | return p + (index & (L2_SIZE - 1)); |
348 | } | |
349 | ||
108c49b8 | 350 | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
92e873b9 | 351 | { |
108c49b8 | 352 | void **lp, **p; |
e3f4e2a4 | 353 | PhysPageDesc *pd; |
92e873b9 | 354 | |
108c49b8 FB |
355 | p = (void **)l1_phys_map; |
356 | #if TARGET_PHYS_ADDR_SPACE_BITS > 32 | |
357 | ||
358 | #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS) | |
359 | #error unsupported TARGET_PHYS_ADDR_SPACE_BITS | |
360 | #endif | |
361 | lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1)); | |
92e873b9 FB |
362 | p = *lp; |
363 | if (!p) { | |
364 | /* allocate if not found */ | |
108c49b8 FB |
365 | if (!alloc) |
366 | return NULL; | |
367 | p = qemu_vmalloc(sizeof(void *) * L1_SIZE); | |
368 | memset(p, 0, sizeof(void *) * L1_SIZE); | |
369 | *lp = p; | |
370 | } | |
371 | #endif | |
372 | lp = p + ((index >> L2_BITS) & (L1_SIZE - 1)); | |
e3f4e2a4 PB |
373 | pd = *lp; |
374 | if (!pd) { | |
375 | int i; | |
108c49b8 FB |
376 | /* allocate if not found */ |
377 | if (!alloc) | |
378 | return NULL; | |
e3f4e2a4 PB |
379 | pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE); |
380 | *lp = pd; | |
67c4d23c | 381 | for (i = 0; i < L2_SIZE; i++) { |
e3f4e2a4 | 382 | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
67c4d23c PB |
383 | pd[i].region_offset = (index + i) << TARGET_PAGE_BITS; |
384 | } | |
92e873b9 | 385 | } |
e3f4e2a4 | 386 | return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1)); |
92e873b9 FB |
387 | } |
388 | ||
108c49b8 | 389 | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
92e873b9 | 390 | { |
108c49b8 | 391 | return phys_page_find_alloc(index, 0); |
92e873b9 FB |
392 | } |
393 | ||
9fa3e853 | 394 | #if !defined(CONFIG_USER_ONLY) |
6a00d601 | 395 | static void tlb_protect_code(ram_addr_t ram_addr); |
5fafdf24 | 396 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
3a7d929e | 397 | target_ulong vaddr); |
c8a706fe PB |
398 | #define mmap_lock() do { } while(0) |
399 | #define mmap_unlock() do { } while(0) | |
9fa3e853 | 400 | #endif |
fd6ce8f6 | 401 | |
4369415f FB |
402 | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
403 | ||
404 | #if defined(CONFIG_USER_ONLY) | |
ccbb4d44 | 405 | /* Currently it is not recommended to allocate big chunks of data in |
4369415f FB |
406 | user mode. It will change when a dedicated libc will be used */ |
407 | #define USE_STATIC_CODE_GEN_BUFFER | |
408 | #endif | |
409 | ||
410 | #ifdef USE_STATIC_CODE_GEN_BUFFER | |
411 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]; | |
412 | #endif | |
413 | ||
8fcd3692 | 414 | static void code_gen_alloc(unsigned long tb_size) |
26a5f13b | 415 | { |
4369415f FB |
416 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
417 | code_gen_buffer = static_code_gen_buffer; | |
418 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; | |
419 | map_exec(code_gen_buffer, code_gen_buffer_size); | |
420 | #else | |
26a5f13b FB |
421 | code_gen_buffer_size = tb_size; |
422 | if (code_gen_buffer_size == 0) { | |
4369415f FB |
423 | #if defined(CONFIG_USER_ONLY) |
424 | /* in user mode, phys_ram_size is not meaningful */ | |
425 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; | |
426 | #else | |
ccbb4d44 | 427 | /* XXX: needs adjustments */ |
94a6b54f | 428 | code_gen_buffer_size = (unsigned long)(ram_size / 4); |
4369415f | 429 | #endif |
26a5f13b FB |
430 | } |
431 | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE) | |
432 | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; | |
433 | /* The code gen buffer location may have constraints depending on | |
434 | the host cpu and OS */ | |
435 | #if defined(__linux__) | |
436 | { | |
437 | int flags; | |
141ac468 BS |
438 | void *start = NULL; |
439 | ||
26a5f13b FB |
440 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
441 | #if defined(__x86_64__) | |
442 | flags |= MAP_32BIT; | |
443 | /* Cannot map more than that */ | |
444 | if (code_gen_buffer_size > (800 * 1024 * 1024)) | |
445 | code_gen_buffer_size = (800 * 1024 * 1024); | |
141ac468 BS |
446 | #elif defined(__sparc_v9__) |
447 | // Map the buffer below 2G, so we can use direct calls and branches | |
448 | flags |= MAP_FIXED; | |
449 | start = (void *) 0x60000000UL; | |
450 | if (code_gen_buffer_size > (512 * 1024 * 1024)) | |
451 | code_gen_buffer_size = (512 * 1024 * 1024); | |
1cb0661e | 452 | #elif defined(__arm__) |
63d41246 | 453 | /* Map the buffer below 32M, so we can use direct calls and branches */ |
1cb0661e AZ |
454 | flags |= MAP_FIXED; |
455 | start = (void *) 0x01000000UL; | |
456 | if (code_gen_buffer_size > 16 * 1024 * 1024) | |
457 | code_gen_buffer_size = 16 * 1024 * 1024; | |
26a5f13b | 458 | #endif |
141ac468 BS |
459 | code_gen_buffer = mmap(start, code_gen_buffer_size, |
460 | PROT_WRITE | PROT_READ | PROT_EXEC, | |
26a5f13b FB |
461 | flags, -1, 0); |
462 | if (code_gen_buffer == MAP_FAILED) { | |
463 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); | |
464 | exit(1); | |
465 | } | |
466 | } | |
c5e97233 | 467 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
06e67a82 AL |
468 | { |
469 | int flags; | |
470 | void *addr = NULL; | |
471 | flags = MAP_PRIVATE | MAP_ANONYMOUS; | |
472 | #if defined(__x86_64__) | |
473 | /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume | |
474 | * 0x40000000 is free */ | |
475 | flags |= MAP_FIXED; | |
476 | addr = (void *)0x40000000; | |
477 | /* Cannot map more than that */ | |
478 | if (code_gen_buffer_size > (800 * 1024 * 1024)) | |
479 | code_gen_buffer_size = (800 * 1024 * 1024); | |
480 | #endif | |
481 | code_gen_buffer = mmap(addr, code_gen_buffer_size, | |
482 | PROT_WRITE | PROT_READ | PROT_EXEC, | |
483 | flags, -1, 0); | |
484 | if (code_gen_buffer == MAP_FAILED) { | |
485 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); | |
486 | exit(1); | |
487 | } | |
488 | } | |
26a5f13b FB |
489 | #else |
490 | code_gen_buffer = qemu_malloc(code_gen_buffer_size); | |
26a5f13b FB |
491 | map_exec(code_gen_buffer, code_gen_buffer_size); |
492 | #endif | |
4369415f | 493 | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
26a5f13b FB |
494 | map_exec(code_gen_prologue, sizeof(code_gen_prologue)); |
495 | code_gen_buffer_max_size = code_gen_buffer_size - | |
496 | code_gen_max_block_size(); | |
497 | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; | |
498 | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock)); | |
499 | } | |
500 | ||
501 | /* Must be called before using the QEMU cpus. 'tb_size' is the size | |
502 | (in bytes) allocated to the translation buffer. Zero means default | |
503 | size. */ | |
504 | void cpu_exec_init_all(unsigned long tb_size) | |
505 | { | |
26a5f13b FB |
506 | cpu_gen_init(); |
507 | code_gen_alloc(tb_size); | |
508 | code_gen_ptr = code_gen_buffer; | |
4369415f | 509 | page_init(); |
e2eef170 | 510 | #if !defined(CONFIG_USER_ONLY) |
26a5f13b | 511 | io_mem_init(); |
e2eef170 | 512 | #endif |
26a5f13b FB |
513 | } |
514 | ||
9656f324 PB |
515 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
516 | ||
517 | #define CPU_COMMON_SAVE_VERSION 1 | |
518 | ||
519 | static void cpu_common_save(QEMUFile *f, void *opaque) | |
520 | { | |
521 | CPUState *env = opaque; | |
522 | ||
b0a46a33 JK |
523 | cpu_synchronize_state(env, 0); |
524 | ||
9656f324 PB |
525 | qemu_put_be32s(f, &env->halted); |
526 | qemu_put_be32s(f, &env->interrupt_request); | |
527 | } | |
528 | ||
529 | static int cpu_common_load(QEMUFile *f, void *opaque, int version_id) | |
530 | { | |
531 | CPUState *env = opaque; | |
532 | ||
533 | if (version_id != CPU_COMMON_SAVE_VERSION) | |
534 | return -EINVAL; | |
535 | ||
536 | qemu_get_be32s(f, &env->halted); | |
75f482ae | 537 | qemu_get_be32s(f, &env->interrupt_request); |
3098dba0 AJ |
538 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
539 | version_id is increased. */ | |
540 | env->interrupt_request &= ~0x01; | |
9656f324 | 541 | tlb_flush(env, 1); |
b0a46a33 | 542 | cpu_synchronize_state(env, 1); |
9656f324 PB |
543 | |
544 | return 0; | |
545 | } | |
546 | #endif | |
547 | ||
950f1472 GC |
548 | CPUState *qemu_get_cpu(int cpu) |
549 | { | |
550 | CPUState *env = first_cpu; | |
551 | ||
552 | while (env) { | |
553 | if (env->cpu_index == cpu) | |
554 | break; | |
555 | env = env->next_cpu; | |
556 | } | |
557 | ||
558 | return env; | |
559 | } | |
560 | ||
6a00d601 | 561 | void cpu_exec_init(CPUState *env) |
fd6ce8f6 | 562 | { |
6a00d601 FB |
563 | CPUState **penv; |
564 | int cpu_index; | |
565 | ||
c2764719 PB |
566 | #if defined(CONFIG_USER_ONLY) |
567 | cpu_list_lock(); | |
568 | #endif | |
6a00d601 FB |
569 | env->next_cpu = NULL; |
570 | penv = &first_cpu; | |
571 | cpu_index = 0; | |
572 | while (*penv != NULL) { | |
1e9fa730 | 573 | penv = &(*penv)->next_cpu; |
6a00d601 FB |
574 | cpu_index++; |
575 | } | |
576 | env->cpu_index = cpu_index; | |
268a362c | 577 | env->numa_node = 0; |
c0ce998e AL |
578 | TAILQ_INIT(&env->breakpoints); |
579 | TAILQ_INIT(&env->watchpoints); | |
6a00d601 | 580 | *penv = env; |
c2764719 PB |
581 | #if defined(CONFIG_USER_ONLY) |
582 | cpu_list_unlock(); | |
583 | #endif | |
b3c7724c | 584 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
9656f324 PB |
585 | register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION, |
586 | cpu_common_save, cpu_common_load, env); | |
b3c7724c PB |
587 | register_savevm("cpu", cpu_index, CPU_SAVE_VERSION, |
588 | cpu_save, cpu_load, env); | |
589 | #endif | |
fd6ce8f6 FB |
590 | } |
591 | ||
9fa3e853 FB |
592 | static inline void invalidate_page_bitmap(PageDesc *p) |
593 | { | |
594 | if (p->code_bitmap) { | |
59817ccb | 595 | qemu_free(p->code_bitmap); |
9fa3e853 FB |
596 | p->code_bitmap = NULL; |
597 | } | |
598 | p->code_write_count = 0; | |
599 | } | |
600 | ||
fd6ce8f6 FB |
601 | /* set to NULL all the 'first_tb' fields in all PageDescs */ |
602 | static void page_flush_tb(void) | |
603 | { | |
604 | int i, j; | |
605 | PageDesc *p; | |
606 | ||
607 | for(i = 0; i < L1_SIZE; i++) { | |
608 | p = l1_map[i]; | |
609 | if (p) { | |
9fa3e853 FB |
610 | for(j = 0; j < L2_SIZE; j++) { |
611 | p->first_tb = NULL; | |
612 | invalidate_page_bitmap(p); | |
613 | p++; | |
614 | } | |
fd6ce8f6 FB |
615 | } |
616 | } | |
617 | } | |
618 | ||
619 | /* flush all the translation blocks */ | |
d4e8164f | 620 | /* XXX: tb_flush is currently not thread safe */ |
6a00d601 | 621 | void tb_flush(CPUState *env1) |
fd6ce8f6 | 622 | { |
6a00d601 | 623 | CPUState *env; |
0124311e | 624 | #if defined(DEBUG_FLUSH) |
ab3d1727 BS |
625 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
626 | (unsigned long)(code_gen_ptr - code_gen_buffer), | |
627 | nb_tbs, nb_tbs > 0 ? | |
628 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); | |
fd6ce8f6 | 629 | #endif |
26a5f13b | 630 | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
a208e54a PB |
631 | cpu_abort(env1, "Internal error: code buffer overflow\n"); |
632 | ||
fd6ce8f6 | 633 | nb_tbs = 0; |
3b46e624 | 634 | |
6a00d601 FB |
635 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
636 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); | |
637 | } | |
9fa3e853 | 638 | |
8a8a608f | 639 | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
fd6ce8f6 | 640 | page_flush_tb(); |
9fa3e853 | 641 | |
fd6ce8f6 | 642 | code_gen_ptr = code_gen_buffer; |
d4e8164f FB |
643 | /* XXX: flush processor icache at this point if cache flush is |
644 | expensive */ | |
e3db7226 | 645 | tb_flush_count++; |
fd6ce8f6 FB |
646 | } |
647 | ||
648 | #ifdef DEBUG_TB_CHECK | |
649 | ||
bc98a7ef | 650 | static void tb_invalidate_check(target_ulong address) |
fd6ce8f6 FB |
651 | { |
652 | TranslationBlock *tb; | |
653 | int i; | |
654 | address &= TARGET_PAGE_MASK; | |
99773bd4 PB |
655 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
656 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { | |
fd6ce8f6 FB |
657 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || |
658 | address >= tb->pc + tb->size)) { | |
659 | printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n", | |
99773bd4 | 660 | address, (long)tb->pc, tb->size); |
fd6ce8f6 FB |
661 | } |
662 | } | |
663 | } | |
664 | } | |
665 | ||
666 | /* verify that all the pages have correct rights for code */ | |
667 | static void tb_page_check(void) | |
668 | { | |
669 | TranslationBlock *tb; | |
670 | int i, flags1, flags2; | |
3b46e624 | 671 | |
99773bd4 PB |
672 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
673 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { | |
fd6ce8f6 FB |
674 | flags1 = page_get_flags(tb->pc); |
675 | flags2 = page_get_flags(tb->pc + tb->size - 1); | |
676 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { | |
677 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", | |
99773bd4 | 678 | (long)tb->pc, tb->size, flags1, flags2); |
fd6ce8f6 FB |
679 | } |
680 | } | |
681 | } | |
682 | } | |
683 | ||
bdaf78e0 | 684 | static void tb_jmp_check(TranslationBlock *tb) |
d4e8164f FB |
685 | { |
686 | TranslationBlock *tb1; | |
687 | unsigned int n1; | |
688 | ||
689 | /* suppress any remaining jumps to this TB */ | |
690 | tb1 = tb->jmp_first; | |
691 | for(;;) { | |
692 | n1 = (long)tb1 & 3; | |
693 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
694 | if (n1 == 2) | |
695 | break; | |
696 | tb1 = tb1->jmp_next[n1]; | |
697 | } | |
698 | /* check end of list */ | |
699 | if (tb1 != tb) { | |
700 | printf("ERROR: jmp_list from 0x%08lx\n", (long)tb); | |
701 | } | |
702 | } | |
703 | ||
fd6ce8f6 FB |
704 | #endif |
705 | ||
706 | /* invalidate one TB */ | |
707 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, | |
708 | int next_offset) | |
709 | { | |
710 | TranslationBlock *tb1; | |
711 | for(;;) { | |
712 | tb1 = *ptb; | |
713 | if (tb1 == tb) { | |
714 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); | |
715 | break; | |
716 | } | |
717 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); | |
718 | } | |
719 | } | |
720 | ||
9fa3e853 FB |
721 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
722 | { | |
723 | TranslationBlock *tb1; | |
724 | unsigned int n1; | |
725 | ||
726 | for(;;) { | |
727 | tb1 = *ptb; | |
728 | n1 = (long)tb1 & 3; | |
729 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
730 | if (tb1 == tb) { | |
731 | *ptb = tb1->page_next[n1]; | |
732 | break; | |
733 | } | |
734 | ptb = &tb1->page_next[n1]; | |
735 | } | |
736 | } | |
737 | ||
d4e8164f FB |
738 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
739 | { | |
740 | TranslationBlock *tb1, **ptb; | |
741 | unsigned int n1; | |
742 | ||
743 | ptb = &tb->jmp_next[n]; | |
744 | tb1 = *ptb; | |
745 | if (tb1) { | |
746 | /* find tb(n) in circular list */ | |
747 | for(;;) { | |
748 | tb1 = *ptb; | |
749 | n1 = (long)tb1 & 3; | |
750 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
751 | if (n1 == n && tb1 == tb) | |
752 | break; | |
753 | if (n1 == 2) { | |
754 | ptb = &tb1->jmp_first; | |
755 | } else { | |
756 | ptb = &tb1->jmp_next[n1]; | |
757 | } | |
758 | } | |
759 | /* now we can suppress tb(n) from the list */ | |
760 | *ptb = tb->jmp_next[n]; | |
761 | ||
762 | tb->jmp_next[n] = NULL; | |
763 | } | |
764 | } | |
765 | ||
766 | /* reset the jump entry 'n' of a TB so that it is not chained to | |
767 | another TB */ | |
768 | static inline void tb_reset_jump(TranslationBlock *tb, int n) | |
769 | { | |
770 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); | |
771 | } | |
772 | ||
2e70f6ef | 773 | void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr) |
fd6ce8f6 | 774 | { |
6a00d601 | 775 | CPUState *env; |
8a40a180 | 776 | PageDesc *p; |
d4e8164f | 777 | unsigned int h, n1; |
00f82b8a | 778 | target_phys_addr_t phys_pc; |
8a40a180 | 779 | TranslationBlock *tb1, *tb2; |
3b46e624 | 780 | |
8a40a180 FB |
781 | /* remove the TB from the hash list */ |
782 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
783 | h = tb_phys_hash_func(phys_pc); | |
5fafdf24 | 784 | tb_remove(&tb_phys_hash[h], tb, |
8a40a180 FB |
785 | offsetof(TranslationBlock, phys_hash_next)); |
786 | ||
787 | /* remove the TB from the page list */ | |
788 | if (tb->page_addr[0] != page_addr) { | |
789 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); | |
790 | tb_page_remove(&p->first_tb, tb); | |
791 | invalidate_page_bitmap(p); | |
792 | } | |
793 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { | |
794 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); | |
795 | tb_page_remove(&p->first_tb, tb); | |
796 | invalidate_page_bitmap(p); | |
797 | } | |
798 | ||
36bdbe54 | 799 | tb_invalidated_flag = 1; |
59817ccb | 800 | |
fd6ce8f6 | 801 | /* remove the TB from the hash list */ |
8a40a180 | 802 | h = tb_jmp_cache_hash_func(tb->pc); |
6a00d601 FB |
803 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
804 | if (env->tb_jmp_cache[h] == tb) | |
805 | env->tb_jmp_cache[h] = NULL; | |
806 | } | |
d4e8164f FB |
807 | |
808 | /* suppress this TB from the two jump lists */ | |
809 | tb_jmp_remove(tb, 0); | |
810 | tb_jmp_remove(tb, 1); | |
811 | ||
812 | /* suppress any remaining jumps to this TB */ | |
813 | tb1 = tb->jmp_first; | |
814 | for(;;) { | |
815 | n1 = (long)tb1 & 3; | |
816 | if (n1 == 2) | |
817 | break; | |
818 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
819 | tb2 = tb1->jmp_next[n1]; | |
820 | tb_reset_jump(tb1, n1); | |
821 | tb1->jmp_next[n1] = NULL; | |
822 | tb1 = tb2; | |
823 | } | |
824 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ | |
9fa3e853 | 825 | |
e3db7226 | 826 | tb_phys_invalidate_count++; |
9fa3e853 FB |
827 | } |
828 | ||
829 | static inline void set_bits(uint8_t *tab, int start, int len) | |
830 | { | |
831 | int end, mask, end1; | |
832 | ||
833 | end = start + len; | |
834 | tab += start >> 3; | |
835 | mask = 0xff << (start & 7); | |
836 | if ((start & ~7) == (end & ~7)) { | |
837 | if (start < end) { | |
838 | mask &= ~(0xff << (end & 7)); | |
839 | *tab |= mask; | |
840 | } | |
841 | } else { | |
842 | *tab++ |= mask; | |
843 | start = (start + 8) & ~7; | |
844 | end1 = end & ~7; | |
845 | while (start < end1) { | |
846 | *tab++ = 0xff; | |
847 | start += 8; | |
848 | } | |
849 | if (start < end) { | |
850 | mask = ~(0xff << (end & 7)); | |
851 | *tab |= mask; | |
852 | } | |
853 | } | |
854 | } | |
855 | ||
856 | static void build_page_bitmap(PageDesc *p) | |
857 | { | |
858 | int n, tb_start, tb_end; | |
859 | TranslationBlock *tb; | |
3b46e624 | 860 | |
b2a7081a | 861 | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8); |
9fa3e853 FB |
862 | |
863 | tb = p->first_tb; | |
864 | while (tb != NULL) { | |
865 | n = (long)tb & 3; | |
866 | tb = (TranslationBlock *)((long)tb & ~3); | |
867 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
868 | if (n == 0) { | |
869 | /* NOTE: tb_end may be after the end of the page, but | |
870 | it is not a problem */ | |
871 | tb_start = tb->pc & ~TARGET_PAGE_MASK; | |
872 | tb_end = tb_start + tb->size; | |
873 | if (tb_end > TARGET_PAGE_SIZE) | |
874 | tb_end = TARGET_PAGE_SIZE; | |
875 | } else { | |
876 | tb_start = 0; | |
877 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
878 | } | |
879 | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); | |
880 | tb = tb->page_next[n]; | |
881 | } | |
882 | } | |
883 | ||
2e70f6ef PB |
884 | TranslationBlock *tb_gen_code(CPUState *env, |
885 | target_ulong pc, target_ulong cs_base, | |
886 | int flags, int cflags) | |
d720b93d FB |
887 | { |
888 | TranslationBlock *tb; | |
889 | uint8_t *tc_ptr; | |
890 | target_ulong phys_pc, phys_page2, virt_page2; | |
891 | int code_gen_size; | |
892 | ||
c27004ec FB |
893 | phys_pc = get_phys_addr_code(env, pc); |
894 | tb = tb_alloc(pc); | |
d720b93d FB |
895 | if (!tb) { |
896 | /* flush must be done */ | |
897 | tb_flush(env); | |
898 | /* cannot fail at this point */ | |
c27004ec | 899 | tb = tb_alloc(pc); |
2e70f6ef PB |
900 | /* Don't forget to invalidate previous TB info. */ |
901 | tb_invalidated_flag = 1; | |
d720b93d FB |
902 | } |
903 | tc_ptr = code_gen_ptr; | |
904 | tb->tc_ptr = tc_ptr; | |
905 | tb->cs_base = cs_base; | |
906 | tb->flags = flags; | |
907 | tb->cflags = cflags; | |
d07bde88 | 908 | cpu_gen_code(env, tb, &code_gen_size); |
d720b93d | 909 | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
3b46e624 | 910 | |
d720b93d | 911 | /* check next page if needed */ |
c27004ec | 912 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
d720b93d | 913 | phys_page2 = -1; |
c27004ec | 914 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
d720b93d FB |
915 | phys_page2 = get_phys_addr_code(env, virt_page2); |
916 | } | |
917 | tb_link_phys(tb, phys_pc, phys_page2); | |
2e70f6ef | 918 | return tb; |
d720b93d | 919 | } |
3b46e624 | 920 | |
9fa3e853 FB |
921 | /* invalidate all TBs which intersect with the target physical page |
922 | starting in range [start;end[. NOTE: start and end must refer to | |
d720b93d FB |
923 | the same physical page. 'is_cpu_write_access' should be true if called |
924 | from a real cpu write access: the virtual CPU will exit the current | |
925 | TB if code is modified inside this TB. */ | |
00f82b8a | 926 | void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end, |
d720b93d FB |
927 | int is_cpu_write_access) |
928 | { | |
6b917547 | 929 | TranslationBlock *tb, *tb_next, *saved_tb; |
d720b93d | 930 | CPUState *env = cpu_single_env; |
9fa3e853 | 931 | target_ulong tb_start, tb_end; |
6b917547 AL |
932 | PageDesc *p; |
933 | int n; | |
934 | #ifdef TARGET_HAS_PRECISE_SMC | |
935 | int current_tb_not_found = is_cpu_write_access; | |
936 | TranslationBlock *current_tb = NULL; | |
937 | int current_tb_modified = 0; | |
938 | target_ulong current_pc = 0; | |
939 | target_ulong current_cs_base = 0; | |
940 | int current_flags = 0; | |
941 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
9fa3e853 FB |
942 | |
943 | p = page_find(start >> TARGET_PAGE_BITS); | |
5fafdf24 | 944 | if (!p) |
9fa3e853 | 945 | return; |
5fafdf24 | 946 | if (!p->code_bitmap && |
d720b93d FB |
947 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
948 | is_cpu_write_access) { | |
9fa3e853 FB |
949 | /* build code bitmap */ |
950 | build_page_bitmap(p); | |
951 | } | |
952 | ||
953 | /* we remove all the TBs in the range [start, end[ */ | |
954 | /* XXX: see if in some cases it could be faster to invalidate all the code */ | |
955 | tb = p->first_tb; | |
956 | while (tb != NULL) { | |
957 | n = (long)tb & 3; | |
958 | tb = (TranslationBlock *)((long)tb & ~3); | |
959 | tb_next = tb->page_next[n]; | |
960 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
961 | if (n == 0) { | |
962 | /* NOTE: tb_end may be after the end of the page, but | |
963 | it is not a problem */ | |
964 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
965 | tb_end = tb_start + tb->size; | |
966 | } else { | |
967 | tb_start = tb->page_addr[1]; | |
968 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
969 | } | |
970 | if (!(tb_end <= start || tb_start >= end)) { | |
d720b93d FB |
971 | #ifdef TARGET_HAS_PRECISE_SMC |
972 | if (current_tb_not_found) { | |
973 | current_tb_not_found = 0; | |
974 | current_tb = NULL; | |
2e70f6ef | 975 | if (env->mem_io_pc) { |
d720b93d | 976 | /* now we have a real cpu fault */ |
2e70f6ef | 977 | current_tb = tb_find_pc(env->mem_io_pc); |
d720b93d FB |
978 | } |
979 | } | |
980 | if (current_tb == tb && | |
2e70f6ef | 981 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
d720b93d FB |
982 | /* If we are modifying the current TB, we must stop |
983 | its execution. We could be more precise by checking | |
984 | that the modification is after the current PC, but it | |
985 | would require a specialized function to partially | |
986 | restore the CPU state */ | |
3b46e624 | 987 | |
d720b93d | 988 | current_tb_modified = 1; |
5fafdf24 | 989 | cpu_restore_state(current_tb, env, |
2e70f6ef | 990 | env->mem_io_pc, NULL); |
6b917547 AL |
991 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
992 | ¤t_flags); | |
d720b93d FB |
993 | } |
994 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
6f5a9f7e FB |
995 | /* we need to do that to handle the case where a signal |
996 | occurs while doing tb_phys_invalidate() */ | |
997 | saved_tb = NULL; | |
998 | if (env) { | |
999 | saved_tb = env->current_tb; | |
1000 | env->current_tb = NULL; | |
1001 | } | |
9fa3e853 | 1002 | tb_phys_invalidate(tb, -1); |
6f5a9f7e FB |
1003 | if (env) { |
1004 | env->current_tb = saved_tb; | |
1005 | if (env->interrupt_request && env->current_tb) | |
1006 | cpu_interrupt(env, env->interrupt_request); | |
1007 | } | |
9fa3e853 FB |
1008 | } |
1009 | tb = tb_next; | |
1010 | } | |
1011 | #if !defined(CONFIG_USER_ONLY) | |
1012 | /* if no code remaining, no need to continue to use slow writes */ | |
1013 | if (!p->first_tb) { | |
1014 | invalidate_page_bitmap(p); | |
d720b93d | 1015 | if (is_cpu_write_access) { |
2e70f6ef | 1016 | tlb_unprotect_code_phys(env, start, env->mem_io_vaddr); |
d720b93d FB |
1017 | } |
1018 | } | |
1019 | #endif | |
1020 | #ifdef TARGET_HAS_PRECISE_SMC | |
1021 | if (current_tb_modified) { | |
1022 | /* we generate a block containing just the instruction | |
1023 | modifying the memory. It will ensure that it cannot modify | |
1024 | itself */ | |
ea1c1802 | 1025 | env->current_tb = NULL; |
2e70f6ef | 1026 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
d720b93d | 1027 | cpu_resume_from_signal(env, NULL); |
9fa3e853 | 1028 | } |
fd6ce8f6 | 1029 | #endif |
9fa3e853 | 1030 | } |
fd6ce8f6 | 1031 | |
9fa3e853 | 1032 | /* len must be <= 8 and start must be a multiple of len */ |
00f82b8a | 1033 | static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len) |
9fa3e853 FB |
1034 | { |
1035 | PageDesc *p; | |
1036 | int offset, b; | |
59817ccb | 1037 | #if 0 |
a4193c8a | 1038 | if (1) { |
93fcfe39 AL |
1039 | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n", |
1040 | cpu_single_env->mem_io_vaddr, len, | |
1041 | cpu_single_env->eip, | |
1042 | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base); | |
59817ccb FB |
1043 | } |
1044 | #endif | |
9fa3e853 | 1045 | p = page_find(start >> TARGET_PAGE_BITS); |
5fafdf24 | 1046 | if (!p) |
9fa3e853 FB |
1047 | return; |
1048 | if (p->code_bitmap) { | |
1049 | offset = start & ~TARGET_PAGE_MASK; | |
1050 | b = p->code_bitmap[offset >> 3] >> (offset & 7); | |
1051 | if (b & ((1 << len) - 1)) | |
1052 | goto do_invalidate; | |
1053 | } else { | |
1054 | do_invalidate: | |
d720b93d | 1055 | tb_invalidate_phys_page_range(start, start + len, 1); |
9fa3e853 FB |
1056 | } |
1057 | } | |
1058 | ||
9fa3e853 | 1059 | #if !defined(CONFIG_SOFTMMU) |
00f82b8a | 1060 | static void tb_invalidate_phys_page(target_phys_addr_t addr, |
d720b93d | 1061 | unsigned long pc, void *puc) |
9fa3e853 | 1062 | { |
6b917547 | 1063 | TranslationBlock *tb; |
9fa3e853 | 1064 | PageDesc *p; |
6b917547 | 1065 | int n; |
d720b93d | 1066 | #ifdef TARGET_HAS_PRECISE_SMC |
6b917547 | 1067 | TranslationBlock *current_tb = NULL; |
d720b93d | 1068 | CPUState *env = cpu_single_env; |
6b917547 AL |
1069 | int current_tb_modified = 0; |
1070 | target_ulong current_pc = 0; | |
1071 | target_ulong current_cs_base = 0; | |
1072 | int current_flags = 0; | |
d720b93d | 1073 | #endif |
9fa3e853 FB |
1074 | |
1075 | addr &= TARGET_PAGE_MASK; | |
1076 | p = page_find(addr >> TARGET_PAGE_BITS); | |
5fafdf24 | 1077 | if (!p) |
9fa3e853 FB |
1078 | return; |
1079 | tb = p->first_tb; | |
d720b93d FB |
1080 | #ifdef TARGET_HAS_PRECISE_SMC |
1081 | if (tb && pc != 0) { | |
1082 | current_tb = tb_find_pc(pc); | |
1083 | } | |
1084 | #endif | |
9fa3e853 FB |
1085 | while (tb != NULL) { |
1086 | n = (long)tb & 3; | |
1087 | tb = (TranslationBlock *)((long)tb & ~3); | |
d720b93d FB |
1088 | #ifdef TARGET_HAS_PRECISE_SMC |
1089 | if (current_tb == tb && | |
2e70f6ef | 1090 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
d720b93d FB |
1091 | /* If we are modifying the current TB, we must stop |
1092 | its execution. We could be more precise by checking | |
1093 | that the modification is after the current PC, but it | |
1094 | would require a specialized function to partially | |
1095 | restore the CPU state */ | |
3b46e624 | 1096 | |
d720b93d FB |
1097 | current_tb_modified = 1; |
1098 | cpu_restore_state(current_tb, env, pc, puc); | |
6b917547 AL |
1099 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1100 | ¤t_flags); | |
d720b93d FB |
1101 | } |
1102 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
9fa3e853 FB |
1103 | tb_phys_invalidate(tb, addr); |
1104 | tb = tb->page_next[n]; | |
1105 | } | |
fd6ce8f6 | 1106 | p->first_tb = NULL; |
d720b93d FB |
1107 | #ifdef TARGET_HAS_PRECISE_SMC |
1108 | if (current_tb_modified) { | |
1109 | /* we generate a block containing just the instruction | |
1110 | modifying the memory. It will ensure that it cannot modify | |
1111 | itself */ | |
ea1c1802 | 1112 | env->current_tb = NULL; |
2e70f6ef | 1113 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
d720b93d FB |
1114 | cpu_resume_from_signal(env, puc); |
1115 | } | |
1116 | #endif | |
fd6ce8f6 | 1117 | } |
9fa3e853 | 1118 | #endif |
fd6ce8f6 FB |
1119 | |
1120 | /* add the tb in the target page and protect it if necessary */ | |
5fafdf24 | 1121 | static inline void tb_alloc_page(TranslationBlock *tb, |
53a5960a | 1122 | unsigned int n, target_ulong page_addr) |
fd6ce8f6 FB |
1123 | { |
1124 | PageDesc *p; | |
9fa3e853 FB |
1125 | TranslationBlock *last_first_tb; |
1126 | ||
1127 | tb->page_addr[n] = page_addr; | |
3a7d929e | 1128 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS); |
9fa3e853 FB |
1129 | tb->page_next[n] = p->first_tb; |
1130 | last_first_tb = p->first_tb; | |
1131 | p->first_tb = (TranslationBlock *)((long)tb | n); | |
1132 | invalidate_page_bitmap(p); | |
fd6ce8f6 | 1133 | |
107db443 | 1134 | #if defined(TARGET_HAS_SMC) || 1 |
d720b93d | 1135 | |
9fa3e853 | 1136 | #if defined(CONFIG_USER_ONLY) |
fd6ce8f6 | 1137 | if (p->flags & PAGE_WRITE) { |
53a5960a PB |
1138 | target_ulong addr; |
1139 | PageDesc *p2; | |
9fa3e853 FB |
1140 | int prot; |
1141 | ||
fd6ce8f6 FB |
1142 | /* force the host page as non writable (writes will have a |
1143 | page fault + mprotect overhead) */ | |
53a5960a | 1144 | page_addr &= qemu_host_page_mask; |
fd6ce8f6 | 1145 | prot = 0; |
53a5960a PB |
1146 | for(addr = page_addr; addr < page_addr + qemu_host_page_size; |
1147 | addr += TARGET_PAGE_SIZE) { | |
1148 | ||
1149 | p2 = page_find (addr >> TARGET_PAGE_BITS); | |
1150 | if (!p2) | |
1151 | continue; | |
1152 | prot |= p2->flags; | |
1153 | p2->flags &= ~PAGE_WRITE; | |
1154 | page_get_flags(addr); | |
1155 | } | |
5fafdf24 | 1156 | mprotect(g2h(page_addr), qemu_host_page_size, |
fd6ce8f6 FB |
1157 | (prot & PAGE_BITS) & ~PAGE_WRITE); |
1158 | #ifdef DEBUG_TB_INVALIDATE | |
ab3d1727 | 1159 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
53a5960a | 1160 | page_addr); |
fd6ce8f6 | 1161 | #endif |
fd6ce8f6 | 1162 | } |
9fa3e853 FB |
1163 | #else |
1164 | /* if some code is already present, then the pages are already | |
1165 | protected. So we handle the case where only the first TB is | |
1166 | allocated in a physical page */ | |
1167 | if (!last_first_tb) { | |
6a00d601 | 1168 | tlb_protect_code(page_addr); |
9fa3e853 FB |
1169 | } |
1170 | #endif | |
d720b93d FB |
1171 | |
1172 | #endif /* TARGET_HAS_SMC */ | |
fd6ce8f6 FB |
1173 | } |
1174 | ||
1175 | /* Allocate a new translation block. Flush the translation buffer if | |
1176 | too many translation blocks or too much generated code. */ | |
c27004ec | 1177 | TranslationBlock *tb_alloc(target_ulong pc) |
fd6ce8f6 FB |
1178 | { |
1179 | TranslationBlock *tb; | |
fd6ce8f6 | 1180 | |
26a5f13b FB |
1181 | if (nb_tbs >= code_gen_max_blocks || |
1182 | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) | |
d4e8164f | 1183 | return NULL; |
fd6ce8f6 FB |
1184 | tb = &tbs[nb_tbs++]; |
1185 | tb->pc = pc; | |
b448f2f3 | 1186 | tb->cflags = 0; |
d4e8164f FB |
1187 | return tb; |
1188 | } | |
1189 | ||
2e70f6ef PB |
1190 | void tb_free(TranslationBlock *tb) |
1191 | { | |
bf20dc07 | 1192 | /* In practice this is mostly used for single use temporary TB |
2e70f6ef PB |
1193 | Ignore the hard cases and just back up if this TB happens to |
1194 | be the last one generated. */ | |
1195 | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { | |
1196 | code_gen_ptr = tb->tc_ptr; | |
1197 | nb_tbs--; | |
1198 | } | |
1199 | } | |
1200 | ||
9fa3e853 FB |
1201 | /* add a new TB and link it to the physical page tables. phys_page2 is |
1202 | (-1) to indicate that only one page contains the TB. */ | |
5fafdf24 | 1203 | void tb_link_phys(TranslationBlock *tb, |
9fa3e853 | 1204 | target_ulong phys_pc, target_ulong phys_page2) |
d4e8164f | 1205 | { |
9fa3e853 FB |
1206 | unsigned int h; |
1207 | TranslationBlock **ptb; | |
1208 | ||
c8a706fe PB |
1209 | /* Grab the mmap lock to stop another thread invalidating this TB |
1210 | before we are done. */ | |
1211 | mmap_lock(); | |
9fa3e853 FB |
1212 | /* add in the physical hash table */ |
1213 | h = tb_phys_hash_func(phys_pc); | |
1214 | ptb = &tb_phys_hash[h]; | |
1215 | tb->phys_hash_next = *ptb; | |
1216 | *ptb = tb; | |
fd6ce8f6 FB |
1217 | |
1218 | /* add in the page list */ | |
9fa3e853 FB |
1219 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); |
1220 | if (phys_page2 != -1) | |
1221 | tb_alloc_page(tb, 1, phys_page2); | |
1222 | else | |
1223 | tb->page_addr[1] = -1; | |
9fa3e853 | 1224 | |
d4e8164f FB |
1225 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
1226 | tb->jmp_next[0] = NULL; | |
1227 | tb->jmp_next[1] = NULL; | |
1228 | ||
1229 | /* init original jump addresses */ | |
1230 | if (tb->tb_next_offset[0] != 0xffff) | |
1231 | tb_reset_jump(tb, 0); | |
1232 | if (tb->tb_next_offset[1] != 0xffff) | |
1233 | tb_reset_jump(tb, 1); | |
8a40a180 FB |
1234 | |
1235 | #ifdef DEBUG_TB_CHECK | |
1236 | tb_page_check(); | |
1237 | #endif | |
c8a706fe | 1238 | mmap_unlock(); |
fd6ce8f6 FB |
1239 | } |
1240 | ||
9fa3e853 FB |
1241 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
1242 | tb[1].tc_ptr. Return NULL if not found */ | |
1243 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) | |
fd6ce8f6 | 1244 | { |
9fa3e853 FB |
1245 | int m_min, m_max, m; |
1246 | unsigned long v; | |
1247 | TranslationBlock *tb; | |
a513fe19 FB |
1248 | |
1249 | if (nb_tbs <= 0) | |
1250 | return NULL; | |
1251 | if (tc_ptr < (unsigned long)code_gen_buffer || | |
1252 | tc_ptr >= (unsigned long)code_gen_ptr) | |
1253 | return NULL; | |
1254 | /* binary search (cf Knuth) */ | |
1255 | m_min = 0; | |
1256 | m_max = nb_tbs - 1; | |
1257 | while (m_min <= m_max) { | |
1258 | m = (m_min + m_max) >> 1; | |
1259 | tb = &tbs[m]; | |
1260 | v = (unsigned long)tb->tc_ptr; | |
1261 | if (v == tc_ptr) | |
1262 | return tb; | |
1263 | else if (tc_ptr < v) { | |
1264 | m_max = m - 1; | |
1265 | } else { | |
1266 | m_min = m + 1; | |
1267 | } | |
5fafdf24 | 1268 | } |
a513fe19 FB |
1269 | return &tbs[m_max]; |
1270 | } | |
7501267e | 1271 | |
ea041c0e FB |
1272 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
1273 | ||
1274 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) | |
1275 | { | |
1276 | TranslationBlock *tb1, *tb_next, **ptb; | |
1277 | unsigned int n1; | |
1278 | ||
1279 | tb1 = tb->jmp_next[n]; | |
1280 | if (tb1 != NULL) { | |
1281 | /* find head of list */ | |
1282 | for(;;) { | |
1283 | n1 = (long)tb1 & 3; | |
1284 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
1285 | if (n1 == 2) | |
1286 | break; | |
1287 | tb1 = tb1->jmp_next[n1]; | |
1288 | } | |
1289 | /* we are now sure now that tb jumps to tb1 */ | |
1290 | tb_next = tb1; | |
1291 | ||
1292 | /* remove tb from the jmp_first list */ | |
1293 | ptb = &tb_next->jmp_first; | |
1294 | for(;;) { | |
1295 | tb1 = *ptb; | |
1296 | n1 = (long)tb1 & 3; | |
1297 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
1298 | if (n1 == n && tb1 == tb) | |
1299 | break; | |
1300 | ptb = &tb1->jmp_next[n1]; | |
1301 | } | |
1302 | *ptb = tb->jmp_next[n]; | |
1303 | tb->jmp_next[n] = NULL; | |
3b46e624 | 1304 | |
ea041c0e FB |
1305 | /* suppress the jump to next tb in generated code */ |
1306 | tb_reset_jump(tb, n); | |
1307 | ||
0124311e | 1308 | /* suppress jumps in the tb on which we could have jumped */ |
ea041c0e FB |
1309 | tb_reset_jump_recursive(tb_next); |
1310 | } | |
1311 | } | |
1312 | ||
1313 | static void tb_reset_jump_recursive(TranslationBlock *tb) | |
1314 | { | |
1315 | tb_reset_jump_recursive2(tb, 0); | |
1316 | tb_reset_jump_recursive2(tb, 1); | |
1317 | } | |
1318 | ||
1fddef4b | 1319 | #if defined(TARGET_HAS_ICE) |
d720b93d FB |
1320 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
1321 | { | |
9b3c35e0 JM |
1322 | target_phys_addr_t addr; |
1323 | target_ulong pd; | |
c2f07f81 PB |
1324 | ram_addr_t ram_addr; |
1325 | PhysPageDesc *p; | |
d720b93d | 1326 | |
c2f07f81 PB |
1327 | addr = cpu_get_phys_page_debug(env, pc); |
1328 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
1329 | if (!p) { | |
1330 | pd = IO_MEM_UNASSIGNED; | |
1331 | } else { | |
1332 | pd = p->phys_offset; | |
1333 | } | |
1334 | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); | |
706cd4b5 | 1335 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
d720b93d | 1336 | } |
c27004ec | 1337 | #endif |
d720b93d | 1338 | |
6658ffb8 | 1339 | /* Add a watchpoint. */ |
a1d1bb31 AL |
1340 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
1341 | int flags, CPUWatchpoint **watchpoint) | |
6658ffb8 | 1342 | { |
b4051334 | 1343 | target_ulong len_mask = ~(len - 1); |
c0ce998e | 1344 | CPUWatchpoint *wp; |
6658ffb8 | 1345 | |
b4051334 AL |
1346 | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */ |
1347 | if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) { | |
1348 | fprintf(stderr, "qemu: tried to set invalid watchpoint at " | |
1349 | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); | |
1350 | return -EINVAL; | |
1351 | } | |
a1d1bb31 | 1352 | wp = qemu_malloc(sizeof(*wp)); |
a1d1bb31 AL |
1353 | |
1354 | wp->vaddr = addr; | |
b4051334 | 1355 | wp->len_mask = len_mask; |
a1d1bb31 AL |
1356 | wp->flags = flags; |
1357 | ||
2dc9f411 | 1358 | /* keep all GDB-injected watchpoints in front */ |
c0ce998e AL |
1359 | if (flags & BP_GDB) |
1360 | TAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); | |
1361 | else | |
1362 | TAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); | |
6658ffb8 | 1363 | |
6658ffb8 | 1364 | tlb_flush_page(env, addr); |
a1d1bb31 AL |
1365 | |
1366 | if (watchpoint) | |
1367 | *watchpoint = wp; | |
1368 | return 0; | |
6658ffb8 PB |
1369 | } |
1370 | ||
a1d1bb31 AL |
1371 | /* Remove a specific watchpoint. */ |
1372 | int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len, | |
1373 | int flags) | |
6658ffb8 | 1374 | { |
b4051334 | 1375 | target_ulong len_mask = ~(len - 1); |
a1d1bb31 | 1376 | CPUWatchpoint *wp; |
6658ffb8 | 1377 | |
c0ce998e | 1378 | TAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 | 1379 | if (addr == wp->vaddr && len_mask == wp->len_mask |
6e140f28 | 1380 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
a1d1bb31 | 1381 | cpu_watchpoint_remove_by_ref(env, wp); |
6658ffb8 PB |
1382 | return 0; |
1383 | } | |
1384 | } | |
a1d1bb31 | 1385 | return -ENOENT; |
6658ffb8 PB |
1386 | } |
1387 | ||
a1d1bb31 AL |
1388 | /* Remove a specific watchpoint by reference. */ |
1389 | void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint) | |
1390 | { | |
c0ce998e | 1391 | TAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
7d03f82f | 1392 | |
a1d1bb31 AL |
1393 | tlb_flush_page(env, watchpoint->vaddr); |
1394 | ||
1395 | qemu_free(watchpoint); | |
1396 | } | |
1397 | ||
1398 | /* Remove all matching watchpoints. */ | |
1399 | void cpu_watchpoint_remove_all(CPUState *env, int mask) | |
1400 | { | |
c0ce998e | 1401 | CPUWatchpoint *wp, *next; |
a1d1bb31 | 1402 | |
c0ce998e | 1403 | TAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
a1d1bb31 AL |
1404 | if (wp->flags & mask) |
1405 | cpu_watchpoint_remove_by_ref(env, wp); | |
c0ce998e | 1406 | } |
7d03f82f EI |
1407 | } |
1408 | ||
a1d1bb31 AL |
1409 | /* Add a breakpoint. */ |
1410 | int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags, | |
1411 | CPUBreakpoint **breakpoint) | |
4c3a88a2 | 1412 | { |
1fddef4b | 1413 | #if defined(TARGET_HAS_ICE) |
c0ce998e | 1414 | CPUBreakpoint *bp; |
3b46e624 | 1415 | |
a1d1bb31 | 1416 | bp = qemu_malloc(sizeof(*bp)); |
4c3a88a2 | 1417 | |
a1d1bb31 AL |
1418 | bp->pc = pc; |
1419 | bp->flags = flags; | |
1420 | ||
2dc9f411 | 1421 | /* keep all GDB-injected breakpoints in front */ |
c0ce998e AL |
1422 | if (flags & BP_GDB) |
1423 | TAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); | |
1424 | else | |
1425 | TAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); | |
3b46e624 | 1426 | |
d720b93d | 1427 | breakpoint_invalidate(env, pc); |
a1d1bb31 AL |
1428 | |
1429 | if (breakpoint) | |
1430 | *breakpoint = bp; | |
4c3a88a2 FB |
1431 | return 0; |
1432 | #else | |
a1d1bb31 | 1433 | return -ENOSYS; |
4c3a88a2 FB |
1434 | #endif |
1435 | } | |
1436 | ||
a1d1bb31 AL |
1437 | /* Remove a specific breakpoint. */ |
1438 | int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags) | |
1439 | { | |
7d03f82f | 1440 | #if defined(TARGET_HAS_ICE) |
a1d1bb31 AL |
1441 | CPUBreakpoint *bp; |
1442 | ||
c0ce998e | 1443 | TAILQ_FOREACH(bp, &env->breakpoints, entry) { |
a1d1bb31 AL |
1444 | if (bp->pc == pc && bp->flags == flags) { |
1445 | cpu_breakpoint_remove_by_ref(env, bp); | |
1446 | return 0; | |
1447 | } | |
7d03f82f | 1448 | } |
a1d1bb31 AL |
1449 | return -ENOENT; |
1450 | #else | |
1451 | return -ENOSYS; | |
7d03f82f EI |
1452 | #endif |
1453 | } | |
1454 | ||
a1d1bb31 AL |
1455 | /* Remove a specific breakpoint by reference. */ |
1456 | void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint) | |
4c3a88a2 | 1457 | { |
1fddef4b | 1458 | #if defined(TARGET_HAS_ICE) |
c0ce998e | 1459 | TAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
d720b93d | 1460 | |
a1d1bb31 AL |
1461 | breakpoint_invalidate(env, breakpoint->pc); |
1462 | ||
1463 | qemu_free(breakpoint); | |
1464 | #endif | |
1465 | } | |
1466 | ||
1467 | /* Remove all matching breakpoints. */ | |
1468 | void cpu_breakpoint_remove_all(CPUState *env, int mask) | |
1469 | { | |
1470 | #if defined(TARGET_HAS_ICE) | |
c0ce998e | 1471 | CPUBreakpoint *bp, *next; |
a1d1bb31 | 1472 | |
c0ce998e | 1473 | TAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
a1d1bb31 AL |
1474 | if (bp->flags & mask) |
1475 | cpu_breakpoint_remove_by_ref(env, bp); | |
c0ce998e | 1476 | } |
4c3a88a2 FB |
1477 | #endif |
1478 | } | |
1479 | ||
c33a346e FB |
1480 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
1481 | CPU loop after each instruction */ | |
1482 | void cpu_single_step(CPUState *env, int enabled) | |
1483 | { | |
1fddef4b | 1484 | #if defined(TARGET_HAS_ICE) |
c33a346e FB |
1485 | if (env->singlestep_enabled != enabled) { |
1486 | env->singlestep_enabled = enabled; | |
e22a25c9 AL |
1487 | if (kvm_enabled()) |
1488 | kvm_update_guest_debug(env, 0); | |
1489 | else { | |
ccbb4d44 | 1490 | /* must flush all the translated code to avoid inconsistencies */ |
e22a25c9 AL |
1491 | /* XXX: only flush what is necessary */ |
1492 | tb_flush(env); | |
1493 | } | |
c33a346e FB |
1494 | } |
1495 | #endif | |
1496 | } | |
1497 | ||
34865134 FB |
1498 | /* enable or disable low levels log */ |
1499 | void cpu_set_log(int log_flags) | |
1500 | { | |
1501 | loglevel = log_flags; | |
1502 | if (loglevel && !logfile) { | |
11fcfab4 | 1503 | logfile = fopen(logfilename, log_append ? "a" : "w"); |
34865134 FB |
1504 | if (!logfile) { |
1505 | perror(logfilename); | |
1506 | _exit(1); | |
1507 | } | |
9fa3e853 FB |
1508 | #if !defined(CONFIG_SOFTMMU) |
1509 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ | |
1510 | { | |
b55266b5 | 1511 | static char logfile_buf[4096]; |
9fa3e853 FB |
1512 | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
1513 | } | |
1514 | #else | |
34865134 | 1515 | setvbuf(logfile, NULL, _IOLBF, 0); |
9fa3e853 | 1516 | #endif |
e735b91c PB |
1517 | log_append = 1; |
1518 | } | |
1519 | if (!loglevel && logfile) { | |
1520 | fclose(logfile); | |
1521 | logfile = NULL; | |
34865134 FB |
1522 | } |
1523 | } | |
1524 | ||
1525 | void cpu_set_log_filename(const char *filename) | |
1526 | { | |
1527 | logfilename = strdup(filename); | |
e735b91c PB |
1528 | if (logfile) { |
1529 | fclose(logfile); | |
1530 | logfile = NULL; | |
1531 | } | |
1532 | cpu_set_log(loglevel); | |
34865134 | 1533 | } |
c33a346e | 1534 | |
3098dba0 | 1535 | static void cpu_unlink_tb(CPUState *env) |
ea041c0e | 1536 | { |
3098dba0 AJ |
1537 | #if defined(USE_NPTL) |
1538 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the | |
1539 | problem and hope the cpu will stop of its own accord. For userspace | |
1540 | emulation this often isn't actually as bad as it sounds. Often | |
1541 | signals are used primarily to interrupt blocking syscalls. */ | |
1542 | #else | |
ea041c0e | 1543 | TranslationBlock *tb; |
15a51156 | 1544 | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED; |
59817ccb | 1545 | |
3098dba0 AJ |
1546 | tb = env->current_tb; |
1547 | /* if the cpu is currently executing code, we must unlink it and | |
1548 | all the potentially executing TB */ | |
1549 | if (tb && !testandset(&interrupt_lock)) { | |
1550 | env->current_tb = NULL; | |
1551 | tb_reset_jump_recursive(tb); | |
1552 | resetlock(&interrupt_lock); | |
be214e6c | 1553 | } |
3098dba0 AJ |
1554 | #endif |
1555 | } | |
1556 | ||
1557 | /* mask must never be zero, except for A20 change call */ | |
1558 | void cpu_interrupt(CPUState *env, int mask) | |
1559 | { | |
1560 | int old_mask; | |
be214e6c | 1561 | |
2e70f6ef | 1562 | old_mask = env->interrupt_request; |
68a79315 | 1563 | env->interrupt_request |= mask; |
3098dba0 | 1564 | |
8edac960 AL |
1565 | #ifndef CONFIG_USER_ONLY |
1566 | /* | |
1567 | * If called from iothread context, wake the target cpu in | |
1568 | * case its halted. | |
1569 | */ | |
1570 | if (!qemu_cpu_self(env)) { | |
1571 | qemu_cpu_kick(env); | |
1572 | return; | |
1573 | } | |
1574 | #endif | |
1575 | ||
2e70f6ef | 1576 | if (use_icount) { |
266910c4 | 1577 | env->icount_decr.u16.high = 0xffff; |
2e70f6ef | 1578 | #ifndef CONFIG_USER_ONLY |
2e70f6ef | 1579 | if (!can_do_io(env) |
be214e6c | 1580 | && (mask & ~old_mask) != 0) { |
2e70f6ef PB |
1581 | cpu_abort(env, "Raised interrupt while not in I/O function"); |
1582 | } | |
1583 | #endif | |
1584 | } else { | |
3098dba0 | 1585 | cpu_unlink_tb(env); |
ea041c0e FB |
1586 | } |
1587 | } | |
1588 | ||
b54ad049 FB |
1589 | void cpu_reset_interrupt(CPUState *env, int mask) |
1590 | { | |
1591 | env->interrupt_request &= ~mask; | |
1592 | } | |
1593 | ||
3098dba0 AJ |
1594 | void cpu_exit(CPUState *env) |
1595 | { | |
1596 | env->exit_request = 1; | |
1597 | cpu_unlink_tb(env); | |
1598 | } | |
1599 | ||
c7cd6a37 | 1600 | const CPULogItem cpu_log_items[] = { |
5fafdf24 | 1601 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
f193c797 FB |
1602 | "show generated host assembly code for each compiled TB" }, |
1603 | { CPU_LOG_TB_IN_ASM, "in_asm", | |
1604 | "show target assembly code for each compiled TB" }, | |
5fafdf24 | 1605 | { CPU_LOG_TB_OP, "op", |
57fec1fe | 1606 | "show micro ops for each compiled TB" }, |
f193c797 | 1607 | { CPU_LOG_TB_OP_OPT, "op_opt", |
e01a1157 BS |
1608 | "show micro ops " |
1609 | #ifdef TARGET_I386 | |
1610 | "before eflags optimization and " | |
f193c797 | 1611 | #endif |
e01a1157 | 1612 | "after liveness analysis" }, |
f193c797 FB |
1613 | { CPU_LOG_INT, "int", |
1614 | "show interrupts/exceptions in short format" }, | |
1615 | { CPU_LOG_EXEC, "exec", | |
1616 | "show trace before each executed TB (lots of logs)" }, | |
9fddaa0c | 1617 | { CPU_LOG_TB_CPU, "cpu", |
e91c8a77 | 1618 | "show CPU state before block translation" }, |
f193c797 FB |
1619 | #ifdef TARGET_I386 |
1620 | { CPU_LOG_PCALL, "pcall", | |
1621 | "show protected mode far calls/returns/exceptions" }, | |
eca1bdf4 AL |
1622 | { CPU_LOG_RESET, "cpu_reset", |
1623 | "show CPU state before CPU resets" }, | |
f193c797 | 1624 | #endif |
8e3a9fd2 | 1625 | #ifdef DEBUG_IOPORT |
fd872598 FB |
1626 | { CPU_LOG_IOPORT, "ioport", |
1627 | "show all i/o ports accesses" }, | |
8e3a9fd2 | 1628 | #endif |
f193c797 FB |
1629 | { 0, NULL, NULL }, |
1630 | }; | |
1631 | ||
1632 | static int cmp1(const char *s1, int n, const char *s2) | |
1633 | { | |
1634 | if (strlen(s2) != n) | |
1635 | return 0; | |
1636 | return memcmp(s1, s2, n) == 0; | |
1637 | } | |
3b46e624 | 1638 | |
f193c797 FB |
1639 | /* takes a comma separated list of log masks. Return 0 if error. */ |
1640 | int cpu_str_to_log_mask(const char *str) | |
1641 | { | |
c7cd6a37 | 1642 | const CPULogItem *item; |
f193c797 FB |
1643 | int mask; |
1644 | const char *p, *p1; | |
1645 | ||
1646 | p = str; | |
1647 | mask = 0; | |
1648 | for(;;) { | |
1649 | p1 = strchr(p, ','); | |
1650 | if (!p1) | |
1651 | p1 = p + strlen(p); | |
8e3a9fd2 FB |
1652 | if(cmp1(p,p1-p,"all")) { |
1653 | for(item = cpu_log_items; item->mask != 0; item++) { | |
1654 | mask |= item->mask; | |
1655 | } | |
1656 | } else { | |
f193c797 FB |
1657 | for(item = cpu_log_items; item->mask != 0; item++) { |
1658 | if (cmp1(p, p1 - p, item->name)) | |
1659 | goto found; | |
1660 | } | |
1661 | return 0; | |
8e3a9fd2 | 1662 | } |
f193c797 FB |
1663 | found: |
1664 | mask |= item->mask; | |
1665 | if (*p1 != ',') | |
1666 | break; | |
1667 | p = p1 + 1; | |
1668 | } | |
1669 | return mask; | |
1670 | } | |
ea041c0e | 1671 | |
7501267e FB |
1672 | void cpu_abort(CPUState *env, const char *fmt, ...) |
1673 | { | |
1674 | va_list ap; | |
493ae1f0 | 1675 | va_list ap2; |
7501267e FB |
1676 | |
1677 | va_start(ap, fmt); | |
493ae1f0 | 1678 | va_copy(ap2, ap); |
7501267e FB |
1679 | fprintf(stderr, "qemu: fatal: "); |
1680 | vfprintf(stderr, fmt, ap); | |
1681 | fprintf(stderr, "\n"); | |
1682 | #ifdef TARGET_I386 | |
7fe48483 FB |
1683 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
1684 | #else | |
1685 | cpu_dump_state(env, stderr, fprintf, 0); | |
7501267e | 1686 | #endif |
93fcfe39 AL |
1687 | if (qemu_log_enabled()) { |
1688 | qemu_log("qemu: fatal: "); | |
1689 | qemu_log_vprintf(fmt, ap2); | |
1690 | qemu_log("\n"); | |
f9373291 | 1691 | #ifdef TARGET_I386 |
93fcfe39 | 1692 | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
f9373291 | 1693 | #else |
93fcfe39 | 1694 | log_cpu_state(env, 0); |
f9373291 | 1695 | #endif |
31b1a7b4 | 1696 | qemu_log_flush(); |
93fcfe39 | 1697 | qemu_log_close(); |
924edcae | 1698 | } |
493ae1f0 | 1699 | va_end(ap2); |
f9373291 | 1700 | va_end(ap); |
7501267e FB |
1701 | abort(); |
1702 | } | |
1703 | ||
c5be9f08 TS |
1704 | CPUState *cpu_copy(CPUState *env) |
1705 | { | |
01ba9816 | 1706 | CPUState *new_env = cpu_init(env->cpu_model_str); |
c5be9f08 TS |
1707 | CPUState *next_cpu = new_env->next_cpu; |
1708 | int cpu_index = new_env->cpu_index; | |
5a38f081 AL |
1709 | #if defined(TARGET_HAS_ICE) |
1710 | CPUBreakpoint *bp; | |
1711 | CPUWatchpoint *wp; | |
1712 | #endif | |
1713 | ||
c5be9f08 | 1714 | memcpy(new_env, env, sizeof(CPUState)); |
5a38f081 AL |
1715 | |
1716 | /* Preserve chaining and index. */ | |
c5be9f08 TS |
1717 | new_env->next_cpu = next_cpu; |
1718 | new_env->cpu_index = cpu_index; | |
5a38f081 AL |
1719 | |
1720 | /* Clone all break/watchpoints. | |
1721 | Note: Once we support ptrace with hw-debug register access, make sure | |
1722 | BP_CPU break/watchpoints are handled correctly on clone. */ | |
1723 | TAILQ_INIT(&env->breakpoints); | |
1724 | TAILQ_INIT(&env->watchpoints); | |
1725 | #if defined(TARGET_HAS_ICE) | |
1726 | TAILQ_FOREACH(bp, &env->breakpoints, entry) { | |
1727 | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL); | |
1728 | } | |
1729 | TAILQ_FOREACH(wp, &env->watchpoints, entry) { | |
1730 | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1, | |
1731 | wp->flags, NULL); | |
1732 | } | |
1733 | #endif | |
1734 | ||
c5be9f08 TS |
1735 | return new_env; |
1736 | } | |
1737 | ||
0124311e FB |
1738 | #if !defined(CONFIG_USER_ONLY) |
1739 | ||
5c751e99 EI |
1740 | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
1741 | { | |
1742 | unsigned int i; | |
1743 | ||
1744 | /* Discard jump cache entries for any tb which might potentially | |
1745 | overlap the flushed page. */ | |
1746 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); | |
1747 | memset (&env->tb_jmp_cache[i], 0, | |
1748 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); | |
1749 | ||
1750 | i = tb_jmp_cache_hash_page(addr); | |
1751 | memset (&env->tb_jmp_cache[i], 0, | |
1752 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); | |
1753 | } | |
1754 | ||
ee8b7021 FB |
1755 | /* NOTE: if flush_global is true, also flush global entries (not |
1756 | implemented yet) */ | |
1757 | void tlb_flush(CPUState *env, int flush_global) | |
33417e70 | 1758 | { |
33417e70 | 1759 | int i; |
0124311e | 1760 | |
9fa3e853 FB |
1761 | #if defined(DEBUG_TLB) |
1762 | printf("tlb_flush:\n"); | |
1763 | #endif | |
0124311e FB |
1764 | /* must reset current TB so that interrupts cannot modify the |
1765 | links while we are modifying them */ | |
1766 | env->current_tb = NULL; | |
1767 | ||
33417e70 | 1768 | for(i = 0; i < CPU_TLB_SIZE; i++) { |
cfde4bd9 IY |
1769 | int mmu_idx; |
1770 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
1771 | env->tlb_table[mmu_idx][i].addr_read = -1; | |
1772 | env->tlb_table[mmu_idx][i].addr_write = -1; | |
1773 | env->tlb_table[mmu_idx][i].addr_code = -1; | |
1774 | } | |
33417e70 | 1775 | } |
9fa3e853 | 1776 | |
8a40a180 | 1777 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
9fa3e853 | 1778 | |
640f42e4 | 1779 | #ifdef CONFIG_KQEMU |
0a962c02 FB |
1780 | if (env->kqemu_enabled) { |
1781 | kqemu_flush(env, flush_global); | |
1782 | } | |
9fa3e853 | 1783 | #endif |
e3db7226 | 1784 | tlb_flush_count++; |
33417e70 FB |
1785 | } |
1786 | ||
274da6b2 | 1787 | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
61382a50 | 1788 | { |
5fafdf24 | 1789 | if (addr == (tlb_entry->addr_read & |
84b7b8e7 | 1790 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
5fafdf24 | 1791 | addr == (tlb_entry->addr_write & |
84b7b8e7 | 1792 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
5fafdf24 | 1793 | addr == (tlb_entry->addr_code & |
84b7b8e7 FB |
1794 | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
1795 | tlb_entry->addr_read = -1; | |
1796 | tlb_entry->addr_write = -1; | |
1797 | tlb_entry->addr_code = -1; | |
1798 | } | |
61382a50 FB |
1799 | } |
1800 | ||
2e12669a | 1801 | void tlb_flush_page(CPUState *env, target_ulong addr) |
33417e70 | 1802 | { |
8a40a180 | 1803 | int i; |
cfde4bd9 | 1804 | int mmu_idx; |
0124311e | 1805 | |
9fa3e853 | 1806 | #if defined(DEBUG_TLB) |
108c49b8 | 1807 | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
9fa3e853 | 1808 | #endif |
0124311e FB |
1809 | /* must reset current TB so that interrupts cannot modify the |
1810 | links while we are modifying them */ | |
1811 | env->current_tb = NULL; | |
61382a50 FB |
1812 | |
1813 | addr &= TARGET_PAGE_MASK; | |
1814 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); | |
cfde4bd9 IY |
1815 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
1816 | tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr); | |
0124311e | 1817 | |
5c751e99 | 1818 | tlb_flush_jmp_cache(env, addr); |
9fa3e853 | 1819 | |
640f42e4 | 1820 | #ifdef CONFIG_KQEMU |
0a962c02 FB |
1821 | if (env->kqemu_enabled) { |
1822 | kqemu_flush_page(env, addr); | |
1823 | } | |
1824 | #endif | |
9fa3e853 FB |
1825 | } |
1826 | ||
9fa3e853 FB |
1827 | /* update the TLBs so that writes to code in the virtual page 'addr' |
1828 | can be detected */ | |
6a00d601 | 1829 | static void tlb_protect_code(ram_addr_t ram_addr) |
9fa3e853 | 1830 | { |
5fafdf24 | 1831 | cpu_physical_memory_reset_dirty(ram_addr, |
6a00d601 FB |
1832 | ram_addr + TARGET_PAGE_SIZE, |
1833 | CODE_DIRTY_FLAG); | |
9fa3e853 FB |
1834 | } |
1835 | ||
9fa3e853 | 1836 | /* update the TLB so that writes in physical page 'phys_addr' are no longer |
3a7d929e | 1837 | tested for self modifying code */ |
5fafdf24 | 1838 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
3a7d929e | 1839 | target_ulong vaddr) |
9fa3e853 | 1840 | { |
3a7d929e | 1841 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG; |
1ccde1cb FB |
1842 | } |
1843 | ||
5fafdf24 | 1844 | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
1ccde1cb FB |
1845 | unsigned long start, unsigned long length) |
1846 | { | |
1847 | unsigned long addr; | |
84b7b8e7 FB |
1848 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
1849 | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; | |
1ccde1cb | 1850 | if ((addr - start) < length) { |
0f459d16 | 1851 | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
1ccde1cb FB |
1852 | } |
1853 | } | |
1854 | } | |
1855 | ||
5579c7f3 | 1856 | /* Note: start and end must be within the same ram block. */ |
3a7d929e | 1857 | void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, |
0a962c02 | 1858 | int dirty_flags) |
1ccde1cb FB |
1859 | { |
1860 | CPUState *env; | |
4f2ac237 | 1861 | unsigned long length, start1; |
0a962c02 FB |
1862 | int i, mask, len; |
1863 | uint8_t *p; | |
1ccde1cb FB |
1864 | |
1865 | start &= TARGET_PAGE_MASK; | |
1866 | end = TARGET_PAGE_ALIGN(end); | |
1867 | ||
1868 | length = end - start; | |
1869 | if (length == 0) | |
1870 | return; | |
0a962c02 | 1871 | len = length >> TARGET_PAGE_BITS; |
640f42e4 | 1872 | #ifdef CONFIG_KQEMU |
6a00d601 FB |
1873 | /* XXX: should not depend on cpu context */ |
1874 | env = first_cpu; | |
3a7d929e | 1875 | if (env->kqemu_enabled) { |
f23db169 FB |
1876 | ram_addr_t addr; |
1877 | addr = start; | |
1878 | for(i = 0; i < len; i++) { | |
1879 | kqemu_set_notdirty(env, addr); | |
1880 | addr += TARGET_PAGE_SIZE; | |
1881 | } | |
3a7d929e FB |
1882 | } |
1883 | #endif | |
f23db169 FB |
1884 | mask = ~dirty_flags; |
1885 | p = phys_ram_dirty + (start >> TARGET_PAGE_BITS); | |
1886 | for(i = 0; i < len; i++) | |
1887 | p[i] &= mask; | |
1888 | ||
1ccde1cb FB |
1889 | /* we modify the TLB cache so that the dirty bit will be set again |
1890 | when accessing the range */ | |
5579c7f3 PB |
1891 | start1 = (unsigned long)qemu_get_ram_ptr(start); |
1892 | /* Chek that we don't span multiple blocks - this breaks the | |
1893 | address comparisons below. */ | |
1894 | if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1 | |
1895 | != (end - 1) - start) { | |
1896 | abort(); | |
1897 | } | |
1898 | ||
6a00d601 | 1899 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
cfde4bd9 IY |
1900 | int mmu_idx; |
1901 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
1902 | for(i = 0; i < CPU_TLB_SIZE; i++) | |
1903 | tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i], | |
1904 | start1, length); | |
1905 | } | |
6a00d601 | 1906 | } |
1ccde1cb FB |
1907 | } |
1908 | ||
74576198 AL |
1909 | int cpu_physical_memory_set_dirty_tracking(int enable) |
1910 | { | |
1911 | in_migration = enable; | |
b0a46a33 JK |
1912 | if (kvm_enabled()) { |
1913 | return kvm_set_migration_log(enable); | |
1914 | } | |
74576198 AL |
1915 | return 0; |
1916 | } | |
1917 | ||
1918 | int cpu_physical_memory_get_dirty_tracking(void) | |
1919 | { | |
1920 | return in_migration; | |
1921 | } | |
1922 | ||
151f7749 JK |
1923 | int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, |
1924 | target_phys_addr_t end_addr) | |
2bec46dc | 1925 | { |
151f7749 JK |
1926 | int ret = 0; |
1927 | ||
2bec46dc | 1928 | if (kvm_enabled()) |
151f7749 JK |
1929 | ret = kvm_physical_sync_dirty_bitmap(start_addr, end_addr); |
1930 | return ret; | |
2bec46dc AL |
1931 | } |
1932 | ||
3a7d929e FB |
1933 | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
1934 | { | |
1935 | ram_addr_t ram_addr; | |
5579c7f3 | 1936 | void *p; |
3a7d929e | 1937 | |
84b7b8e7 | 1938 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
5579c7f3 PB |
1939 | p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK) |
1940 | + tlb_entry->addend); | |
1941 | ram_addr = qemu_ram_addr_from_host(p); | |
3a7d929e | 1942 | if (!cpu_physical_memory_is_dirty(ram_addr)) { |
0f459d16 | 1943 | tlb_entry->addr_write |= TLB_NOTDIRTY; |
3a7d929e FB |
1944 | } |
1945 | } | |
1946 | } | |
1947 | ||
1948 | /* update the TLB according to the current state of the dirty bits */ | |
1949 | void cpu_tlb_update_dirty(CPUState *env) | |
1950 | { | |
1951 | int i; | |
cfde4bd9 IY |
1952 | int mmu_idx; |
1953 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
1954 | for(i = 0; i < CPU_TLB_SIZE; i++) | |
1955 | tlb_update_dirty(&env->tlb_table[mmu_idx][i]); | |
1956 | } | |
3a7d929e FB |
1957 | } |
1958 | ||
0f459d16 | 1959 | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
1ccde1cb | 1960 | { |
0f459d16 PB |
1961 | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) |
1962 | tlb_entry->addr_write = vaddr; | |
1ccde1cb FB |
1963 | } |
1964 | ||
0f459d16 PB |
1965 | /* update the TLB corresponding to virtual page vaddr |
1966 | so that it is no longer dirty */ | |
1967 | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) | |
1ccde1cb | 1968 | { |
1ccde1cb | 1969 | int i; |
cfde4bd9 | 1970 | int mmu_idx; |
1ccde1cb | 1971 | |
0f459d16 | 1972 | vaddr &= TARGET_PAGE_MASK; |
1ccde1cb | 1973 | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
cfde4bd9 IY |
1974 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
1975 | tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr); | |
9fa3e853 FB |
1976 | } |
1977 | ||
59817ccb FB |
1978 | /* add a new TLB entry. At most one entry for a given virtual address |
1979 | is permitted. Return 0 if OK or 2 if the page could not be mapped | |
1980 | (can only happen in non SOFTMMU mode for I/O pages or pages | |
1981 | conflicting with the host address space). */ | |
5fafdf24 TS |
1982 | int tlb_set_page_exec(CPUState *env, target_ulong vaddr, |
1983 | target_phys_addr_t paddr, int prot, | |
6ebbf390 | 1984 | int mmu_idx, int is_softmmu) |
9fa3e853 | 1985 | { |
92e873b9 | 1986 | PhysPageDesc *p; |
4f2ac237 | 1987 | unsigned long pd; |
9fa3e853 | 1988 | unsigned int index; |
4f2ac237 | 1989 | target_ulong address; |
0f459d16 | 1990 | target_ulong code_address; |
108c49b8 | 1991 | target_phys_addr_t addend; |
9fa3e853 | 1992 | int ret; |
84b7b8e7 | 1993 | CPUTLBEntry *te; |
a1d1bb31 | 1994 | CPUWatchpoint *wp; |
0f459d16 | 1995 | target_phys_addr_t iotlb; |
9fa3e853 | 1996 | |
92e873b9 | 1997 | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
9fa3e853 FB |
1998 | if (!p) { |
1999 | pd = IO_MEM_UNASSIGNED; | |
9fa3e853 FB |
2000 | } else { |
2001 | pd = p->phys_offset; | |
9fa3e853 FB |
2002 | } |
2003 | #if defined(DEBUG_TLB) | |
6ebbf390 JM |
2004 | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n", |
2005 | vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd); | |
9fa3e853 FB |
2006 | #endif |
2007 | ||
2008 | ret = 0; | |
0f459d16 PB |
2009 | address = vaddr; |
2010 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) { | |
2011 | /* IO memory case (romd handled later) */ | |
2012 | address |= TLB_MMIO; | |
2013 | } | |
5579c7f3 | 2014 | addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
0f459d16 PB |
2015 | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
2016 | /* Normal RAM. */ | |
2017 | iotlb = pd & TARGET_PAGE_MASK; | |
2018 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM) | |
2019 | iotlb |= IO_MEM_NOTDIRTY; | |
2020 | else | |
2021 | iotlb |= IO_MEM_ROM; | |
2022 | } else { | |
ccbb4d44 | 2023 | /* IO handlers are currently passed a physical address. |
0f459d16 PB |
2024 | It would be nice to pass an offset from the base address |
2025 | of that region. This would avoid having to special case RAM, | |
2026 | and avoid full address decoding in every device. | |
2027 | We can't use the high bits of pd for this because | |
2028 | IO_MEM_ROMD uses these as a ram address. */ | |
8da3ff18 PB |
2029 | iotlb = (pd & ~TARGET_PAGE_MASK); |
2030 | if (p) { | |
8da3ff18 PB |
2031 | iotlb += p->region_offset; |
2032 | } else { | |
2033 | iotlb += paddr; | |
2034 | } | |
0f459d16 PB |
2035 | } |
2036 | ||
2037 | code_address = address; | |
2038 | /* Make accesses to pages with watchpoints go via the | |
2039 | watchpoint trap routines. */ | |
c0ce998e | 2040 | TAILQ_FOREACH(wp, &env->watchpoints, entry) { |
a1d1bb31 | 2041 | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) { |
0f459d16 PB |
2042 | iotlb = io_mem_watch + paddr; |
2043 | /* TODO: The memory case can be optimized by not trapping | |
2044 | reads of pages with a write breakpoint. */ | |
2045 | address |= TLB_MMIO; | |
6658ffb8 | 2046 | } |
0f459d16 | 2047 | } |
d79acba4 | 2048 | |
0f459d16 PB |
2049 | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
2050 | env->iotlb[mmu_idx][index] = iotlb - vaddr; | |
2051 | te = &env->tlb_table[mmu_idx][index]; | |
2052 | te->addend = addend - vaddr; | |
2053 | if (prot & PAGE_READ) { | |
2054 | te->addr_read = address; | |
2055 | } else { | |
2056 | te->addr_read = -1; | |
2057 | } | |
5c751e99 | 2058 | |
0f459d16 PB |
2059 | if (prot & PAGE_EXEC) { |
2060 | te->addr_code = code_address; | |
2061 | } else { | |
2062 | te->addr_code = -1; | |
2063 | } | |
2064 | if (prot & PAGE_WRITE) { | |
2065 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM || | |
2066 | (pd & IO_MEM_ROMD)) { | |
2067 | /* Write access calls the I/O callback. */ | |
2068 | te->addr_write = address | TLB_MMIO; | |
2069 | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && | |
2070 | !cpu_physical_memory_is_dirty(pd)) { | |
2071 | te->addr_write = address | TLB_NOTDIRTY; | |
9fa3e853 | 2072 | } else { |
0f459d16 | 2073 | te->addr_write = address; |
9fa3e853 | 2074 | } |
0f459d16 PB |
2075 | } else { |
2076 | te->addr_write = -1; | |
9fa3e853 | 2077 | } |
9fa3e853 FB |
2078 | return ret; |
2079 | } | |
2080 | ||
0124311e FB |
2081 | #else |
2082 | ||
ee8b7021 | 2083 | void tlb_flush(CPUState *env, int flush_global) |
0124311e FB |
2084 | { |
2085 | } | |
2086 | ||
2e12669a | 2087 | void tlb_flush_page(CPUState *env, target_ulong addr) |
0124311e FB |
2088 | { |
2089 | } | |
2090 | ||
5fafdf24 TS |
2091 | int tlb_set_page_exec(CPUState *env, target_ulong vaddr, |
2092 | target_phys_addr_t paddr, int prot, | |
6ebbf390 | 2093 | int mmu_idx, int is_softmmu) |
9fa3e853 FB |
2094 | { |
2095 | return 0; | |
2096 | } | |
0124311e | 2097 | |
edf8e2af MW |
2098 | /* |
2099 | * Walks guest process memory "regions" one by one | |
2100 | * and calls callback function 'fn' for each region. | |
2101 | */ | |
2102 | int walk_memory_regions(void *priv, | |
2103 | int (*fn)(void *, unsigned long, unsigned long, unsigned long)) | |
33417e70 | 2104 | { |
9fa3e853 | 2105 | unsigned long start, end; |
edf8e2af | 2106 | PageDesc *p = NULL; |
9fa3e853 | 2107 | int i, j, prot, prot1; |
edf8e2af | 2108 | int rc = 0; |
33417e70 | 2109 | |
edf8e2af | 2110 | start = end = -1; |
9fa3e853 | 2111 | prot = 0; |
edf8e2af MW |
2112 | |
2113 | for (i = 0; i <= L1_SIZE; i++) { | |
2114 | p = (i < L1_SIZE) ? l1_map[i] : NULL; | |
2115 | for (j = 0; j < L2_SIZE; j++) { | |
2116 | prot1 = (p == NULL) ? 0 : p[j].flags; | |
2117 | /* | |
2118 | * "region" is one continuous chunk of memory | |
2119 | * that has same protection flags set. | |
2120 | */ | |
9fa3e853 FB |
2121 | if (prot1 != prot) { |
2122 | end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS); | |
2123 | if (start != -1) { | |
edf8e2af MW |
2124 | rc = (*fn)(priv, start, end, prot); |
2125 | /* callback can stop iteration by returning != 0 */ | |
2126 | if (rc != 0) | |
2127 | return (rc); | |
9fa3e853 FB |
2128 | } |
2129 | if (prot1 != 0) | |
2130 | start = end; | |
2131 | else | |
2132 | start = -1; | |
2133 | prot = prot1; | |
2134 | } | |
edf8e2af | 2135 | if (p == NULL) |
9fa3e853 FB |
2136 | break; |
2137 | } | |
33417e70 | 2138 | } |
edf8e2af MW |
2139 | return (rc); |
2140 | } | |
2141 | ||
2142 | static int dump_region(void *priv, unsigned long start, | |
2143 | unsigned long end, unsigned long prot) | |
2144 | { | |
2145 | FILE *f = (FILE *)priv; | |
2146 | ||
2147 | (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n", | |
2148 | start, end, end - start, | |
2149 | ((prot & PAGE_READ) ? 'r' : '-'), | |
2150 | ((prot & PAGE_WRITE) ? 'w' : '-'), | |
2151 | ((prot & PAGE_EXEC) ? 'x' : '-')); | |
2152 | ||
2153 | return (0); | |
2154 | } | |
2155 | ||
2156 | /* dump memory mappings */ | |
2157 | void page_dump(FILE *f) | |
2158 | { | |
2159 | (void) fprintf(f, "%-8s %-8s %-8s %s\n", | |
2160 | "start", "end", "size", "prot"); | |
2161 | walk_memory_regions(f, dump_region); | |
33417e70 FB |
2162 | } |
2163 | ||
53a5960a | 2164 | int page_get_flags(target_ulong address) |
33417e70 | 2165 | { |
9fa3e853 FB |
2166 | PageDesc *p; |
2167 | ||
2168 | p = page_find(address >> TARGET_PAGE_BITS); | |
33417e70 | 2169 | if (!p) |
9fa3e853 FB |
2170 | return 0; |
2171 | return p->flags; | |
2172 | } | |
2173 | ||
2174 | /* modify the flags of a page and invalidate the code if | |
ccbb4d44 | 2175 | necessary. The flag PAGE_WRITE_ORG is positioned automatically |
9fa3e853 | 2176 | depending on PAGE_WRITE */ |
53a5960a | 2177 | void page_set_flags(target_ulong start, target_ulong end, int flags) |
9fa3e853 FB |
2178 | { |
2179 | PageDesc *p; | |
53a5960a | 2180 | target_ulong addr; |
9fa3e853 | 2181 | |
c8a706fe | 2182 | /* mmap_lock should already be held. */ |
9fa3e853 FB |
2183 | start = start & TARGET_PAGE_MASK; |
2184 | end = TARGET_PAGE_ALIGN(end); | |
2185 | if (flags & PAGE_WRITE) | |
2186 | flags |= PAGE_WRITE_ORG; | |
9fa3e853 FB |
2187 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
2188 | p = page_find_alloc(addr >> TARGET_PAGE_BITS); | |
17e2377a PB |
2189 | /* We may be called for host regions that are outside guest |
2190 | address space. */ | |
2191 | if (!p) | |
2192 | return; | |
9fa3e853 FB |
2193 | /* if the write protection is set, then we invalidate the code |
2194 | inside */ | |
5fafdf24 | 2195 | if (!(p->flags & PAGE_WRITE) && |
9fa3e853 FB |
2196 | (flags & PAGE_WRITE) && |
2197 | p->first_tb) { | |
d720b93d | 2198 | tb_invalidate_phys_page(addr, 0, NULL); |
9fa3e853 FB |
2199 | } |
2200 | p->flags = flags; | |
2201 | } | |
33417e70 FB |
2202 | } |
2203 | ||
3d97b40b TS |
2204 | int page_check_range(target_ulong start, target_ulong len, int flags) |
2205 | { | |
2206 | PageDesc *p; | |
2207 | target_ulong end; | |
2208 | target_ulong addr; | |
2209 | ||
55f280c9 AZ |
2210 | if (start + len < start) |
2211 | /* we've wrapped around */ | |
2212 | return -1; | |
2213 | ||
3d97b40b TS |
2214 | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ |
2215 | start = start & TARGET_PAGE_MASK; | |
2216 | ||
3d97b40b TS |
2217 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
2218 | p = page_find(addr >> TARGET_PAGE_BITS); | |
2219 | if( !p ) | |
2220 | return -1; | |
2221 | if( !(p->flags & PAGE_VALID) ) | |
2222 | return -1; | |
2223 | ||
dae3270c | 2224 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) |
3d97b40b | 2225 | return -1; |
dae3270c FB |
2226 | if (flags & PAGE_WRITE) { |
2227 | if (!(p->flags & PAGE_WRITE_ORG)) | |
2228 | return -1; | |
2229 | /* unprotect the page if it was put read-only because it | |
2230 | contains translated code */ | |
2231 | if (!(p->flags & PAGE_WRITE)) { | |
2232 | if (!page_unprotect(addr, 0, NULL)) | |
2233 | return -1; | |
2234 | } | |
2235 | return 0; | |
2236 | } | |
3d97b40b TS |
2237 | } |
2238 | return 0; | |
2239 | } | |
2240 | ||
9fa3e853 | 2241 | /* called from signal handler: invalidate the code and unprotect the |
ccbb4d44 | 2242 | page. Return TRUE if the fault was successfully handled. */ |
53a5960a | 2243 | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
9fa3e853 FB |
2244 | { |
2245 | unsigned int page_index, prot, pindex; | |
2246 | PageDesc *p, *p1; | |
53a5960a | 2247 | target_ulong host_start, host_end, addr; |
9fa3e853 | 2248 | |
c8a706fe PB |
2249 | /* Technically this isn't safe inside a signal handler. However we |
2250 | know this only ever happens in a synchronous SEGV handler, so in | |
2251 | practice it seems to be ok. */ | |
2252 | mmap_lock(); | |
2253 | ||
83fb7adf | 2254 | host_start = address & qemu_host_page_mask; |
9fa3e853 FB |
2255 | page_index = host_start >> TARGET_PAGE_BITS; |
2256 | p1 = page_find(page_index); | |
c8a706fe PB |
2257 | if (!p1) { |
2258 | mmap_unlock(); | |
9fa3e853 | 2259 | return 0; |
c8a706fe | 2260 | } |
83fb7adf | 2261 | host_end = host_start + qemu_host_page_size; |
9fa3e853 FB |
2262 | p = p1; |
2263 | prot = 0; | |
2264 | for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) { | |
2265 | prot |= p->flags; | |
2266 | p++; | |
2267 | } | |
2268 | /* if the page was really writable, then we change its | |
2269 | protection back to writable */ | |
2270 | if (prot & PAGE_WRITE_ORG) { | |
2271 | pindex = (address - host_start) >> TARGET_PAGE_BITS; | |
2272 | if (!(p1[pindex].flags & PAGE_WRITE)) { | |
5fafdf24 | 2273 | mprotect((void *)g2h(host_start), qemu_host_page_size, |
9fa3e853 FB |
2274 | (prot & PAGE_BITS) | PAGE_WRITE); |
2275 | p1[pindex].flags |= PAGE_WRITE; | |
2276 | /* and since the content will be modified, we must invalidate | |
2277 | the corresponding translated code. */ | |
d720b93d | 2278 | tb_invalidate_phys_page(address, pc, puc); |
9fa3e853 FB |
2279 | #ifdef DEBUG_TB_CHECK |
2280 | tb_invalidate_check(address); | |
2281 | #endif | |
c8a706fe | 2282 | mmap_unlock(); |
9fa3e853 FB |
2283 | return 1; |
2284 | } | |
2285 | } | |
c8a706fe | 2286 | mmap_unlock(); |
9fa3e853 FB |
2287 | return 0; |
2288 | } | |
2289 | ||
6a00d601 FB |
2290 | static inline void tlb_set_dirty(CPUState *env, |
2291 | unsigned long addr, target_ulong vaddr) | |
1ccde1cb FB |
2292 | { |
2293 | } | |
9fa3e853 FB |
2294 | #endif /* defined(CONFIG_USER_ONLY) */ |
2295 | ||
e2eef170 | 2296 | #if !defined(CONFIG_USER_ONLY) |
8da3ff18 | 2297 | |
db7b5426 | 2298 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
8da3ff18 | 2299 | ram_addr_t memory, ram_addr_t region_offset); |
00f82b8a | 2300 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
8da3ff18 | 2301 | ram_addr_t orig_memory, ram_addr_t region_offset); |
db7b5426 BS |
2302 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \ |
2303 | need_subpage) \ | |
2304 | do { \ | |
2305 | if (addr > start_addr) \ | |
2306 | start_addr2 = 0; \ | |
2307 | else { \ | |
2308 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ | |
2309 | if (start_addr2 > 0) \ | |
2310 | need_subpage = 1; \ | |
2311 | } \ | |
2312 | \ | |
49e9fba2 | 2313 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \ |
db7b5426 BS |
2314 | end_addr2 = TARGET_PAGE_SIZE - 1; \ |
2315 | else { \ | |
2316 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \ | |
2317 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ | |
2318 | need_subpage = 1; \ | |
2319 | } \ | |
2320 | } while (0) | |
2321 | ||
33417e70 FB |
2322 | /* register physical memory. 'size' must be a multiple of the target |
2323 | page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an | |
8da3ff18 PB |
2324 | io memory page. The address used when calling the IO function is |
2325 | the offset from the start of the region, plus region_offset. Both | |
ccbb4d44 | 2326 | start_addr and region_offset are rounded down to a page boundary |
8da3ff18 PB |
2327 | before calculating this offset. This should not be a problem unless |
2328 | the low bits of start_addr and region_offset differ. */ | |
2329 | void cpu_register_physical_memory_offset(target_phys_addr_t start_addr, | |
2330 | ram_addr_t size, | |
2331 | ram_addr_t phys_offset, | |
2332 | ram_addr_t region_offset) | |
33417e70 | 2333 | { |
108c49b8 | 2334 | target_phys_addr_t addr, end_addr; |
92e873b9 | 2335 | PhysPageDesc *p; |
9d42037b | 2336 | CPUState *env; |
00f82b8a | 2337 | ram_addr_t orig_size = size; |
db7b5426 | 2338 | void *subpage; |
33417e70 | 2339 | |
640f42e4 | 2340 | #ifdef CONFIG_KQEMU |
da260249 FB |
2341 | /* XXX: should not depend on cpu context */ |
2342 | env = first_cpu; | |
2343 | if (env->kqemu_enabled) { | |
2344 | kqemu_set_phys_mem(start_addr, size, phys_offset); | |
2345 | } | |
2346 | #endif | |
7ba1e619 AL |
2347 | if (kvm_enabled()) |
2348 | kvm_set_phys_mem(start_addr, size, phys_offset); | |
2349 | ||
67c4d23c PB |
2350 | if (phys_offset == IO_MEM_UNASSIGNED) { |
2351 | region_offset = start_addr; | |
2352 | } | |
8da3ff18 | 2353 | region_offset &= TARGET_PAGE_MASK; |
5fd386f6 | 2354 | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK; |
49e9fba2 BS |
2355 | end_addr = start_addr + (target_phys_addr_t)size; |
2356 | for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) { | |
db7b5426 BS |
2357 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
2358 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) { | |
00f82b8a | 2359 | ram_addr_t orig_memory = p->phys_offset; |
db7b5426 BS |
2360 | target_phys_addr_t start_addr2, end_addr2; |
2361 | int need_subpage = 0; | |
2362 | ||
2363 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, | |
2364 | need_subpage); | |
4254fab8 | 2365 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
db7b5426 BS |
2366 | if (!(orig_memory & IO_MEM_SUBPAGE)) { |
2367 | subpage = subpage_init((addr & TARGET_PAGE_MASK), | |
8da3ff18 PB |
2368 | &p->phys_offset, orig_memory, |
2369 | p->region_offset); | |
db7b5426 BS |
2370 | } else { |
2371 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) | |
2372 | >> IO_MEM_SHIFT]; | |
2373 | } | |
8da3ff18 PB |
2374 | subpage_register(subpage, start_addr2, end_addr2, phys_offset, |
2375 | region_offset); | |
2376 | p->region_offset = 0; | |
db7b5426 BS |
2377 | } else { |
2378 | p->phys_offset = phys_offset; | |
2379 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || | |
2380 | (phys_offset & IO_MEM_ROMD)) | |
2381 | phys_offset += TARGET_PAGE_SIZE; | |
2382 | } | |
2383 | } else { | |
2384 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1); | |
2385 | p->phys_offset = phys_offset; | |
8da3ff18 | 2386 | p->region_offset = region_offset; |
db7b5426 | 2387 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
8da3ff18 | 2388 | (phys_offset & IO_MEM_ROMD)) { |
db7b5426 | 2389 | phys_offset += TARGET_PAGE_SIZE; |
0e8f0967 | 2390 | } else { |
db7b5426 BS |
2391 | target_phys_addr_t start_addr2, end_addr2; |
2392 | int need_subpage = 0; | |
2393 | ||
2394 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, | |
2395 | end_addr2, need_subpage); | |
2396 | ||
4254fab8 | 2397 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
db7b5426 | 2398 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
8da3ff18 | 2399 | &p->phys_offset, IO_MEM_UNASSIGNED, |
67c4d23c | 2400 | addr & TARGET_PAGE_MASK); |
db7b5426 | 2401 | subpage_register(subpage, start_addr2, end_addr2, |
8da3ff18 PB |
2402 | phys_offset, region_offset); |
2403 | p->region_offset = 0; | |
db7b5426 BS |
2404 | } |
2405 | } | |
2406 | } | |
8da3ff18 | 2407 | region_offset += TARGET_PAGE_SIZE; |
33417e70 | 2408 | } |
3b46e624 | 2409 | |
9d42037b FB |
2410 | /* since each CPU stores ram addresses in its TLB cache, we must |
2411 | reset the modified entries */ | |
2412 | /* XXX: slow ! */ | |
2413 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
2414 | tlb_flush(env, 1); | |
2415 | } | |
33417e70 FB |
2416 | } |
2417 | ||
ba863458 | 2418 | /* XXX: temporary until new memory mapping API */ |
00f82b8a | 2419 | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
ba863458 FB |
2420 | { |
2421 | PhysPageDesc *p; | |
2422 | ||
2423 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
2424 | if (!p) | |
2425 | return IO_MEM_UNASSIGNED; | |
2426 | return p->phys_offset; | |
2427 | } | |
2428 | ||
f65ed4c1 AL |
2429 | void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) |
2430 | { | |
2431 | if (kvm_enabled()) | |
2432 | kvm_coalesce_mmio_region(addr, size); | |
2433 | } | |
2434 | ||
2435 | void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) | |
2436 | { | |
2437 | if (kvm_enabled()) | |
2438 | kvm_uncoalesce_mmio_region(addr, size); | |
2439 | } | |
2440 | ||
640f42e4 | 2441 | #ifdef CONFIG_KQEMU |
e9a1ab19 | 2442 | /* XXX: better than nothing */ |
94a6b54f | 2443 | static ram_addr_t kqemu_ram_alloc(ram_addr_t size) |
e9a1ab19 FB |
2444 | { |
2445 | ram_addr_t addr; | |
94a6b54f | 2446 | if ((last_ram_offset + size) > kqemu_phys_ram_size) { |
012a7045 | 2447 | fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n", |
94a6b54f | 2448 | (uint64_t)size, (uint64_t)kqemu_phys_ram_size); |
e9a1ab19 FB |
2449 | abort(); |
2450 | } | |
94a6b54f PB |
2451 | addr = last_ram_offset; |
2452 | last_ram_offset = TARGET_PAGE_ALIGN(last_ram_offset + size); | |
e9a1ab19 FB |
2453 | return addr; |
2454 | } | |
94a6b54f PB |
2455 | #endif |
2456 | ||
2457 | ram_addr_t qemu_ram_alloc(ram_addr_t size) | |
2458 | { | |
2459 | RAMBlock *new_block; | |
2460 | ||
640f42e4 | 2461 | #ifdef CONFIG_KQEMU |
94a6b54f PB |
2462 | if (kqemu_phys_ram_base) { |
2463 | return kqemu_ram_alloc(size); | |
2464 | } | |
2465 | #endif | |
2466 | ||
2467 | size = TARGET_PAGE_ALIGN(size); | |
2468 | new_block = qemu_malloc(sizeof(*new_block)); | |
2469 | ||
2470 | new_block->host = qemu_vmalloc(size); | |
2471 | new_block->offset = last_ram_offset; | |
2472 | new_block->length = size; | |
2473 | ||
2474 | new_block->next = ram_blocks; | |
2475 | ram_blocks = new_block; | |
2476 | ||
2477 | phys_ram_dirty = qemu_realloc(phys_ram_dirty, | |
2478 | (last_ram_offset + size) >> TARGET_PAGE_BITS); | |
2479 | memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS), | |
2480 | 0xff, size >> TARGET_PAGE_BITS); | |
2481 | ||
2482 | last_ram_offset += size; | |
2483 | ||
6f0437e8 JK |
2484 | if (kvm_enabled()) |
2485 | kvm_setup_guest_memory(new_block->host, size); | |
2486 | ||
94a6b54f PB |
2487 | return new_block->offset; |
2488 | } | |
e9a1ab19 FB |
2489 | |
2490 | void qemu_ram_free(ram_addr_t addr) | |
2491 | { | |
94a6b54f | 2492 | /* TODO: implement this. */ |
e9a1ab19 FB |
2493 | } |
2494 | ||
dc828ca1 | 2495 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
5579c7f3 PB |
2496 | With the exception of the softmmu code in this file, this should |
2497 | only be used for local memory (e.g. video ram) that the device owns, | |
2498 | and knows it isn't going to access beyond the end of the block. | |
2499 | ||
2500 | It should not be used for general purpose DMA. | |
2501 | Use cpu_physical_memory_map/cpu_physical_memory_rw instead. | |
2502 | */ | |
dc828ca1 PB |
2503 | void *qemu_get_ram_ptr(ram_addr_t addr) |
2504 | { | |
94a6b54f PB |
2505 | RAMBlock *prev; |
2506 | RAMBlock **prevp; | |
2507 | RAMBlock *block; | |
2508 | ||
640f42e4 | 2509 | #ifdef CONFIG_KQEMU |
94a6b54f PB |
2510 | if (kqemu_phys_ram_base) { |
2511 | return kqemu_phys_ram_base + addr; | |
2512 | } | |
2513 | #endif | |
2514 | ||
2515 | prev = NULL; | |
2516 | prevp = &ram_blocks; | |
2517 | block = ram_blocks; | |
2518 | while (block && (block->offset > addr | |
2519 | || block->offset + block->length <= addr)) { | |
2520 | if (prev) | |
2521 | prevp = &prev->next; | |
2522 | prev = block; | |
2523 | block = block->next; | |
2524 | } | |
2525 | if (!block) { | |
2526 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
2527 | abort(); | |
2528 | } | |
2529 | /* Move this entry to to start of the list. */ | |
2530 | if (prev) { | |
2531 | prev->next = block->next; | |
2532 | block->next = *prevp; | |
2533 | *prevp = block; | |
2534 | } | |
2535 | return block->host + (addr - block->offset); | |
dc828ca1 PB |
2536 | } |
2537 | ||
5579c7f3 PB |
2538 | /* Some of the softmmu routines need to translate from a host pointer |
2539 | (typically a TLB entry) back to a ram offset. */ | |
2540 | ram_addr_t qemu_ram_addr_from_host(void *ptr) | |
2541 | { | |
94a6b54f PB |
2542 | RAMBlock *prev; |
2543 | RAMBlock **prevp; | |
2544 | RAMBlock *block; | |
2545 | uint8_t *host = ptr; | |
2546 | ||
640f42e4 | 2547 | #ifdef CONFIG_KQEMU |
94a6b54f PB |
2548 | if (kqemu_phys_ram_base) { |
2549 | return host - kqemu_phys_ram_base; | |
2550 | } | |
2551 | #endif | |
2552 | ||
2553 | prev = NULL; | |
2554 | prevp = &ram_blocks; | |
2555 | block = ram_blocks; | |
2556 | while (block && (block->host > host | |
2557 | || block->host + block->length <= host)) { | |
2558 | if (prev) | |
2559 | prevp = &prev->next; | |
2560 | prev = block; | |
2561 | block = block->next; | |
2562 | } | |
2563 | if (!block) { | |
2564 | fprintf(stderr, "Bad ram pointer %p\n", ptr); | |
2565 | abort(); | |
2566 | } | |
2567 | return block->offset + (host - block->host); | |
5579c7f3 PB |
2568 | } |
2569 | ||
a4193c8a | 2570 | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
33417e70 | 2571 | { |
67d3b957 | 2572 | #ifdef DEBUG_UNASSIGNED |
ab3d1727 | 2573 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
b4f0a316 | 2574 | #endif |
0a6f8a6d | 2575 | #if defined(TARGET_SPARC) |
e18231a3 BS |
2576 | do_unassigned_access(addr, 0, 0, 0, 1); |
2577 | #endif | |
2578 | return 0; | |
2579 | } | |
2580 | ||
2581 | static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr) | |
2582 | { | |
2583 | #ifdef DEBUG_UNASSIGNED | |
2584 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); | |
2585 | #endif | |
0a6f8a6d | 2586 | #if defined(TARGET_SPARC) |
e18231a3 BS |
2587 | do_unassigned_access(addr, 0, 0, 0, 2); |
2588 | #endif | |
2589 | return 0; | |
2590 | } | |
2591 | ||
2592 | static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr) | |
2593 | { | |
2594 | #ifdef DEBUG_UNASSIGNED | |
2595 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); | |
2596 | #endif | |
0a6f8a6d | 2597 | #if defined(TARGET_SPARC) |
e18231a3 | 2598 | do_unassigned_access(addr, 0, 0, 0, 4); |
67d3b957 | 2599 | #endif |
33417e70 FB |
2600 | return 0; |
2601 | } | |
2602 | ||
a4193c8a | 2603 | static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
33417e70 | 2604 | { |
67d3b957 | 2605 | #ifdef DEBUG_UNASSIGNED |
ab3d1727 | 2606 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
67d3b957 | 2607 | #endif |
0a6f8a6d | 2608 | #if defined(TARGET_SPARC) |
e18231a3 BS |
2609 | do_unassigned_access(addr, 1, 0, 0, 1); |
2610 | #endif | |
2611 | } | |
2612 | ||
2613 | static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) | |
2614 | { | |
2615 | #ifdef DEBUG_UNASSIGNED | |
2616 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); | |
2617 | #endif | |
0a6f8a6d | 2618 | #if defined(TARGET_SPARC) |
e18231a3 BS |
2619 | do_unassigned_access(addr, 1, 0, 0, 2); |
2620 | #endif | |
2621 | } | |
2622 | ||
2623 | static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) | |
2624 | { | |
2625 | #ifdef DEBUG_UNASSIGNED | |
2626 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); | |
2627 | #endif | |
0a6f8a6d | 2628 | #if defined(TARGET_SPARC) |
e18231a3 | 2629 | do_unassigned_access(addr, 1, 0, 0, 4); |
b4f0a316 | 2630 | #endif |
33417e70 FB |
2631 | } |
2632 | ||
2633 | static CPUReadMemoryFunc *unassigned_mem_read[3] = { | |
2634 | unassigned_mem_readb, | |
e18231a3 BS |
2635 | unassigned_mem_readw, |
2636 | unassigned_mem_readl, | |
33417e70 FB |
2637 | }; |
2638 | ||
2639 | static CPUWriteMemoryFunc *unassigned_mem_write[3] = { | |
2640 | unassigned_mem_writeb, | |
e18231a3 BS |
2641 | unassigned_mem_writew, |
2642 | unassigned_mem_writel, | |
33417e70 FB |
2643 | }; |
2644 | ||
0f459d16 PB |
2645 | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
2646 | uint32_t val) | |
9fa3e853 | 2647 | { |
3a7d929e | 2648 | int dirty_flags; |
3a7d929e FB |
2649 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2650 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { | |
9fa3e853 | 2651 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e FB |
2652 | tb_invalidate_phys_page_fast(ram_addr, 1); |
2653 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; | |
9fa3e853 | 2654 | #endif |
3a7d929e | 2655 | } |
5579c7f3 | 2656 | stb_p(qemu_get_ram_ptr(ram_addr), val); |
640f42e4 | 2657 | #ifdef CONFIG_KQEMU |
f32fc648 FB |
2658 | if (cpu_single_env->kqemu_enabled && |
2659 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) | |
2660 | kqemu_modify_page(cpu_single_env, ram_addr); | |
2661 | #endif | |
f23db169 FB |
2662 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
2663 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; | |
2664 | /* we remove the notdirty callback only if the code has been | |
2665 | flushed */ | |
2666 | if (dirty_flags == 0xff) | |
2e70f6ef | 2667 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
2668 | } |
2669 | ||
0f459d16 PB |
2670 | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
2671 | uint32_t val) | |
9fa3e853 | 2672 | { |
3a7d929e | 2673 | int dirty_flags; |
3a7d929e FB |
2674 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2675 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { | |
9fa3e853 | 2676 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e FB |
2677 | tb_invalidate_phys_page_fast(ram_addr, 2); |
2678 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; | |
9fa3e853 | 2679 | #endif |
3a7d929e | 2680 | } |
5579c7f3 | 2681 | stw_p(qemu_get_ram_ptr(ram_addr), val); |
640f42e4 | 2682 | #ifdef CONFIG_KQEMU |
f32fc648 FB |
2683 | if (cpu_single_env->kqemu_enabled && |
2684 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) | |
2685 | kqemu_modify_page(cpu_single_env, ram_addr); | |
2686 | #endif | |
f23db169 FB |
2687 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
2688 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; | |
2689 | /* we remove the notdirty callback only if the code has been | |
2690 | flushed */ | |
2691 | if (dirty_flags == 0xff) | |
2e70f6ef | 2692 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
2693 | } |
2694 | ||
0f459d16 PB |
2695 | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
2696 | uint32_t val) | |
9fa3e853 | 2697 | { |
3a7d929e | 2698 | int dirty_flags; |
3a7d929e FB |
2699 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2700 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { | |
9fa3e853 | 2701 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e FB |
2702 | tb_invalidate_phys_page_fast(ram_addr, 4); |
2703 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; | |
9fa3e853 | 2704 | #endif |
3a7d929e | 2705 | } |
5579c7f3 | 2706 | stl_p(qemu_get_ram_ptr(ram_addr), val); |
640f42e4 | 2707 | #ifdef CONFIG_KQEMU |
f32fc648 FB |
2708 | if (cpu_single_env->kqemu_enabled && |
2709 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) | |
2710 | kqemu_modify_page(cpu_single_env, ram_addr); | |
2711 | #endif | |
f23db169 FB |
2712 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
2713 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; | |
2714 | /* we remove the notdirty callback only if the code has been | |
2715 | flushed */ | |
2716 | if (dirty_flags == 0xff) | |
2e70f6ef | 2717 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
2718 | } |
2719 | ||
3a7d929e | 2720 | static CPUReadMemoryFunc *error_mem_read[3] = { |
9fa3e853 FB |
2721 | NULL, /* never used */ |
2722 | NULL, /* never used */ | |
2723 | NULL, /* never used */ | |
2724 | }; | |
2725 | ||
1ccde1cb FB |
2726 | static CPUWriteMemoryFunc *notdirty_mem_write[3] = { |
2727 | notdirty_mem_writeb, | |
2728 | notdirty_mem_writew, | |
2729 | notdirty_mem_writel, | |
2730 | }; | |
2731 | ||
0f459d16 | 2732 | /* Generate a debug exception if a watchpoint has been hit. */ |
b4051334 | 2733 | static void check_watchpoint(int offset, int len_mask, int flags) |
0f459d16 PB |
2734 | { |
2735 | CPUState *env = cpu_single_env; | |
06d55cc1 AL |
2736 | target_ulong pc, cs_base; |
2737 | TranslationBlock *tb; | |
0f459d16 | 2738 | target_ulong vaddr; |
a1d1bb31 | 2739 | CPUWatchpoint *wp; |
06d55cc1 | 2740 | int cpu_flags; |
0f459d16 | 2741 | |
06d55cc1 AL |
2742 | if (env->watchpoint_hit) { |
2743 | /* We re-entered the check after replacing the TB. Now raise | |
2744 | * the debug interrupt so that is will trigger after the | |
2745 | * current instruction. */ | |
2746 | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); | |
2747 | return; | |
2748 | } | |
2e70f6ef | 2749 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
c0ce998e | 2750 | TAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 AL |
2751 | if ((vaddr == (wp->vaddr & len_mask) || |
2752 | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { | |
6e140f28 AL |
2753 | wp->flags |= BP_WATCHPOINT_HIT; |
2754 | if (!env->watchpoint_hit) { | |
2755 | env->watchpoint_hit = wp; | |
2756 | tb = tb_find_pc(env->mem_io_pc); | |
2757 | if (!tb) { | |
2758 | cpu_abort(env, "check_watchpoint: could not find TB for " | |
2759 | "pc=%p", (void *)env->mem_io_pc); | |
2760 | } | |
2761 | cpu_restore_state(tb, env, env->mem_io_pc, NULL); | |
2762 | tb_phys_invalidate(tb, -1); | |
2763 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { | |
2764 | env->exception_index = EXCP_DEBUG; | |
2765 | } else { | |
2766 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); | |
2767 | tb_gen_code(env, pc, cs_base, cpu_flags, 1); | |
2768 | } | |
2769 | cpu_resume_from_signal(env, NULL); | |
06d55cc1 | 2770 | } |
6e140f28 AL |
2771 | } else { |
2772 | wp->flags &= ~BP_WATCHPOINT_HIT; | |
0f459d16 PB |
2773 | } |
2774 | } | |
2775 | } | |
2776 | ||
6658ffb8 PB |
2777 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
2778 | so these check for a hit then pass through to the normal out-of-line | |
2779 | phys routines. */ | |
2780 | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) | |
2781 | { | |
b4051334 | 2782 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ); |
6658ffb8 PB |
2783 | return ldub_phys(addr); |
2784 | } | |
2785 | ||
2786 | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) | |
2787 | { | |
b4051334 | 2788 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ); |
6658ffb8 PB |
2789 | return lduw_phys(addr); |
2790 | } | |
2791 | ||
2792 | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) | |
2793 | { | |
b4051334 | 2794 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ); |
6658ffb8 PB |
2795 | return ldl_phys(addr); |
2796 | } | |
2797 | ||
6658ffb8 PB |
2798 | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
2799 | uint32_t val) | |
2800 | { | |
b4051334 | 2801 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE); |
6658ffb8 PB |
2802 | stb_phys(addr, val); |
2803 | } | |
2804 | ||
2805 | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, | |
2806 | uint32_t val) | |
2807 | { | |
b4051334 | 2808 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE); |
6658ffb8 PB |
2809 | stw_phys(addr, val); |
2810 | } | |
2811 | ||
2812 | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, | |
2813 | uint32_t val) | |
2814 | { | |
b4051334 | 2815 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE); |
6658ffb8 PB |
2816 | stl_phys(addr, val); |
2817 | } | |
2818 | ||
2819 | static CPUReadMemoryFunc *watch_mem_read[3] = { | |
2820 | watch_mem_readb, | |
2821 | watch_mem_readw, | |
2822 | watch_mem_readl, | |
2823 | }; | |
2824 | ||
2825 | static CPUWriteMemoryFunc *watch_mem_write[3] = { | |
2826 | watch_mem_writeb, | |
2827 | watch_mem_writew, | |
2828 | watch_mem_writel, | |
2829 | }; | |
6658ffb8 | 2830 | |
db7b5426 BS |
2831 | static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr, |
2832 | unsigned int len) | |
2833 | { | |
db7b5426 BS |
2834 | uint32_t ret; |
2835 | unsigned int idx; | |
2836 | ||
8da3ff18 | 2837 | idx = SUBPAGE_IDX(addr); |
db7b5426 BS |
2838 | #if defined(DEBUG_SUBPAGE) |
2839 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, | |
2840 | mmio, len, addr, idx); | |
2841 | #endif | |
8da3ff18 PB |
2842 | ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], |
2843 | addr + mmio->region_offset[idx][0][len]); | |
db7b5426 BS |
2844 | |
2845 | return ret; | |
2846 | } | |
2847 | ||
2848 | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, | |
2849 | uint32_t value, unsigned int len) | |
2850 | { | |
db7b5426 BS |
2851 | unsigned int idx; |
2852 | ||
8da3ff18 | 2853 | idx = SUBPAGE_IDX(addr); |
db7b5426 BS |
2854 | #if defined(DEBUG_SUBPAGE) |
2855 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__, | |
2856 | mmio, len, addr, idx, value); | |
2857 | #endif | |
8da3ff18 PB |
2858 | (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], |
2859 | addr + mmio->region_offset[idx][1][len], | |
2860 | value); | |
db7b5426 BS |
2861 | } |
2862 | ||
2863 | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) | |
2864 | { | |
2865 | #if defined(DEBUG_SUBPAGE) | |
2866 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); | |
2867 | #endif | |
2868 | ||
2869 | return subpage_readlen(opaque, addr, 0); | |
2870 | } | |
2871 | ||
2872 | static void subpage_writeb (void *opaque, target_phys_addr_t addr, | |
2873 | uint32_t value) | |
2874 | { | |
2875 | #if defined(DEBUG_SUBPAGE) | |
2876 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); | |
2877 | #endif | |
2878 | subpage_writelen(opaque, addr, value, 0); | |
2879 | } | |
2880 | ||
2881 | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) | |
2882 | { | |
2883 | #if defined(DEBUG_SUBPAGE) | |
2884 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); | |
2885 | #endif | |
2886 | ||
2887 | return subpage_readlen(opaque, addr, 1); | |
2888 | } | |
2889 | ||
2890 | static void subpage_writew (void *opaque, target_phys_addr_t addr, | |
2891 | uint32_t value) | |
2892 | { | |
2893 | #if defined(DEBUG_SUBPAGE) | |
2894 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); | |
2895 | #endif | |
2896 | subpage_writelen(opaque, addr, value, 1); | |
2897 | } | |
2898 | ||
2899 | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) | |
2900 | { | |
2901 | #if defined(DEBUG_SUBPAGE) | |
2902 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); | |
2903 | #endif | |
2904 | ||
2905 | return subpage_readlen(opaque, addr, 2); | |
2906 | } | |
2907 | ||
2908 | static void subpage_writel (void *opaque, | |
2909 | target_phys_addr_t addr, uint32_t value) | |
2910 | { | |
2911 | #if defined(DEBUG_SUBPAGE) | |
2912 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); | |
2913 | #endif | |
2914 | subpage_writelen(opaque, addr, value, 2); | |
2915 | } | |
2916 | ||
2917 | static CPUReadMemoryFunc *subpage_read[] = { | |
2918 | &subpage_readb, | |
2919 | &subpage_readw, | |
2920 | &subpage_readl, | |
2921 | }; | |
2922 | ||
2923 | static CPUWriteMemoryFunc *subpage_write[] = { | |
2924 | &subpage_writeb, | |
2925 | &subpage_writew, | |
2926 | &subpage_writel, | |
2927 | }; | |
2928 | ||
2929 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, | |
8da3ff18 | 2930 | ram_addr_t memory, ram_addr_t region_offset) |
db7b5426 BS |
2931 | { |
2932 | int idx, eidx; | |
4254fab8 | 2933 | unsigned int i; |
db7b5426 BS |
2934 | |
2935 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) | |
2936 | return -1; | |
2937 | idx = SUBPAGE_IDX(start); | |
2938 | eidx = SUBPAGE_IDX(end); | |
2939 | #if defined(DEBUG_SUBPAGE) | |
2940 | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__, | |
2941 | mmio, start, end, idx, eidx, memory); | |
2942 | #endif | |
2943 | memory >>= IO_MEM_SHIFT; | |
2944 | for (; idx <= eidx; idx++) { | |
4254fab8 | 2945 | for (i = 0; i < 4; i++) { |
3ee89922 BS |
2946 | if (io_mem_read[memory][i]) { |
2947 | mmio->mem_read[idx][i] = &io_mem_read[memory][i]; | |
2948 | mmio->opaque[idx][0][i] = io_mem_opaque[memory]; | |
8da3ff18 | 2949 | mmio->region_offset[idx][0][i] = region_offset; |
3ee89922 BS |
2950 | } |
2951 | if (io_mem_write[memory][i]) { | |
2952 | mmio->mem_write[idx][i] = &io_mem_write[memory][i]; | |
2953 | mmio->opaque[idx][1][i] = io_mem_opaque[memory]; | |
8da3ff18 | 2954 | mmio->region_offset[idx][1][i] = region_offset; |
3ee89922 | 2955 | } |
4254fab8 | 2956 | } |
db7b5426 BS |
2957 | } |
2958 | ||
2959 | return 0; | |
2960 | } | |
2961 | ||
00f82b8a | 2962 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
8da3ff18 | 2963 | ram_addr_t orig_memory, ram_addr_t region_offset) |
db7b5426 BS |
2964 | { |
2965 | subpage_t *mmio; | |
2966 | int subpage_memory; | |
2967 | ||
2968 | mmio = qemu_mallocz(sizeof(subpage_t)); | |
1eec614b AL |
2969 | |
2970 | mmio->base = base; | |
1eed09cb | 2971 | subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio); |
db7b5426 | 2972 | #if defined(DEBUG_SUBPAGE) |
1eec614b AL |
2973 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
2974 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); | |
db7b5426 | 2975 | #endif |
1eec614b AL |
2976 | *phys = subpage_memory | IO_MEM_SUBPAGE; |
2977 | subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory, | |
8da3ff18 | 2978 | region_offset); |
db7b5426 BS |
2979 | |
2980 | return mmio; | |
2981 | } | |
2982 | ||
88715657 AL |
2983 | static int get_free_io_mem_idx(void) |
2984 | { | |
2985 | int i; | |
2986 | ||
2987 | for (i = 0; i<IO_MEM_NB_ENTRIES; i++) | |
2988 | if (!io_mem_used[i]) { | |
2989 | io_mem_used[i] = 1; | |
2990 | return i; | |
2991 | } | |
2992 | ||
2993 | return -1; | |
2994 | } | |
2995 | ||
33417e70 FB |
2996 | /* mem_read and mem_write are arrays of functions containing the |
2997 | function to access byte (index 0), word (index 1) and dword (index | |
0b4e6e3e | 2998 | 2). Functions can be omitted with a NULL function pointer. |
3ee89922 | 2999 | If io_index is non zero, the corresponding io zone is |
4254fab8 BS |
3000 | modified. If it is zero, a new io zone is allocated. The return |
3001 | value can be used with cpu_register_physical_memory(). (-1) is | |
3002 | returned if error. */ | |
1eed09cb AK |
3003 | static int cpu_register_io_memory_fixed(int io_index, |
3004 | CPUReadMemoryFunc **mem_read, | |
3005 | CPUWriteMemoryFunc **mem_write, | |
3006 | void *opaque) | |
33417e70 | 3007 | { |
4254fab8 | 3008 | int i, subwidth = 0; |
33417e70 FB |
3009 | |
3010 | if (io_index <= 0) { | |
88715657 AL |
3011 | io_index = get_free_io_mem_idx(); |
3012 | if (io_index == -1) | |
3013 | return io_index; | |
33417e70 | 3014 | } else { |
1eed09cb | 3015 | io_index >>= IO_MEM_SHIFT; |
33417e70 FB |
3016 | if (io_index >= IO_MEM_NB_ENTRIES) |
3017 | return -1; | |
3018 | } | |
b5ff1b31 | 3019 | |
33417e70 | 3020 | for(i = 0;i < 3; i++) { |
4254fab8 BS |
3021 | if (!mem_read[i] || !mem_write[i]) |
3022 | subwidth = IO_MEM_SUBWIDTH; | |
33417e70 FB |
3023 | io_mem_read[io_index][i] = mem_read[i]; |
3024 | io_mem_write[io_index][i] = mem_write[i]; | |
3025 | } | |
a4193c8a | 3026 | io_mem_opaque[io_index] = opaque; |
4254fab8 | 3027 | return (io_index << IO_MEM_SHIFT) | subwidth; |
33417e70 | 3028 | } |
61382a50 | 3029 | |
1eed09cb AK |
3030 | int cpu_register_io_memory(CPUReadMemoryFunc **mem_read, |
3031 | CPUWriteMemoryFunc **mem_write, | |
3032 | void *opaque) | |
3033 | { | |
3034 | return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque); | |
3035 | } | |
3036 | ||
88715657 AL |
3037 | void cpu_unregister_io_memory(int io_table_address) |
3038 | { | |
3039 | int i; | |
3040 | int io_index = io_table_address >> IO_MEM_SHIFT; | |
3041 | ||
3042 | for (i=0;i < 3; i++) { | |
3043 | io_mem_read[io_index][i] = unassigned_mem_read[i]; | |
3044 | io_mem_write[io_index][i] = unassigned_mem_write[i]; | |
3045 | } | |
3046 | io_mem_opaque[io_index] = NULL; | |
3047 | io_mem_used[io_index] = 0; | |
3048 | } | |
3049 | ||
e9179ce1 AK |
3050 | static void io_mem_init(void) |
3051 | { | |
3052 | int i; | |
3053 | ||
3054 | cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL); | |
3055 | cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL); | |
3056 | cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL); | |
3057 | for (i=0; i<5; i++) | |
3058 | io_mem_used[i] = 1; | |
3059 | ||
3060 | io_mem_watch = cpu_register_io_memory(watch_mem_read, | |
3061 | watch_mem_write, NULL); | |
3062 | #ifdef CONFIG_KQEMU | |
3063 | if (kqemu_phys_ram_base) { | |
3064 | /* alloc dirty bits array */ | |
3065 | phys_ram_dirty = qemu_vmalloc(kqemu_phys_ram_size >> TARGET_PAGE_BITS); | |
3066 | memset(phys_ram_dirty, 0xff, kqemu_phys_ram_size >> TARGET_PAGE_BITS); | |
3067 | } | |
3068 | #endif | |
3069 | } | |
3070 | ||
e2eef170 PB |
3071 | #endif /* !defined(CONFIG_USER_ONLY) */ |
3072 | ||
13eb76e0 FB |
3073 | /* physical memory access (slow version, mainly for debug) */ |
3074 | #if defined(CONFIG_USER_ONLY) | |
5fafdf24 | 3075 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
13eb76e0 FB |
3076 | int len, int is_write) |
3077 | { | |
3078 | int l, flags; | |
3079 | target_ulong page; | |
53a5960a | 3080 | void * p; |
13eb76e0 FB |
3081 | |
3082 | while (len > 0) { | |
3083 | page = addr & TARGET_PAGE_MASK; | |
3084 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3085 | if (l > len) | |
3086 | l = len; | |
3087 | flags = page_get_flags(page); | |
3088 | if (!(flags & PAGE_VALID)) | |
3089 | return; | |
3090 | if (is_write) { | |
3091 | if (!(flags & PAGE_WRITE)) | |
3092 | return; | |
579a97f7 | 3093 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 3094 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
579a97f7 FB |
3095 | /* FIXME - should this return an error rather than just fail? */ |
3096 | return; | |
72fb7daa AJ |
3097 | memcpy(p, buf, l); |
3098 | unlock_user(p, addr, l); | |
13eb76e0 FB |
3099 | } else { |
3100 | if (!(flags & PAGE_READ)) | |
3101 | return; | |
579a97f7 | 3102 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 3103 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
579a97f7 FB |
3104 | /* FIXME - should this return an error rather than just fail? */ |
3105 | return; | |
72fb7daa | 3106 | memcpy(buf, p, l); |
5b257578 | 3107 | unlock_user(p, addr, 0); |
13eb76e0 FB |
3108 | } |
3109 | len -= l; | |
3110 | buf += l; | |
3111 | addr += l; | |
3112 | } | |
3113 | } | |
8df1cd07 | 3114 | |
13eb76e0 | 3115 | #else |
5fafdf24 | 3116 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
13eb76e0 FB |
3117 | int len, int is_write) |
3118 | { | |
3119 | int l, io_index; | |
3120 | uint8_t *ptr; | |
3121 | uint32_t val; | |
2e12669a FB |
3122 | target_phys_addr_t page; |
3123 | unsigned long pd; | |
92e873b9 | 3124 | PhysPageDesc *p; |
3b46e624 | 3125 | |
13eb76e0 FB |
3126 | while (len > 0) { |
3127 | page = addr & TARGET_PAGE_MASK; | |
3128 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3129 | if (l > len) | |
3130 | l = len; | |
92e873b9 | 3131 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
13eb76e0 FB |
3132 | if (!p) { |
3133 | pd = IO_MEM_UNASSIGNED; | |
3134 | } else { | |
3135 | pd = p->phys_offset; | |
3136 | } | |
3b46e624 | 3137 | |
13eb76e0 | 3138 | if (is_write) { |
3a7d929e | 3139 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
6c2934db | 3140 | target_phys_addr_t addr1 = addr; |
13eb76e0 | 3141 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 | 3142 | if (p) |
6c2934db | 3143 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
6a00d601 FB |
3144 | /* XXX: could force cpu_single_env to NULL to avoid |
3145 | potential bugs */ | |
6c2934db | 3146 | if (l >= 4 && ((addr1 & 3) == 0)) { |
1c213d19 | 3147 | /* 32 bit write access */ |
c27004ec | 3148 | val = ldl_p(buf); |
6c2934db | 3149 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val); |
13eb76e0 | 3150 | l = 4; |
6c2934db | 3151 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
1c213d19 | 3152 | /* 16 bit write access */ |
c27004ec | 3153 | val = lduw_p(buf); |
6c2934db | 3154 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val); |
13eb76e0 FB |
3155 | l = 2; |
3156 | } else { | |
1c213d19 | 3157 | /* 8 bit write access */ |
c27004ec | 3158 | val = ldub_p(buf); |
6c2934db | 3159 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val); |
13eb76e0 FB |
3160 | l = 1; |
3161 | } | |
3162 | } else { | |
b448f2f3 FB |
3163 | unsigned long addr1; |
3164 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
13eb76e0 | 3165 | /* RAM case */ |
5579c7f3 | 3166 | ptr = qemu_get_ram_ptr(addr1); |
13eb76e0 | 3167 | memcpy(ptr, buf, l); |
3a7d929e FB |
3168 | if (!cpu_physical_memory_is_dirty(addr1)) { |
3169 | /* invalidate code */ | |
3170 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); | |
3171 | /* set dirty bit */ | |
5fafdf24 | 3172 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
f23db169 | 3173 | (0xff & ~CODE_DIRTY_FLAG); |
3a7d929e | 3174 | } |
13eb76e0 FB |
3175 | } |
3176 | } else { | |
5fafdf24 | 3177 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
2a4188a3 | 3178 | !(pd & IO_MEM_ROMD)) { |
6c2934db | 3179 | target_phys_addr_t addr1 = addr; |
13eb76e0 FB |
3180 | /* I/O case */ |
3181 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 | 3182 | if (p) |
6c2934db AJ |
3183 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3184 | if (l >= 4 && ((addr1 & 3) == 0)) { | |
13eb76e0 | 3185 | /* 32 bit read access */ |
6c2934db | 3186 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1); |
c27004ec | 3187 | stl_p(buf, val); |
13eb76e0 | 3188 | l = 4; |
6c2934db | 3189 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
13eb76e0 | 3190 | /* 16 bit read access */ |
6c2934db | 3191 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1); |
c27004ec | 3192 | stw_p(buf, val); |
13eb76e0 FB |
3193 | l = 2; |
3194 | } else { | |
1c213d19 | 3195 | /* 8 bit read access */ |
6c2934db | 3196 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1); |
c27004ec | 3197 | stb_p(buf, val); |
13eb76e0 FB |
3198 | l = 1; |
3199 | } | |
3200 | } else { | |
3201 | /* RAM case */ | |
5579c7f3 | 3202 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
13eb76e0 FB |
3203 | (addr & ~TARGET_PAGE_MASK); |
3204 | memcpy(buf, ptr, l); | |
3205 | } | |
3206 | } | |
3207 | len -= l; | |
3208 | buf += l; | |
3209 | addr += l; | |
3210 | } | |
3211 | } | |
8df1cd07 | 3212 | |
d0ecd2aa | 3213 | /* used for ROM loading : can write in RAM and ROM */ |
5fafdf24 | 3214 | void cpu_physical_memory_write_rom(target_phys_addr_t addr, |
d0ecd2aa FB |
3215 | const uint8_t *buf, int len) |
3216 | { | |
3217 | int l; | |
3218 | uint8_t *ptr; | |
3219 | target_phys_addr_t page; | |
3220 | unsigned long pd; | |
3221 | PhysPageDesc *p; | |
3b46e624 | 3222 | |
d0ecd2aa FB |
3223 | while (len > 0) { |
3224 | page = addr & TARGET_PAGE_MASK; | |
3225 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3226 | if (l > len) | |
3227 | l = len; | |
3228 | p = phys_page_find(page >> TARGET_PAGE_BITS); | |
3229 | if (!p) { | |
3230 | pd = IO_MEM_UNASSIGNED; | |
3231 | } else { | |
3232 | pd = p->phys_offset; | |
3233 | } | |
3b46e624 | 3234 | |
d0ecd2aa | 3235 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM && |
2a4188a3 FB |
3236 | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
3237 | !(pd & IO_MEM_ROMD)) { | |
d0ecd2aa FB |
3238 | /* do nothing */ |
3239 | } else { | |
3240 | unsigned long addr1; | |
3241 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
3242 | /* ROM/RAM case */ | |
5579c7f3 | 3243 | ptr = qemu_get_ram_ptr(addr1); |
d0ecd2aa FB |
3244 | memcpy(ptr, buf, l); |
3245 | } | |
3246 | len -= l; | |
3247 | buf += l; | |
3248 | addr += l; | |
3249 | } | |
3250 | } | |
3251 | ||
6d16c2f8 AL |
3252 | typedef struct { |
3253 | void *buffer; | |
3254 | target_phys_addr_t addr; | |
3255 | target_phys_addr_t len; | |
3256 | } BounceBuffer; | |
3257 | ||
3258 | static BounceBuffer bounce; | |
3259 | ||
ba223c29 AL |
3260 | typedef struct MapClient { |
3261 | void *opaque; | |
3262 | void (*callback)(void *opaque); | |
3263 | LIST_ENTRY(MapClient) link; | |
3264 | } MapClient; | |
3265 | ||
3266 | static LIST_HEAD(map_client_list, MapClient) map_client_list | |
3267 | = LIST_HEAD_INITIALIZER(map_client_list); | |
3268 | ||
3269 | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) | |
3270 | { | |
3271 | MapClient *client = qemu_malloc(sizeof(*client)); | |
3272 | ||
3273 | client->opaque = opaque; | |
3274 | client->callback = callback; | |
3275 | LIST_INSERT_HEAD(&map_client_list, client, link); | |
3276 | return client; | |
3277 | } | |
3278 | ||
3279 | void cpu_unregister_map_client(void *_client) | |
3280 | { | |
3281 | MapClient *client = (MapClient *)_client; | |
3282 | ||
3283 | LIST_REMOVE(client, link); | |
3284 | } | |
3285 | ||
3286 | static void cpu_notify_map_clients(void) | |
3287 | { | |
3288 | MapClient *client; | |
3289 | ||
3290 | while (!LIST_EMPTY(&map_client_list)) { | |
3291 | client = LIST_FIRST(&map_client_list); | |
3292 | client->callback(client->opaque); | |
3293 | LIST_REMOVE(client, link); | |
3294 | } | |
3295 | } | |
3296 | ||
6d16c2f8 AL |
3297 | /* Map a physical memory region into a host virtual address. |
3298 | * May map a subset of the requested range, given by and returned in *plen. | |
3299 | * May return NULL if resources needed to perform the mapping are exhausted. | |
3300 | * Use only for reads OR writes - not for read-modify-write operations. | |
ba223c29 AL |
3301 | * Use cpu_register_map_client() to know when retrying the map operation is |
3302 | * likely to succeed. | |
6d16c2f8 AL |
3303 | */ |
3304 | void *cpu_physical_memory_map(target_phys_addr_t addr, | |
3305 | target_phys_addr_t *plen, | |
3306 | int is_write) | |
3307 | { | |
3308 | target_phys_addr_t len = *plen; | |
3309 | target_phys_addr_t done = 0; | |
3310 | int l; | |
3311 | uint8_t *ret = NULL; | |
3312 | uint8_t *ptr; | |
3313 | target_phys_addr_t page; | |
3314 | unsigned long pd; | |
3315 | PhysPageDesc *p; | |
3316 | unsigned long addr1; | |
3317 | ||
3318 | while (len > 0) { | |
3319 | page = addr & TARGET_PAGE_MASK; | |
3320 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3321 | if (l > len) | |
3322 | l = len; | |
3323 | p = phys_page_find(page >> TARGET_PAGE_BITS); | |
3324 | if (!p) { | |
3325 | pd = IO_MEM_UNASSIGNED; | |
3326 | } else { | |
3327 | pd = p->phys_offset; | |
3328 | } | |
3329 | ||
3330 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { | |
3331 | if (done || bounce.buffer) { | |
3332 | break; | |
3333 | } | |
3334 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); | |
3335 | bounce.addr = addr; | |
3336 | bounce.len = l; | |
3337 | if (!is_write) { | |
3338 | cpu_physical_memory_rw(addr, bounce.buffer, l, 0); | |
3339 | } | |
3340 | ptr = bounce.buffer; | |
3341 | } else { | |
3342 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
5579c7f3 | 3343 | ptr = qemu_get_ram_ptr(addr1); |
6d16c2f8 AL |
3344 | } |
3345 | if (!done) { | |
3346 | ret = ptr; | |
3347 | } else if (ret + done != ptr) { | |
3348 | break; | |
3349 | } | |
3350 | ||
3351 | len -= l; | |
3352 | addr += l; | |
3353 | done += l; | |
3354 | } | |
3355 | *plen = done; | |
3356 | return ret; | |
3357 | } | |
3358 | ||
3359 | /* Unmaps a memory region previously mapped by cpu_physical_memory_map(). | |
3360 | * Will also mark the memory as dirty if is_write == 1. access_len gives | |
3361 | * the amount of memory that was actually read or written by the caller. | |
3362 | */ | |
3363 | void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len, | |
3364 | int is_write, target_phys_addr_t access_len) | |
3365 | { | |
3366 | if (buffer != bounce.buffer) { | |
3367 | if (is_write) { | |
5579c7f3 | 3368 | ram_addr_t addr1 = qemu_ram_addr_from_host(buffer); |
6d16c2f8 AL |
3369 | while (access_len) { |
3370 | unsigned l; | |
3371 | l = TARGET_PAGE_SIZE; | |
3372 | if (l > access_len) | |
3373 | l = access_len; | |
3374 | if (!cpu_physical_memory_is_dirty(addr1)) { | |
3375 | /* invalidate code */ | |
3376 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); | |
3377 | /* set dirty bit */ | |
3378 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= | |
3379 | (0xff & ~CODE_DIRTY_FLAG); | |
3380 | } | |
3381 | addr1 += l; | |
3382 | access_len -= l; | |
3383 | } | |
3384 | } | |
3385 | return; | |
3386 | } | |
3387 | if (is_write) { | |
3388 | cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len); | |
3389 | } | |
3390 | qemu_free(bounce.buffer); | |
3391 | bounce.buffer = NULL; | |
ba223c29 | 3392 | cpu_notify_map_clients(); |
6d16c2f8 | 3393 | } |
d0ecd2aa | 3394 | |
8df1cd07 FB |
3395 | /* warning: addr must be aligned */ |
3396 | uint32_t ldl_phys(target_phys_addr_t addr) | |
3397 | { | |
3398 | int io_index; | |
3399 | uint8_t *ptr; | |
3400 | uint32_t val; | |
3401 | unsigned long pd; | |
3402 | PhysPageDesc *p; | |
3403 | ||
3404 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
3405 | if (!p) { | |
3406 | pd = IO_MEM_UNASSIGNED; | |
3407 | } else { | |
3408 | pd = p->phys_offset; | |
3409 | } | |
3b46e624 | 3410 | |
5fafdf24 | 3411 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
2a4188a3 | 3412 | !(pd & IO_MEM_ROMD)) { |
8df1cd07 FB |
3413 | /* I/O case */ |
3414 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
3415 | if (p) |
3416 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
8df1cd07 FB |
3417 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
3418 | } else { | |
3419 | /* RAM case */ | |
5579c7f3 | 3420 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
8df1cd07 FB |
3421 | (addr & ~TARGET_PAGE_MASK); |
3422 | val = ldl_p(ptr); | |
3423 | } | |
3424 | return val; | |
3425 | } | |
3426 | ||
84b7b8e7 FB |
3427 | /* warning: addr must be aligned */ |
3428 | uint64_t ldq_phys(target_phys_addr_t addr) | |
3429 | { | |
3430 | int io_index; | |
3431 | uint8_t *ptr; | |
3432 | uint64_t val; | |
3433 | unsigned long pd; | |
3434 | PhysPageDesc *p; | |
3435 | ||
3436 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
3437 | if (!p) { | |
3438 | pd = IO_MEM_UNASSIGNED; | |
3439 | } else { | |
3440 | pd = p->phys_offset; | |
3441 | } | |
3b46e624 | 3442 | |
2a4188a3 FB |
3443 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
3444 | !(pd & IO_MEM_ROMD)) { | |
84b7b8e7 FB |
3445 | /* I/O case */ |
3446 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
3447 | if (p) |
3448 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
84b7b8e7 FB |
3449 | #ifdef TARGET_WORDS_BIGENDIAN |
3450 | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; | |
3451 | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); | |
3452 | #else | |
3453 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); | |
3454 | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; | |
3455 | #endif | |
3456 | } else { | |
3457 | /* RAM case */ | |
5579c7f3 | 3458 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
84b7b8e7 FB |
3459 | (addr & ~TARGET_PAGE_MASK); |
3460 | val = ldq_p(ptr); | |
3461 | } | |
3462 | return val; | |
3463 | } | |
3464 | ||
aab33094 FB |
3465 | /* XXX: optimize */ |
3466 | uint32_t ldub_phys(target_phys_addr_t addr) | |
3467 | { | |
3468 | uint8_t val; | |
3469 | cpu_physical_memory_read(addr, &val, 1); | |
3470 | return val; | |
3471 | } | |
3472 | ||
3473 | /* XXX: optimize */ | |
3474 | uint32_t lduw_phys(target_phys_addr_t addr) | |
3475 | { | |
3476 | uint16_t val; | |
3477 | cpu_physical_memory_read(addr, (uint8_t *)&val, 2); | |
3478 | return tswap16(val); | |
3479 | } | |
3480 | ||
8df1cd07 FB |
3481 | /* warning: addr must be aligned. The ram page is not masked as dirty |
3482 | and the code inside is not invalidated. It is useful if the dirty | |
3483 | bits are used to track modified PTEs */ | |
3484 | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val) | |
3485 | { | |
3486 | int io_index; | |
3487 | uint8_t *ptr; | |
3488 | unsigned long pd; | |
3489 | PhysPageDesc *p; | |
3490 | ||
3491 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
3492 | if (!p) { | |
3493 | pd = IO_MEM_UNASSIGNED; | |
3494 | } else { | |
3495 | pd = p->phys_offset; | |
3496 | } | |
3b46e624 | 3497 | |
3a7d929e | 3498 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
8df1cd07 | 3499 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 PB |
3500 | if (p) |
3501 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
8df1cd07 FB |
3502 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
3503 | } else { | |
74576198 | 3504 | unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
5579c7f3 | 3505 | ptr = qemu_get_ram_ptr(addr1); |
8df1cd07 | 3506 | stl_p(ptr, val); |
74576198 AL |
3507 | |
3508 | if (unlikely(in_migration)) { | |
3509 | if (!cpu_physical_memory_is_dirty(addr1)) { | |
3510 | /* invalidate code */ | |
3511 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); | |
3512 | /* set dirty bit */ | |
3513 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= | |
3514 | (0xff & ~CODE_DIRTY_FLAG); | |
3515 | } | |
3516 | } | |
8df1cd07 FB |
3517 | } |
3518 | } | |
3519 | ||
bc98a7ef JM |
3520 | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val) |
3521 | { | |
3522 | int io_index; | |
3523 | uint8_t *ptr; | |
3524 | unsigned long pd; | |
3525 | PhysPageDesc *p; | |
3526 | ||
3527 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
3528 | if (!p) { | |
3529 | pd = IO_MEM_UNASSIGNED; | |
3530 | } else { | |
3531 | pd = p->phys_offset; | |
3532 | } | |
3b46e624 | 3533 | |
bc98a7ef JM |
3534 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
3535 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
3536 | if (p) |
3537 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
bc98a7ef JM |
3538 | #ifdef TARGET_WORDS_BIGENDIAN |
3539 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); | |
3540 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); | |
3541 | #else | |
3542 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); | |
3543 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); | |
3544 | #endif | |
3545 | } else { | |
5579c7f3 | 3546 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bc98a7ef JM |
3547 | (addr & ~TARGET_PAGE_MASK); |
3548 | stq_p(ptr, val); | |
3549 | } | |
3550 | } | |
3551 | ||
8df1cd07 | 3552 | /* warning: addr must be aligned */ |
8df1cd07 FB |
3553 | void stl_phys(target_phys_addr_t addr, uint32_t val) |
3554 | { | |
3555 | int io_index; | |
3556 | uint8_t *ptr; | |
3557 | unsigned long pd; | |
3558 | PhysPageDesc *p; | |
3559 | ||
3560 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
3561 | if (!p) { | |
3562 | pd = IO_MEM_UNASSIGNED; | |
3563 | } else { | |
3564 | pd = p->phys_offset; | |
3565 | } | |
3b46e624 | 3566 | |
3a7d929e | 3567 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
8df1cd07 | 3568 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 PB |
3569 | if (p) |
3570 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
8df1cd07 FB |
3571 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
3572 | } else { | |
3573 | unsigned long addr1; | |
3574 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
3575 | /* RAM case */ | |
5579c7f3 | 3576 | ptr = qemu_get_ram_ptr(addr1); |
8df1cd07 | 3577 | stl_p(ptr, val); |
3a7d929e FB |
3578 | if (!cpu_physical_memory_is_dirty(addr1)) { |
3579 | /* invalidate code */ | |
3580 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); | |
3581 | /* set dirty bit */ | |
f23db169 FB |
3582 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
3583 | (0xff & ~CODE_DIRTY_FLAG); | |
3a7d929e | 3584 | } |
8df1cd07 FB |
3585 | } |
3586 | } | |
3587 | ||
aab33094 FB |
3588 | /* XXX: optimize */ |
3589 | void stb_phys(target_phys_addr_t addr, uint32_t val) | |
3590 | { | |
3591 | uint8_t v = val; | |
3592 | cpu_physical_memory_write(addr, &v, 1); | |
3593 | } | |
3594 | ||
3595 | /* XXX: optimize */ | |
3596 | void stw_phys(target_phys_addr_t addr, uint32_t val) | |
3597 | { | |
3598 | uint16_t v = tswap16(val); | |
3599 | cpu_physical_memory_write(addr, (const uint8_t *)&v, 2); | |
3600 | } | |
3601 | ||
3602 | /* XXX: optimize */ | |
3603 | void stq_phys(target_phys_addr_t addr, uint64_t val) | |
3604 | { | |
3605 | val = tswap64(val); | |
3606 | cpu_physical_memory_write(addr, (const uint8_t *)&val, 8); | |
3607 | } | |
3608 | ||
13eb76e0 FB |
3609 | #endif |
3610 | ||
5e2972fd | 3611 | /* virtual memory access for debug (includes writing to ROM) */ |
5fafdf24 | 3612 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
b448f2f3 | 3613 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
3614 | { |
3615 | int l; | |
9b3c35e0 JM |
3616 | target_phys_addr_t phys_addr; |
3617 | target_ulong page; | |
13eb76e0 FB |
3618 | |
3619 | while (len > 0) { | |
3620 | page = addr & TARGET_PAGE_MASK; | |
3621 | phys_addr = cpu_get_phys_page_debug(env, page); | |
3622 | /* if no physical page mapped, return an error */ | |
3623 | if (phys_addr == -1) | |
3624 | return -1; | |
3625 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3626 | if (l > len) | |
3627 | l = len; | |
5e2972fd AL |
3628 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
3629 | #if !defined(CONFIG_USER_ONLY) | |
3630 | if (is_write) | |
3631 | cpu_physical_memory_write_rom(phys_addr, buf, l); | |
3632 | else | |
3633 | #endif | |
3634 | cpu_physical_memory_rw(phys_addr, buf, l, is_write); | |
13eb76e0 FB |
3635 | len -= l; |
3636 | buf += l; | |
3637 | addr += l; | |
3638 | } | |
3639 | return 0; | |
3640 | } | |
3641 | ||
2e70f6ef PB |
3642 | /* in deterministic execution mode, instructions doing device I/Os |
3643 | must be at the end of the TB */ | |
3644 | void cpu_io_recompile(CPUState *env, void *retaddr) | |
3645 | { | |
3646 | TranslationBlock *tb; | |
3647 | uint32_t n, cflags; | |
3648 | target_ulong pc, cs_base; | |
3649 | uint64_t flags; | |
3650 | ||
3651 | tb = tb_find_pc((unsigned long)retaddr); | |
3652 | if (!tb) { | |
3653 | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", | |
3654 | retaddr); | |
3655 | } | |
3656 | n = env->icount_decr.u16.low + tb->icount; | |
3657 | cpu_restore_state(tb, env, (unsigned long)retaddr, NULL); | |
3658 | /* Calculate how many instructions had been executed before the fault | |
bf20dc07 | 3659 | occurred. */ |
2e70f6ef PB |
3660 | n = n - env->icount_decr.u16.low; |
3661 | /* Generate a new TB ending on the I/O insn. */ | |
3662 | n++; | |
3663 | /* On MIPS and SH, delay slot instructions can only be restarted if | |
3664 | they were already the first instruction in the TB. If this is not | |
bf20dc07 | 3665 | the first instruction in a TB then re-execute the preceding |
2e70f6ef PB |
3666 | branch. */ |
3667 | #if defined(TARGET_MIPS) | |
3668 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { | |
3669 | env->active_tc.PC -= 4; | |
3670 | env->icount_decr.u16.low++; | |
3671 | env->hflags &= ~MIPS_HFLAG_BMASK; | |
3672 | } | |
3673 | #elif defined(TARGET_SH4) | |
3674 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 | |
3675 | && n > 1) { | |
3676 | env->pc -= 2; | |
3677 | env->icount_decr.u16.low++; | |
3678 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); | |
3679 | } | |
3680 | #endif | |
3681 | /* This should never happen. */ | |
3682 | if (n > CF_COUNT_MASK) | |
3683 | cpu_abort(env, "TB too big during recompile"); | |
3684 | ||
3685 | cflags = n | CF_LAST_IO; | |
3686 | pc = tb->pc; | |
3687 | cs_base = tb->cs_base; | |
3688 | flags = tb->flags; | |
3689 | tb_phys_invalidate(tb, -1); | |
3690 | /* FIXME: In theory this could raise an exception. In practice | |
3691 | we have already translated the block once so it's probably ok. */ | |
3692 | tb_gen_code(env, pc, cs_base, flags, cflags); | |
bf20dc07 | 3693 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
2e70f6ef PB |
3694 | the first in the TB) then we end up generating a whole new TB and |
3695 | repeating the fault, which is horribly inefficient. | |
3696 | Better would be to execute just this insn uncached, or generate a | |
3697 | second new TB. */ | |
3698 | cpu_resume_from_signal(env, NULL); | |
3699 | } | |
3700 | ||
e3db7226 FB |
3701 | void dump_exec_info(FILE *f, |
3702 | int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) | |
3703 | { | |
3704 | int i, target_code_size, max_target_code_size; | |
3705 | int direct_jmp_count, direct_jmp2_count, cross_page; | |
3706 | TranslationBlock *tb; | |
3b46e624 | 3707 | |
e3db7226 FB |
3708 | target_code_size = 0; |
3709 | max_target_code_size = 0; | |
3710 | cross_page = 0; | |
3711 | direct_jmp_count = 0; | |
3712 | direct_jmp2_count = 0; | |
3713 | for(i = 0; i < nb_tbs; i++) { | |
3714 | tb = &tbs[i]; | |
3715 | target_code_size += tb->size; | |
3716 | if (tb->size > max_target_code_size) | |
3717 | max_target_code_size = tb->size; | |
3718 | if (tb->page_addr[1] != -1) | |
3719 | cross_page++; | |
3720 | if (tb->tb_next_offset[0] != 0xffff) { | |
3721 | direct_jmp_count++; | |
3722 | if (tb->tb_next_offset[1] != 0xffff) { | |
3723 | direct_jmp2_count++; | |
3724 | } | |
3725 | } | |
3726 | } | |
3727 | /* XXX: avoid using doubles ? */ | |
57fec1fe | 3728 | cpu_fprintf(f, "Translation buffer state:\n"); |
26a5f13b FB |
3729 | cpu_fprintf(f, "gen code size %ld/%ld\n", |
3730 | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); | |
3731 | cpu_fprintf(f, "TB count %d/%d\n", | |
3732 | nb_tbs, code_gen_max_blocks); | |
5fafdf24 | 3733 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
e3db7226 FB |
3734 | nb_tbs ? target_code_size / nb_tbs : 0, |
3735 | max_target_code_size); | |
5fafdf24 | 3736 | cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n", |
e3db7226 FB |
3737 | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0, |
3738 | target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0); | |
5fafdf24 TS |
3739 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", |
3740 | cross_page, | |
e3db7226 FB |
3741 | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
3742 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", | |
5fafdf24 | 3743 | direct_jmp_count, |
e3db7226 FB |
3744 | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
3745 | direct_jmp2_count, | |
3746 | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); | |
57fec1fe | 3747 | cpu_fprintf(f, "\nStatistics:\n"); |
e3db7226 FB |
3748 | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count); |
3749 | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count); | |
3750 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); | |
b67d9a52 | 3751 | tcg_dump_info(f, cpu_fprintf); |
e3db7226 FB |
3752 | } |
3753 | ||
5fafdf24 | 3754 | #if !defined(CONFIG_USER_ONLY) |
61382a50 FB |
3755 | |
3756 | #define MMUSUFFIX _cmmu | |
3757 | #define GETPC() NULL | |
3758 | #define env cpu_single_env | |
b769d8fe | 3759 | #define SOFTMMU_CODE_ACCESS |
61382a50 FB |
3760 | |
3761 | #define SHIFT 0 | |
3762 | #include "softmmu_template.h" | |
3763 | ||
3764 | #define SHIFT 1 | |
3765 | #include "softmmu_template.h" | |
3766 | ||
3767 | #define SHIFT 2 | |
3768 | #include "softmmu_template.h" | |
3769 | ||
3770 | #define SHIFT 3 | |
3771 | #include "softmmu_template.h" | |
3772 | ||
3773 | #undef env | |
3774 | ||
3775 | #endif |