]>
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
54936004 | 18 | */ |
67b915a5 | 19 | #include "config.h" |
d5a8f07c FB |
20 | #ifdef _WIN32 |
21 | #include <windows.h> | |
22 | #else | |
a98d49b1 | 23 | #include <sys/types.h> |
d5a8f07c FB |
24 | #include <sys/mman.h> |
25 | #endif | |
54936004 | 26 | |
055403b2 | 27 | #include "qemu-common.h" |
6180a181 | 28 | #include "cpu.h" |
b67d9a52 | 29 | #include "tcg.h" |
b3c7724c | 30 | #include "hw/hw.h" |
cc9e98cb | 31 | #include "hw/qdev.h" |
74576198 | 32 | #include "osdep.h" |
7ba1e619 | 33 | #include "kvm.h" |
432d268c | 34 | #include "hw/xen.h" |
29e922b6 | 35 | #include "qemu-timer.h" |
62152b8a AK |
36 | #include "memory.h" |
37 | #include "exec-memory.h" | |
53a5960a PB |
38 | #if defined(CONFIG_USER_ONLY) |
39 | #include <qemu.h> | |
f01576f1 JL |
40 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
41 | #include <sys/param.h> | |
42 | #if __FreeBSD_version >= 700104 | |
43 | #define HAVE_KINFO_GETVMMAP | |
44 | #define sigqueue sigqueue_freebsd /* avoid redefinition */ | |
45 | #include <sys/time.h> | |
46 | #include <sys/proc.h> | |
47 | #include <machine/profile.h> | |
48 | #define _KERNEL | |
49 | #include <sys/user.h> | |
50 | #undef _KERNEL | |
51 | #undef sigqueue | |
52 | #include <libutil.h> | |
53 | #endif | |
54 | #endif | |
432d268c JN |
55 | #else /* !CONFIG_USER_ONLY */ |
56 | #include "xen-mapcache.h" | |
6506e4f9 | 57 | #include "trace.h" |
53a5960a | 58 | #endif |
54936004 | 59 | |
fd6ce8f6 | 60 | //#define DEBUG_TB_INVALIDATE |
66e85a21 | 61 | //#define DEBUG_FLUSH |
9fa3e853 | 62 | //#define DEBUG_TLB |
67d3b957 | 63 | //#define DEBUG_UNASSIGNED |
fd6ce8f6 FB |
64 | |
65 | /* make various TB consistency checks */ | |
5fafdf24 TS |
66 | //#define DEBUG_TB_CHECK |
67 | //#define DEBUG_TLB_CHECK | |
fd6ce8f6 | 68 | |
1196be37 | 69 | //#define DEBUG_IOPORT |
db7b5426 | 70 | //#define DEBUG_SUBPAGE |
1196be37 | 71 | |
99773bd4 PB |
72 | #if !defined(CONFIG_USER_ONLY) |
73 | /* TB consistency checks only implemented for usermode emulation. */ | |
74 | #undef DEBUG_TB_CHECK | |
75 | #endif | |
76 | ||
9fa3e853 FB |
77 | #define SMC_BITMAP_USE_THRESHOLD 10 |
78 | ||
bdaf78e0 | 79 | static TranslationBlock *tbs; |
24ab68ac | 80 | static int code_gen_max_blocks; |
9fa3e853 | 81 | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
bdaf78e0 | 82 | static int nb_tbs; |
eb51d102 | 83 | /* any access to the tbs or the page table must use this lock */ |
c227f099 | 84 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; |
fd6ce8f6 | 85 | |
141ac468 BS |
86 | #if defined(__arm__) || defined(__sparc_v9__) |
87 | /* The prologue must be reachable with a direct jump. ARM and Sparc64 | |
88 | have limited branch ranges (possibly also PPC) so place it in a | |
d03d860b BS |
89 | section close to code segment. */ |
90 | #define code_gen_section \ | |
91 | __attribute__((__section__(".gen_code"))) \ | |
92 | __attribute__((aligned (32))) | |
f8e2af11 SW |
93 | #elif defined(_WIN32) |
94 | /* Maximum alignment for Win32 is 16. */ | |
95 | #define code_gen_section \ | |
96 | __attribute__((aligned (16))) | |
d03d860b BS |
97 | #else |
98 | #define code_gen_section \ | |
99 | __attribute__((aligned (32))) | |
100 | #endif | |
101 | ||
102 | uint8_t code_gen_prologue[1024] code_gen_section; | |
bdaf78e0 BS |
103 | static uint8_t *code_gen_buffer; |
104 | static unsigned long code_gen_buffer_size; | |
26a5f13b | 105 | /* threshold to flush the translated code buffer */ |
bdaf78e0 | 106 | static unsigned long code_gen_buffer_max_size; |
24ab68ac | 107 | static uint8_t *code_gen_ptr; |
fd6ce8f6 | 108 | |
e2eef170 | 109 | #if !defined(CONFIG_USER_ONLY) |
9fa3e853 | 110 | int phys_ram_fd; |
74576198 | 111 | static int in_migration; |
94a6b54f | 112 | |
f471a17e | 113 | RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list) }; |
62152b8a AK |
114 | |
115 | static MemoryRegion *system_memory; | |
116 | ||
e2eef170 | 117 | #endif |
9fa3e853 | 118 | |
6a00d601 FB |
119 | CPUState *first_cpu; |
120 | /* current CPU in the current thread. It is only valid inside | |
121 | cpu_exec() */ | |
5fafdf24 | 122 | CPUState *cpu_single_env; |
2e70f6ef | 123 | /* 0 = Do not count executed instructions. |
bf20dc07 | 124 | 1 = Precise instruction counting. |
2e70f6ef PB |
125 | 2 = Adaptive rate instruction counting. */ |
126 | int use_icount = 0; | |
127 | /* Current instruction counter. While executing translated code this may | |
128 | include some instructions that have not yet been executed. */ | |
129 | int64_t qemu_icount; | |
6a00d601 | 130 | |
54936004 | 131 | typedef struct PageDesc { |
92e873b9 | 132 | /* list of TBs intersecting this ram page */ |
fd6ce8f6 | 133 | TranslationBlock *first_tb; |
9fa3e853 FB |
134 | /* in order to optimize self modifying code, we count the number |
135 | of lookups we do to a given page to use a bitmap */ | |
136 | unsigned int code_write_count; | |
137 | uint8_t *code_bitmap; | |
138 | #if defined(CONFIG_USER_ONLY) | |
139 | unsigned long flags; | |
140 | #endif | |
54936004 FB |
141 | } PageDesc; |
142 | ||
41c1b1c9 | 143 | /* In system mode we want L1_MAP to be based on ram offsets, |
5cd2c5b6 RH |
144 | while in user mode we want it to be based on virtual addresses. */ |
145 | #if !defined(CONFIG_USER_ONLY) | |
41c1b1c9 PB |
146 | #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS |
147 | # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS | |
148 | #else | |
5cd2c5b6 | 149 | # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS |
41c1b1c9 | 150 | #endif |
bedb69ea | 151 | #else |
5cd2c5b6 | 152 | # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS |
bedb69ea | 153 | #endif |
54936004 | 154 | |
5cd2c5b6 RH |
155 | /* Size of the L2 (and L3, etc) page tables. */ |
156 | #define L2_BITS 10 | |
54936004 FB |
157 | #define L2_SIZE (1 << L2_BITS) |
158 | ||
5cd2c5b6 RH |
159 | /* The bits remaining after N lower levels of page tables. */ |
160 | #define P_L1_BITS_REM \ | |
161 | ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) | |
162 | #define V_L1_BITS_REM \ | |
163 | ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) | |
164 | ||
165 | /* Size of the L1 page table. Avoid silly small sizes. */ | |
166 | #if P_L1_BITS_REM < 4 | |
167 | #define P_L1_BITS (P_L1_BITS_REM + L2_BITS) | |
168 | #else | |
169 | #define P_L1_BITS P_L1_BITS_REM | |
170 | #endif | |
171 | ||
172 | #if V_L1_BITS_REM < 4 | |
173 | #define V_L1_BITS (V_L1_BITS_REM + L2_BITS) | |
174 | #else | |
175 | #define V_L1_BITS V_L1_BITS_REM | |
176 | #endif | |
177 | ||
178 | #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS) | |
179 | #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS) | |
180 | ||
181 | #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS) | |
182 | #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS) | |
183 | ||
83fb7adf FB |
184 | unsigned long qemu_real_host_page_size; |
185 | unsigned long qemu_host_page_bits; | |
186 | unsigned long qemu_host_page_size; | |
187 | unsigned long qemu_host_page_mask; | |
54936004 | 188 | |
5cd2c5b6 RH |
189 | /* This is a multi-level map on the virtual address space. |
190 | The bottom level has pointers to PageDesc. */ | |
191 | static void *l1_map[V_L1_SIZE]; | |
54936004 | 192 | |
e2eef170 | 193 | #if !defined(CONFIG_USER_ONLY) |
41c1b1c9 PB |
194 | typedef struct PhysPageDesc { |
195 | /* offset in host memory of the page + io_index in the low bits */ | |
196 | ram_addr_t phys_offset; | |
197 | ram_addr_t region_offset; | |
198 | } PhysPageDesc; | |
199 | ||
5cd2c5b6 RH |
200 | /* This is a multi-level map on the physical address space. |
201 | The bottom level has pointers to PhysPageDesc. */ | |
202 | static void *l1_phys_map[P_L1_SIZE]; | |
6d9a1304 | 203 | |
e2eef170 | 204 | static void io_mem_init(void); |
62152b8a | 205 | static void memory_map_init(void); |
e2eef170 | 206 | |
33417e70 | 207 | /* io memory support */ |
33417e70 FB |
208 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; |
209 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; | |
a4193c8a | 210 | void *io_mem_opaque[IO_MEM_NB_ENTRIES]; |
511d2b14 | 211 | static char io_mem_used[IO_MEM_NB_ENTRIES]; |
6658ffb8 PB |
212 | static int io_mem_watch; |
213 | #endif | |
33417e70 | 214 | |
34865134 | 215 | /* log support */ |
1e8b27ca JR |
216 | #ifdef WIN32 |
217 | static const char *logfilename = "qemu.log"; | |
218 | #else | |
d9b630fd | 219 | static const char *logfilename = "/tmp/qemu.log"; |
1e8b27ca | 220 | #endif |
34865134 FB |
221 | FILE *logfile; |
222 | int loglevel; | |
e735b91c | 223 | static int log_append = 0; |
34865134 | 224 | |
e3db7226 | 225 | /* statistics */ |
b3755a91 | 226 | #if !defined(CONFIG_USER_ONLY) |
e3db7226 | 227 | static int tlb_flush_count; |
b3755a91 | 228 | #endif |
e3db7226 FB |
229 | static int tb_flush_count; |
230 | static int tb_phys_invalidate_count; | |
231 | ||
7cb69cae FB |
232 | #ifdef _WIN32 |
233 | static void map_exec(void *addr, long size) | |
234 | { | |
235 | DWORD old_protect; | |
236 | VirtualProtect(addr, size, | |
237 | PAGE_EXECUTE_READWRITE, &old_protect); | |
238 | ||
239 | } | |
240 | #else | |
241 | static void map_exec(void *addr, long size) | |
242 | { | |
4369415f | 243 | unsigned long start, end, page_size; |
7cb69cae | 244 | |
4369415f | 245 | page_size = getpagesize(); |
7cb69cae | 246 | start = (unsigned long)addr; |
4369415f | 247 | start &= ~(page_size - 1); |
7cb69cae FB |
248 | |
249 | end = (unsigned long)addr + size; | |
4369415f FB |
250 | end += page_size - 1; |
251 | end &= ~(page_size - 1); | |
7cb69cae FB |
252 | |
253 | mprotect((void *)start, end - start, | |
254 | PROT_READ | PROT_WRITE | PROT_EXEC); | |
255 | } | |
256 | #endif | |
257 | ||
b346ff46 | 258 | static void page_init(void) |
54936004 | 259 | { |
83fb7adf | 260 | /* NOTE: we can always suppose that qemu_host_page_size >= |
54936004 | 261 | TARGET_PAGE_SIZE */ |
c2b48b69 AL |
262 | #ifdef _WIN32 |
263 | { | |
264 | SYSTEM_INFO system_info; | |
265 | ||
266 | GetSystemInfo(&system_info); | |
267 | qemu_real_host_page_size = system_info.dwPageSize; | |
268 | } | |
269 | #else | |
270 | qemu_real_host_page_size = getpagesize(); | |
271 | #endif | |
83fb7adf FB |
272 | if (qemu_host_page_size == 0) |
273 | qemu_host_page_size = qemu_real_host_page_size; | |
274 | if (qemu_host_page_size < TARGET_PAGE_SIZE) | |
275 | qemu_host_page_size = TARGET_PAGE_SIZE; | |
276 | qemu_host_page_bits = 0; | |
277 | while ((1 << qemu_host_page_bits) < qemu_host_page_size) | |
278 | qemu_host_page_bits++; | |
279 | qemu_host_page_mask = ~(qemu_host_page_size - 1); | |
50a9569b | 280 | |
2e9a5713 | 281 | #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY) |
50a9569b | 282 | { |
f01576f1 JL |
283 | #ifdef HAVE_KINFO_GETVMMAP |
284 | struct kinfo_vmentry *freep; | |
285 | int i, cnt; | |
286 | ||
287 | freep = kinfo_getvmmap(getpid(), &cnt); | |
288 | if (freep) { | |
289 | mmap_lock(); | |
290 | for (i = 0; i < cnt; i++) { | |
291 | unsigned long startaddr, endaddr; | |
292 | ||
293 | startaddr = freep[i].kve_start; | |
294 | endaddr = freep[i].kve_end; | |
295 | if (h2g_valid(startaddr)) { | |
296 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; | |
297 | ||
298 | if (h2g_valid(endaddr)) { | |
299 | endaddr = h2g(endaddr); | |
fd436907 | 300 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
f01576f1 JL |
301 | } else { |
302 | #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS | |
303 | endaddr = ~0ul; | |
fd436907 | 304 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
f01576f1 JL |
305 | #endif |
306 | } | |
307 | } | |
308 | } | |
309 | free(freep); | |
310 | mmap_unlock(); | |
311 | } | |
312 | #else | |
50a9569b | 313 | FILE *f; |
50a9569b | 314 | |
0776590d | 315 | last_brk = (unsigned long)sbrk(0); |
5cd2c5b6 | 316 | |
fd436907 | 317 | f = fopen("/compat/linux/proc/self/maps", "r"); |
50a9569b | 318 | if (f) { |
5cd2c5b6 RH |
319 | mmap_lock(); |
320 | ||
50a9569b | 321 | do { |
5cd2c5b6 RH |
322 | unsigned long startaddr, endaddr; |
323 | int n; | |
324 | ||
325 | n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr); | |
326 | ||
327 | if (n == 2 && h2g_valid(startaddr)) { | |
328 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; | |
329 | ||
330 | if (h2g_valid(endaddr)) { | |
331 | endaddr = h2g(endaddr); | |
332 | } else { | |
333 | endaddr = ~0ul; | |
334 | } | |
335 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); | |
50a9569b AZ |
336 | } |
337 | } while (!feof(f)); | |
5cd2c5b6 | 338 | |
50a9569b | 339 | fclose(f); |
5cd2c5b6 | 340 | mmap_unlock(); |
50a9569b | 341 | } |
f01576f1 | 342 | #endif |
50a9569b AZ |
343 | } |
344 | #endif | |
54936004 FB |
345 | } |
346 | ||
41c1b1c9 | 347 | static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) |
54936004 | 348 | { |
41c1b1c9 PB |
349 | PageDesc *pd; |
350 | void **lp; | |
351 | int i; | |
352 | ||
5cd2c5b6 | 353 | #if defined(CONFIG_USER_ONLY) |
2e9a5713 | 354 | /* We can't use qemu_malloc because it may recurse into a locked mutex. */ |
5cd2c5b6 RH |
355 | # define ALLOC(P, SIZE) \ |
356 | do { \ | |
357 | P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \ | |
358 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \ | |
5cd2c5b6 RH |
359 | } while (0) |
360 | #else | |
361 | # define ALLOC(P, SIZE) \ | |
362 | do { P = qemu_mallocz(SIZE); } while (0) | |
17e2377a | 363 | #endif |
434929bf | 364 | |
5cd2c5b6 RH |
365 | /* Level 1. Always allocated. */ |
366 | lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1)); | |
367 | ||
368 | /* Level 2..N-1. */ | |
369 | for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) { | |
370 | void **p = *lp; | |
371 | ||
372 | if (p == NULL) { | |
373 | if (!alloc) { | |
374 | return NULL; | |
375 | } | |
376 | ALLOC(p, sizeof(void *) * L2_SIZE); | |
377 | *lp = p; | |
17e2377a | 378 | } |
5cd2c5b6 RH |
379 | |
380 | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1)); | |
381 | } | |
382 | ||
383 | pd = *lp; | |
384 | if (pd == NULL) { | |
385 | if (!alloc) { | |
386 | return NULL; | |
387 | } | |
388 | ALLOC(pd, sizeof(PageDesc) * L2_SIZE); | |
389 | *lp = pd; | |
54936004 | 390 | } |
5cd2c5b6 RH |
391 | |
392 | #undef ALLOC | |
5cd2c5b6 RH |
393 | |
394 | return pd + (index & (L2_SIZE - 1)); | |
54936004 FB |
395 | } |
396 | ||
41c1b1c9 | 397 | static inline PageDesc *page_find(tb_page_addr_t index) |
54936004 | 398 | { |
5cd2c5b6 | 399 | return page_find_alloc(index, 0); |
fd6ce8f6 FB |
400 | } |
401 | ||
6d9a1304 | 402 | #if !defined(CONFIG_USER_ONLY) |
c227f099 | 403 | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
92e873b9 | 404 | { |
e3f4e2a4 | 405 | PhysPageDesc *pd; |
5cd2c5b6 RH |
406 | void **lp; |
407 | int i; | |
92e873b9 | 408 | |
5cd2c5b6 RH |
409 | /* Level 1. Always allocated. */ |
410 | lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1)); | |
108c49b8 | 411 | |
5cd2c5b6 RH |
412 | /* Level 2..N-1. */ |
413 | for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) { | |
414 | void **p = *lp; | |
415 | if (p == NULL) { | |
416 | if (!alloc) { | |
417 | return NULL; | |
418 | } | |
419 | *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE); | |
420 | } | |
421 | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1)); | |
108c49b8 | 422 | } |
5cd2c5b6 | 423 | |
e3f4e2a4 | 424 | pd = *lp; |
5cd2c5b6 | 425 | if (pd == NULL) { |
e3f4e2a4 | 426 | int i; |
5cd2c5b6 RH |
427 | |
428 | if (!alloc) { | |
108c49b8 | 429 | return NULL; |
5cd2c5b6 RH |
430 | } |
431 | ||
432 | *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE); | |
433 | ||
67c4d23c | 434 | for (i = 0; i < L2_SIZE; i++) { |
5cd2c5b6 RH |
435 | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
436 | pd[i].region_offset = (index + i) << TARGET_PAGE_BITS; | |
67c4d23c | 437 | } |
92e873b9 | 438 | } |
5cd2c5b6 RH |
439 | |
440 | return pd + (index & (L2_SIZE - 1)); | |
92e873b9 FB |
441 | } |
442 | ||
c227f099 | 443 | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
92e873b9 | 444 | { |
108c49b8 | 445 | return phys_page_find_alloc(index, 0); |
92e873b9 FB |
446 | } |
447 | ||
c227f099 AL |
448 | static void tlb_protect_code(ram_addr_t ram_addr); |
449 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, | |
3a7d929e | 450 | target_ulong vaddr); |
c8a706fe PB |
451 | #define mmap_lock() do { } while(0) |
452 | #define mmap_unlock() do { } while(0) | |
9fa3e853 | 453 | #endif |
fd6ce8f6 | 454 | |
4369415f FB |
455 | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
456 | ||
457 | #if defined(CONFIG_USER_ONLY) | |
ccbb4d44 | 458 | /* Currently it is not recommended to allocate big chunks of data in |
4369415f FB |
459 | user mode. It will change when a dedicated libc will be used */ |
460 | #define USE_STATIC_CODE_GEN_BUFFER | |
461 | #endif | |
462 | ||
463 | #ifdef USE_STATIC_CODE_GEN_BUFFER | |
ebf50fb3 AJ |
464 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE] |
465 | __attribute__((aligned (CODE_GEN_ALIGN))); | |
4369415f FB |
466 | #endif |
467 | ||
8fcd3692 | 468 | static void code_gen_alloc(unsigned long tb_size) |
26a5f13b | 469 | { |
4369415f FB |
470 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
471 | code_gen_buffer = static_code_gen_buffer; | |
472 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; | |
473 | map_exec(code_gen_buffer, code_gen_buffer_size); | |
474 | #else | |
26a5f13b FB |
475 | code_gen_buffer_size = tb_size; |
476 | if (code_gen_buffer_size == 0) { | |
4369415f FB |
477 | #if defined(CONFIG_USER_ONLY) |
478 | /* in user mode, phys_ram_size is not meaningful */ | |
479 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; | |
480 | #else | |
ccbb4d44 | 481 | /* XXX: needs adjustments */ |
94a6b54f | 482 | code_gen_buffer_size = (unsigned long)(ram_size / 4); |
4369415f | 483 | #endif |
26a5f13b FB |
484 | } |
485 | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE) | |
486 | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; | |
487 | /* The code gen buffer location may have constraints depending on | |
488 | the host cpu and OS */ | |
489 | #if defined(__linux__) | |
490 | { | |
491 | int flags; | |
141ac468 BS |
492 | void *start = NULL; |
493 | ||
26a5f13b FB |
494 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
495 | #if defined(__x86_64__) | |
496 | flags |= MAP_32BIT; | |
497 | /* Cannot map more than that */ | |
498 | if (code_gen_buffer_size > (800 * 1024 * 1024)) | |
499 | code_gen_buffer_size = (800 * 1024 * 1024); | |
141ac468 BS |
500 | #elif defined(__sparc_v9__) |
501 | // Map the buffer below 2G, so we can use direct calls and branches | |
502 | flags |= MAP_FIXED; | |
503 | start = (void *) 0x60000000UL; | |
504 | if (code_gen_buffer_size > (512 * 1024 * 1024)) | |
505 | code_gen_buffer_size = (512 * 1024 * 1024); | |
1cb0661e | 506 | #elif defined(__arm__) |
63d41246 | 507 | /* Map the buffer below 32M, so we can use direct calls and branches */ |
1cb0661e AZ |
508 | flags |= MAP_FIXED; |
509 | start = (void *) 0x01000000UL; | |
510 | if (code_gen_buffer_size > 16 * 1024 * 1024) | |
511 | code_gen_buffer_size = 16 * 1024 * 1024; | |
eba0b893 RH |
512 | #elif defined(__s390x__) |
513 | /* Map the buffer so that we can use direct calls and branches. */ | |
514 | /* We have a +- 4GB range on the branches; leave some slop. */ | |
515 | if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) { | |
516 | code_gen_buffer_size = 3ul * 1024 * 1024 * 1024; | |
517 | } | |
518 | start = (void *)0x90000000UL; | |
26a5f13b | 519 | #endif |
141ac468 BS |
520 | code_gen_buffer = mmap(start, code_gen_buffer_size, |
521 | PROT_WRITE | PROT_READ | PROT_EXEC, | |
26a5f13b FB |
522 | flags, -1, 0); |
523 | if (code_gen_buffer == MAP_FAILED) { | |
524 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); | |
525 | exit(1); | |
526 | } | |
527 | } | |
cbb608a5 BS |
528 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ |
529 | || defined(__DragonFly__) || defined(__OpenBSD__) | |
06e67a82 AL |
530 | { |
531 | int flags; | |
532 | void *addr = NULL; | |
533 | flags = MAP_PRIVATE | MAP_ANONYMOUS; | |
534 | #if defined(__x86_64__) | |
535 | /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume | |
536 | * 0x40000000 is free */ | |
537 | flags |= MAP_FIXED; | |
538 | addr = (void *)0x40000000; | |
539 | /* Cannot map more than that */ | |
540 | if (code_gen_buffer_size > (800 * 1024 * 1024)) | |
541 | code_gen_buffer_size = (800 * 1024 * 1024); | |
4cd31ad2 BS |
542 | #elif defined(__sparc_v9__) |
543 | // Map the buffer below 2G, so we can use direct calls and branches | |
544 | flags |= MAP_FIXED; | |
545 | addr = (void *) 0x60000000UL; | |
546 | if (code_gen_buffer_size > (512 * 1024 * 1024)) { | |
547 | code_gen_buffer_size = (512 * 1024 * 1024); | |
548 | } | |
06e67a82 AL |
549 | #endif |
550 | code_gen_buffer = mmap(addr, code_gen_buffer_size, | |
551 | PROT_WRITE | PROT_READ | PROT_EXEC, | |
552 | flags, -1, 0); | |
553 | if (code_gen_buffer == MAP_FAILED) { | |
554 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); | |
555 | exit(1); | |
556 | } | |
557 | } | |
26a5f13b FB |
558 | #else |
559 | code_gen_buffer = qemu_malloc(code_gen_buffer_size); | |
26a5f13b FB |
560 | map_exec(code_gen_buffer, code_gen_buffer_size); |
561 | #endif | |
4369415f | 562 | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
26a5f13b | 563 | map_exec(code_gen_prologue, sizeof(code_gen_prologue)); |
a884da8a PM |
564 | code_gen_buffer_max_size = code_gen_buffer_size - |
565 | (TCG_MAX_OP_SIZE * OPC_BUF_SIZE); | |
26a5f13b FB |
566 | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; |
567 | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock)); | |
568 | } | |
569 | ||
570 | /* Must be called before using the QEMU cpus. 'tb_size' is the size | |
571 | (in bytes) allocated to the translation buffer. Zero means default | |
572 | size. */ | |
573 | void cpu_exec_init_all(unsigned long tb_size) | |
574 | { | |
26a5f13b FB |
575 | cpu_gen_init(); |
576 | code_gen_alloc(tb_size); | |
577 | code_gen_ptr = code_gen_buffer; | |
4369415f | 578 | page_init(); |
e2eef170 | 579 | #if !defined(CONFIG_USER_ONLY) |
62152b8a | 580 | memory_map_init(); |
26a5f13b | 581 | io_mem_init(); |
e2eef170 | 582 | #endif |
9002ec79 RH |
583 | #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE) |
584 | /* There's no guest base to take into account, so go ahead and | |
585 | initialize the prologue now. */ | |
586 | tcg_prologue_init(&tcg_ctx); | |
587 | #endif | |
26a5f13b FB |
588 | } |
589 | ||
9656f324 PB |
590 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
591 | ||
e59fb374 | 592 | static int cpu_common_post_load(void *opaque, int version_id) |
e7f4eff7 JQ |
593 | { |
594 | CPUState *env = opaque; | |
9656f324 | 595 | |
3098dba0 AJ |
596 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
597 | version_id is increased. */ | |
598 | env->interrupt_request &= ~0x01; | |
9656f324 PB |
599 | tlb_flush(env, 1); |
600 | ||
601 | return 0; | |
602 | } | |
e7f4eff7 JQ |
603 | |
604 | static const VMStateDescription vmstate_cpu_common = { | |
605 | .name = "cpu_common", | |
606 | .version_id = 1, | |
607 | .minimum_version_id = 1, | |
608 | .minimum_version_id_old = 1, | |
e7f4eff7 JQ |
609 | .post_load = cpu_common_post_load, |
610 | .fields = (VMStateField []) { | |
611 | VMSTATE_UINT32(halted, CPUState), | |
612 | VMSTATE_UINT32(interrupt_request, CPUState), | |
613 | VMSTATE_END_OF_LIST() | |
614 | } | |
615 | }; | |
9656f324 PB |
616 | #endif |
617 | ||
950f1472 GC |
618 | CPUState *qemu_get_cpu(int cpu) |
619 | { | |
620 | CPUState *env = first_cpu; | |
621 | ||
622 | while (env) { | |
623 | if (env->cpu_index == cpu) | |
624 | break; | |
625 | env = env->next_cpu; | |
626 | } | |
627 | ||
628 | return env; | |
629 | } | |
630 | ||
6a00d601 | 631 | void cpu_exec_init(CPUState *env) |
fd6ce8f6 | 632 | { |
6a00d601 FB |
633 | CPUState **penv; |
634 | int cpu_index; | |
635 | ||
c2764719 PB |
636 | #if defined(CONFIG_USER_ONLY) |
637 | cpu_list_lock(); | |
638 | #endif | |
6a00d601 FB |
639 | env->next_cpu = NULL; |
640 | penv = &first_cpu; | |
641 | cpu_index = 0; | |
642 | while (*penv != NULL) { | |
1e9fa730 | 643 | penv = &(*penv)->next_cpu; |
6a00d601 FB |
644 | cpu_index++; |
645 | } | |
646 | env->cpu_index = cpu_index; | |
268a362c | 647 | env->numa_node = 0; |
72cf2d4f BS |
648 | QTAILQ_INIT(&env->breakpoints); |
649 | QTAILQ_INIT(&env->watchpoints); | |
dc7a09cf JK |
650 | #ifndef CONFIG_USER_ONLY |
651 | env->thread_id = qemu_get_thread_id(); | |
652 | #endif | |
6a00d601 | 653 | *penv = env; |
c2764719 PB |
654 | #if defined(CONFIG_USER_ONLY) |
655 | cpu_list_unlock(); | |
656 | #endif | |
b3c7724c | 657 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
0be71e32 AW |
658 | vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env); |
659 | register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION, | |
b3c7724c PB |
660 | cpu_save, cpu_load, env); |
661 | #endif | |
fd6ce8f6 FB |
662 | } |
663 | ||
d1a1eb74 TG |
664 | /* Allocate a new translation block. Flush the translation buffer if |
665 | too many translation blocks or too much generated code. */ | |
666 | static TranslationBlock *tb_alloc(target_ulong pc) | |
667 | { | |
668 | TranslationBlock *tb; | |
669 | ||
670 | if (nb_tbs >= code_gen_max_blocks || | |
671 | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) | |
672 | return NULL; | |
673 | tb = &tbs[nb_tbs++]; | |
674 | tb->pc = pc; | |
675 | tb->cflags = 0; | |
676 | return tb; | |
677 | } | |
678 | ||
679 | void tb_free(TranslationBlock *tb) | |
680 | { | |
681 | /* In practice this is mostly used for single use temporary TB | |
682 | Ignore the hard cases and just back up if this TB happens to | |
683 | be the last one generated. */ | |
684 | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { | |
685 | code_gen_ptr = tb->tc_ptr; | |
686 | nb_tbs--; | |
687 | } | |
688 | } | |
689 | ||
9fa3e853 FB |
690 | static inline void invalidate_page_bitmap(PageDesc *p) |
691 | { | |
692 | if (p->code_bitmap) { | |
59817ccb | 693 | qemu_free(p->code_bitmap); |
9fa3e853 FB |
694 | p->code_bitmap = NULL; |
695 | } | |
696 | p->code_write_count = 0; | |
697 | } | |
698 | ||
5cd2c5b6 RH |
699 | /* Set to NULL all the 'first_tb' fields in all PageDescs. */ |
700 | ||
701 | static void page_flush_tb_1 (int level, void **lp) | |
fd6ce8f6 | 702 | { |
5cd2c5b6 | 703 | int i; |
fd6ce8f6 | 704 | |
5cd2c5b6 RH |
705 | if (*lp == NULL) { |
706 | return; | |
707 | } | |
708 | if (level == 0) { | |
709 | PageDesc *pd = *lp; | |
7296abac | 710 | for (i = 0; i < L2_SIZE; ++i) { |
5cd2c5b6 RH |
711 | pd[i].first_tb = NULL; |
712 | invalidate_page_bitmap(pd + i); | |
fd6ce8f6 | 713 | } |
5cd2c5b6 RH |
714 | } else { |
715 | void **pp = *lp; | |
7296abac | 716 | for (i = 0; i < L2_SIZE; ++i) { |
5cd2c5b6 RH |
717 | page_flush_tb_1 (level - 1, pp + i); |
718 | } | |
719 | } | |
720 | } | |
721 | ||
722 | static void page_flush_tb(void) | |
723 | { | |
724 | int i; | |
725 | for (i = 0; i < V_L1_SIZE; i++) { | |
726 | page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i); | |
fd6ce8f6 FB |
727 | } |
728 | } | |
729 | ||
730 | /* flush all the translation blocks */ | |
d4e8164f | 731 | /* XXX: tb_flush is currently not thread safe */ |
6a00d601 | 732 | void tb_flush(CPUState *env1) |
fd6ce8f6 | 733 | { |
6a00d601 | 734 | CPUState *env; |
0124311e | 735 | #if defined(DEBUG_FLUSH) |
ab3d1727 BS |
736 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
737 | (unsigned long)(code_gen_ptr - code_gen_buffer), | |
738 | nb_tbs, nb_tbs > 0 ? | |
739 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); | |
fd6ce8f6 | 740 | #endif |
26a5f13b | 741 | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
a208e54a PB |
742 | cpu_abort(env1, "Internal error: code buffer overflow\n"); |
743 | ||
fd6ce8f6 | 744 | nb_tbs = 0; |
3b46e624 | 745 | |
6a00d601 FB |
746 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
747 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); | |
748 | } | |
9fa3e853 | 749 | |
8a8a608f | 750 | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
fd6ce8f6 | 751 | page_flush_tb(); |
9fa3e853 | 752 | |
fd6ce8f6 | 753 | code_gen_ptr = code_gen_buffer; |
d4e8164f FB |
754 | /* XXX: flush processor icache at this point if cache flush is |
755 | expensive */ | |
e3db7226 | 756 | tb_flush_count++; |
fd6ce8f6 FB |
757 | } |
758 | ||
759 | #ifdef DEBUG_TB_CHECK | |
760 | ||
bc98a7ef | 761 | static void tb_invalidate_check(target_ulong address) |
fd6ce8f6 FB |
762 | { |
763 | TranslationBlock *tb; | |
764 | int i; | |
765 | address &= TARGET_PAGE_MASK; | |
99773bd4 PB |
766 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
767 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { | |
fd6ce8f6 FB |
768 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || |
769 | address >= tb->pc + tb->size)) { | |
0bf9e31a BS |
770 | printf("ERROR invalidate: address=" TARGET_FMT_lx |
771 | " PC=%08lx size=%04x\n", | |
99773bd4 | 772 | address, (long)tb->pc, tb->size); |
fd6ce8f6 FB |
773 | } |
774 | } | |
775 | } | |
776 | } | |
777 | ||
778 | /* verify that all the pages have correct rights for code */ | |
779 | static void tb_page_check(void) | |
780 | { | |
781 | TranslationBlock *tb; | |
782 | int i, flags1, flags2; | |
3b46e624 | 783 | |
99773bd4 PB |
784 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
785 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { | |
fd6ce8f6 FB |
786 | flags1 = page_get_flags(tb->pc); |
787 | flags2 = page_get_flags(tb->pc + tb->size - 1); | |
788 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { | |
789 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", | |
99773bd4 | 790 | (long)tb->pc, tb->size, flags1, flags2); |
fd6ce8f6 FB |
791 | } |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | #endif | |
797 | ||
798 | /* invalidate one TB */ | |
799 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, | |
800 | int next_offset) | |
801 | { | |
802 | TranslationBlock *tb1; | |
803 | for(;;) { | |
804 | tb1 = *ptb; | |
805 | if (tb1 == tb) { | |
806 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); | |
807 | break; | |
808 | } | |
809 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); | |
810 | } | |
811 | } | |
812 | ||
9fa3e853 FB |
813 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
814 | { | |
815 | TranslationBlock *tb1; | |
816 | unsigned int n1; | |
817 | ||
818 | for(;;) { | |
819 | tb1 = *ptb; | |
820 | n1 = (long)tb1 & 3; | |
821 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
822 | if (tb1 == tb) { | |
823 | *ptb = tb1->page_next[n1]; | |
824 | break; | |
825 | } | |
826 | ptb = &tb1->page_next[n1]; | |
827 | } | |
828 | } | |
829 | ||
d4e8164f FB |
830 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
831 | { | |
832 | TranslationBlock *tb1, **ptb; | |
833 | unsigned int n1; | |
834 | ||
835 | ptb = &tb->jmp_next[n]; | |
836 | tb1 = *ptb; | |
837 | if (tb1) { | |
838 | /* find tb(n) in circular list */ | |
839 | for(;;) { | |
840 | tb1 = *ptb; | |
841 | n1 = (long)tb1 & 3; | |
842 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
843 | if (n1 == n && tb1 == tb) | |
844 | break; | |
845 | if (n1 == 2) { | |
846 | ptb = &tb1->jmp_first; | |
847 | } else { | |
848 | ptb = &tb1->jmp_next[n1]; | |
849 | } | |
850 | } | |
851 | /* now we can suppress tb(n) from the list */ | |
852 | *ptb = tb->jmp_next[n]; | |
853 | ||
854 | tb->jmp_next[n] = NULL; | |
855 | } | |
856 | } | |
857 | ||
858 | /* reset the jump entry 'n' of a TB so that it is not chained to | |
859 | another TB */ | |
860 | static inline void tb_reset_jump(TranslationBlock *tb, int n) | |
861 | { | |
862 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); | |
863 | } | |
864 | ||
41c1b1c9 | 865 | void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) |
fd6ce8f6 | 866 | { |
6a00d601 | 867 | CPUState *env; |
8a40a180 | 868 | PageDesc *p; |
d4e8164f | 869 | unsigned int h, n1; |
41c1b1c9 | 870 | tb_page_addr_t phys_pc; |
8a40a180 | 871 | TranslationBlock *tb1, *tb2; |
3b46e624 | 872 | |
8a40a180 FB |
873 | /* remove the TB from the hash list */ |
874 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
875 | h = tb_phys_hash_func(phys_pc); | |
5fafdf24 | 876 | tb_remove(&tb_phys_hash[h], tb, |
8a40a180 FB |
877 | offsetof(TranslationBlock, phys_hash_next)); |
878 | ||
879 | /* remove the TB from the page list */ | |
880 | if (tb->page_addr[0] != page_addr) { | |
881 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); | |
882 | tb_page_remove(&p->first_tb, tb); | |
883 | invalidate_page_bitmap(p); | |
884 | } | |
885 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { | |
886 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); | |
887 | tb_page_remove(&p->first_tb, tb); | |
888 | invalidate_page_bitmap(p); | |
889 | } | |
890 | ||
36bdbe54 | 891 | tb_invalidated_flag = 1; |
59817ccb | 892 | |
fd6ce8f6 | 893 | /* remove the TB from the hash list */ |
8a40a180 | 894 | h = tb_jmp_cache_hash_func(tb->pc); |
6a00d601 FB |
895 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
896 | if (env->tb_jmp_cache[h] == tb) | |
897 | env->tb_jmp_cache[h] = NULL; | |
898 | } | |
d4e8164f FB |
899 | |
900 | /* suppress this TB from the two jump lists */ | |
901 | tb_jmp_remove(tb, 0); | |
902 | tb_jmp_remove(tb, 1); | |
903 | ||
904 | /* suppress any remaining jumps to this TB */ | |
905 | tb1 = tb->jmp_first; | |
906 | for(;;) { | |
907 | n1 = (long)tb1 & 3; | |
908 | if (n1 == 2) | |
909 | break; | |
910 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
911 | tb2 = tb1->jmp_next[n1]; | |
912 | tb_reset_jump(tb1, n1); | |
913 | tb1->jmp_next[n1] = NULL; | |
914 | tb1 = tb2; | |
915 | } | |
916 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ | |
9fa3e853 | 917 | |
e3db7226 | 918 | tb_phys_invalidate_count++; |
9fa3e853 FB |
919 | } |
920 | ||
921 | static inline void set_bits(uint8_t *tab, int start, int len) | |
922 | { | |
923 | int end, mask, end1; | |
924 | ||
925 | end = start + len; | |
926 | tab += start >> 3; | |
927 | mask = 0xff << (start & 7); | |
928 | if ((start & ~7) == (end & ~7)) { | |
929 | if (start < end) { | |
930 | mask &= ~(0xff << (end & 7)); | |
931 | *tab |= mask; | |
932 | } | |
933 | } else { | |
934 | *tab++ |= mask; | |
935 | start = (start + 8) & ~7; | |
936 | end1 = end & ~7; | |
937 | while (start < end1) { | |
938 | *tab++ = 0xff; | |
939 | start += 8; | |
940 | } | |
941 | if (start < end) { | |
942 | mask = ~(0xff << (end & 7)); | |
943 | *tab |= mask; | |
944 | } | |
945 | } | |
946 | } | |
947 | ||
948 | static void build_page_bitmap(PageDesc *p) | |
949 | { | |
950 | int n, tb_start, tb_end; | |
951 | TranslationBlock *tb; | |
3b46e624 | 952 | |
b2a7081a | 953 | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8); |
9fa3e853 FB |
954 | |
955 | tb = p->first_tb; | |
956 | while (tb != NULL) { | |
957 | n = (long)tb & 3; | |
958 | tb = (TranslationBlock *)((long)tb & ~3); | |
959 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
960 | if (n == 0) { | |
961 | /* NOTE: tb_end may be after the end of the page, but | |
962 | it is not a problem */ | |
963 | tb_start = tb->pc & ~TARGET_PAGE_MASK; | |
964 | tb_end = tb_start + tb->size; | |
965 | if (tb_end > TARGET_PAGE_SIZE) | |
966 | tb_end = TARGET_PAGE_SIZE; | |
967 | } else { | |
968 | tb_start = 0; | |
969 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
970 | } | |
971 | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); | |
972 | tb = tb->page_next[n]; | |
973 | } | |
974 | } | |
975 | ||
2e70f6ef PB |
976 | TranslationBlock *tb_gen_code(CPUState *env, |
977 | target_ulong pc, target_ulong cs_base, | |
978 | int flags, int cflags) | |
d720b93d FB |
979 | { |
980 | TranslationBlock *tb; | |
981 | uint8_t *tc_ptr; | |
41c1b1c9 PB |
982 | tb_page_addr_t phys_pc, phys_page2; |
983 | target_ulong virt_page2; | |
d720b93d FB |
984 | int code_gen_size; |
985 | ||
41c1b1c9 | 986 | phys_pc = get_page_addr_code(env, pc); |
c27004ec | 987 | tb = tb_alloc(pc); |
d720b93d FB |
988 | if (!tb) { |
989 | /* flush must be done */ | |
990 | tb_flush(env); | |
991 | /* cannot fail at this point */ | |
c27004ec | 992 | tb = tb_alloc(pc); |
2e70f6ef PB |
993 | /* Don't forget to invalidate previous TB info. */ |
994 | tb_invalidated_flag = 1; | |
d720b93d FB |
995 | } |
996 | tc_ptr = code_gen_ptr; | |
997 | tb->tc_ptr = tc_ptr; | |
998 | tb->cs_base = cs_base; | |
999 | tb->flags = flags; | |
1000 | tb->cflags = cflags; | |
d07bde88 | 1001 | cpu_gen_code(env, tb, &code_gen_size); |
d720b93d | 1002 | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
3b46e624 | 1003 | |
d720b93d | 1004 | /* check next page if needed */ |
c27004ec | 1005 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
d720b93d | 1006 | phys_page2 = -1; |
c27004ec | 1007 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
41c1b1c9 | 1008 | phys_page2 = get_page_addr_code(env, virt_page2); |
d720b93d | 1009 | } |
41c1b1c9 | 1010 | tb_link_page(tb, phys_pc, phys_page2); |
2e70f6ef | 1011 | return tb; |
d720b93d | 1012 | } |
3b46e624 | 1013 | |
9fa3e853 FB |
1014 | /* invalidate all TBs which intersect with the target physical page |
1015 | starting in range [start;end[. NOTE: start and end must refer to | |
d720b93d FB |
1016 | the same physical page. 'is_cpu_write_access' should be true if called |
1017 | from a real cpu write access: the virtual CPU will exit the current | |
1018 | TB if code is modified inside this TB. */ | |
41c1b1c9 | 1019 | void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end, |
d720b93d FB |
1020 | int is_cpu_write_access) |
1021 | { | |
6b917547 | 1022 | TranslationBlock *tb, *tb_next, *saved_tb; |
d720b93d | 1023 | CPUState *env = cpu_single_env; |
41c1b1c9 | 1024 | tb_page_addr_t tb_start, tb_end; |
6b917547 AL |
1025 | PageDesc *p; |
1026 | int n; | |
1027 | #ifdef TARGET_HAS_PRECISE_SMC | |
1028 | int current_tb_not_found = is_cpu_write_access; | |
1029 | TranslationBlock *current_tb = NULL; | |
1030 | int current_tb_modified = 0; | |
1031 | target_ulong current_pc = 0; | |
1032 | target_ulong current_cs_base = 0; | |
1033 | int current_flags = 0; | |
1034 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
9fa3e853 FB |
1035 | |
1036 | p = page_find(start >> TARGET_PAGE_BITS); | |
5fafdf24 | 1037 | if (!p) |
9fa3e853 | 1038 | return; |
5fafdf24 | 1039 | if (!p->code_bitmap && |
d720b93d FB |
1040 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
1041 | is_cpu_write_access) { | |
9fa3e853 FB |
1042 | /* build code bitmap */ |
1043 | build_page_bitmap(p); | |
1044 | } | |
1045 | ||
1046 | /* we remove all the TBs in the range [start, end[ */ | |
1047 | /* XXX: see if in some cases it could be faster to invalidate all the code */ | |
1048 | tb = p->first_tb; | |
1049 | while (tb != NULL) { | |
1050 | n = (long)tb & 3; | |
1051 | tb = (TranslationBlock *)((long)tb & ~3); | |
1052 | tb_next = tb->page_next[n]; | |
1053 | /* NOTE: this is subtle as a TB may span two physical pages */ | |
1054 | if (n == 0) { | |
1055 | /* NOTE: tb_end may be after the end of the page, but | |
1056 | it is not a problem */ | |
1057 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); | |
1058 | tb_end = tb_start + tb->size; | |
1059 | } else { | |
1060 | tb_start = tb->page_addr[1]; | |
1061 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); | |
1062 | } | |
1063 | if (!(tb_end <= start || tb_start >= end)) { | |
d720b93d FB |
1064 | #ifdef TARGET_HAS_PRECISE_SMC |
1065 | if (current_tb_not_found) { | |
1066 | current_tb_not_found = 0; | |
1067 | current_tb = NULL; | |
2e70f6ef | 1068 | if (env->mem_io_pc) { |
d720b93d | 1069 | /* now we have a real cpu fault */ |
2e70f6ef | 1070 | current_tb = tb_find_pc(env->mem_io_pc); |
d720b93d FB |
1071 | } |
1072 | } | |
1073 | if (current_tb == tb && | |
2e70f6ef | 1074 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
d720b93d FB |
1075 | /* If we are modifying the current TB, we must stop |
1076 | its execution. We could be more precise by checking | |
1077 | that the modification is after the current PC, but it | |
1078 | would require a specialized function to partially | |
1079 | restore the CPU state */ | |
3b46e624 | 1080 | |
d720b93d | 1081 | current_tb_modified = 1; |
618ba8e6 | 1082 | cpu_restore_state(current_tb, env, env->mem_io_pc); |
6b917547 AL |
1083 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1084 | ¤t_flags); | |
d720b93d FB |
1085 | } |
1086 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
6f5a9f7e FB |
1087 | /* we need to do that to handle the case where a signal |
1088 | occurs while doing tb_phys_invalidate() */ | |
1089 | saved_tb = NULL; | |
1090 | if (env) { | |
1091 | saved_tb = env->current_tb; | |
1092 | env->current_tb = NULL; | |
1093 | } | |
9fa3e853 | 1094 | tb_phys_invalidate(tb, -1); |
6f5a9f7e FB |
1095 | if (env) { |
1096 | env->current_tb = saved_tb; | |
1097 | if (env->interrupt_request && env->current_tb) | |
1098 | cpu_interrupt(env, env->interrupt_request); | |
1099 | } | |
9fa3e853 FB |
1100 | } |
1101 | tb = tb_next; | |
1102 | } | |
1103 | #if !defined(CONFIG_USER_ONLY) | |
1104 | /* if no code remaining, no need to continue to use slow writes */ | |
1105 | if (!p->first_tb) { | |
1106 | invalidate_page_bitmap(p); | |
d720b93d | 1107 | if (is_cpu_write_access) { |
2e70f6ef | 1108 | tlb_unprotect_code_phys(env, start, env->mem_io_vaddr); |
d720b93d FB |
1109 | } |
1110 | } | |
1111 | #endif | |
1112 | #ifdef TARGET_HAS_PRECISE_SMC | |
1113 | if (current_tb_modified) { | |
1114 | /* we generate a block containing just the instruction | |
1115 | modifying the memory. It will ensure that it cannot modify | |
1116 | itself */ | |
ea1c1802 | 1117 | env->current_tb = NULL; |
2e70f6ef | 1118 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
d720b93d | 1119 | cpu_resume_from_signal(env, NULL); |
9fa3e853 | 1120 | } |
fd6ce8f6 | 1121 | #endif |
9fa3e853 | 1122 | } |
fd6ce8f6 | 1123 | |
9fa3e853 | 1124 | /* len must be <= 8 and start must be a multiple of len */ |
41c1b1c9 | 1125 | static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len) |
9fa3e853 FB |
1126 | { |
1127 | PageDesc *p; | |
1128 | int offset, b; | |
59817ccb | 1129 | #if 0 |
a4193c8a | 1130 | if (1) { |
93fcfe39 AL |
1131 | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n", |
1132 | cpu_single_env->mem_io_vaddr, len, | |
1133 | cpu_single_env->eip, | |
1134 | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base); | |
59817ccb FB |
1135 | } |
1136 | #endif | |
9fa3e853 | 1137 | p = page_find(start >> TARGET_PAGE_BITS); |
5fafdf24 | 1138 | if (!p) |
9fa3e853 FB |
1139 | return; |
1140 | if (p->code_bitmap) { | |
1141 | offset = start & ~TARGET_PAGE_MASK; | |
1142 | b = p->code_bitmap[offset >> 3] >> (offset & 7); | |
1143 | if (b & ((1 << len) - 1)) | |
1144 | goto do_invalidate; | |
1145 | } else { | |
1146 | do_invalidate: | |
d720b93d | 1147 | tb_invalidate_phys_page_range(start, start + len, 1); |
9fa3e853 FB |
1148 | } |
1149 | } | |
1150 | ||
9fa3e853 | 1151 | #if !defined(CONFIG_SOFTMMU) |
41c1b1c9 | 1152 | static void tb_invalidate_phys_page(tb_page_addr_t addr, |
d720b93d | 1153 | unsigned long pc, void *puc) |
9fa3e853 | 1154 | { |
6b917547 | 1155 | TranslationBlock *tb; |
9fa3e853 | 1156 | PageDesc *p; |
6b917547 | 1157 | int n; |
d720b93d | 1158 | #ifdef TARGET_HAS_PRECISE_SMC |
6b917547 | 1159 | TranslationBlock *current_tb = NULL; |
d720b93d | 1160 | CPUState *env = cpu_single_env; |
6b917547 AL |
1161 | int current_tb_modified = 0; |
1162 | target_ulong current_pc = 0; | |
1163 | target_ulong current_cs_base = 0; | |
1164 | int current_flags = 0; | |
d720b93d | 1165 | #endif |
9fa3e853 FB |
1166 | |
1167 | addr &= TARGET_PAGE_MASK; | |
1168 | p = page_find(addr >> TARGET_PAGE_BITS); | |
5fafdf24 | 1169 | if (!p) |
9fa3e853 FB |
1170 | return; |
1171 | tb = p->first_tb; | |
d720b93d FB |
1172 | #ifdef TARGET_HAS_PRECISE_SMC |
1173 | if (tb && pc != 0) { | |
1174 | current_tb = tb_find_pc(pc); | |
1175 | } | |
1176 | #endif | |
9fa3e853 FB |
1177 | while (tb != NULL) { |
1178 | n = (long)tb & 3; | |
1179 | tb = (TranslationBlock *)((long)tb & ~3); | |
d720b93d FB |
1180 | #ifdef TARGET_HAS_PRECISE_SMC |
1181 | if (current_tb == tb && | |
2e70f6ef | 1182 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
d720b93d FB |
1183 | /* If we are modifying the current TB, we must stop |
1184 | its execution. We could be more precise by checking | |
1185 | that the modification is after the current PC, but it | |
1186 | would require a specialized function to partially | |
1187 | restore the CPU state */ | |
3b46e624 | 1188 | |
d720b93d | 1189 | current_tb_modified = 1; |
618ba8e6 | 1190 | cpu_restore_state(current_tb, env, pc); |
6b917547 AL |
1191 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1192 | ¤t_flags); | |
d720b93d FB |
1193 | } |
1194 | #endif /* TARGET_HAS_PRECISE_SMC */ | |
9fa3e853 FB |
1195 | tb_phys_invalidate(tb, addr); |
1196 | tb = tb->page_next[n]; | |
1197 | } | |
fd6ce8f6 | 1198 | p->first_tb = NULL; |
d720b93d FB |
1199 | #ifdef TARGET_HAS_PRECISE_SMC |
1200 | if (current_tb_modified) { | |
1201 | /* we generate a block containing just the instruction | |
1202 | modifying the memory. It will ensure that it cannot modify | |
1203 | itself */ | |
ea1c1802 | 1204 | env->current_tb = NULL; |
2e70f6ef | 1205 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
d720b93d FB |
1206 | cpu_resume_from_signal(env, puc); |
1207 | } | |
1208 | #endif | |
fd6ce8f6 | 1209 | } |
9fa3e853 | 1210 | #endif |
fd6ce8f6 FB |
1211 | |
1212 | /* add the tb in the target page and protect it if necessary */ | |
5fafdf24 | 1213 | static inline void tb_alloc_page(TranslationBlock *tb, |
41c1b1c9 | 1214 | unsigned int n, tb_page_addr_t page_addr) |
fd6ce8f6 FB |
1215 | { |
1216 | PageDesc *p; | |
4429ab44 JQ |
1217 | #ifndef CONFIG_USER_ONLY |
1218 | bool page_already_protected; | |
1219 | #endif | |
9fa3e853 FB |
1220 | |
1221 | tb->page_addr[n] = page_addr; | |
5cd2c5b6 | 1222 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1); |
9fa3e853 | 1223 | tb->page_next[n] = p->first_tb; |
4429ab44 JQ |
1224 | #ifndef CONFIG_USER_ONLY |
1225 | page_already_protected = p->first_tb != NULL; | |
1226 | #endif | |
9fa3e853 FB |
1227 | p->first_tb = (TranslationBlock *)((long)tb | n); |
1228 | invalidate_page_bitmap(p); | |
fd6ce8f6 | 1229 | |
107db443 | 1230 | #if defined(TARGET_HAS_SMC) || 1 |
d720b93d | 1231 | |
9fa3e853 | 1232 | #if defined(CONFIG_USER_ONLY) |
fd6ce8f6 | 1233 | if (p->flags & PAGE_WRITE) { |
53a5960a PB |
1234 | target_ulong addr; |
1235 | PageDesc *p2; | |
9fa3e853 FB |
1236 | int prot; |
1237 | ||
fd6ce8f6 FB |
1238 | /* force the host page as non writable (writes will have a |
1239 | page fault + mprotect overhead) */ | |
53a5960a | 1240 | page_addr &= qemu_host_page_mask; |
fd6ce8f6 | 1241 | prot = 0; |
53a5960a PB |
1242 | for(addr = page_addr; addr < page_addr + qemu_host_page_size; |
1243 | addr += TARGET_PAGE_SIZE) { | |
1244 | ||
1245 | p2 = page_find (addr >> TARGET_PAGE_BITS); | |
1246 | if (!p2) | |
1247 | continue; | |
1248 | prot |= p2->flags; | |
1249 | p2->flags &= ~PAGE_WRITE; | |
53a5960a | 1250 | } |
5fafdf24 | 1251 | mprotect(g2h(page_addr), qemu_host_page_size, |
fd6ce8f6 FB |
1252 | (prot & PAGE_BITS) & ~PAGE_WRITE); |
1253 | #ifdef DEBUG_TB_INVALIDATE | |
ab3d1727 | 1254 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
53a5960a | 1255 | page_addr); |
fd6ce8f6 | 1256 | #endif |
fd6ce8f6 | 1257 | } |
9fa3e853 FB |
1258 | #else |
1259 | /* if some code is already present, then the pages are already | |
1260 | protected. So we handle the case where only the first TB is | |
1261 | allocated in a physical page */ | |
4429ab44 | 1262 | if (!page_already_protected) { |
6a00d601 | 1263 | tlb_protect_code(page_addr); |
9fa3e853 FB |
1264 | } |
1265 | #endif | |
d720b93d FB |
1266 | |
1267 | #endif /* TARGET_HAS_SMC */ | |
fd6ce8f6 FB |
1268 | } |
1269 | ||
9fa3e853 FB |
1270 | /* add a new TB and link it to the physical page tables. phys_page2 is |
1271 | (-1) to indicate that only one page contains the TB. */ | |
41c1b1c9 PB |
1272 | void tb_link_page(TranslationBlock *tb, |
1273 | tb_page_addr_t phys_pc, tb_page_addr_t phys_page2) | |
d4e8164f | 1274 | { |
9fa3e853 FB |
1275 | unsigned int h; |
1276 | TranslationBlock **ptb; | |
1277 | ||
c8a706fe PB |
1278 | /* Grab the mmap lock to stop another thread invalidating this TB |
1279 | before we are done. */ | |
1280 | mmap_lock(); | |
9fa3e853 FB |
1281 | /* add in the physical hash table */ |
1282 | h = tb_phys_hash_func(phys_pc); | |
1283 | ptb = &tb_phys_hash[h]; | |
1284 | tb->phys_hash_next = *ptb; | |
1285 | *ptb = tb; | |
fd6ce8f6 FB |
1286 | |
1287 | /* add in the page list */ | |
9fa3e853 FB |
1288 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); |
1289 | if (phys_page2 != -1) | |
1290 | tb_alloc_page(tb, 1, phys_page2); | |
1291 | else | |
1292 | tb->page_addr[1] = -1; | |
9fa3e853 | 1293 | |
d4e8164f FB |
1294 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
1295 | tb->jmp_next[0] = NULL; | |
1296 | tb->jmp_next[1] = NULL; | |
1297 | ||
1298 | /* init original jump addresses */ | |
1299 | if (tb->tb_next_offset[0] != 0xffff) | |
1300 | tb_reset_jump(tb, 0); | |
1301 | if (tb->tb_next_offset[1] != 0xffff) | |
1302 | tb_reset_jump(tb, 1); | |
8a40a180 FB |
1303 | |
1304 | #ifdef DEBUG_TB_CHECK | |
1305 | tb_page_check(); | |
1306 | #endif | |
c8a706fe | 1307 | mmap_unlock(); |
fd6ce8f6 FB |
1308 | } |
1309 | ||
9fa3e853 FB |
1310 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
1311 | tb[1].tc_ptr. Return NULL if not found */ | |
1312 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) | |
fd6ce8f6 | 1313 | { |
9fa3e853 FB |
1314 | int m_min, m_max, m; |
1315 | unsigned long v; | |
1316 | TranslationBlock *tb; | |
a513fe19 FB |
1317 | |
1318 | if (nb_tbs <= 0) | |
1319 | return NULL; | |
1320 | if (tc_ptr < (unsigned long)code_gen_buffer || | |
1321 | tc_ptr >= (unsigned long)code_gen_ptr) | |
1322 | return NULL; | |
1323 | /* binary search (cf Knuth) */ | |
1324 | m_min = 0; | |
1325 | m_max = nb_tbs - 1; | |
1326 | while (m_min <= m_max) { | |
1327 | m = (m_min + m_max) >> 1; | |
1328 | tb = &tbs[m]; | |
1329 | v = (unsigned long)tb->tc_ptr; | |
1330 | if (v == tc_ptr) | |
1331 | return tb; | |
1332 | else if (tc_ptr < v) { | |
1333 | m_max = m - 1; | |
1334 | } else { | |
1335 | m_min = m + 1; | |
1336 | } | |
5fafdf24 | 1337 | } |
a513fe19 FB |
1338 | return &tbs[m_max]; |
1339 | } | |
7501267e | 1340 | |
ea041c0e FB |
1341 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
1342 | ||
1343 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) | |
1344 | { | |
1345 | TranslationBlock *tb1, *tb_next, **ptb; | |
1346 | unsigned int n1; | |
1347 | ||
1348 | tb1 = tb->jmp_next[n]; | |
1349 | if (tb1 != NULL) { | |
1350 | /* find head of list */ | |
1351 | for(;;) { | |
1352 | n1 = (long)tb1 & 3; | |
1353 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
1354 | if (n1 == 2) | |
1355 | break; | |
1356 | tb1 = tb1->jmp_next[n1]; | |
1357 | } | |
1358 | /* we are now sure now that tb jumps to tb1 */ | |
1359 | tb_next = tb1; | |
1360 | ||
1361 | /* remove tb from the jmp_first list */ | |
1362 | ptb = &tb_next->jmp_first; | |
1363 | for(;;) { | |
1364 | tb1 = *ptb; | |
1365 | n1 = (long)tb1 & 3; | |
1366 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
1367 | if (n1 == n && tb1 == tb) | |
1368 | break; | |
1369 | ptb = &tb1->jmp_next[n1]; | |
1370 | } | |
1371 | *ptb = tb->jmp_next[n]; | |
1372 | tb->jmp_next[n] = NULL; | |
3b46e624 | 1373 | |
ea041c0e FB |
1374 | /* suppress the jump to next tb in generated code */ |
1375 | tb_reset_jump(tb, n); | |
1376 | ||
0124311e | 1377 | /* suppress jumps in the tb on which we could have jumped */ |
ea041c0e FB |
1378 | tb_reset_jump_recursive(tb_next); |
1379 | } | |
1380 | } | |
1381 | ||
1382 | static void tb_reset_jump_recursive(TranslationBlock *tb) | |
1383 | { | |
1384 | tb_reset_jump_recursive2(tb, 0); | |
1385 | tb_reset_jump_recursive2(tb, 1); | |
1386 | } | |
1387 | ||
1fddef4b | 1388 | #if defined(TARGET_HAS_ICE) |
94df27fd PB |
1389 | #if defined(CONFIG_USER_ONLY) |
1390 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) | |
1391 | { | |
1392 | tb_invalidate_phys_page_range(pc, pc + 1, 0); | |
1393 | } | |
1394 | #else | |
d720b93d FB |
1395 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
1396 | { | |
c227f099 | 1397 | target_phys_addr_t addr; |
9b3c35e0 | 1398 | target_ulong pd; |
c227f099 | 1399 | ram_addr_t ram_addr; |
c2f07f81 | 1400 | PhysPageDesc *p; |
d720b93d | 1401 | |
c2f07f81 PB |
1402 | addr = cpu_get_phys_page_debug(env, pc); |
1403 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
1404 | if (!p) { | |
1405 | pd = IO_MEM_UNASSIGNED; | |
1406 | } else { | |
1407 | pd = p->phys_offset; | |
1408 | } | |
1409 | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); | |
706cd4b5 | 1410 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
d720b93d | 1411 | } |
c27004ec | 1412 | #endif |
94df27fd | 1413 | #endif /* TARGET_HAS_ICE */ |
d720b93d | 1414 | |
c527ee8f PB |
1415 | #if defined(CONFIG_USER_ONLY) |
1416 | void cpu_watchpoint_remove_all(CPUState *env, int mask) | |
1417 | ||
1418 | { | |
1419 | } | |
1420 | ||
1421 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, | |
1422 | int flags, CPUWatchpoint **watchpoint) | |
1423 | { | |
1424 | return -ENOSYS; | |
1425 | } | |
1426 | #else | |
6658ffb8 | 1427 | /* Add a watchpoint. */ |
a1d1bb31 AL |
1428 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
1429 | int flags, CPUWatchpoint **watchpoint) | |
6658ffb8 | 1430 | { |
b4051334 | 1431 | target_ulong len_mask = ~(len - 1); |
c0ce998e | 1432 | CPUWatchpoint *wp; |
6658ffb8 | 1433 | |
b4051334 AL |
1434 | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */ |
1435 | if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) { | |
1436 | fprintf(stderr, "qemu: tried to set invalid watchpoint at " | |
1437 | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); | |
1438 | return -EINVAL; | |
1439 | } | |
a1d1bb31 | 1440 | wp = qemu_malloc(sizeof(*wp)); |
a1d1bb31 AL |
1441 | |
1442 | wp->vaddr = addr; | |
b4051334 | 1443 | wp->len_mask = len_mask; |
a1d1bb31 AL |
1444 | wp->flags = flags; |
1445 | ||
2dc9f411 | 1446 | /* keep all GDB-injected watchpoints in front */ |
c0ce998e | 1447 | if (flags & BP_GDB) |
72cf2d4f | 1448 | QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); |
c0ce998e | 1449 | else |
72cf2d4f | 1450 | QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); |
6658ffb8 | 1451 | |
6658ffb8 | 1452 | tlb_flush_page(env, addr); |
a1d1bb31 AL |
1453 | |
1454 | if (watchpoint) | |
1455 | *watchpoint = wp; | |
1456 | return 0; | |
6658ffb8 PB |
1457 | } |
1458 | ||
a1d1bb31 AL |
1459 | /* Remove a specific watchpoint. */ |
1460 | int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len, | |
1461 | int flags) | |
6658ffb8 | 1462 | { |
b4051334 | 1463 | target_ulong len_mask = ~(len - 1); |
a1d1bb31 | 1464 | CPUWatchpoint *wp; |
6658ffb8 | 1465 | |
72cf2d4f | 1466 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 | 1467 | if (addr == wp->vaddr && len_mask == wp->len_mask |
6e140f28 | 1468 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
a1d1bb31 | 1469 | cpu_watchpoint_remove_by_ref(env, wp); |
6658ffb8 PB |
1470 | return 0; |
1471 | } | |
1472 | } | |
a1d1bb31 | 1473 | return -ENOENT; |
6658ffb8 PB |
1474 | } |
1475 | ||
a1d1bb31 AL |
1476 | /* Remove a specific watchpoint by reference. */ |
1477 | void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint) | |
1478 | { | |
72cf2d4f | 1479 | QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
7d03f82f | 1480 | |
a1d1bb31 AL |
1481 | tlb_flush_page(env, watchpoint->vaddr); |
1482 | ||
1483 | qemu_free(watchpoint); | |
1484 | } | |
1485 | ||
1486 | /* Remove all matching watchpoints. */ | |
1487 | void cpu_watchpoint_remove_all(CPUState *env, int mask) | |
1488 | { | |
c0ce998e | 1489 | CPUWatchpoint *wp, *next; |
a1d1bb31 | 1490 | |
72cf2d4f | 1491 | QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
a1d1bb31 AL |
1492 | if (wp->flags & mask) |
1493 | cpu_watchpoint_remove_by_ref(env, wp); | |
c0ce998e | 1494 | } |
7d03f82f | 1495 | } |
c527ee8f | 1496 | #endif |
7d03f82f | 1497 | |
a1d1bb31 AL |
1498 | /* Add a breakpoint. */ |
1499 | int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags, | |
1500 | CPUBreakpoint **breakpoint) | |
4c3a88a2 | 1501 | { |
1fddef4b | 1502 | #if defined(TARGET_HAS_ICE) |
c0ce998e | 1503 | CPUBreakpoint *bp; |
3b46e624 | 1504 | |
a1d1bb31 | 1505 | bp = qemu_malloc(sizeof(*bp)); |
4c3a88a2 | 1506 | |
a1d1bb31 AL |
1507 | bp->pc = pc; |
1508 | bp->flags = flags; | |
1509 | ||
2dc9f411 | 1510 | /* keep all GDB-injected breakpoints in front */ |
c0ce998e | 1511 | if (flags & BP_GDB) |
72cf2d4f | 1512 | QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); |
c0ce998e | 1513 | else |
72cf2d4f | 1514 | QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); |
3b46e624 | 1515 | |
d720b93d | 1516 | breakpoint_invalidate(env, pc); |
a1d1bb31 AL |
1517 | |
1518 | if (breakpoint) | |
1519 | *breakpoint = bp; | |
4c3a88a2 FB |
1520 | return 0; |
1521 | #else | |
a1d1bb31 | 1522 | return -ENOSYS; |
4c3a88a2 FB |
1523 | #endif |
1524 | } | |
1525 | ||
a1d1bb31 AL |
1526 | /* Remove a specific breakpoint. */ |
1527 | int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags) | |
1528 | { | |
7d03f82f | 1529 | #if defined(TARGET_HAS_ICE) |
a1d1bb31 AL |
1530 | CPUBreakpoint *bp; |
1531 | ||
72cf2d4f | 1532 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
a1d1bb31 AL |
1533 | if (bp->pc == pc && bp->flags == flags) { |
1534 | cpu_breakpoint_remove_by_ref(env, bp); | |
1535 | return 0; | |
1536 | } | |
7d03f82f | 1537 | } |
a1d1bb31 AL |
1538 | return -ENOENT; |
1539 | #else | |
1540 | return -ENOSYS; | |
7d03f82f EI |
1541 | #endif |
1542 | } | |
1543 | ||
a1d1bb31 AL |
1544 | /* Remove a specific breakpoint by reference. */ |
1545 | void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint) | |
4c3a88a2 | 1546 | { |
1fddef4b | 1547 | #if defined(TARGET_HAS_ICE) |
72cf2d4f | 1548 | QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
d720b93d | 1549 | |
a1d1bb31 AL |
1550 | breakpoint_invalidate(env, breakpoint->pc); |
1551 | ||
1552 | qemu_free(breakpoint); | |
1553 | #endif | |
1554 | } | |
1555 | ||
1556 | /* Remove all matching breakpoints. */ | |
1557 | void cpu_breakpoint_remove_all(CPUState *env, int mask) | |
1558 | { | |
1559 | #if defined(TARGET_HAS_ICE) | |
c0ce998e | 1560 | CPUBreakpoint *bp, *next; |
a1d1bb31 | 1561 | |
72cf2d4f | 1562 | QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
a1d1bb31 AL |
1563 | if (bp->flags & mask) |
1564 | cpu_breakpoint_remove_by_ref(env, bp); | |
c0ce998e | 1565 | } |
4c3a88a2 FB |
1566 | #endif |
1567 | } | |
1568 | ||
c33a346e FB |
1569 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
1570 | CPU loop after each instruction */ | |
1571 | void cpu_single_step(CPUState *env, int enabled) | |
1572 | { | |
1fddef4b | 1573 | #if defined(TARGET_HAS_ICE) |
c33a346e FB |
1574 | if (env->singlestep_enabled != enabled) { |
1575 | env->singlestep_enabled = enabled; | |
e22a25c9 AL |
1576 | if (kvm_enabled()) |
1577 | kvm_update_guest_debug(env, 0); | |
1578 | else { | |
ccbb4d44 | 1579 | /* must flush all the translated code to avoid inconsistencies */ |
e22a25c9 AL |
1580 | /* XXX: only flush what is necessary */ |
1581 | tb_flush(env); | |
1582 | } | |
c33a346e FB |
1583 | } |
1584 | #endif | |
1585 | } | |
1586 | ||
34865134 FB |
1587 | /* enable or disable low levels log */ |
1588 | void cpu_set_log(int log_flags) | |
1589 | { | |
1590 | loglevel = log_flags; | |
1591 | if (loglevel && !logfile) { | |
11fcfab4 | 1592 | logfile = fopen(logfilename, log_append ? "a" : "w"); |
34865134 FB |
1593 | if (!logfile) { |
1594 | perror(logfilename); | |
1595 | _exit(1); | |
1596 | } | |
9fa3e853 FB |
1597 | #if !defined(CONFIG_SOFTMMU) |
1598 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ | |
1599 | { | |
b55266b5 | 1600 | static char logfile_buf[4096]; |
9fa3e853 FB |
1601 | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
1602 | } | |
bf65f53f FN |
1603 | #elif !defined(_WIN32) |
1604 | /* Win32 doesn't support line-buffering and requires size >= 2 */ | |
34865134 | 1605 | setvbuf(logfile, NULL, _IOLBF, 0); |
9fa3e853 | 1606 | #endif |
e735b91c PB |
1607 | log_append = 1; |
1608 | } | |
1609 | if (!loglevel && logfile) { | |
1610 | fclose(logfile); | |
1611 | logfile = NULL; | |
34865134 FB |
1612 | } |
1613 | } | |
1614 | ||
1615 | void cpu_set_log_filename(const char *filename) | |
1616 | { | |
1617 | logfilename = strdup(filename); | |
e735b91c PB |
1618 | if (logfile) { |
1619 | fclose(logfile); | |
1620 | logfile = NULL; | |
1621 | } | |
1622 | cpu_set_log(loglevel); | |
34865134 | 1623 | } |
c33a346e | 1624 | |
3098dba0 | 1625 | static void cpu_unlink_tb(CPUState *env) |
ea041c0e | 1626 | { |
3098dba0 AJ |
1627 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the |
1628 | problem and hope the cpu will stop of its own accord. For userspace | |
1629 | emulation this often isn't actually as bad as it sounds. Often | |
1630 | signals are used primarily to interrupt blocking syscalls. */ | |
ea041c0e | 1631 | TranslationBlock *tb; |
c227f099 | 1632 | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED; |
59817ccb | 1633 | |
cab1b4bd | 1634 | spin_lock(&interrupt_lock); |
3098dba0 AJ |
1635 | tb = env->current_tb; |
1636 | /* if the cpu is currently executing code, we must unlink it and | |
1637 | all the potentially executing TB */ | |
f76cfe56 | 1638 | if (tb) { |
3098dba0 AJ |
1639 | env->current_tb = NULL; |
1640 | tb_reset_jump_recursive(tb); | |
be214e6c | 1641 | } |
cab1b4bd | 1642 | spin_unlock(&interrupt_lock); |
3098dba0 AJ |
1643 | } |
1644 | ||
97ffbd8d | 1645 | #ifndef CONFIG_USER_ONLY |
3098dba0 | 1646 | /* mask must never be zero, except for A20 change call */ |
ec6959d0 | 1647 | static void tcg_handle_interrupt(CPUState *env, int mask) |
3098dba0 AJ |
1648 | { |
1649 | int old_mask; | |
be214e6c | 1650 | |
2e70f6ef | 1651 | old_mask = env->interrupt_request; |
68a79315 | 1652 | env->interrupt_request |= mask; |
3098dba0 | 1653 | |
8edac960 AL |
1654 | /* |
1655 | * If called from iothread context, wake the target cpu in | |
1656 | * case its halted. | |
1657 | */ | |
b7680cb6 | 1658 | if (!qemu_cpu_is_self(env)) { |
8edac960 AL |
1659 | qemu_cpu_kick(env); |
1660 | return; | |
1661 | } | |
8edac960 | 1662 | |
2e70f6ef | 1663 | if (use_icount) { |
266910c4 | 1664 | env->icount_decr.u16.high = 0xffff; |
2e70f6ef | 1665 | if (!can_do_io(env) |
be214e6c | 1666 | && (mask & ~old_mask) != 0) { |
2e70f6ef PB |
1667 | cpu_abort(env, "Raised interrupt while not in I/O function"); |
1668 | } | |
2e70f6ef | 1669 | } else { |
3098dba0 | 1670 | cpu_unlink_tb(env); |
ea041c0e FB |
1671 | } |
1672 | } | |
1673 | ||
ec6959d0 JK |
1674 | CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt; |
1675 | ||
97ffbd8d JK |
1676 | #else /* CONFIG_USER_ONLY */ |
1677 | ||
1678 | void cpu_interrupt(CPUState *env, int mask) | |
1679 | { | |
1680 | env->interrupt_request |= mask; | |
1681 | cpu_unlink_tb(env); | |
1682 | } | |
1683 | #endif /* CONFIG_USER_ONLY */ | |
1684 | ||
b54ad049 FB |
1685 | void cpu_reset_interrupt(CPUState *env, int mask) |
1686 | { | |
1687 | env->interrupt_request &= ~mask; | |
1688 | } | |
1689 | ||
3098dba0 AJ |
1690 | void cpu_exit(CPUState *env) |
1691 | { | |
1692 | env->exit_request = 1; | |
1693 | cpu_unlink_tb(env); | |
1694 | } | |
1695 | ||
c7cd6a37 | 1696 | const CPULogItem cpu_log_items[] = { |
5fafdf24 | 1697 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
f193c797 FB |
1698 | "show generated host assembly code for each compiled TB" }, |
1699 | { CPU_LOG_TB_IN_ASM, "in_asm", | |
1700 | "show target assembly code for each compiled TB" }, | |
5fafdf24 | 1701 | { CPU_LOG_TB_OP, "op", |
57fec1fe | 1702 | "show micro ops for each compiled TB" }, |
f193c797 | 1703 | { CPU_LOG_TB_OP_OPT, "op_opt", |
e01a1157 BS |
1704 | "show micro ops " |
1705 | #ifdef TARGET_I386 | |
1706 | "before eflags optimization and " | |
f193c797 | 1707 | #endif |
e01a1157 | 1708 | "after liveness analysis" }, |
f193c797 FB |
1709 | { CPU_LOG_INT, "int", |
1710 | "show interrupts/exceptions in short format" }, | |
1711 | { CPU_LOG_EXEC, "exec", | |
1712 | "show trace before each executed TB (lots of logs)" }, | |
9fddaa0c | 1713 | { CPU_LOG_TB_CPU, "cpu", |
e91c8a77 | 1714 | "show CPU state before block translation" }, |
f193c797 FB |
1715 | #ifdef TARGET_I386 |
1716 | { CPU_LOG_PCALL, "pcall", | |
1717 | "show protected mode far calls/returns/exceptions" }, | |
eca1bdf4 AL |
1718 | { CPU_LOG_RESET, "cpu_reset", |
1719 | "show CPU state before CPU resets" }, | |
f193c797 | 1720 | #endif |
8e3a9fd2 | 1721 | #ifdef DEBUG_IOPORT |
fd872598 FB |
1722 | { CPU_LOG_IOPORT, "ioport", |
1723 | "show all i/o ports accesses" }, | |
8e3a9fd2 | 1724 | #endif |
f193c797 FB |
1725 | { 0, NULL, NULL }, |
1726 | }; | |
1727 | ||
f6f3fbca MT |
1728 | #ifndef CONFIG_USER_ONLY |
1729 | static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list | |
1730 | = QLIST_HEAD_INITIALIZER(memory_client_list); | |
1731 | ||
1732 | static void cpu_notify_set_memory(target_phys_addr_t start_addr, | |
9742bf26 | 1733 | ram_addr_t size, |
0fd542fb MT |
1734 | ram_addr_t phys_offset, |
1735 | bool log_dirty) | |
f6f3fbca MT |
1736 | { |
1737 | CPUPhysMemoryClient *client; | |
1738 | QLIST_FOREACH(client, &memory_client_list, list) { | |
0fd542fb | 1739 | client->set_memory(client, start_addr, size, phys_offset, log_dirty); |
f6f3fbca MT |
1740 | } |
1741 | } | |
1742 | ||
1743 | static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start, | |
9742bf26 | 1744 | target_phys_addr_t end) |
f6f3fbca MT |
1745 | { |
1746 | CPUPhysMemoryClient *client; | |
1747 | QLIST_FOREACH(client, &memory_client_list, list) { | |
1748 | int r = client->sync_dirty_bitmap(client, start, end); | |
1749 | if (r < 0) | |
1750 | return r; | |
1751 | } | |
1752 | return 0; | |
1753 | } | |
1754 | ||
1755 | static int cpu_notify_migration_log(int enable) | |
1756 | { | |
1757 | CPUPhysMemoryClient *client; | |
1758 | QLIST_FOREACH(client, &memory_client_list, list) { | |
1759 | int r = client->migration_log(client, enable); | |
1760 | if (r < 0) | |
1761 | return r; | |
1762 | } | |
1763 | return 0; | |
1764 | } | |
1765 | ||
2173a75f AW |
1766 | struct last_map { |
1767 | target_phys_addr_t start_addr; | |
1768 | ram_addr_t size; | |
1769 | ram_addr_t phys_offset; | |
1770 | }; | |
1771 | ||
8d4c78e7 AW |
1772 | /* The l1_phys_map provides the upper P_L1_BITs of the guest physical |
1773 | * address. Each intermediate table provides the next L2_BITs of guest | |
1774 | * physical address space. The number of levels vary based on host and | |
1775 | * guest configuration, making it efficient to build the final guest | |
1776 | * physical address by seeding the L1 offset and shifting and adding in | |
1777 | * each L2 offset as we recurse through them. */ | |
2173a75f AW |
1778 | static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level, |
1779 | void **lp, target_phys_addr_t addr, | |
1780 | struct last_map *map) | |
f6f3fbca | 1781 | { |
5cd2c5b6 | 1782 | int i; |
f6f3fbca | 1783 | |
5cd2c5b6 RH |
1784 | if (*lp == NULL) { |
1785 | return; | |
1786 | } | |
1787 | if (level == 0) { | |
1788 | PhysPageDesc *pd = *lp; | |
8d4c78e7 | 1789 | addr <<= L2_BITS + TARGET_PAGE_BITS; |
7296abac | 1790 | for (i = 0; i < L2_SIZE; ++i) { |
5cd2c5b6 | 1791 | if (pd[i].phys_offset != IO_MEM_UNASSIGNED) { |
2173a75f AW |
1792 | target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS; |
1793 | ||
1794 | if (map->size && | |
1795 | start_addr == map->start_addr + map->size && | |
1796 | pd[i].phys_offset == map->phys_offset + map->size) { | |
1797 | ||
1798 | map->size += TARGET_PAGE_SIZE; | |
1799 | continue; | |
1800 | } else if (map->size) { | |
1801 | client->set_memory(client, map->start_addr, | |
1802 | map->size, map->phys_offset, false); | |
1803 | } | |
1804 | ||
1805 | map->start_addr = start_addr; | |
1806 | map->size = TARGET_PAGE_SIZE; | |
1807 | map->phys_offset = pd[i].phys_offset; | |
f6f3fbca | 1808 | } |
5cd2c5b6 RH |
1809 | } |
1810 | } else { | |
1811 | void **pp = *lp; | |
7296abac | 1812 | for (i = 0; i < L2_SIZE; ++i) { |
8d4c78e7 | 1813 | phys_page_for_each_1(client, level - 1, pp + i, |
2173a75f | 1814 | (addr << L2_BITS) | i, map); |
f6f3fbca MT |
1815 | } |
1816 | } | |
1817 | } | |
1818 | ||
1819 | static void phys_page_for_each(CPUPhysMemoryClient *client) | |
1820 | { | |
5cd2c5b6 | 1821 | int i; |
2173a75f AW |
1822 | struct last_map map = { }; |
1823 | ||
5cd2c5b6 RH |
1824 | for (i = 0; i < P_L1_SIZE; ++i) { |
1825 | phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1, | |
2173a75f AW |
1826 | l1_phys_map + i, i, &map); |
1827 | } | |
1828 | if (map.size) { | |
1829 | client->set_memory(client, map.start_addr, map.size, map.phys_offset, | |
1830 | false); | |
f6f3fbca | 1831 | } |
f6f3fbca MT |
1832 | } |
1833 | ||
1834 | void cpu_register_phys_memory_client(CPUPhysMemoryClient *client) | |
1835 | { | |
1836 | QLIST_INSERT_HEAD(&memory_client_list, client, list); | |
1837 | phys_page_for_each(client); | |
1838 | } | |
1839 | ||
1840 | void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client) | |
1841 | { | |
1842 | QLIST_REMOVE(client, list); | |
1843 | } | |
1844 | #endif | |
1845 | ||
f193c797 FB |
1846 | static int cmp1(const char *s1, int n, const char *s2) |
1847 | { | |
1848 | if (strlen(s2) != n) | |
1849 | return 0; | |
1850 | return memcmp(s1, s2, n) == 0; | |
1851 | } | |
3b46e624 | 1852 | |
f193c797 FB |
1853 | /* takes a comma separated list of log masks. Return 0 if error. */ |
1854 | int cpu_str_to_log_mask(const char *str) | |
1855 | { | |
c7cd6a37 | 1856 | const CPULogItem *item; |
f193c797 FB |
1857 | int mask; |
1858 | const char *p, *p1; | |
1859 | ||
1860 | p = str; | |
1861 | mask = 0; | |
1862 | for(;;) { | |
1863 | p1 = strchr(p, ','); | |
1864 | if (!p1) | |
1865 | p1 = p + strlen(p); | |
9742bf26 YT |
1866 | if(cmp1(p,p1-p,"all")) { |
1867 | for(item = cpu_log_items; item->mask != 0; item++) { | |
1868 | mask |= item->mask; | |
1869 | } | |
1870 | } else { | |
1871 | for(item = cpu_log_items; item->mask != 0; item++) { | |
1872 | if (cmp1(p, p1 - p, item->name)) | |
1873 | goto found; | |
1874 | } | |
1875 | return 0; | |
f193c797 | 1876 | } |
f193c797 FB |
1877 | found: |
1878 | mask |= item->mask; | |
1879 | if (*p1 != ',') | |
1880 | break; | |
1881 | p = p1 + 1; | |
1882 | } | |
1883 | return mask; | |
1884 | } | |
ea041c0e | 1885 | |
7501267e FB |
1886 | void cpu_abort(CPUState *env, const char *fmt, ...) |
1887 | { | |
1888 | va_list ap; | |
493ae1f0 | 1889 | va_list ap2; |
7501267e FB |
1890 | |
1891 | va_start(ap, fmt); | |
493ae1f0 | 1892 | va_copy(ap2, ap); |
7501267e FB |
1893 | fprintf(stderr, "qemu: fatal: "); |
1894 | vfprintf(stderr, fmt, ap); | |
1895 | fprintf(stderr, "\n"); | |
1896 | #ifdef TARGET_I386 | |
7fe48483 FB |
1897 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
1898 | #else | |
1899 | cpu_dump_state(env, stderr, fprintf, 0); | |
7501267e | 1900 | #endif |
93fcfe39 AL |
1901 | if (qemu_log_enabled()) { |
1902 | qemu_log("qemu: fatal: "); | |
1903 | qemu_log_vprintf(fmt, ap2); | |
1904 | qemu_log("\n"); | |
f9373291 | 1905 | #ifdef TARGET_I386 |
93fcfe39 | 1906 | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
f9373291 | 1907 | #else |
93fcfe39 | 1908 | log_cpu_state(env, 0); |
f9373291 | 1909 | #endif |
31b1a7b4 | 1910 | qemu_log_flush(); |
93fcfe39 | 1911 | qemu_log_close(); |
924edcae | 1912 | } |
493ae1f0 | 1913 | va_end(ap2); |
f9373291 | 1914 | va_end(ap); |
fd052bf6 RV |
1915 | #if defined(CONFIG_USER_ONLY) |
1916 | { | |
1917 | struct sigaction act; | |
1918 | sigfillset(&act.sa_mask); | |
1919 | act.sa_handler = SIG_DFL; | |
1920 | sigaction(SIGABRT, &act, NULL); | |
1921 | } | |
1922 | #endif | |
7501267e FB |
1923 | abort(); |
1924 | } | |
1925 | ||
c5be9f08 TS |
1926 | CPUState *cpu_copy(CPUState *env) |
1927 | { | |
01ba9816 | 1928 | CPUState *new_env = cpu_init(env->cpu_model_str); |
c5be9f08 TS |
1929 | CPUState *next_cpu = new_env->next_cpu; |
1930 | int cpu_index = new_env->cpu_index; | |
5a38f081 AL |
1931 | #if defined(TARGET_HAS_ICE) |
1932 | CPUBreakpoint *bp; | |
1933 | CPUWatchpoint *wp; | |
1934 | #endif | |
1935 | ||
c5be9f08 | 1936 | memcpy(new_env, env, sizeof(CPUState)); |
5a38f081 AL |
1937 | |
1938 | /* Preserve chaining and index. */ | |
c5be9f08 TS |
1939 | new_env->next_cpu = next_cpu; |
1940 | new_env->cpu_index = cpu_index; | |
5a38f081 AL |
1941 | |
1942 | /* Clone all break/watchpoints. | |
1943 | Note: Once we support ptrace with hw-debug register access, make sure | |
1944 | BP_CPU break/watchpoints are handled correctly on clone. */ | |
72cf2d4f BS |
1945 | QTAILQ_INIT(&env->breakpoints); |
1946 | QTAILQ_INIT(&env->watchpoints); | |
5a38f081 | 1947 | #if defined(TARGET_HAS_ICE) |
72cf2d4f | 1948 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
5a38f081 AL |
1949 | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL); |
1950 | } | |
72cf2d4f | 1951 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
5a38f081 AL |
1952 | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1, |
1953 | wp->flags, NULL); | |
1954 | } | |
1955 | #endif | |
1956 | ||
c5be9f08 TS |
1957 | return new_env; |
1958 | } | |
1959 | ||
0124311e FB |
1960 | #if !defined(CONFIG_USER_ONLY) |
1961 | ||
5c751e99 EI |
1962 | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
1963 | { | |
1964 | unsigned int i; | |
1965 | ||
1966 | /* Discard jump cache entries for any tb which might potentially | |
1967 | overlap the flushed page. */ | |
1968 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); | |
1969 | memset (&env->tb_jmp_cache[i], 0, | |
9742bf26 | 1970 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
5c751e99 EI |
1971 | |
1972 | i = tb_jmp_cache_hash_page(addr); | |
1973 | memset (&env->tb_jmp_cache[i], 0, | |
9742bf26 | 1974 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
5c751e99 EI |
1975 | } |
1976 | ||
08738984 IK |
1977 | static CPUTLBEntry s_cputlb_empty_entry = { |
1978 | .addr_read = -1, | |
1979 | .addr_write = -1, | |
1980 | .addr_code = -1, | |
1981 | .addend = -1, | |
1982 | }; | |
1983 | ||
ee8b7021 FB |
1984 | /* NOTE: if flush_global is true, also flush global entries (not |
1985 | implemented yet) */ | |
1986 | void tlb_flush(CPUState *env, int flush_global) | |
33417e70 | 1987 | { |
33417e70 | 1988 | int i; |
0124311e | 1989 | |
9fa3e853 FB |
1990 | #if defined(DEBUG_TLB) |
1991 | printf("tlb_flush:\n"); | |
1992 | #endif | |
0124311e FB |
1993 | /* must reset current TB so that interrupts cannot modify the |
1994 | links while we are modifying them */ | |
1995 | env->current_tb = NULL; | |
1996 | ||
33417e70 | 1997 | for(i = 0; i < CPU_TLB_SIZE; i++) { |
cfde4bd9 IY |
1998 | int mmu_idx; |
1999 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
08738984 | 2000 | env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry; |
cfde4bd9 | 2001 | } |
33417e70 | 2002 | } |
9fa3e853 | 2003 | |
8a40a180 | 2004 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
9fa3e853 | 2005 | |
d4c430a8 PB |
2006 | env->tlb_flush_addr = -1; |
2007 | env->tlb_flush_mask = 0; | |
e3db7226 | 2008 | tlb_flush_count++; |
33417e70 FB |
2009 | } |
2010 | ||
274da6b2 | 2011 | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
61382a50 | 2012 | { |
5fafdf24 | 2013 | if (addr == (tlb_entry->addr_read & |
84b7b8e7 | 2014 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
5fafdf24 | 2015 | addr == (tlb_entry->addr_write & |
84b7b8e7 | 2016 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
5fafdf24 | 2017 | addr == (tlb_entry->addr_code & |
84b7b8e7 | 2018 | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
08738984 | 2019 | *tlb_entry = s_cputlb_empty_entry; |
84b7b8e7 | 2020 | } |
61382a50 FB |
2021 | } |
2022 | ||
2e12669a | 2023 | void tlb_flush_page(CPUState *env, target_ulong addr) |
33417e70 | 2024 | { |
8a40a180 | 2025 | int i; |
cfde4bd9 | 2026 | int mmu_idx; |
0124311e | 2027 | |
9fa3e853 | 2028 | #if defined(DEBUG_TLB) |
108c49b8 | 2029 | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
9fa3e853 | 2030 | #endif |
d4c430a8 PB |
2031 | /* Check if we need to flush due to large pages. */ |
2032 | if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) { | |
2033 | #if defined(DEBUG_TLB) | |
2034 | printf("tlb_flush_page: forced full flush (" | |
2035 | TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", | |
2036 | env->tlb_flush_addr, env->tlb_flush_mask); | |
2037 | #endif | |
2038 | tlb_flush(env, 1); | |
2039 | return; | |
2040 | } | |
0124311e FB |
2041 | /* must reset current TB so that interrupts cannot modify the |
2042 | links while we are modifying them */ | |
2043 | env->current_tb = NULL; | |
61382a50 FB |
2044 | |
2045 | addr &= TARGET_PAGE_MASK; | |
2046 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); | |
cfde4bd9 IY |
2047 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
2048 | tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr); | |
0124311e | 2049 | |
5c751e99 | 2050 | tlb_flush_jmp_cache(env, addr); |
9fa3e853 FB |
2051 | } |
2052 | ||
9fa3e853 FB |
2053 | /* update the TLBs so that writes to code in the virtual page 'addr' |
2054 | can be detected */ | |
c227f099 | 2055 | static void tlb_protect_code(ram_addr_t ram_addr) |
9fa3e853 | 2056 | { |
5fafdf24 | 2057 | cpu_physical_memory_reset_dirty(ram_addr, |
6a00d601 FB |
2058 | ram_addr + TARGET_PAGE_SIZE, |
2059 | CODE_DIRTY_FLAG); | |
9fa3e853 FB |
2060 | } |
2061 | ||
9fa3e853 | 2062 | /* update the TLB so that writes in physical page 'phys_addr' are no longer |
3a7d929e | 2063 | tested for self modifying code */ |
c227f099 | 2064 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
3a7d929e | 2065 | target_ulong vaddr) |
9fa3e853 | 2066 | { |
f7c11b53 | 2067 | cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG); |
1ccde1cb FB |
2068 | } |
2069 | ||
5fafdf24 | 2070 | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
1ccde1cb FB |
2071 | unsigned long start, unsigned long length) |
2072 | { | |
2073 | unsigned long addr; | |
84b7b8e7 FB |
2074 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
2075 | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; | |
1ccde1cb | 2076 | if ((addr - start) < length) { |
0f459d16 | 2077 | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
1ccde1cb FB |
2078 | } |
2079 | } | |
2080 | } | |
2081 | ||
5579c7f3 | 2082 | /* Note: start and end must be within the same ram block. */ |
c227f099 | 2083 | void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, |
0a962c02 | 2084 | int dirty_flags) |
1ccde1cb FB |
2085 | { |
2086 | CPUState *env; | |
4f2ac237 | 2087 | unsigned long length, start1; |
f7c11b53 | 2088 | int i; |
1ccde1cb FB |
2089 | |
2090 | start &= TARGET_PAGE_MASK; | |
2091 | end = TARGET_PAGE_ALIGN(end); | |
2092 | ||
2093 | length = end - start; | |
2094 | if (length == 0) | |
2095 | return; | |
f7c11b53 | 2096 | cpu_physical_memory_mask_dirty_range(start, length, dirty_flags); |
f23db169 | 2097 | |
1ccde1cb FB |
2098 | /* we modify the TLB cache so that the dirty bit will be set again |
2099 | when accessing the range */ | |
b2e0a138 | 2100 | start1 = (unsigned long)qemu_safe_ram_ptr(start); |
a57d23e4 | 2101 | /* Check that we don't span multiple blocks - this breaks the |
5579c7f3 | 2102 | address comparisons below. */ |
b2e0a138 | 2103 | if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1 |
5579c7f3 PB |
2104 | != (end - 1) - start) { |
2105 | abort(); | |
2106 | } | |
2107 | ||
6a00d601 | 2108 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
cfde4bd9 IY |
2109 | int mmu_idx; |
2110 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
2111 | for(i = 0; i < CPU_TLB_SIZE; i++) | |
2112 | tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i], | |
2113 | start1, length); | |
2114 | } | |
6a00d601 | 2115 | } |
1ccde1cb FB |
2116 | } |
2117 | ||
74576198 AL |
2118 | int cpu_physical_memory_set_dirty_tracking(int enable) |
2119 | { | |
f6f3fbca | 2120 | int ret = 0; |
74576198 | 2121 | in_migration = enable; |
f6f3fbca MT |
2122 | ret = cpu_notify_migration_log(!!enable); |
2123 | return ret; | |
74576198 AL |
2124 | } |
2125 | ||
2126 | int cpu_physical_memory_get_dirty_tracking(void) | |
2127 | { | |
2128 | return in_migration; | |
2129 | } | |
2130 | ||
c227f099 AL |
2131 | int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, |
2132 | target_phys_addr_t end_addr) | |
2bec46dc | 2133 | { |
7b8f3b78 | 2134 | int ret; |
151f7749 | 2135 | |
f6f3fbca | 2136 | ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr); |
151f7749 | 2137 | return ret; |
2bec46dc AL |
2138 | } |
2139 | ||
e5896b12 AP |
2140 | int cpu_physical_log_start(target_phys_addr_t start_addr, |
2141 | ram_addr_t size) | |
2142 | { | |
2143 | CPUPhysMemoryClient *client; | |
2144 | QLIST_FOREACH(client, &memory_client_list, list) { | |
2145 | if (client->log_start) { | |
2146 | int r = client->log_start(client, start_addr, size); | |
2147 | if (r < 0) { | |
2148 | return r; | |
2149 | } | |
2150 | } | |
2151 | } | |
2152 | return 0; | |
2153 | } | |
2154 | ||
2155 | int cpu_physical_log_stop(target_phys_addr_t start_addr, | |
2156 | ram_addr_t size) | |
2157 | { | |
2158 | CPUPhysMemoryClient *client; | |
2159 | QLIST_FOREACH(client, &memory_client_list, list) { | |
2160 | if (client->log_stop) { | |
2161 | int r = client->log_stop(client, start_addr, size); | |
2162 | if (r < 0) { | |
2163 | return r; | |
2164 | } | |
2165 | } | |
2166 | } | |
2167 | return 0; | |
2168 | } | |
2169 | ||
3a7d929e FB |
2170 | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
2171 | { | |
c227f099 | 2172 | ram_addr_t ram_addr; |
5579c7f3 | 2173 | void *p; |
3a7d929e | 2174 | |
84b7b8e7 | 2175 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
5579c7f3 PB |
2176 | p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK) |
2177 | + tlb_entry->addend); | |
e890261f | 2178 | ram_addr = qemu_ram_addr_from_host_nofail(p); |
3a7d929e | 2179 | if (!cpu_physical_memory_is_dirty(ram_addr)) { |
0f459d16 | 2180 | tlb_entry->addr_write |= TLB_NOTDIRTY; |
3a7d929e FB |
2181 | } |
2182 | } | |
2183 | } | |
2184 | ||
2185 | /* update the TLB according to the current state of the dirty bits */ | |
2186 | void cpu_tlb_update_dirty(CPUState *env) | |
2187 | { | |
2188 | int i; | |
cfde4bd9 IY |
2189 | int mmu_idx; |
2190 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { | |
2191 | for(i = 0; i < CPU_TLB_SIZE; i++) | |
2192 | tlb_update_dirty(&env->tlb_table[mmu_idx][i]); | |
2193 | } | |
3a7d929e FB |
2194 | } |
2195 | ||
0f459d16 | 2196 | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
1ccde1cb | 2197 | { |
0f459d16 PB |
2198 | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) |
2199 | tlb_entry->addr_write = vaddr; | |
1ccde1cb FB |
2200 | } |
2201 | ||
0f459d16 PB |
2202 | /* update the TLB corresponding to virtual page vaddr |
2203 | so that it is no longer dirty */ | |
2204 | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) | |
1ccde1cb | 2205 | { |
1ccde1cb | 2206 | int i; |
cfde4bd9 | 2207 | int mmu_idx; |
1ccde1cb | 2208 | |
0f459d16 | 2209 | vaddr &= TARGET_PAGE_MASK; |
1ccde1cb | 2210 | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
cfde4bd9 IY |
2211 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
2212 | tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr); | |
9fa3e853 FB |
2213 | } |
2214 | ||
d4c430a8 PB |
2215 | /* Our TLB does not support large pages, so remember the area covered by |
2216 | large pages and trigger a full TLB flush if these are invalidated. */ | |
2217 | static void tlb_add_large_page(CPUState *env, target_ulong vaddr, | |
2218 | target_ulong size) | |
2219 | { | |
2220 | target_ulong mask = ~(size - 1); | |
2221 | ||
2222 | if (env->tlb_flush_addr == (target_ulong)-1) { | |
2223 | env->tlb_flush_addr = vaddr & mask; | |
2224 | env->tlb_flush_mask = mask; | |
2225 | return; | |
2226 | } | |
2227 | /* Extend the existing region to include the new page. | |
2228 | This is a compromise between unnecessary flushes and the cost | |
2229 | of maintaining a full variable size TLB. */ | |
2230 | mask &= env->tlb_flush_mask; | |
2231 | while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) { | |
2232 | mask <<= 1; | |
2233 | } | |
2234 | env->tlb_flush_addr &= mask; | |
2235 | env->tlb_flush_mask = mask; | |
2236 | } | |
2237 | ||
2238 | /* Add a new TLB entry. At most one entry for a given virtual address | |
2239 | is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the | |
2240 | supplied size is only used by tlb_flush_page. */ | |
2241 | void tlb_set_page(CPUState *env, target_ulong vaddr, | |
2242 | target_phys_addr_t paddr, int prot, | |
2243 | int mmu_idx, target_ulong size) | |
9fa3e853 | 2244 | { |
92e873b9 | 2245 | PhysPageDesc *p; |
4f2ac237 | 2246 | unsigned long pd; |
9fa3e853 | 2247 | unsigned int index; |
4f2ac237 | 2248 | target_ulong address; |
0f459d16 | 2249 | target_ulong code_address; |
355b1943 | 2250 | unsigned long addend; |
84b7b8e7 | 2251 | CPUTLBEntry *te; |
a1d1bb31 | 2252 | CPUWatchpoint *wp; |
c227f099 | 2253 | target_phys_addr_t iotlb; |
9fa3e853 | 2254 | |
d4c430a8 PB |
2255 | assert(size >= TARGET_PAGE_SIZE); |
2256 | if (size != TARGET_PAGE_SIZE) { | |
2257 | tlb_add_large_page(env, vaddr, size); | |
2258 | } | |
92e873b9 | 2259 | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
9fa3e853 FB |
2260 | if (!p) { |
2261 | pd = IO_MEM_UNASSIGNED; | |
9fa3e853 FB |
2262 | } else { |
2263 | pd = p->phys_offset; | |
9fa3e853 FB |
2264 | } |
2265 | #if defined(DEBUG_TLB) | |
7fd3f494 SW |
2266 | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx |
2267 | " prot=%x idx=%d pd=0x%08lx\n", | |
2268 | vaddr, paddr, prot, mmu_idx, pd); | |
9fa3e853 FB |
2269 | #endif |
2270 | ||
0f459d16 PB |
2271 | address = vaddr; |
2272 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) { | |
2273 | /* IO memory case (romd handled later) */ | |
2274 | address |= TLB_MMIO; | |
2275 | } | |
5579c7f3 | 2276 | addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
0f459d16 PB |
2277 | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
2278 | /* Normal RAM. */ | |
2279 | iotlb = pd & TARGET_PAGE_MASK; | |
2280 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM) | |
2281 | iotlb |= IO_MEM_NOTDIRTY; | |
2282 | else | |
2283 | iotlb |= IO_MEM_ROM; | |
2284 | } else { | |
ccbb4d44 | 2285 | /* IO handlers are currently passed a physical address. |
0f459d16 PB |
2286 | It would be nice to pass an offset from the base address |
2287 | of that region. This would avoid having to special case RAM, | |
2288 | and avoid full address decoding in every device. | |
2289 | We can't use the high bits of pd for this because | |
2290 | IO_MEM_ROMD uses these as a ram address. */ | |
8da3ff18 PB |
2291 | iotlb = (pd & ~TARGET_PAGE_MASK); |
2292 | if (p) { | |
8da3ff18 PB |
2293 | iotlb += p->region_offset; |
2294 | } else { | |
2295 | iotlb += paddr; | |
2296 | } | |
0f459d16 PB |
2297 | } |
2298 | ||
2299 | code_address = address; | |
2300 | /* Make accesses to pages with watchpoints go via the | |
2301 | watchpoint trap routines. */ | |
72cf2d4f | 2302 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
a1d1bb31 | 2303 | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) { |
bf298f83 JK |
2304 | /* Avoid trapping reads of pages with a write breakpoint. */ |
2305 | if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) { | |
2306 | iotlb = io_mem_watch + paddr; | |
2307 | address |= TLB_MMIO; | |
2308 | break; | |
2309 | } | |
6658ffb8 | 2310 | } |
0f459d16 | 2311 | } |
d79acba4 | 2312 | |
0f459d16 PB |
2313 | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
2314 | env->iotlb[mmu_idx][index] = iotlb - vaddr; | |
2315 | te = &env->tlb_table[mmu_idx][index]; | |
2316 | te->addend = addend - vaddr; | |
2317 | if (prot & PAGE_READ) { | |
2318 | te->addr_read = address; | |
2319 | } else { | |
2320 | te->addr_read = -1; | |
2321 | } | |
5c751e99 | 2322 | |
0f459d16 PB |
2323 | if (prot & PAGE_EXEC) { |
2324 | te->addr_code = code_address; | |
2325 | } else { | |
2326 | te->addr_code = -1; | |
2327 | } | |
2328 | if (prot & PAGE_WRITE) { | |
2329 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM || | |
2330 | (pd & IO_MEM_ROMD)) { | |
2331 | /* Write access calls the I/O callback. */ | |
2332 | te->addr_write = address | TLB_MMIO; | |
2333 | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && | |
2334 | !cpu_physical_memory_is_dirty(pd)) { | |
2335 | te->addr_write = address | TLB_NOTDIRTY; | |
9fa3e853 | 2336 | } else { |
0f459d16 | 2337 | te->addr_write = address; |
9fa3e853 | 2338 | } |
0f459d16 PB |
2339 | } else { |
2340 | te->addr_write = -1; | |
9fa3e853 | 2341 | } |
9fa3e853 FB |
2342 | } |
2343 | ||
0124311e FB |
2344 | #else |
2345 | ||
ee8b7021 | 2346 | void tlb_flush(CPUState *env, int flush_global) |
0124311e FB |
2347 | { |
2348 | } | |
2349 | ||
2e12669a | 2350 | void tlb_flush_page(CPUState *env, target_ulong addr) |
0124311e FB |
2351 | { |
2352 | } | |
2353 | ||
edf8e2af MW |
2354 | /* |
2355 | * Walks guest process memory "regions" one by one | |
2356 | * and calls callback function 'fn' for each region. | |
2357 | */ | |
5cd2c5b6 RH |
2358 | |
2359 | struct walk_memory_regions_data | |
2360 | { | |
2361 | walk_memory_regions_fn fn; | |
2362 | void *priv; | |
2363 | unsigned long start; | |
2364 | int prot; | |
2365 | }; | |
2366 | ||
2367 | static int walk_memory_regions_end(struct walk_memory_regions_data *data, | |
b480d9b7 | 2368 | abi_ulong end, int new_prot) |
5cd2c5b6 RH |
2369 | { |
2370 | if (data->start != -1ul) { | |
2371 | int rc = data->fn(data->priv, data->start, end, data->prot); | |
2372 | if (rc != 0) { | |
2373 | return rc; | |
2374 | } | |
2375 | } | |
2376 | ||
2377 | data->start = (new_prot ? end : -1ul); | |
2378 | data->prot = new_prot; | |
2379 | ||
2380 | return 0; | |
2381 | } | |
2382 | ||
2383 | static int walk_memory_regions_1(struct walk_memory_regions_data *data, | |
b480d9b7 | 2384 | abi_ulong base, int level, void **lp) |
5cd2c5b6 | 2385 | { |
b480d9b7 | 2386 | abi_ulong pa; |
5cd2c5b6 RH |
2387 | int i, rc; |
2388 | ||
2389 | if (*lp == NULL) { | |
2390 | return walk_memory_regions_end(data, base, 0); | |
2391 | } | |
2392 | ||
2393 | if (level == 0) { | |
2394 | PageDesc *pd = *lp; | |
7296abac | 2395 | for (i = 0; i < L2_SIZE; ++i) { |
5cd2c5b6 RH |
2396 | int prot = pd[i].flags; |
2397 | ||
2398 | pa = base | (i << TARGET_PAGE_BITS); | |
2399 | if (prot != data->prot) { | |
2400 | rc = walk_memory_regions_end(data, pa, prot); | |
2401 | if (rc != 0) { | |
2402 | return rc; | |
9fa3e853 | 2403 | } |
9fa3e853 | 2404 | } |
5cd2c5b6 RH |
2405 | } |
2406 | } else { | |
2407 | void **pp = *lp; | |
7296abac | 2408 | for (i = 0; i < L2_SIZE; ++i) { |
b480d9b7 PB |
2409 | pa = base | ((abi_ulong)i << |
2410 | (TARGET_PAGE_BITS + L2_BITS * level)); | |
5cd2c5b6 RH |
2411 | rc = walk_memory_regions_1(data, pa, level - 1, pp + i); |
2412 | if (rc != 0) { | |
2413 | return rc; | |
2414 | } | |
2415 | } | |
2416 | } | |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | int walk_memory_regions(void *priv, walk_memory_regions_fn fn) | |
2422 | { | |
2423 | struct walk_memory_regions_data data; | |
2424 | unsigned long i; | |
2425 | ||
2426 | data.fn = fn; | |
2427 | data.priv = priv; | |
2428 | data.start = -1ul; | |
2429 | data.prot = 0; | |
2430 | ||
2431 | for (i = 0; i < V_L1_SIZE; i++) { | |
b480d9b7 | 2432 | int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT, |
5cd2c5b6 RH |
2433 | V_L1_SHIFT / L2_BITS - 1, l1_map + i); |
2434 | if (rc != 0) { | |
2435 | return rc; | |
9fa3e853 | 2436 | } |
33417e70 | 2437 | } |
5cd2c5b6 RH |
2438 | |
2439 | return walk_memory_regions_end(&data, 0, 0); | |
edf8e2af MW |
2440 | } |
2441 | ||
b480d9b7 PB |
2442 | static int dump_region(void *priv, abi_ulong start, |
2443 | abi_ulong end, unsigned long prot) | |
edf8e2af MW |
2444 | { |
2445 | FILE *f = (FILE *)priv; | |
2446 | ||
b480d9b7 PB |
2447 | (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx |
2448 | " "TARGET_ABI_FMT_lx" %c%c%c\n", | |
edf8e2af MW |
2449 | start, end, end - start, |
2450 | ((prot & PAGE_READ) ? 'r' : '-'), | |
2451 | ((prot & PAGE_WRITE) ? 'w' : '-'), | |
2452 | ((prot & PAGE_EXEC) ? 'x' : '-')); | |
2453 | ||
2454 | return (0); | |
2455 | } | |
2456 | ||
2457 | /* dump memory mappings */ | |
2458 | void page_dump(FILE *f) | |
2459 | { | |
2460 | (void) fprintf(f, "%-8s %-8s %-8s %s\n", | |
2461 | "start", "end", "size", "prot"); | |
2462 | walk_memory_regions(f, dump_region); | |
33417e70 FB |
2463 | } |
2464 | ||
53a5960a | 2465 | int page_get_flags(target_ulong address) |
33417e70 | 2466 | { |
9fa3e853 FB |
2467 | PageDesc *p; |
2468 | ||
2469 | p = page_find(address >> TARGET_PAGE_BITS); | |
33417e70 | 2470 | if (!p) |
9fa3e853 FB |
2471 | return 0; |
2472 | return p->flags; | |
2473 | } | |
2474 | ||
376a7909 RH |
2475 | /* Modify the flags of a page and invalidate the code if necessary. |
2476 | The flag PAGE_WRITE_ORG is positioned automatically depending | |
2477 | on PAGE_WRITE. The mmap_lock should already be held. */ | |
53a5960a | 2478 | void page_set_flags(target_ulong start, target_ulong end, int flags) |
9fa3e853 | 2479 | { |
376a7909 RH |
2480 | target_ulong addr, len; |
2481 | ||
2482 | /* This function should never be called with addresses outside the | |
2483 | guest address space. If this assert fires, it probably indicates | |
2484 | a missing call to h2g_valid. */ | |
b480d9b7 PB |
2485 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS |
2486 | assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); | |
376a7909 RH |
2487 | #endif |
2488 | assert(start < end); | |
9fa3e853 FB |
2489 | |
2490 | start = start & TARGET_PAGE_MASK; | |
2491 | end = TARGET_PAGE_ALIGN(end); | |
376a7909 RH |
2492 | |
2493 | if (flags & PAGE_WRITE) { | |
9fa3e853 | 2494 | flags |= PAGE_WRITE_ORG; |
376a7909 RH |
2495 | } |
2496 | ||
2497 | for (addr = start, len = end - start; | |
2498 | len != 0; | |
2499 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { | |
2500 | PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); | |
2501 | ||
2502 | /* If the write protection bit is set, then we invalidate | |
2503 | the code inside. */ | |
5fafdf24 | 2504 | if (!(p->flags & PAGE_WRITE) && |
9fa3e853 FB |
2505 | (flags & PAGE_WRITE) && |
2506 | p->first_tb) { | |
d720b93d | 2507 | tb_invalidate_phys_page(addr, 0, NULL); |
9fa3e853 FB |
2508 | } |
2509 | p->flags = flags; | |
2510 | } | |
33417e70 FB |
2511 | } |
2512 | ||
3d97b40b TS |
2513 | int page_check_range(target_ulong start, target_ulong len, int flags) |
2514 | { | |
2515 | PageDesc *p; | |
2516 | target_ulong end; | |
2517 | target_ulong addr; | |
2518 | ||
376a7909 RH |
2519 | /* This function should never be called with addresses outside the |
2520 | guest address space. If this assert fires, it probably indicates | |
2521 | a missing call to h2g_valid. */ | |
338e9e6c BS |
2522 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS |
2523 | assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); | |
376a7909 RH |
2524 | #endif |
2525 | ||
3e0650a9 RH |
2526 | if (len == 0) { |
2527 | return 0; | |
2528 | } | |
376a7909 RH |
2529 | if (start + len - 1 < start) { |
2530 | /* We've wrapped around. */ | |
55f280c9 | 2531 | return -1; |
376a7909 | 2532 | } |
55f280c9 | 2533 | |
3d97b40b TS |
2534 | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ |
2535 | start = start & TARGET_PAGE_MASK; | |
2536 | ||
376a7909 RH |
2537 | for (addr = start, len = end - start; |
2538 | len != 0; | |
2539 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { | |
3d97b40b TS |
2540 | p = page_find(addr >> TARGET_PAGE_BITS); |
2541 | if( !p ) | |
2542 | return -1; | |
2543 | if( !(p->flags & PAGE_VALID) ) | |
2544 | return -1; | |
2545 | ||
dae3270c | 2546 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) |
3d97b40b | 2547 | return -1; |
dae3270c FB |
2548 | if (flags & PAGE_WRITE) { |
2549 | if (!(p->flags & PAGE_WRITE_ORG)) | |
2550 | return -1; | |
2551 | /* unprotect the page if it was put read-only because it | |
2552 | contains translated code */ | |
2553 | if (!(p->flags & PAGE_WRITE)) { | |
2554 | if (!page_unprotect(addr, 0, NULL)) | |
2555 | return -1; | |
2556 | } | |
2557 | return 0; | |
2558 | } | |
3d97b40b TS |
2559 | } |
2560 | return 0; | |
2561 | } | |
2562 | ||
9fa3e853 | 2563 | /* called from signal handler: invalidate the code and unprotect the |
ccbb4d44 | 2564 | page. Return TRUE if the fault was successfully handled. */ |
53a5960a | 2565 | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
9fa3e853 | 2566 | { |
45d679d6 AJ |
2567 | unsigned int prot; |
2568 | PageDesc *p; | |
53a5960a | 2569 | target_ulong host_start, host_end, addr; |
9fa3e853 | 2570 | |
c8a706fe PB |
2571 | /* Technically this isn't safe inside a signal handler. However we |
2572 | know this only ever happens in a synchronous SEGV handler, so in | |
2573 | practice it seems to be ok. */ | |
2574 | mmap_lock(); | |
2575 | ||
45d679d6 AJ |
2576 | p = page_find(address >> TARGET_PAGE_BITS); |
2577 | if (!p) { | |
c8a706fe | 2578 | mmap_unlock(); |
9fa3e853 | 2579 | return 0; |
c8a706fe | 2580 | } |
45d679d6 | 2581 | |
9fa3e853 FB |
2582 | /* if the page was really writable, then we change its |
2583 | protection back to writable */ | |
45d679d6 AJ |
2584 | if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) { |
2585 | host_start = address & qemu_host_page_mask; | |
2586 | host_end = host_start + qemu_host_page_size; | |
2587 | ||
2588 | prot = 0; | |
2589 | for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) { | |
2590 | p = page_find(addr >> TARGET_PAGE_BITS); | |
2591 | p->flags |= PAGE_WRITE; | |
2592 | prot |= p->flags; | |
2593 | ||
9fa3e853 FB |
2594 | /* and since the content will be modified, we must invalidate |
2595 | the corresponding translated code. */ | |
45d679d6 | 2596 | tb_invalidate_phys_page(addr, pc, puc); |
9fa3e853 | 2597 | #ifdef DEBUG_TB_CHECK |
45d679d6 | 2598 | tb_invalidate_check(addr); |
9fa3e853 | 2599 | #endif |
9fa3e853 | 2600 | } |
45d679d6 AJ |
2601 | mprotect((void *)g2h(host_start), qemu_host_page_size, |
2602 | prot & PAGE_BITS); | |
2603 | ||
2604 | mmap_unlock(); | |
2605 | return 1; | |
9fa3e853 | 2606 | } |
c8a706fe | 2607 | mmap_unlock(); |
9fa3e853 FB |
2608 | return 0; |
2609 | } | |
2610 | ||
6a00d601 FB |
2611 | static inline void tlb_set_dirty(CPUState *env, |
2612 | unsigned long addr, target_ulong vaddr) | |
1ccde1cb FB |
2613 | { |
2614 | } | |
9fa3e853 FB |
2615 | #endif /* defined(CONFIG_USER_ONLY) */ |
2616 | ||
e2eef170 | 2617 | #if !defined(CONFIG_USER_ONLY) |
8da3ff18 | 2618 | |
c04b2b78 PB |
2619 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
2620 | typedef struct subpage_t { | |
2621 | target_phys_addr_t base; | |
f6405247 RH |
2622 | ram_addr_t sub_io_index[TARGET_PAGE_SIZE]; |
2623 | ram_addr_t region_offset[TARGET_PAGE_SIZE]; | |
c04b2b78 PB |
2624 | } subpage_t; |
2625 | ||
c227f099 AL |
2626 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
2627 | ram_addr_t memory, ram_addr_t region_offset); | |
f6405247 RH |
2628 | static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
2629 | ram_addr_t orig_memory, | |
2630 | ram_addr_t region_offset); | |
db7b5426 BS |
2631 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \ |
2632 | need_subpage) \ | |
2633 | do { \ | |
2634 | if (addr > start_addr) \ | |
2635 | start_addr2 = 0; \ | |
2636 | else { \ | |
2637 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ | |
2638 | if (start_addr2 > 0) \ | |
2639 | need_subpage = 1; \ | |
2640 | } \ | |
2641 | \ | |
49e9fba2 | 2642 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \ |
db7b5426 BS |
2643 | end_addr2 = TARGET_PAGE_SIZE - 1; \ |
2644 | else { \ | |
2645 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \ | |
2646 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ | |
2647 | need_subpage = 1; \ | |
2648 | } \ | |
2649 | } while (0) | |
2650 | ||
8f2498f9 MT |
2651 | /* register physical memory. |
2652 | For RAM, 'size' must be a multiple of the target page size. | |
2653 | If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an | |
8da3ff18 PB |
2654 | io memory page. The address used when calling the IO function is |
2655 | the offset from the start of the region, plus region_offset. Both | |
ccbb4d44 | 2656 | start_addr and region_offset are rounded down to a page boundary |
8da3ff18 PB |
2657 | before calculating this offset. This should not be a problem unless |
2658 | the low bits of start_addr and region_offset differ. */ | |
0fd542fb | 2659 | void cpu_register_physical_memory_log(target_phys_addr_t start_addr, |
c227f099 AL |
2660 | ram_addr_t size, |
2661 | ram_addr_t phys_offset, | |
0fd542fb MT |
2662 | ram_addr_t region_offset, |
2663 | bool log_dirty) | |
33417e70 | 2664 | { |
c227f099 | 2665 | target_phys_addr_t addr, end_addr; |
92e873b9 | 2666 | PhysPageDesc *p; |
9d42037b | 2667 | CPUState *env; |
c227f099 | 2668 | ram_addr_t orig_size = size; |
f6405247 | 2669 | subpage_t *subpage; |
33417e70 | 2670 | |
3b8e6a2d | 2671 | assert(size); |
0fd542fb | 2672 | cpu_notify_set_memory(start_addr, size, phys_offset, log_dirty); |
f6f3fbca | 2673 | |
67c4d23c PB |
2674 | if (phys_offset == IO_MEM_UNASSIGNED) { |
2675 | region_offset = start_addr; | |
2676 | } | |
8da3ff18 | 2677 | region_offset &= TARGET_PAGE_MASK; |
5fd386f6 | 2678 | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK; |
c227f099 | 2679 | end_addr = start_addr + (target_phys_addr_t)size; |
3b8e6a2d EI |
2680 | |
2681 | addr = start_addr; | |
2682 | do { | |
db7b5426 BS |
2683 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
2684 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) { | |
c227f099 AL |
2685 | ram_addr_t orig_memory = p->phys_offset; |
2686 | target_phys_addr_t start_addr2, end_addr2; | |
db7b5426 BS |
2687 | int need_subpage = 0; |
2688 | ||
2689 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, | |
2690 | need_subpage); | |
f6405247 | 2691 | if (need_subpage) { |
db7b5426 BS |
2692 | if (!(orig_memory & IO_MEM_SUBPAGE)) { |
2693 | subpage = subpage_init((addr & TARGET_PAGE_MASK), | |
8da3ff18 PB |
2694 | &p->phys_offset, orig_memory, |
2695 | p->region_offset); | |
db7b5426 BS |
2696 | } else { |
2697 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) | |
2698 | >> IO_MEM_SHIFT]; | |
2699 | } | |
8da3ff18 PB |
2700 | subpage_register(subpage, start_addr2, end_addr2, phys_offset, |
2701 | region_offset); | |
2702 | p->region_offset = 0; | |
db7b5426 BS |
2703 | } else { |
2704 | p->phys_offset = phys_offset; | |
2705 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || | |
2706 | (phys_offset & IO_MEM_ROMD)) | |
2707 | phys_offset += TARGET_PAGE_SIZE; | |
2708 | } | |
2709 | } else { | |
2710 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1); | |
2711 | p->phys_offset = phys_offset; | |
8da3ff18 | 2712 | p->region_offset = region_offset; |
db7b5426 | 2713 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
8da3ff18 | 2714 | (phys_offset & IO_MEM_ROMD)) { |
db7b5426 | 2715 | phys_offset += TARGET_PAGE_SIZE; |
0e8f0967 | 2716 | } else { |
c227f099 | 2717 | target_phys_addr_t start_addr2, end_addr2; |
db7b5426 BS |
2718 | int need_subpage = 0; |
2719 | ||
2720 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, | |
2721 | end_addr2, need_subpage); | |
2722 | ||
f6405247 | 2723 | if (need_subpage) { |
db7b5426 | 2724 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
8da3ff18 | 2725 | &p->phys_offset, IO_MEM_UNASSIGNED, |
67c4d23c | 2726 | addr & TARGET_PAGE_MASK); |
db7b5426 | 2727 | subpage_register(subpage, start_addr2, end_addr2, |
8da3ff18 PB |
2728 | phys_offset, region_offset); |
2729 | p->region_offset = 0; | |
db7b5426 BS |
2730 | } |
2731 | } | |
2732 | } | |
8da3ff18 | 2733 | region_offset += TARGET_PAGE_SIZE; |
3b8e6a2d EI |
2734 | addr += TARGET_PAGE_SIZE; |
2735 | } while (addr != end_addr); | |
3b46e624 | 2736 | |
9d42037b FB |
2737 | /* since each CPU stores ram addresses in its TLB cache, we must |
2738 | reset the modified entries */ | |
2739 | /* XXX: slow ! */ | |
2740 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
2741 | tlb_flush(env, 1); | |
2742 | } | |
33417e70 FB |
2743 | } |
2744 | ||
ba863458 | 2745 | /* XXX: temporary until new memory mapping API */ |
c227f099 | 2746 | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
ba863458 FB |
2747 | { |
2748 | PhysPageDesc *p; | |
2749 | ||
2750 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
2751 | if (!p) | |
2752 | return IO_MEM_UNASSIGNED; | |
2753 | return p->phys_offset; | |
2754 | } | |
2755 | ||
c227f099 | 2756 | void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) |
f65ed4c1 AL |
2757 | { |
2758 | if (kvm_enabled()) | |
2759 | kvm_coalesce_mmio_region(addr, size); | |
2760 | } | |
2761 | ||
c227f099 | 2762 | void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) |
f65ed4c1 AL |
2763 | { |
2764 | if (kvm_enabled()) | |
2765 | kvm_uncoalesce_mmio_region(addr, size); | |
2766 | } | |
2767 | ||
62a2744c SY |
2768 | void qemu_flush_coalesced_mmio_buffer(void) |
2769 | { | |
2770 | if (kvm_enabled()) | |
2771 | kvm_flush_coalesced_mmio_buffer(); | |
2772 | } | |
2773 | ||
c902760f MT |
2774 | #if defined(__linux__) && !defined(TARGET_S390X) |
2775 | ||
2776 | #include <sys/vfs.h> | |
2777 | ||
2778 | #define HUGETLBFS_MAGIC 0x958458f6 | |
2779 | ||
2780 | static long gethugepagesize(const char *path) | |
2781 | { | |
2782 | struct statfs fs; | |
2783 | int ret; | |
2784 | ||
2785 | do { | |
9742bf26 | 2786 | ret = statfs(path, &fs); |
c902760f MT |
2787 | } while (ret != 0 && errno == EINTR); |
2788 | ||
2789 | if (ret != 0) { | |
9742bf26 YT |
2790 | perror(path); |
2791 | return 0; | |
c902760f MT |
2792 | } |
2793 | ||
2794 | if (fs.f_type != HUGETLBFS_MAGIC) | |
9742bf26 | 2795 | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); |
c902760f MT |
2796 | |
2797 | return fs.f_bsize; | |
2798 | } | |
2799 | ||
04b16653 AW |
2800 | static void *file_ram_alloc(RAMBlock *block, |
2801 | ram_addr_t memory, | |
2802 | const char *path) | |
c902760f MT |
2803 | { |
2804 | char *filename; | |
2805 | void *area; | |
2806 | int fd; | |
2807 | #ifdef MAP_POPULATE | |
2808 | int flags; | |
2809 | #endif | |
2810 | unsigned long hpagesize; | |
2811 | ||
2812 | hpagesize = gethugepagesize(path); | |
2813 | if (!hpagesize) { | |
9742bf26 | 2814 | return NULL; |
c902760f MT |
2815 | } |
2816 | ||
2817 | if (memory < hpagesize) { | |
2818 | return NULL; | |
2819 | } | |
2820 | ||
2821 | if (kvm_enabled() && !kvm_has_sync_mmu()) { | |
2822 | fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n"); | |
2823 | return NULL; | |
2824 | } | |
2825 | ||
2826 | if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) { | |
9742bf26 | 2827 | return NULL; |
c902760f MT |
2828 | } |
2829 | ||
2830 | fd = mkstemp(filename); | |
2831 | if (fd < 0) { | |
9742bf26 YT |
2832 | perror("unable to create backing store for hugepages"); |
2833 | free(filename); | |
2834 | return NULL; | |
c902760f MT |
2835 | } |
2836 | unlink(filename); | |
2837 | free(filename); | |
2838 | ||
2839 | memory = (memory+hpagesize-1) & ~(hpagesize-1); | |
2840 | ||
2841 | /* | |
2842 | * ftruncate is not supported by hugetlbfs in older | |
2843 | * hosts, so don't bother bailing out on errors. | |
2844 | * If anything goes wrong with it under other filesystems, | |
2845 | * mmap will fail. | |
2846 | */ | |
2847 | if (ftruncate(fd, memory)) | |
9742bf26 | 2848 | perror("ftruncate"); |
c902760f MT |
2849 | |
2850 | #ifdef MAP_POPULATE | |
2851 | /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case | |
2852 | * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED | |
2853 | * to sidestep this quirk. | |
2854 | */ | |
2855 | flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE; | |
2856 | area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0); | |
2857 | #else | |
2858 | area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
2859 | #endif | |
2860 | if (area == MAP_FAILED) { | |
9742bf26 YT |
2861 | perror("file_ram_alloc: can't mmap RAM pages"); |
2862 | close(fd); | |
2863 | return (NULL); | |
c902760f | 2864 | } |
04b16653 | 2865 | block->fd = fd; |
c902760f MT |
2866 | return area; |
2867 | } | |
2868 | #endif | |
2869 | ||
d17b5288 | 2870 | static ram_addr_t find_ram_offset(ram_addr_t size) |
04b16653 AW |
2871 | { |
2872 | RAMBlock *block, *next_block; | |
f15fbc4b | 2873 | ram_addr_t offset = 0, mingap = RAM_ADDR_MAX; |
04b16653 AW |
2874 | |
2875 | if (QLIST_EMPTY(&ram_list.blocks)) | |
2876 | return 0; | |
2877 | ||
2878 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
f15fbc4b | 2879 | ram_addr_t end, next = RAM_ADDR_MAX; |
04b16653 AW |
2880 | |
2881 | end = block->offset + block->length; | |
2882 | ||
2883 | QLIST_FOREACH(next_block, &ram_list.blocks, next) { | |
2884 | if (next_block->offset >= end) { | |
2885 | next = MIN(next, next_block->offset); | |
2886 | } | |
2887 | } | |
2888 | if (next - end >= size && next - end < mingap) { | |
2889 | offset = end; | |
2890 | mingap = next - end; | |
2891 | } | |
2892 | } | |
2893 | return offset; | |
2894 | } | |
2895 | ||
2896 | static ram_addr_t last_ram_offset(void) | |
d17b5288 AW |
2897 | { |
2898 | RAMBlock *block; | |
2899 | ram_addr_t last = 0; | |
2900 | ||
2901 | QLIST_FOREACH(block, &ram_list.blocks, next) | |
2902 | last = MAX(last, block->offset + block->length); | |
2903 | ||
2904 | return last; | |
2905 | } | |
2906 | ||
84b89d78 | 2907 | ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name, |
6977dfe6 | 2908 | ram_addr_t size, void *host) |
84b89d78 CM |
2909 | { |
2910 | RAMBlock *new_block, *block; | |
2911 | ||
2912 | size = TARGET_PAGE_ALIGN(size); | |
2913 | new_block = qemu_mallocz(sizeof(*new_block)); | |
2914 | ||
2915 | if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { | |
2916 | char *id = dev->parent_bus->info->get_dev_path(dev); | |
2917 | if (id) { | |
2918 | snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id); | |
2919 | qemu_free(id); | |
2920 | } | |
2921 | } | |
2922 | pstrcat(new_block->idstr, sizeof(new_block->idstr), name); | |
2923 | ||
2924 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
2925 | if (!strcmp(block->idstr, new_block->idstr)) { | |
2926 | fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n", | |
2927 | new_block->idstr); | |
2928 | abort(); | |
2929 | } | |
2930 | } | |
2931 | ||
432d268c | 2932 | new_block->offset = find_ram_offset(size); |
6977dfe6 YT |
2933 | if (host) { |
2934 | new_block->host = host; | |
cd19cfa2 | 2935 | new_block->flags |= RAM_PREALLOC_MASK; |
6977dfe6 YT |
2936 | } else { |
2937 | if (mem_path) { | |
c902760f | 2938 | #if defined (__linux__) && !defined(TARGET_S390X) |
6977dfe6 YT |
2939 | new_block->host = file_ram_alloc(new_block, size, mem_path); |
2940 | if (!new_block->host) { | |
2941 | new_block->host = qemu_vmalloc(size); | |
e78815a5 | 2942 | qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE); |
6977dfe6 | 2943 | } |
c902760f | 2944 | #else |
6977dfe6 YT |
2945 | fprintf(stderr, "-mem-path option unsupported\n"); |
2946 | exit(1); | |
c902760f | 2947 | #endif |
6977dfe6 | 2948 | } else { |
6b02494d | 2949 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) |
ff83678a CB |
2950 | /* S390 KVM requires the topmost vma of the RAM to be smaller than |
2951 | an system defined value, which is at least 256GB. Larger systems | |
2952 | have larger values. We put the guest between the end of data | |
2953 | segment (system break) and this value. We use 32GB as a base to | |
2954 | have enough room for the system break to grow. */ | |
2955 | new_block->host = mmap((void*)0x800000000, size, | |
6977dfe6 | 2956 | PROT_EXEC|PROT_READ|PROT_WRITE, |
ff83678a | 2957 | MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); |
fb8b2735 AG |
2958 | if (new_block->host == MAP_FAILED) { |
2959 | fprintf(stderr, "Allocating RAM failed\n"); | |
2960 | abort(); | |
2961 | } | |
6b02494d | 2962 | #else |
868bb33f | 2963 | if (xen_enabled()) { |
432d268c JN |
2964 | xen_ram_alloc(new_block->offset, size); |
2965 | } else { | |
2966 | new_block->host = qemu_vmalloc(size); | |
2967 | } | |
6b02494d | 2968 | #endif |
e78815a5 | 2969 | qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE); |
6977dfe6 | 2970 | } |
c902760f | 2971 | } |
94a6b54f PB |
2972 | new_block->length = size; |
2973 | ||
f471a17e | 2974 | QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next); |
94a6b54f | 2975 | |
f471a17e | 2976 | ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty, |
04b16653 | 2977 | last_ram_offset() >> TARGET_PAGE_BITS); |
d17b5288 | 2978 | memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS), |
94a6b54f PB |
2979 | 0xff, size >> TARGET_PAGE_BITS); |
2980 | ||
6f0437e8 JK |
2981 | if (kvm_enabled()) |
2982 | kvm_setup_guest_memory(new_block->host, size); | |
2983 | ||
94a6b54f PB |
2984 | return new_block->offset; |
2985 | } | |
e9a1ab19 | 2986 | |
6977dfe6 YT |
2987 | ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) |
2988 | { | |
2989 | return qemu_ram_alloc_from_ptr(dev, name, size, NULL); | |
2990 | } | |
2991 | ||
1f2e98b6 AW |
2992 | void qemu_ram_free_from_ptr(ram_addr_t addr) |
2993 | { | |
2994 | RAMBlock *block; | |
2995 | ||
2996 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
2997 | if (addr == block->offset) { | |
2998 | QLIST_REMOVE(block, next); | |
2999 | qemu_free(block); | |
3000 | return; | |
3001 | } | |
3002 | } | |
3003 | } | |
3004 | ||
c227f099 | 3005 | void qemu_ram_free(ram_addr_t addr) |
e9a1ab19 | 3006 | { |
04b16653 AW |
3007 | RAMBlock *block; |
3008 | ||
3009 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
3010 | if (addr == block->offset) { | |
3011 | QLIST_REMOVE(block, next); | |
cd19cfa2 HY |
3012 | if (block->flags & RAM_PREALLOC_MASK) { |
3013 | ; | |
3014 | } else if (mem_path) { | |
04b16653 AW |
3015 | #if defined (__linux__) && !defined(TARGET_S390X) |
3016 | if (block->fd) { | |
3017 | munmap(block->host, block->length); | |
3018 | close(block->fd); | |
3019 | } else { | |
3020 | qemu_vfree(block->host); | |
3021 | } | |
fd28aa13 JK |
3022 | #else |
3023 | abort(); | |
04b16653 AW |
3024 | #endif |
3025 | } else { | |
3026 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) | |
3027 | munmap(block->host, block->length); | |
3028 | #else | |
868bb33f | 3029 | if (xen_enabled()) { |
e41d7c69 | 3030 | xen_invalidate_map_cache_entry(block->host); |
432d268c JN |
3031 | } else { |
3032 | qemu_vfree(block->host); | |
3033 | } | |
04b16653 AW |
3034 | #endif |
3035 | } | |
3036 | qemu_free(block); | |
3037 | return; | |
3038 | } | |
3039 | } | |
3040 | ||
e9a1ab19 FB |
3041 | } |
3042 | ||
cd19cfa2 HY |
3043 | #ifndef _WIN32 |
3044 | void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) | |
3045 | { | |
3046 | RAMBlock *block; | |
3047 | ram_addr_t offset; | |
3048 | int flags; | |
3049 | void *area, *vaddr; | |
3050 | ||
3051 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
3052 | offset = addr - block->offset; | |
3053 | if (offset < block->length) { | |
3054 | vaddr = block->host + offset; | |
3055 | if (block->flags & RAM_PREALLOC_MASK) { | |
3056 | ; | |
3057 | } else { | |
3058 | flags = MAP_FIXED; | |
3059 | munmap(vaddr, length); | |
3060 | if (mem_path) { | |
3061 | #if defined(__linux__) && !defined(TARGET_S390X) | |
3062 | if (block->fd) { | |
3063 | #ifdef MAP_POPULATE | |
3064 | flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED : | |
3065 | MAP_PRIVATE; | |
3066 | #else | |
3067 | flags |= MAP_PRIVATE; | |
3068 | #endif | |
3069 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
3070 | flags, block->fd, offset); | |
3071 | } else { | |
3072 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; | |
3073 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
3074 | flags, -1, 0); | |
3075 | } | |
fd28aa13 JK |
3076 | #else |
3077 | abort(); | |
cd19cfa2 HY |
3078 | #endif |
3079 | } else { | |
3080 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) | |
3081 | flags |= MAP_SHARED | MAP_ANONYMOUS; | |
3082 | area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE, | |
3083 | flags, -1, 0); | |
3084 | #else | |
3085 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; | |
3086 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
3087 | flags, -1, 0); | |
3088 | #endif | |
3089 | } | |
3090 | if (area != vaddr) { | |
f15fbc4b AP |
3091 | fprintf(stderr, "Could not remap addr: " |
3092 | RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n", | |
cd19cfa2 HY |
3093 | length, addr); |
3094 | exit(1); | |
3095 | } | |
3096 | qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE); | |
3097 | } | |
3098 | return; | |
3099 | } | |
3100 | } | |
3101 | } | |
3102 | #endif /* !_WIN32 */ | |
3103 | ||
dc828ca1 | 3104 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
5579c7f3 PB |
3105 | With the exception of the softmmu code in this file, this should |
3106 | only be used for local memory (e.g. video ram) that the device owns, | |
3107 | and knows it isn't going to access beyond the end of the block. | |
3108 | ||
3109 | It should not be used for general purpose DMA. | |
3110 | Use cpu_physical_memory_map/cpu_physical_memory_rw instead. | |
3111 | */ | |
c227f099 | 3112 | void *qemu_get_ram_ptr(ram_addr_t addr) |
dc828ca1 | 3113 | { |
94a6b54f PB |
3114 | RAMBlock *block; |
3115 | ||
f471a17e AW |
3116 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
3117 | if (addr - block->offset < block->length) { | |
7d82af38 VP |
3118 | /* Move this entry to to start of the list. */ |
3119 | if (block != QLIST_FIRST(&ram_list.blocks)) { | |
3120 | QLIST_REMOVE(block, next); | |
3121 | QLIST_INSERT_HEAD(&ram_list.blocks, block, next); | |
3122 | } | |
868bb33f | 3123 | if (xen_enabled()) { |
432d268c JN |
3124 | /* We need to check if the requested address is in the RAM |
3125 | * because we don't want to map the entire memory in QEMU. | |
712c2b41 | 3126 | * In that case just map until the end of the page. |
432d268c JN |
3127 | */ |
3128 | if (block->offset == 0) { | |
e41d7c69 | 3129 | return xen_map_cache(addr, 0, 0); |
432d268c | 3130 | } else if (block->host == NULL) { |
e41d7c69 JK |
3131 | block->host = |
3132 | xen_map_cache(block->offset, block->length, 1); | |
432d268c JN |
3133 | } |
3134 | } | |
f471a17e AW |
3135 | return block->host + (addr - block->offset); |
3136 | } | |
94a6b54f | 3137 | } |
f471a17e AW |
3138 | |
3139 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
3140 | abort(); | |
3141 | ||
3142 | return NULL; | |
dc828ca1 PB |
3143 | } |
3144 | ||
b2e0a138 MT |
3145 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
3146 | * Same as qemu_get_ram_ptr but avoid reordering ramblocks. | |
3147 | */ | |
3148 | void *qemu_safe_ram_ptr(ram_addr_t addr) | |
3149 | { | |
3150 | RAMBlock *block; | |
3151 | ||
3152 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
3153 | if (addr - block->offset < block->length) { | |
868bb33f | 3154 | if (xen_enabled()) { |
432d268c JN |
3155 | /* We need to check if the requested address is in the RAM |
3156 | * because we don't want to map the entire memory in QEMU. | |
712c2b41 | 3157 | * In that case just map until the end of the page. |
432d268c JN |
3158 | */ |
3159 | if (block->offset == 0) { | |
e41d7c69 | 3160 | return xen_map_cache(addr, 0, 0); |
432d268c | 3161 | } else if (block->host == NULL) { |
e41d7c69 JK |
3162 | block->host = |
3163 | xen_map_cache(block->offset, block->length, 1); | |
432d268c JN |
3164 | } |
3165 | } | |
b2e0a138 MT |
3166 | return block->host + (addr - block->offset); |
3167 | } | |
3168 | } | |
3169 | ||
3170 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
3171 | abort(); | |
3172 | ||
3173 | return NULL; | |
3174 | } | |
3175 | ||
38bee5dc SS |
3176 | /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr |
3177 | * but takes a size argument */ | |
8ab934f9 | 3178 | void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size) |
38bee5dc | 3179 | { |
8ab934f9 SS |
3180 | if (*size == 0) { |
3181 | return NULL; | |
3182 | } | |
868bb33f | 3183 | if (xen_enabled()) { |
e41d7c69 | 3184 | return xen_map_cache(addr, *size, 1); |
868bb33f | 3185 | } else { |
38bee5dc SS |
3186 | RAMBlock *block; |
3187 | ||
3188 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
3189 | if (addr - block->offset < block->length) { | |
3190 | if (addr - block->offset + *size > block->length) | |
3191 | *size = block->length - addr + block->offset; | |
3192 | return block->host + (addr - block->offset); | |
3193 | } | |
3194 | } | |
3195 | ||
3196 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
3197 | abort(); | |
38bee5dc SS |
3198 | } |
3199 | } | |
3200 | ||
050a0ddf AP |
3201 | void qemu_put_ram_ptr(void *addr) |
3202 | { | |
3203 | trace_qemu_put_ram_ptr(addr); | |
050a0ddf AP |
3204 | } |
3205 | ||
e890261f | 3206 | int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) |
5579c7f3 | 3207 | { |
94a6b54f PB |
3208 | RAMBlock *block; |
3209 | uint8_t *host = ptr; | |
3210 | ||
868bb33f | 3211 | if (xen_enabled()) { |
e41d7c69 | 3212 | *ram_addr = xen_ram_addr_from_mapcache(ptr); |
712c2b41 SS |
3213 | return 0; |
3214 | } | |
3215 | ||
f471a17e | 3216 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
432d268c JN |
3217 | /* This case append when the block is not mapped. */ |
3218 | if (block->host == NULL) { | |
3219 | continue; | |
3220 | } | |
f471a17e | 3221 | if (host - block->host < block->length) { |
e890261f MT |
3222 | *ram_addr = block->offset + (host - block->host); |
3223 | return 0; | |
f471a17e | 3224 | } |
94a6b54f | 3225 | } |
432d268c | 3226 | |
e890261f MT |
3227 | return -1; |
3228 | } | |
f471a17e | 3229 | |
e890261f MT |
3230 | /* Some of the softmmu routines need to translate from a host pointer |
3231 | (typically a TLB entry) back to a ram offset. */ | |
3232 | ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr) | |
3233 | { | |
3234 | ram_addr_t ram_addr; | |
f471a17e | 3235 | |
e890261f MT |
3236 | if (qemu_ram_addr_from_host(ptr, &ram_addr)) { |
3237 | fprintf(stderr, "Bad ram pointer %p\n", ptr); | |
3238 | abort(); | |
3239 | } | |
3240 | return ram_addr; | |
5579c7f3 PB |
3241 | } |
3242 | ||
c227f099 | 3243 | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
33417e70 | 3244 | { |
67d3b957 | 3245 | #ifdef DEBUG_UNASSIGNED |
ab3d1727 | 3246 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
b4f0a316 | 3247 | #endif |
5b450407 | 3248 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3249 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 1); |
e18231a3 BS |
3250 | #endif |
3251 | return 0; | |
3252 | } | |
3253 | ||
c227f099 | 3254 | static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr) |
e18231a3 BS |
3255 | { |
3256 | #ifdef DEBUG_UNASSIGNED | |
3257 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); | |
3258 | #endif | |
5b450407 | 3259 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3260 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 2); |
e18231a3 BS |
3261 | #endif |
3262 | return 0; | |
3263 | } | |
3264 | ||
c227f099 | 3265 | static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr) |
e18231a3 BS |
3266 | { |
3267 | #ifdef DEBUG_UNASSIGNED | |
3268 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); | |
3269 | #endif | |
5b450407 | 3270 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3271 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 4); |
67d3b957 | 3272 | #endif |
33417e70 FB |
3273 | return 0; |
3274 | } | |
3275 | ||
c227f099 | 3276 | static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
33417e70 | 3277 | { |
67d3b957 | 3278 | #ifdef DEBUG_UNASSIGNED |
ab3d1727 | 3279 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
67d3b957 | 3280 | #endif |
5b450407 | 3281 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3282 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 1); |
e18231a3 BS |
3283 | #endif |
3284 | } | |
3285 | ||
c227f099 | 3286 | static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
e18231a3 BS |
3287 | { |
3288 | #ifdef DEBUG_UNASSIGNED | |
3289 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); | |
3290 | #endif | |
5b450407 | 3291 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3292 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 2); |
e18231a3 BS |
3293 | #endif |
3294 | } | |
3295 | ||
c227f099 | 3296 | static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
e18231a3 BS |
3297 | { |
3298 | #ifdef DEBUG_UNASSIGNED | |
3299 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); | |
3300 | #endif | |
5b450407 | 3301 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
b14ef7c9 | 3302 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 4); |
b4f0a316 | 3303 | #endif |
33417e70 FB |
3304 | } |
3305 | ||
d60efc6b | 3306 | static CPUReadMemoryFunc * const unassigned_mem_read[3] = { |
33417e70 | 3307 | unassigned_mem_readb, |
e18231a3 BS |
3308 | unassigned_mem_readw, |
3309 | unassigned_mem_readl, | |
33417e70 FB |
3310 | }; |
3311 | ||
d60efc6b | 3312 | static CPUWriteMemoryFunc * const unassigned_mem_write[3] = { |
33417e70 | 3313 | unassigned_mem_writeb, |
e18231a3 BS |
3314 | unassigned_mem_writew, |
3315 | unassigned_mem_writel, | |
33417e70 FB |
3316 | }; |
3317 | ||
c227f099 | 3318 | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
0f459d16 | 3319 | uint32_t val) |
9fa3e853 | 3320 | { |
3a7d929e | 3321 | int dirty_flags; |
f7c11b53 | 3322 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
3a7d929e | 3323 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
9fa3e853 | 3324 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e | 3325 | tb_invalidate_phys_page_fast(ram_addr, 1); |
f7c11b53 | 3326 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
9fa3e853 | 3327 | #endif |
3a7d929e | 3328 | } |
5579c7f3 | 3329 | stb_p(qemu_get_ram_ptr(ram_addr), val); |
f23db169 | 3330 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
f7c11b53 | 3331 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
f23db169 FB |
3332 | /* we remove the notdirty callback only if the code has been |
3333 | flushed */ | |
3334 | if (dirty_flags == 0xff) | |
2e70f6ef | 3335 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
3336 | } |
3337 | ||
c227f099 | 3338 | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
0f459d16 | 3339 | uint32_t val) |
9fa3e853 | 3340 | { |
3a7d929e | 3341 | int dirty_flags; |
f7c11b53 | 3342 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
3a7d929e | 3343 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
9fa3e853 | 3344 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e | 3345 | tb_invalidate_phys_page_fast(ram_addr, 2); |
f7c11b53 | 3346 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
9fa3e853 | 3347 | #endif |
3a7d929e | 3348 | } |
5579c7f3 | 3349 | stw_p(qemu_get_ram_ptr(ram_addr), val); |
f23db169 | 3350 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
f7c11b53 | 3351 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
f23db169 FB |
3352 | /* we remove the notdirty callback only if the code has been |
3353 | flushed */ | |
3354 | if (dirty_flags == 0xff) | |
2e70f6ef | 3355 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
3356 | } |
3357 | ||
c227f099 | 3358 | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
0f459d16 | 3359 | uint32_t val) |
9fa3e853 | 3360 | { |
3a7d929e | 3361 | int dirty_flags; |
f7c11b53 | 3362 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
3a7d929e | 3363 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
9fa3e853 | 3364 | #if !defined(CONFIG_USER_ONLY) |
3a7d929e | 3365 | tb_invalidate_phys_page_fast(ram_addr, 4); |
f7c11b53 | 3366 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
9fa3e853 | 3367 | #endif |
3a7d929e | 3368 | } |
5579c7f3 | 3369 | stl_p(qemu_get_ram_ptr(ram_addr), val); |
f23db169 | 3370 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
f7c11b53 | 3371 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
f23db169 FB |
3372 | /* we remove the notdirty callback only if the code has been |
3373 | flushed */ | |
3374 | if (dirty_flags == 0xff) | |
2e70f6ef | 3375 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
9fa3e853 FB |
3376 | } |
3377 | ||
d60efc6b | 3378 | static CPUReadMemoryFunc * const error_mem_read[3] = { |
9fa3e853 FB |
3379 | NULL, /* never used */ |
3380 | NULL, /* never used */ | |
3381 | NULL, /* never used */ | |
3382 | }; | |
3383 | ||
d60efc6b | 3384 | static CPUWriteMemoryFunc * const notdirty_mem_write[3] = { |
1ccde1cb FB |
3385 | notdirty_mem_writeb, |
3386 | notdirty_mem_writew, | |
3387 | notdirty_mem_writel, | |
3388 | }; | |
3389 | ||
0f459d16 | 3390 | /* Generate a debug exception if a watchpoint has been hit. */ |
b4051334 | 3391 | static void check_watchpoint(int offset, int len_mask, int flags) |
0f459d16 PB |
3392 | { |
3393 | CPUState *env = cpu_single_env; | |
06d55cc1 AL |
3394 | target_ulong pc, cs_base; |
3395 | TranslationBlock *tb; | |
0f459d16 | 3396 | target_ulong vaddr; |
a1d1bb31 | 3397 | CPUWatchpoint *wp; |
06d55cc1 | 3398 | int cpu_flags; |
0f459d16 | 3399 | |
06d55cc1 AL |
3400 | if (env->watchpoint_hit) { |
3401 | /* We re-entered the check after replacing the TB. Now raise | |
3402 | * the debug interrupt so that is will trigger after the | |
3403 | * current instruction. */ | |
3404 | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); | |
3405 | return; | |
3406 | } | |
2e70f6ef | 3407 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
72cf2d4f | 3408 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 AL |
3409 | if ((vaddr == (wp->vaddr & len_mask) || |
3410 | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { | |
6e140f28 AL |
3411 | wp->flags |= BP_WATCHPOINT_HIT; |
3412 | if (!env->watchpoint_hit) { | |
3413 | env->watchpoint_hit = wp; | |
3414 | tb = tb_find_pc(env->mem_io_pc); | |
3415 | if (!tb) { | |
3416 | cpu_abort(env, "check_watchpoint: could not find TB for " | |
3417 | "pc=%p", (void *)env->mem_io_pc); | |
3418 | } | |
618ba8e6 | 3419 | cpu_restore_state(tb, env, env->mem_io_pc); |
6e140f28 AL |
3420 | tb_phys_invalidate(tb, -1); |
3421 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { | |
3422 | env->exception_index = EXCP_DEBUG; | |
3423 | } else { | |
3424 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); | |
3425 | tb_gen_code(env, pc, cs_base, cpu_flags, 1); | |
3426 | } | |
3427 | cpu_resume_from_signal(env, NULL); | |
06d55cc1 | 3428 | } |
6e140f28 AL |
3429 | } else { |
3430 | wp->flags &= ~BP_WATCHPOINT_HIT; | |
0f459d16 PB |
3431 | } |
3432 | } | |
3433 | } | |
3434 | ||
6658ffb8 PB |
3435 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
3436 | so these check for a hit then pass through to the normal out-of-line | |
3437 | phys routines. */ | |
c227f099 | 3438 | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) |
6658ffb8 | 3439 | { |
b4051334 | 3440 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ); |
6658ffb8 PB |
3441 | return ldub_phys(addr); |
3442 | } | |
3443 | ||
c227f099 | 3444 | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) |
6658ffb8 | 3445 | { |
b4051334 | 3446 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ); |
6658ffb8 PB |
3447 | return lduw_phys(addr); |
3448 | } | |
3449 | ||
c227f099 | 3450 | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) |
6658ffb8 | 3451 | { |
b4051334 | 3452 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ); |
6658ffb8 PB |
3453 | return ldl_phys(addr); |
3454 | } | |
3455 | ||
c227f099 | 3456 | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
6658ffb8 PB |
3457 | uint32_t val) |
3458 | { | |
b4051334 | 3459 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE); |
6658ffb8 PB |
3460 | stb_phys(addr, val); |
3461 | } | |
3462 | ||
c227f099 | 3463 | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, |
6658ffb8 PB |
3464 | uint32_t val) |
3465 | { | |
b4051334 | 3466 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE); |
6658ffb8 PB |
3467 | stw_phys(addr, val); |
3468 | } | |
3469 | ||
c227f099 | 3470 | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, |
6658ffb8 PB |
3471 | uint32_t val) |
3472 | { | |
b4051334 | 3473 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE); |
6658ffb8 PB |
3474 | stl_phys(addr, val); |
3475 | } | |
3476 | ||
d60efc6b | 3477 | static CPUReadMemoryFunc * const watch_mem_read[3] = { |
6658ffb8 PB |
3478 | watch_mem_readb, |
3479 | watch_mem_readw, | |
3480 | watch_mem_readl, | |
3481 | }; | |
3482 | ||
d60efc6b | 3483 | static CPUWriteMemoryFunc * const watch_mem_write[3] = { |
6658ffb8 PB |
3484 | watch_mem_writeb, |
3485 | watch_mem_writew, | |
3486 | watch_mem_writel, | |
3487 | }; | |
6658ffb8 | 3488 | |
f6405247 RH |
3489 | static inline uint32_t subpage_readlen (subpage_t *mmio, |
3490 | target_phys_addr_t addr, | |
3491 | unsigned int len) | |
db7b5426 | 3492 | { |
f6405247 | 3493 | unsigned int idx = SUBPAGE_IDX(addr); |
db7b5426 BS |
3494 | #if defined(DEBUG_SUBPAGE) |
3495 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, | |
3496 | mmio, len, addr, idx); | |
3497 | #endif | |
db7b5426 | 3498 | |
f6405247 RH |
3499 | addr += mmio->region_offset[idx]; |
3500 | idx = mmio->sub_io_index[idx]; | |
3501 | return io_mem_read[idx][len](io_mem_opaque[idx], addr); | |
db7b5426 BS |
3502 | } |
3503 | ||
c227f099 | 3504 | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, |
f6405247 | 3505 | uint32_t value, unsigned int len) |
db7b5426 | 3506 | { |
f6405247 | 3507 | unsigned int idx = SUBPAGE_IDX(addr); |
db7b5426 | 3508 | #if defined(DEBUG_SUBPAGE) |
f6405247 RH |
3509 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", |
3510 | __func__, mmio, len, addr, idx, value); | |
db7b5426 | 3511 | #endif |
f6405247 RH |
3512 | |
3513 | addr += mmio->region_offset[idx]; | |
3514 | idx = mmio->sub_io_index[idx]; | |
3515 | io_mem_write[idx][len](io_mem_opaque[idx], addr, value); | |
db7b5426 BS |
3516 | } |
3517 | ||
c227f099 | 3518 | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) |
db7b5426 | 3519 | { |
db7b5426 BS |
3520 | return subpage_readlen(opaque, addr, 0); |
3521 | } | |
3522 | ||
c227f099 | 3523 | static void subpage_writeb (void *opaque, target_phys_addr_t addr, |
db7b5426 BS |
3524 | uint32_t value) |
3525 | { | |
db7b5426 BS |
3526 | subpage_writelen(opaque, addr, value, 0); |
3527 | } | |
3528 | ||
c227f099 | 3529 | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) |
db7b5426 | 3530 | { |
db7b5426 BS |
3531 | return subpage_readlen(opaque, addr, 1); |
3532 | } | |
3533 | ||
c227f099 | 3534 | static void subpage_writew (void *opaque, target_phys_addr_t addr, |
db7b5426 BS |
3535 | uint32_t value) |
3536 | { | |
db7b5426 BS |
3537 | subpage_writelen(opaque, addr, value, 1); |
3538 | } | |
3539 | ||
c227f099 | 3540 | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) |
db7b5426 | 3541 | { |
db7b5426 BS |
3542 | return subpage_readlen(opaque, addr, 2); |
3543 | } | |
3544 | ||
f6405247 RH |
3545 | static void subpage_writel (void *opaque, target_phys_addr_t addr, |
3546 | uint32_t value) | |
db7b5426 | 3547 | { |
db7b5426 BS |
3548 | subpage_writelen(opaque, addr, value, 2); |
3549 | } | |
3550 | ||
d60efc6b | 3551 | static CPUReadMemoryFunc * const subpage_read[] = { |
db7b5426 BS |
3552 | &subpage_readb, |
3553 | &subpage_readw, | |
3554 | &subpage_readl, | |
3555 | }; | |
3556 | ||
d60efc6b | 3557 | static CPUWriteMemoryFunc * const subpage_write[] = { |
db7b5426 BS |
3558 | &subpage_writeb, |
3559 | &subpage_writew, | |
3560 | &subpage_writel, | |
3561 | }; | |
3562 | ||
c227f099 AL |
3563 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
3564 | ram_addr_t memory, ram_addr_t region_offset) | |
db7b5426 BS |
3565 | { |
3566 | int idx, eidx; | |
3567 | ||
3568 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) | |
3569 | return -1; | |
3570 | idx = SUBPAGE_IDX(start); | |
3571 | eidx = SUBPAGE_IDX(end); | |
3572 | #if defined(DEBUG_SUBPAGE) | |
0bf9e31a | 3573 | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__, |
db7b5426 BS |
3574 | mmio, start, end, idx, eidx, memory); |
3575 | #endif | |
95c318f5 GN |
3576 | if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM) |
3577 | memory = IO_MEM_UNASSIGNED; | |
f6405247 | 3578 | memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
db7b5426 | 3579 | for (; idx <= eidx; idx++) { |
f6405247 RH |
3580 | mmio->sub_io_index[idx] = memory; |
3581 | mmio->region_offset[idx] = region_offset; | |
db7b5426 BS |
3582 | } |
3583 | ||
3584 | return 0; | |
3585 | } | |
3586 | ||
f6405247 RH |
3587 | static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
3588 | ram_addr_t orig_memory, | |
3589 | ram_addr_t region_offset) | |
db7b5426 | 3590 | { |
c227f099 | 3591 | subpage_t *mmio; |
db7b5426 BS |
3592 | int subpage_memory; |
3593 | ||
c227f099 | 3594 | mmio = qemu_mallocz(sizeof(subpage_t)); |
1eec614b AL |
3595 | |
3596 | mmio->base = base; | |
2507c12a AG |
3597 | subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio, |
3598 | DEVICE_NATIVE_ENDIAN); | |
db7b5426 | 3599 | #if defined(DEBUG_SUBPAGE) |
1eec614b AL |
3600 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
3601 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); | |
db7b5426 | 3602 | #endif |
1eec614b | 3603 | *phys = subpage_memory | IO_MEM_SUBPAGE; |
f6405247 | 3604 | subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset); |
db7b5426 BS |
3605 | |
3606 | return mmio; | |
3607 | } | |
3608 | ||
88715657 AL |
3609 | static int get_free_io_mem_idx(void) |
3610 | { | |
3611 | int i; | |
3612 | ||
3613 | for (i = 0; i<IO_MEM_NB_ENTRIES; i++) | |
3614 | if (!io_mem_used[i]) { | |
3615 | io_mem_used[i] = 1; | |
3616 | return i; | |
3617 | } | |
c6703b47 | 3618 | fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES); |
88715657 AL |
3619 | return -1; |
3620 | } | |
3621 | ||
dd310534 AG |
3622 | /* |
3623 | * Usually, devices operate in little endian mode. There are devices out | |
3624 | * there that operate in big endian too. Each device gets byte swapped | |
3625 | * mmio if plugged onto a CPU that does the other endianness. | |
3626 | * | |
3627 | * CPU Device swap? | |
3628 | * | |
3629 | * little little no | |
3630 | * little big yes | |
3631 | * big little yes | |
3632 | * big big no | |
3633 | */ | |
3634 | ||
3635 | typedef struct SwapEndianContainer { | |
3636 | CPUReadMemoryFunc *read[3]; | |
3637 | CPUWriteMemoryFunc *write[3]; | |
3638 | void *opaque; | |
3639 | } SwapEndianContainer; | |
3640 | ||
3641 | static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr) | |
3642 | { | |
3643 | uint32_t val; | |
3644 | SwapEndianContainer *c = opaque; | |
3645 | val = c->read[0](c->opaque, addr); | |
3646 | return val; | |
3647 | } | |
3648 | ||
3649 | static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr) | |
3650 | { | |
3651 | uint32_t val; | |
3652 | SwapEndianContainer *c = opaque; | |
3653 | val = bswap16(c->read[1](c->opaque, addr)); | |
3654 | return val; | |
3655 | } | |
3656 | ||
3657 | static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr) | |
3658 | { | |
3659 | uint32_t val; | |
3660 | SwapEndianContainer *c = opaque; | |
3661 | val = bswap32(c->read[2](c->opaque, addr)); | |
3662 | return val; | |
3663 | } | |
3664 | ||
3665 | static CPUReadMemoryFunc * const swapendian_readfn[3]={ | |
3666 | swapendian_mem_readb, | |
3667 | swapendian_mem_readw, | |
3668 | swapendian_mem_readl | |
3669 | }; | |
3670 | ||
3671 | static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr, | |
3672 | uint32_t val) | |
3673 | { | |
3674 | SwapEndianContainer *c = opaque; | |
3675 | c->write[0](c->opaque, addr, val); | |
3676 | } | |
3677 | ||
3678 | static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr, | |
3679 | uint32_t val) | |
3680 | { | |
3681 | SwapEndianContainer *c = opaque; | |
3682 | c->write[1](c->opaque, addr, bswap16(val)); | |
3683 | } | |
3684 | ||
3685 | static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr, | |
3686 | uint32_t val) | |
3687 | { | |
3688 | SwapEndianContainer *c = opaque; | |
3689 | c->write[2](c->opaque, addr, bswap32(val)); | |
3690 | } | |
3691 | ||
3692 | static CPUWriteMemoryFunc * const swapendian_writefn[3]={ | |
3693 | swapendian_mem_writeb, | |
3694 | swapendian_mem_writew, | |
3695 | swapendian_mem_writel | |
3696 | }; | |
3697 | ||
3698 | static void swapendian_init(int io_index) | |
3699 | { | |
3700 | SwapEndianContainer *c = qemu_malloc(sizeof(SwapEndianContainer)); | |
3701 | int i; | |
3702 | ||
3703 | /* Swap mmio for big endian targets */ | |
3704 | c->opaque = io_mem_opaque[io_index]; | |
3705 | for (i = 0; i < 3; i++) { | |
3706 | c->read[i] = io_mem_read[io_index][i]; | |
3707 | c->write[i] = io_mem_write[io_index][i]; | |
3708 | ||
3709 | io_mem_read[io_index][i] = swapendian_readfn[i]; | |
3710 | io_mem_write[io_index][i] = swapendian_writefn[i]; | |
3711 | } | |
3712 | io_mem_opaque[io_index] = c; | |
3713 | } | |
3714 | ||
3715 | static void swapendian_del(int io_index) | |
3716 | { | |
3717 | if (io_mem_read[io_index][0] == swapendian_readfn[0]) { | |
3718 | qemu_free(io_mem_opaque[io_index]); | |
3719 | } | |
3720 | } | |
3721 | ||
33417e70 FB |
3722 | /* mem_read and mem_write are arrays of functions containing the |
3723 | function to access byte (index 0), word (index 1) and dword (index | |
0b4e6e3e | 3724 | 2). Functions can be omitted with a NULL function pointer. |
3ee89922 | 3725 | If io_index is non zero, the corresponding io zone is |
4254fab8 BS |
3726 | modified. If it is zero, a new io zone is allocated. The return |
3727 | value can be used with cpu_register_physical_memory(). (-1) is | |
3728 | returned if error. */ | |
1eed09cb | 3729 | static int cpu_register_io_memory_fixed(int io_index, |
d60efc6b BS |
3730 | CPUReadMemoryFunc * const *mem_read, |
3731 | CPUWriteMemoryFunc * const *mem_write, | |
dd310534 | 3732 | void *opaque, enum device_endian endian) |
33417e70 | 3733 | { |
3cab721d RH |
3734 | int i; |
3735 | ||
33417e70 | 3736 | if (io_index <= 0) { |
88715657 AL |
3737 | io_index = get_free_io_mem_idx(); |
3738 | if (io_index == -1) | |
3739 | return io_index; | |
33417e70 | 3740 | } else { |
1eed09cb | 3741 | io_index >>= IO_MEM_SHIFT; |
33417e70 FB |
3742 | if (io_index >= IO_MEM_NB_ENTRIES) |
3743 | return -1; | |
3744 | } | |
b5ff1b31 | 3745 | |
3cab721d RH |
3746 | for (i = 0; i < 3; ++i) { |
3747 | io_mem_read[io_index][i] | |
3748 | = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]); | |
3749 | } | |
3750 | for (i = 0; i < 3; ++i) { | |
3751 | io_mem_write[io_index][i] | |
3752 | = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]); | |
3753 | } | |
a4193c8a | 3754 | io_mem_opaque[io_index] = opaque; |
f6405247 | 3755 | |
dd310534 AG |
3756 | switch (endian) { |
3757 | case DEVICE_BIG_ENDIAN: | |
3758 | #ifndef TARGET_WORDS_BIGENDIAN | |
3759 | swapendian_init(io_index); | |
3760 | #endif | |
3761 | break; | |
3762 | case DEVICE_LITTLE_ENDIAN: | |
3763 | #ifdef TARGET_WORDS_BIGENDIAN | |
3764 | swapendian_init(io_index); | |
3765 | #endif | |
3766 | break; | |
3767 | case DEVICE_NATIVE_ENDIAN: | |
3768 | default: | |
3769 | break; | |
3770 | } | |
3771 | ||
f6405247 | 3772 | return (io_index << IO_MEM_SHIFT); |
33417e70 | 3773 | } |
61382a50 | 3774 | |
d60efc6b BS |
3775 | int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read, |
3776 | CPUWriteMemoryFunc * const *mem_write, | |
dd310534 | 3777 | void *opaque, enum device_endian endian) |
1eed09cb | 3778 | { |
2507c12a | 3779 | return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian); |
1eed09cb AK |
3780 | } |
3781 | ||
88715657 AL |
3782 | void cpu_unregister_io_memory(int io_table_address) |
3783 | { | |
3784 | int i; | |
3785 | int io_index = io_table_address >> IO_MEM_SHIFT; | |
3786 | ||
dd310534 AG |
3787 | swapendian_del(io_index); |
3788 | ||
88715657 AL |
3789 | for (i=0;i < 3; i++) { |
3790 | io_mem_read[io_index][i] = unassigned_mem_read[i]; | |
3791 | io_mem_write[io_index][i] = unassigned_mem_write[i]; | |
3792 | } | |
3793 | io_mem_opaque[io_index] = NULL; | |
3794 | io_mem_used[io_index] = 0; | |
3795 | } | |
3796 | ||
e9179ce1 AK |
3797 | static void io_mem_init(void) |
3798 | { | |
3799 | int i; | |
3800 | ||
2507c12a AG |
3801 | cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, |
3802 | unassigned_mem_write, NULL, | |
3803 | DEVICE_NATIVE_ENDIAN); | |
3804 | cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, | |
3805 | unassigned_mem_write, NULL, | |
3806 | DEVICE_NATIVE_ENDIAN); | |
3807 | cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, | |
3808 | notdirty_mem_write, NULL, | |
3809 | DEVICE_NATIVE_ENDIAN); | |
e9179ce1 AK |
3810 | for (i=0; i<5; i++) |
3811 | io_mem_used[i] = 1; | |
3812 | ||
3813 | io_mem_watch = cpu_register_io_memory(watch_mem_read, | |
2507c12a AG |
3814 | watch_mem_write, NULL, |
3815 | DEVICE_NATIVE_ENDIAN); | |
e9179ce1 AK |
3816 | } |
3817 | ||
62152b8a AK |
3818 | static void memory_map_init(void) |
3819 | { | |
3820 | system_memory = qemu_malloc(sizeof(*system_memory)); | |
3821 | memory_region_init(system_memory, "system", UINT64_MAX); | |
3822 | set_system_memory_map(system_memory); | |
3823 | } | |
3824 | ||
3825 | MemoryRegion *get_system_memory(void) | |
3826 | { | |
3827 | return system_memory; | |
3828 | } | |
3829 | ||
e2eef170 PB |
3830 | #endif /* !defined(CONFIG_USER_ONLY) */ |
3831 | ||
13eb76e0 FB |
3832 | /* physical memory access (slow version, mainly for debug) */ |
3833 | #if defined(CONFIG_USER_ONLY) | |
a68fe89c PB |
3834 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
3835 | uint8_t *buf, int len, int is_write) | |
13eb76e0 FB |
3836 | { |
3837 | int l, flags; | |
3838 | target_ulong page; | |
53a5960a | 3839 | void * p; |
13eb76e0 FB |
3840 | |
3841 | while (len > 0) { | |
3842 | page = addr & TARGET_PAGE_MASK; | |
3843 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3844 | if (l > len) | |
3845 | l = len; | |
3846 | flags = page_get_flags(page); | |
3847 | if (!(flags & PAGE_VALID)) | |
a68fe89c | 3848 | return -1; |
13eb76e0 FB |
3849 | if (is_write) { |
3850 | if (!(flags & PAGE_WRITE)) | |
a68fe89c | 3851 | return -1; |
579a97f7 | 3852 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 3853 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
a68fe89c | 3854 | return -1; |
72fb7daa AJ |
3855 | memcpy(p, buf, l); |
3856 | unlock_user(p, addr, l); | |
13eb76e0 FB |
3857 | } else { |
3858 | if (!(flags & PAGE_READ)) | |
a68fe89c | 3859 | return -1; |
579a97f7 | 3860 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 3861 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
a68fe89c | 3862 | return -1; |
72fb7daa | 3863 | memcpy(buf, p, l); |
5b257578 | 3864 | unlock_user(p, addr, 0); |
13eb76e0 FB |
3865 | } |
3866 | len -= l; | |
3867 | buf += l; | |
3868 | addr += l; | |
3869 | } | |
a68fe89c | 3870 | return 0; |
13eb76e0 | 3871 | } |
8df1cd07 | 3872 | |
13eb76e0 | 3873 | #else |
c227f099 | 3874 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
13eb76e0 FB |
3875 | int len, int is_write) |
3876 | { | |
3877 | int l, io_index; | |
3878 | uint8_t *ptr; | |
3879 | uint32_t val; | |
c227f099 | 3880 | target_phys_addr_t page; |
8ca5692d | 3881 | ram_addr_t pd; |
92e873b9 | 3882 | PhysPageDesc *p; |
3b46e624 | 3883 | |
13eb76e0 FB |
3884 | while (len > 0) { |
3885 | page = addr & TARGET_PAGE_MASK; | |
3886 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3887 | if (l > len) | |
3888 | l = len; | |
92e873b9 | 3889 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
13eb76e0 FB |
3890 | if (!p) { |
3891 | pd = IO_MEM_UNASSIGNED; | |
3892 | } else { | |
3893 | pd = p->phys_offset; | |
3894 | } | |
3b46e624 | 3895 | |
13eb76e0 | 3896 | if (is_write) { |
3a7d929e | 3897 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
c227f099 | 3898 | target_phys_addr_t addr1 = addr; |
13eb76e0 | 3899 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 | 3900 | if (p) |
6c2934db | 3901 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
6a00d601 FB |
3902 | /* XXX: could force cpu_single_env to NULL to avoid |
3903 | potential bugs */ | |
6c2934db | 3904 | if (l >= 4 && ((addr1 & 3) == 0)) { |
1c213d19 | 3905 | /* 32 bit write access */ |
c27004ec | 3906 | val = ldl_p(buf); |
6c2934db | 3907 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val); |
13eb76e0 | 3908 | l = 4; |
6c2934db | 3909 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
1c213d19 | 3910 | /* 16 bit write access */ |
c27004ec | 3911 | val = lduw_p(buf); |
6c2934db | 3912 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val); |
13eb76e0 FB |
3913 | l = 2; |
3914 | } else { | |
1c213d19 | 3915 | /* 8 bit write access */ |
c27004ec | 3916 | val = ldub_p(buf); |
6c2934db | 3917 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val); |
13eb76e0 FB |
3918 | l = 1; |
3919 | } | |
3920 | } else { | |
8ca5692d | 3921 | ram_addr_t addr1; |
b448f2f3 | 3922 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
13eb76e0 | 3923 | /* RAM case */ |
5579c7f3 | 3924 | ptr = qemu_get_ram_ptr(addr1); |
13eb76e0 | 3925 | memcpy(ptr, buf, l); |
3a7d929e FB |
3926 | if (!cpu_physical_memory_is_dirty(addr1)) { |
3927 | /* invalidate code */ | |
3928 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); | |
3929 | /* set dirty bit */ | |
f7c11b53 YT |
3930 | cpu_physical_memory_set_dirty_flags( |
3931 | addr1, (0xff & ~CODE_DIRTY_FLAG)); | |
3a7d929e | 3932 | } |
050a0ddf | 3933 | qemu_put_ram_ptr(ptr); |
13eb76e0 FB |
3934 | } |
3935 | } else { | |
5fafdf24 | 3936 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
2a4188a3 | 3937 | !(pd & IO_MEM_ROMD)) { |
c227f099 | 3938 | target_phys_addr_t addr1 = addr; |
13eb76e0 FB |
3939 | /* I/O case */ |
3940 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 | 3941 | if (p) |
6c2934db AJ |
3942 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3943 | if (l >= 4 && ((addr1 & 3) == 0)) { | |
13eb76e0 | 3944 | /* 32 bit read access */ |
6c2934db | 3945 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1); |
c27004ec | 3946 | stl_p(buf, val); |
13eb76e0 | 3947 | l = 4; |
6c2934db | 3948 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
13eb76e0 | 3949 | /* 16 bit read access */ |
6c2934db | 3950 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1); |
c27004ec | 3951 | stw_p(buf, val); |
13eb76e0 FB |
3952 | l = 2; |
3953 | } else { | |
1c213d19 | 3954 | /* 8 bit read access */ |
6c2934db | 3955 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1); |
c27004ec | 3956 | stb_p(buf, val); |
13eb76e0 FB |
3957 | l = 1; |
3958 | } | |
3959 | } else { | |
3960 | /* RAM case */ | |
050a0ddf AP |
3961 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
3962 | memcpy(buf, ptr + (addr & ~TARGET_PAGE_MASK), l); | |
3963 | qemu_put_ram_ptr(ptr); | |
13eb76e0 FB |
3964 | } |
3965 | } | |
3966 | len -= l; | |
3967 | buf += l; | |
3968 | addr += l; | |
3969 | } | |
3970 | } | |
8df1cd07 | 3971 | |
d0ecd2aa | 3972 | /* used for ROM loading : can write in RAM and ROM */ |
c227f099 | 3973 | void cpu_physical_memory_write_rom(target_phys_addr_t addr, |
d0ecd2aa FB |
3974 | const uint8_t *buf, int len) |
3975 | { | |
3976 | int l; | |
3977 | uint8_t *ptr; | |
c227f099 | 3978 | target_phys_addr_t page; |
d0ecd2aa FB |
3979 | unsigned long pd; |
3980 | PhysPageDesc *p; | |
3b46e624 | 3981 | |
d0ecd2aa FB |
3982 | while (len > 0) { |
3983 | page = addr & TARGET_PAGE_MASK; | |
3984 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3985 | if (l > len) | |
3986 | l = len; | |
3987 | p = phys_page_find(page >> TARGET_PAGE_BITS); | |
3988 | if (!p) { | |
3989 | pd = IO_MEM_UNASSIGNED; | |
3990 | } else { | |
3991 | pd = p->phys_offset; | |
3992 | } | |
3b46e624 | 3993 | |
d0ecd2aa | 3994 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM && |
2a4188a3 FB |
3995 | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
3996 | !(pd & IO_MEM_ROMD)) { | |
d0ecd2aa FB |
3997 | /* do nothing */ |
3998 | } else { | |
3999 | unsigned long addr1; | |
4000 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
4001 | /* ROM/RAM case */ | |
5579c7f3 | 4002 | ptr = qemu_get_ram_ptr(addr1); |
d0ecd2aa | 4003 | memcpy(ptr, buf, l); |
050a0ddf | 4004 | qemu_put_ram_ptr(ptr); |
d0ecd2aa FB |
4005 | } |
4006 | len -= l; | |
4007 | buf += l; | |
4008 | addr += l; | |
4009 | } | |
4010 | } | |
4011 | ||
6d16c2f8 AL |
4012 | typedef struct { |
4013 | void *buffer; | |
c227f099 AL |
4014 | target_phys_addr_t addr; |
4015 | target_phys_addr_t len; | |
6d16c2f8 AL |
4016 | } BounceBuffer; |
4017 | ||
4018 | static BounceBuffer bounce; | |
4019 | ||
ba223c29 AL |
4020 | typedef struct MapClient { |
4021 | void *opaque; | |
4022 | void (*callback)(void *opaque); | |
72cf2d4f | 4023 | QLIST_ENTRY(MapClient) link; |
ba223c29 AL |
4024 | } MapClient; |
4025 | ||
72cf2d4f BS |
4026 | static QLIST_HEAD(map_client_list, MapClient) map_client_list |
4027 | = QLIST_HEAD_INITIALIZER(map_client_list); | |
ba223c29 AL |
4028 | |
4029 | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) | |
4030 | { | |
4031 | MapClient *client = qemu_malloc(sizeof(*client)); | |
4032 | ||
4033 | client->opaque = opaque; | |
4034 | client->callback = callback; | |
72cf2d4f | 4035 | QLIST_INSERT_HEAD(&map_client_list, client, link); |
ba223c29 AL |
4036 | return client; |
4037 | } | |
4038 | ||
4039 | void cpu_unregister_map_client(void *_client) | |
4040 | { | |
4041 | MapClient *client = (MapClient *)_client; | |
4042 | ||
72cf2d4f | 4043 | QLIST_REMOVE(client, link); |
34d5e948 | 4044 | qemu_free(client); |
ba223c29 AL |
4045 | } |
4046 | ||
4047 | static void cpu_notify_map_clients(void) | |
4048 | { | |
4049 | MapClient *client; | |
4050 | ||
72cf2d4f BS |
4051 | while (!QLIST_EMPTY(&map_client_list)) { |
4052 | client = QLIST_FIRST(&map_client_list); | |
ba223c29 | 4053 | client->callback(client->opaque); |
34d5e948 | 4054 | cpu_unregister_map_client(client); |
ba223c29 AL |
4055 | } |
4056 | } | |
4057 | ||
6d16c2f8 AL |
4058 | /* Map a physical memory region into a host virtual address. |
4059 | * May map a subset of the requested range, given by and returned in *plen. | |
4060 | * May return NULL if resources needed to perform the mapping are exhausted. | |
4061 | * Use only for reads OR writes - not for read-modify-write operations. | |
ba223c29 AL |
4062 | * Use cpu_register_map_client() to know when retrying the map operation is |
4063 | * likely to succeed. | |
6d16c2f8 | 4064 | */ |
c227f099 AL |
4065 | void *cpu_physical_memory_map(target_phys_addr_t addr, |
4066 | target_phys_addr_t *plen, | |
6d16c2f8 AL |
4067 | int is_write) |
4068 | { | |
c227f099 | 4069 | target_phys_addr_t len = *plen; |
38bee5dc | 4070 | target_phys_addr_t todo = 0; |
6d16c2f8 | 4071 | int l; |
c227f099 | 4072 | target_phys_addr_t page; |
6d16c2f8 AL |
4073 | unsigned long pd; |
4074 | PhysPageDesc *p; | |
f15fbc4b | 4075 | ram_addr_t raddr = RAM_ADDR_MAX; |
8ab934f9 SS |
4076 | ram_addr_t rlen; |
4077 | void *ret; | |
6d16c2f8 AL |
4078 | |
4079 | while (len > 0) { | |
4080 | page = addr & TARGET_PAGE_MASK; | |
4081 | l = (page + TARGET_PAGE_SIZE) - addr; | |
4082 | if (l > len) | |
4083 | l = len; | |
4084 | p = phys_page_find(page >> TARGET_PAGE_BITS); | |
4085 | if (!p) { | |
4086 | pd = IO_MEM_UNASSIGNED; | |
4087 | } else { | |
4088 | pd = p->phys_offset; | |
4089 | } | |
4090 | ||
4091 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { | |
38bee5dc | 4092 | if (todo || bounce.buffer) { |
6d16c2f8 AL |
4093 | break; |
4094 | } | |
4095 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); | |
4096 | bounce.addr = addr; | |
4097 | bounce.len = l; | |
4098 | if (!is_write) { | |
54f7b4a3 | 4099 | cpu_physical_memory_read(addr, bounce.buffer, l); |
6d16c2f8 | 4100 | } |
38bee5dc SS |
4101 | |
4102 | *plen = l; | |
4103 | return bounce.buffer; | |
6d16c2f8 | 4104 | } |
8ab934f9 SS |
4105 | if (!todo) { |
4106 | raddr = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
4107 | } | |
6d16c2f8 AL |
4108 | |
4109 | len -= l; | |
4110 | addr += l; | |
38bee5dc | 4111 | todo += l; |
6d16c2f8 | 4112 | } |
8ab934f9 SS |
4113 | rlen = todo; |
4114 | ret = qemu_ram_ptr_length(raddr, &rlen); | |
4115 | *plen = rlen; | |
4116 | return ret; | |
6d16c2f8 AL |
4117 | } |
4118 | ||
4119 | /* Unmaps a memory region previously mapped by cpu_physical_memory_map(). | |
4120 | * Will also mark the memory as dirty if is_write == 1. access_len gives | |
4121 | * the amount of memory that was actually read or written by the caller. | |
4122 | */ | |
c227f099 AL |
4123 | void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len, |
4124 | int is_write, target_phys_addr_t access_len) | |
6d16c2f8 AL |
4125 | { |
4126 | if (buffer != bounce.buffer) { | |
4127 | if (is_write) { | |
e890261f | 4128 | ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer); |
6d16c2f8 AL |
4129 | while (access_len) { |
4130 | unsigned l; | |
4131 | l = TARGET_PAGE_SIZE; | |
4132 | if (l > access_len) | |
4133 | l = access_len; | |
4134 | if (!cpu_physical_memory_is_dirty(addr1)) { | |
4135 | /* invalidate code */ | |
4136 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); | |
4137 | /* set dirty bit */ | |
f7c11b53 YT |
4138 | cpu_physical_memory_set_dirty_flags( |
4139 | addr1, (0xff & ~CODE_DIRTY_FLAG)); | |
6d16c2f8 AL |
4140 | } |
4141 | addr1 += l; | |
4142 | access_len -= l; | |
4143 | } | |
4144 | } | |
868bb33f | 4145 | if (xen_enabled()) { |
e41d7c69 | 4146 | xen_invalidate_map_cache_entry(buffer); |
050a0ddf | 4147 | } |
6d16c2f8 AL |
4148 | return; |
4149 | } | |
4150 | if (is_write) { | |
4151 | cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len); | |
4152 | } | |
f8a83245 | 4153 | qemu_vfree(bounce.buffer); |
6d16c2f8 | 4154 | bounce.buffer = NULL; |
ba223c29 | 4155 | cpu_notify_map_clients(); |
6d16c2f8 | 4156 | } |
d0ecd2aa | 4157 | |
8df1cd07 | 4158 | /* warning: addr must be aligned */ |
1e78bcc1 AG |
4159 | static inline uint32_t ldl_phys_internal(target_phys_addr_t addr, |
4160 | enum device_endian endian) | |
8df1cd07 FB |
4161 | { |
4162 | int io_index; | |
4163 | uint8_t *ptr; | |
4164 | uint32_t val; | |
4165 | unsigned long pd; | |
4166 | PhysPageDesc *p; | |
4167 | ||
4168 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4169 | if (!p) { | |
4170 | pd = IO_MEM_UNASSIGNED; | |
4171 | } else { | |
4172 | pd = p->phys_offset; | |
4173 | } | |
3b46e624 | 4174 | |
5fafdf24 | 4175 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
2a4188a3 | 4176 | !(pd & IO_MEM_ROMD)) { |
8df1cd07 FB |
4177 | /* I/O case */ |
4178 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
4179 | if (p) |
4180 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
8df1cd07 | 4181 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
1e78bcc1 AG |
4182 | #if defined(TARGET_WORDS_BIGENDIAN) |
4183 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
4184 | val = bswap32(val); | |
4185 | } | |
4186 | #else | |
4187 | if (endian == DEVICE_BIG_ENDIAN) { | |
4188 | val = bswap32(val); | |
4189 | } | |
4190 | #endif | |
8df1cd07 FB |
4191 | } else { |
4192 | /* RAM case */ | |
5579c7f3 | 4193 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
8df1cd07 | 4194 | (addr & ~TARGET_PAGE_MASK); |
1e78bcc1 AG |
4195 | switch (endian) { |
4196 | case DEVICE_LITTLE_ENDIAN: | |
4197 | val = ldl_le_p(ptr); | |
4198 | break; | |
4199 | case DEVICE_BIG_ENDIAN: | |
4200 | val = ldl_be_p(ptr); | |
4201 | break; | |
4202 | default: | |
4203 | val = ldl_p(ptr); | |
4204 | break; | |
4205 | } | |
8df1cd07 FB |
4206 | } |
4207 | return val; | |
4208 | } | |
4209 | ||
1e78bcc1 AG |
4210 | uint32_t ldl_phys(target_phys_addr_t addr) |
4211 | { | |
4212 | return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
4213 | } | |
4214 | ||
4215 | uint32_t ldl_le_phys(target_phys_addr_t addr) | |
4216 | { | |
4217 | return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
4218 | } | |
4219 | ||
4220 | uint32_t ldl_be_phys(target_phys_addr_t addr) | |
4221 | { | |
4222 | return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
4223 | } | |
4224 | ||
84b7b8e7 | 4225 | /* warning: addr must be aligned */ |
1e78bcc1 AG |
4226 | static inline uint64_t ldq_phys_internal(target_phys_addr_t addr, |
4227 | enum device_endian endian) | |
84b7b8e7 FB |
4228 | { |
4229 | int io_index; | |
4230 | uint8_t *ptr; | |
4231 | uint64_t val; | |
4232 | unsigned long pd; | |
4233 | PhysPageDesc *p; | |
4234 | ||
4235 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4236 | if (!p) { | |
4237 | pd = IO_MEM_UNASSIGNED; | |
4238 | } else { | |
4239 | pd = p->phys_offset; | |
4240 | } | |
3b46e624 | 4241 | |
2a4188a3 FB |
4242 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
4243 | !(pd & IO_MEM_ROMD)) { | |
84b7b8e7 FB |
4244 | /* I/O case */ |
4245 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
4246 | if (p) |
4247 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
1e78bcc1 AG |
4248 | |
4249 | /* XXX This is broken when device endian != cpu endian. | |
4250 | Fix and add "endian" variable check */ | |
84b7b8e7 FB |
4251 | #ifdef TARGET_WORDS_BIGENDIAN |
4252 | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; | |
4253 | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); | |
4254 | #else | |
4255 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); | |
4256 | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; | |
4257 | #endif | |
4258 | } else { | |
4259 | /* RAM case */ | |
5579c7f3 | 4260 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
84b7b8e7 | 4261 | (addr & ~TARGET_PAGE_MASK); |
1e78bcc1 AG |
4262 | switch (endian) { |
4263 | case DEVICE_LITTLE_ENDIAN: | |
4264 | val = ldq_le_p(ptr); | |
4265 | break; | |
4266 | case DEVICE_BIG_ENDIAN: | |
4267 | val = ldq_be_p(ptr); | |
4268 | break; | |
4269 | default: | |
4270 | val = ldq_p(ptr); | |
4271 | break; | |
4272 | } | |
84b7b8e7 FB |
4273 | } |
4274 | return val; | |
4275 | } | |
4276 | ||
1e78bcc1 AG |
4277 | uint64_t ldq_phys(target_phys_addr_t addr) |
4278 | { | |
4279 | return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
4280 | } | |
4281 | ||
4282 | uint64_t ldq_le_phys(target_phys_addr_t addr) | |
4283 | { | |
4284 | return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
4285 | } | |
4286 | ||
4287 | uint64_t ldq_be_phys(target_phys_addr_t addr) | |
4288 | { | |
4289 | return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
4290 | } | |
4291 | ||
aab33094 | 4292 | /* XXX: optimize */ |
c227f099 | 4293 | uint32_t ldub_phys(target_phys_addr_t addr) |
aab33094 FB |
4294 | { |
4295 | uint8_t val; | |
4296 | cpu_physical_memory_read(addr, &val, 1); | |
4297 | return val; | |
4298 | } | |
4299 | ||
733f0b02 | 4300 | /* warning: addr must be aligned */ |
1e78bcc1 AG |
4301 | static inline uint32_t lduw_phys_internal(target_phys_addr_t addr, |
4302 | enum device_endian endian) | |
aab33094 | 4303 | { |
733f0b02 MT |
4304 | int io_index; |
4305 | uint8_t *ptr; | |
4306 | uint64_t val; | |
4307 | unsigned long pd; | |
4308 | PhysPageDesc *p; | |
4309 | ||
4310 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4311 | if (!p) { | |
4312 | pd = IO_MEM_UNASSIGNED; | |
4313 | } else { | |
4314 | pd = p->phys_offset; | |
4315 | } | |
4316 | ||
4317 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && | |
4318 | !(pd & IO_MEM_ROMD)) { | |
4319 | /* I/O case */ | |
4320 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
4321 | if (p) | |
4322 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
4323 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr); | |
1e78bcc1 AG |
4324 | #if defined(TARGET_WORDS_BIGENDIAN) |
4325 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
4326 | val = bswap16(val); | |
4327 | } | |
4328 | #else | |
4329 | if (endian == DEVICE_BIG_ENDIAN) { | |
4330 | val = bswap16(val); | |
4331 | } | |
4332 | #endif | |
733f0b02 MT |
4333 | } else { |
4334 | /* RAM case */ | |
4335 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + | |
4336 | (addr & ~TARGET_PAGE_MASK); | |
1e78bcc1 AG |
4337 | switch (endian) { |
4338 | case DEVICE_LITTLE_ENDIAN: | |
4339 | val = lduw_le_p(ptr); | |
4340 | break; | |
4341 | case DEVICE_BIG_ENDIAN: | |
4342 | val = lduw_be_p(ptr); | |
4343 | break; | |
4344 | default: | |
4345 | val = lduw_p(ptr); | |
4346 | break; | |
4347 | } | |
733f0b02 MT |
4348 | } |
4349 | return val; | |
aab33094 FB |
4350 | } |
4351 | ||
1e78bcc1 AG |
4352 | uint32_t lduw_phys(target_phys_addr_t addr) |
4353 | { | |
4354 | return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
4355 | } | |
4356 | ||
4357 | uint32_t lduw_le_phys(target_phys_addr_t addr) | |
4358 | { | |
4359 | return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
4360 | } | |
4361 | ||
4362 | uint32_t lduw_be_phys(target_phys_addr_t addr) | |
4363 | { | |
4364 | return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
4365 | } | |
4366 | ||
8df1cd07 FB |
4367 | /* warning: addr must be aligned. The ram page is not masked as dirty |
4368 | and the code inside is not invalidated. It is useful if the dirty | |
4369 | bits are used to track modified PTEs */ | |
c227f099 | 4370 | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val) |
8df1cd07 FB |
4371 | { |
4372 | int io_index; | |
4373 | uint8_t *ptr; | |
4374 | unsigned long pd; | |
4375 | PhysPageDesc *p; | |
4376 | ||
4377 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4378 | if (!p) { | |
4379 | pd = IO_MEM_UNASSIGNED; | |
4380 | } else { | |
4381 | pd = p->phys_offset; | |
4382 | } | |
3b46e624 | 4383 | |
3a7d929e | 4384 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
8df1cd07 | 4385 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 PB |
4386 | if (p) |
4387 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
8df1cd07 FB |
4388 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
4389 | } else { | |
74576198 | 4390 | unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
5579c7f3 | 4391 | ptr = qemu_get_ram_ptr(addr1); |
8df1cd07 | 4392 | stl_p(ptr, val); |
74576198 AL |
4393 | |
4394 | if (unlikely(in_migration)) { | |
4395 | if (!cpu_physical_memory_is_dirty(addr1)) { | |
4396 | /* invalidate code */ | |
4397 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); | |
4398 | /* set dirty bit */ | |
f7c11b53 YT |
4399 | cpu_physical_memory_set_dirty_flags( |
4400 | addr1, (0xff & ~CODE_DIRTY_FLAG)); | |
74576198 AL |
4401 | } |
4402 | } | |
8df1cd07 FB |
4403 | } |
4404 | } | |
4405 | ||
c227f099 | 4406 | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val) |
bc98a7ef JM |
4407 | { |
4408 | int io_index; | |
4409 | uint8_t *ptr; | |
4410 | unsigned long pd; | |
4411 | PhysPageDesc *p; | |
4412 | ||
4413 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4414 | if (!p) { | |
4415 | pd = IO_MEM_UNASSIGNED; | |
4416 | } else { | |
4417 | pd = p->phys_offset; | |
4418 | } | |
3b46e624 | 4419 | |
bc98a7ef JM |
4420 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
4421 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
8da3ff18 PB |
4422 | if (p) |
4423 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
bc98a7ef JM |
4424 | #ifdef TARGET_WORDS_BIGENDIAN |
4425 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); | |
4426 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); | |
4427 | #else | |
4428 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); | |
4429 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); | |
4430 | #endif | |
4431 | } else { | |
5579c7f3 | 4432 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bc98a7ef JM |
4433 | (addr & ~TARGET_PAGE_MASK); |
4434 | stq_p(ptr, val); | |
4435 | } | |
4436 | } | |
4437 | ||
8df1cd07 | 4438 | /* warning: addr must be aligned */ |
1e78bcc1 AG |
4439 | static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val, |
4440 | enum device_endian endian) | |
8df1cd07 FB |
4441 | { |
4442 | int io_index; | |
4443 | uint8_t *ptr; | |
4444 | unsigned long pd; | |
4445 | PhysPageDesc *p; | |
4446 | ||
4447 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4448 | if (!p) { | |
4449 | pd = IO_MEM_UNASSIGNED; | |
4450 | } else { | |
4451 | pd = p->phys_offset; | |
4452 | } | |
3b46e624 | 4453 | |
3a7d929e | 4454 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
8df1cd07 | 4455 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
8da3ff18 PB |
4456 | if (p) |
4457 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
1e78bcc1 AG |
4458 | #if defined(TARGET_WORDS_BIGENDIAN) |
4459 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
4460 | val = bswap32(val); | |
4461 | } | |
4462 | #else | |
4463 | if (endian == DEVICE_BIG_ENDIAN) { | |
4464 | val = bswap32(val); | |
4465 | } | |
4466 | #endif | |
8df1cd07 FB |
4467 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
4468 | } else { | |
4469 | unsigned long addr1; | |
4470 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
4471 | /* RAM case */ | |
5579c7f3 | 4472 | ptr = qemu_get_ram_ptr(addr1); |
1e78bcc1 AG |
4473 | switch (endian) { |
4474 | case DEVICE_LITTLE_ENDIAN: | |
4475 | stl_le_p(ptr, val); | |
4476 | break; | |
4477 | case DEVICE_BIG_ENDIAN: | |
4478 | stl_be_p(ptr, val); | |
4479 | break; | |
4480 | default: | |
4481 | stl_p(ptr, val); | |
4482 | break; | |
4483 | } | |
3a7d929e FB |
4484 | if (!cpu_physical_memory_is_dirty(addr1)) { |
4485 | /* invalidate code */ | |
4486 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); | |
4487 | /* set dirty bit */ | |
f7c11b53 YT |
4488 | cpu_physical_memory_set_dirty_flags(addr1, |
4489 | (0xff & ~CODE_DIRTY_FLAG)); | |
3a7d929e | 4490 | } |
8df1cd07 FB |
4491 | } |
4492 | } | |
4493 | ||
1e78bcc1 AG |
4494 | void stl_phys(target_phys_addr_t addr, uint32_t val) |
4495 | { | |
4496 | stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); | |
4497 | } | |
4498 | ||
4499 | void stl_le_phys(target_phys_addr_t addr, uint32_t val) | |
4500 | { | |
4501 | stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); | |
4502 | } | |
4503 | ||
4504 | void stl_be_phys(target_phys_addr_t addr, uint32_t val) | |
4505 | { | |
4506 | stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN); | |
4507 | } | |
4508 | ||
aab33094 | 4509 | /* XXX: optimize */ |
c227f099 | 4510 | void stb_phys(target_phys_addr_t addr, uint32_t val) |
aab33094 FB |
4511 | { |
4512 | uint8_t v = val; | |
4513 | cpu_physical_memory_write(addr, &v, 1); | |
4514 | } | |
4515 | ||
733f0b02 | 4516 | /* warning: addr must be aligned */ |
1e78bcc1 AG |
4517 | static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val, |
4518 | enum device_endian endian) | |
aab33094 | 4519 | { |
733f0b02 MT |
4520 | int io_index; |
4521 | uint8_t *ptr; | |
4522 | unsigned long pd; | |
4523 | PhysPageDesc *p; | |
4524 | ||
4525 | p = phys_page_find(addr >> TARGET_PAGE_BITS); | |
4526 | if (!p) { | |
4527 | pd = IO_MEM_UNASSIGNED; | |
4528 | } else { | |
4529 | pd = p->phys_offset; | |
4530 | } | |
4531 | ||
4532 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { | |
4533 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); | |
4534 | if (p) | |
4535 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; | |
1e78bcc1 AG |
4536 | #if defined(TARGET_WORDS_BIGENDIAN) |
4537 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
4538 | val = bswap16(val); | |
4539 | } | |
4540 | #else | |
4541 | if (endian == DEVICE_BIG_ENDIAN) { | |
4542 | val = bswap16(val); | |
4543 | } | |
4544 | #endif | |
733f0b02 MT |
4545 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val); |
4546 | } else { | |
4547 | unsigned long addr1; | |
4548 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); | |
4549 | /* RAM case */ | |
4550 | ptr = qemu_get_ram_ptr(addr1); | |
1e78bcc1 AG |
4551 | switch (endian) { |
4552 | case DEVICE_LITTLE_ENDIAN: | |
4553 | stw_le_p(ptr, val); | |
4554 | break; | |
4555 | case DEVICE_BIG_ENDIAN: | |
4556 | stw_be_p(ptr, val); | |
4557 | break; | |
4558 | default: | |
4559 | stw_p(ptr, val); | |
4560 | break; | |
4561 | } | |
733f0b02 MT |
4562 | if (!cpu_physical_memory_is_dirty(addr1)) { |
4563 | /* invalidate code */ | |
4564 | tb_invalidate_phys_page_range(addr1, addr1 + 2, 0); | |
4565 | /* set dirty bit */ | |
4566 | cpu_physical_memory_set_dirty_flags(addr1, | |
4567 | (0xff & ~CODE_DIRTY_FLAG)); | |
4568 | } | |
4569 | } | |
aab33094 FB |
4570 | } |
4571 | ||
1e78bcc1 AG |
4572 | void stw_phys(target_phys_addr_t addr, uint32_t val) |
4573 | { | |
4574 | stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); | |
4575 | } | |
4576 | ||
4577 | void stw_le_phys(target_phys_addr_t addr, uint32_t val) | |
4578 | { | |
4579 | stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); | |
4580 | } | |
4581 | ||
4582 | void stw_be_phys(target_phys_addr_t addr, uint32_t val) | |
4583 | { | |
4584 | stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN); | |
4585 | } | |
4586 | ||
aab33094 | 4587 | /* XXX: optimize */ |
c227f099 | 4588 | void stq_phys(target_phys_addr_t addr, uint64_t val) |
aab33094 FB |
4589 | { |
4590 | val = tswap64(val); | |
71d2b725 | 4591 | cpu_physical_memory_write(addr, &val, 8); |
aab33094 FB |
4592 | } |
4593 | ||
1e78bcc1 AG |
4594 | void stq_le_phys(target_phys_addr_t addr, uint64_t val) |
4595 | { | |
4596 | val = cpu_to_le64(val); | |
4597 | cpu_physical_memory_write(addr, &val, 8); | |
4598 | } | |
4599 | ||
4600 | void stq_be_phys(target_phys_addr_t addr, uint64_t val) | |
4601 | { | |
4602 | val = cpu_to_be64(val); | |
4603 | cpu_physical_memory_write(addr, &val, 8); | |
4604 | } | |
4605 | ||
5e2972fd | 4606 | /* virtual memory access for debug (includes writing to ROM) */ |
5fafdf24 | 4607 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
b448f2f3 | 4608 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
4609 | { |
4610 | int l; | |
c227f099 | 4611 | target_phys_addr_t phys_addr; |
9b3c35e0 | 4612 | target_ulong page; |
13eb76e0 FB |
4613 | |
4614 | while (len > 0) { | |
4615 | page = addr & TARGET_PAGE_MASK; | |
4616 | phys_addr = cpu_get_phys_page_debug(env, page); | |
4617 | /* if no physical page mapped, return an error */ | |
4618 | if (phys_addr == -1) | |
4619 | return -1; | |
4620 | l = (page + TARGET_PAGE_SIZE) - addr; | |
4621 | if (l > len) | |
4622 | l = len; | |
5e2972fd | 4623 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
5e2972fd AL |
4624 | if (is_write) |
4625 | cpu_physical_memory_write_rom(phys_addr, buf, l); | |
4626 | else | |
5e2972fd | 4627 | cpu_physical_memory_rw(phys_addr, buf, l, is_write); |
13eb76e0 FB |
4628 | len -= l; |
4629 | buf += l; | |
4630 | addr += l; | |
4631 | } | |
4632 | return 0; | |
4633 | } | |
a68fe89c | 4634 | #endif |
13eb76e0 | 4635 | |
2e70f6ef PB |
4636 | /* in deterministic execution mode, instructions doing device I/Os |
4637 | must be at the end of the TB */ | |
4638 | void cpu_io_recompile(CPUState *env, void *retaddr) | |
4639 | { | |
4640 | TranslationBlock *tb; | |
4641 | uint32_t n, cflags; | |
4642 | target_ulong pc, cs_base; | |
4643 | uint64_t flags; | |
4644 | ||
4645 | tb = tb_find_pc((unsigned long)retaddr); | |
4646 | if (!tb) { | |
4647 | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", | |
4648 | retaddr); | |
4649 | } | |
4650 | n = env->icount_decr.u16.low + tb->icount; | |
618ba8e6 | 4651 | cpu_restore_state(tb, env, (unsigned long)retaddr); |
2e70f6ef | 4652 | /* Calculate how many instructions had been executed before the fault |
bf20dc07 | 4653 | occurred. */ |
2e70f6ef PB |
4654 | n = n - env->icount_decr.u16.low; |
4655 | /* Generate a new TB ending on the I/O insn. */ | |
4656 | n++; | |
4657 | /* On MIPS and SH, delay slot instructions can only be restarted if | |
4658 | they were already the first instruction in the TB. If this is not | |
bf20dc07 | 4659 | the first instruction in a TB then re-execute the preceding |
2e70f6ef PB |
4660 | branch. */ |
4661 | #if defined(TARGET_MIPS) | |
4662 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { | |
4663 | env->active_tc.PC -= 4; | |
4664 | env->icount_decr.u16.low++; | |
4665 | env->hflags &= ~MIPS_HFLAG_BMASK; | |
4666 | } | |
4667 | #elif defined(TARGET_SH4) | |
4668 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 | |
4669 | && n > 1) { | |
4670 | env->pc -= 2; | |
4671 | env->icount_decr.u16.low++; | |
4672 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); | |
4673 | } | |
4674 | #endif | |
4675 | /* This should never happen. */ | |
4676 | if (n > CF_COUNT_MASK) | |
4677 | cpu_abort(env, "TB too big during recompile"); | |
4678 | ||
4679 | cflags = n | CF_LAST_IO; | |
4680 | pc = tb->pc; | |
4681 | cs_base = tb->cs_base; | |
4682 | flags = tb->flags; | |
4683 | tb_phys_invalidate(tb, -1); | |
4684 | /* FIXME: In theory this could raise an exception. In practice | |
4685 | we have already translated the block once so it's probably ok. */ | |
4686 | tb_gen_code(env, pc, cs_base, flags, cflags); | |
bf20dc07 | 4687 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
2e70f6ef PB |
4688 | the first in the TB) then we end up generating a whole new TB and |
4689 | repeating the fault, which is horribly inefficient. | |
4690 | Better would be to execute just this insn uncached, or generate a | |
4691 | second new TB. */ | |
4692 | cpu_resume_from_signal(env, NULL); | |
4693 | } | |
4694 | ||
b3755a91 PB |
4695 | #if !defined(CONFIG_USER_ONLY) |
4696 | ||
055403b2 | 4697 | void dump_exec_info(FILE *f, fprintf_function cpu_fprintf) |
e3db7226 FB |
4698 | { |
4699 | int i, target_code_size, max_target_code_size; | |
4700 | int direct_jmp_count, direct_jmp2_count, cross_page; | |
4701 | TranslationBlock *tb; | |
3b46e624 | 4702 | |
e3db7226 FB |
4703 | target_code_size = 0; |
4704 | max_target_code_size = 0; | |
4705 | cross_page = 0; | |
4706 | direct_jmp_count = 0; | |
4707 | direct_jmp2_count = 0; | |
4708 | for(i = 0; i < nb_tbs; i++) { | |
4709 | tb = &tbs[i]; | |
4710 | target_code_size += tb->size; | |
4711 | if (tb->size > max_target_code_size) | |
4712 | max_target_code_size = tb->size; | |
4713 | if (tb->page_addr[1] != -1) | |
4714 | cross_page++; | |
4715 | if (tb->tb_next_offset[0] != 0xffff) { | |
4716 | direct_jmp_count++; | |
4717 | if (tb->tb_next_offset[1] != 0xffff) { | |
4718 | direct_jmp2_count++; | |
4719 | } | |
4720 | } | |
4721 | } | |
4722 | /* XXX: avoid using doubles ? */ | |
57fec1fe | 4723 | cpu_fprintf(f, "Translation buffer state:\n"); |
055403b2 | 4724 | cpu_fprintf(f, "gen code size %td/%ld\n", |
26a5f13b FB |
4725 | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); |
4726 | cpu_fprintf(f, "TB count %d/%d\n", | |
4727 | nb_tbs, code_gen_max_blocks); | |
5fafdf24 | 4728 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
e3db7226 FB |
4729 | nb_tbs ? target_code_size / nb_tbs : 0, |
4730 | max_target_code_size); | |
055403b2 | 4731 | cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n", |
e3db7226 FB |
4732 | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0, |
4733 | target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0); | |
5fafdf24 TS |
4734 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", |
4735 | cross_page, | |
e3db7226 FB |
4736 | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
4737 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", | |
5fafdf24 | 4738 | direct_jmp_count, |
e3db7226 FB |
4739 | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
4740 | direct_jmp2_count, | |
4741 | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); | |
57fec1fe | 4742 | cpu_fprintf(f, "\nStatistics:\n"); |
e3db7226 FB |
4743 | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count); |
4744 | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count); | |
4745 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); | |
b67d9a52 | 4746 | tcg_dump_info(f, cpu_fprintf); |
e3db7226 FB |
4747 | } |
4748 | ||
61382a50 FB |
4749 | #define MMUSUFFIX _cmmu |
4750 | #define GETPC() NULL | |
4751 | #define env cpu_single_env | |
b769d8fe | 4752 | #define SOFTMMU_CODE_ACCESS |
61382a50 FB |
4753 | |
4754 | #define SHIFT 0 | |
4755 | #include "softmmu_template.h" | |
4756 | ||
4757 | #define SHIFT 1 | |
4758 | #include "softmmu_template.h" | |
4759 | ||
4760 | #define SHIFT 2 | |
4761 | #include "softmmu_template.h" | |
4762 | ||
4763 | #define SHIFT 3 | |
4764 | #include "softmmu_template.h" | |
4765 | ||
4766 | #undef env | |
4767 | ||
4768 | #endif |