Merge commit 'v0.11.0-rc0' into stable-0.11
[qemu-kvm/fedora.git] / exec.c
blobf825fd158cb333ec262ddd6b05ab2f64aca6ccfc
1 /*
2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
20 #ifdef _WIN32
21 #include <windows.h>
22 #else
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <inttypes.h>
34 #include "cpu.h"
35 #include "exec-all.h"
36 #include "qemu-common.h"
37 #include "cache-utils.h"
39 #if !defined(TARGET_IA64)
40 #include "tcg.h"
41 #endif
42 #include "qemu-kvm.h"
44 #include "hw/hw.h"
45 #include "osdep.h"
46 #include "kvm.h"
47 #if defined(CONFIG_USER_ONLY)
48 #include <qemu.h>
49 #endif
51 //#define DEBUG_TB_INVALIDATE
52 //#define DEBUG_FLUSH
53 //#define DEBUG_TLB
54 //#define DEBUG_UNASSIGNED
56 /* make various TB consistency checks */
57 //#define DEBUG_TB_CHECK
58 //#define DEBUG_TLB_CHECK
60 //#define DEBUG_IOPORT
61 //#define DEBUG_SUBPAGE
63 #if !defined(CONFIG_USER_ONLY)
64 /* TB consistency checks only implemented for usermode emulation. */
65 #undef DEBUG_TB_CHECK
66 #endif
68 #define SMC_BITMAP_USE_THRESHOLD 10
70 #if defined(TARGET_SPARC64)
71 #define TARGET_PHYS_ADDR_SPACE_BITS 41
72 #elif defined(TARGET_SPARC)
73 #define TARGET_PHYS_ADDR_SPACE_BITS 36
74 #elif defined(TARGET_ALPHA)
75 #define TARGET_PHYS_ADDR_SPACE_BITS 42
76 #define TARGET_VIRT_ADDR_SPACE_BITS 42
77 #elif defined(TARGET_PPC64)
78 #define TARGET_PHYS_ADDR_SPACE_BITS 42
79 #elif defined(TARGET_X86_64) && !defined(CONFIG_KQEMU)
80 #define TARGET_PHYS_ADDR_SPACE_BITS 42
81 #elif defined(TARGET_I386) && !defined(CONFIG_KQEMU)
82 #define TARGET_PHYS_ADDR_SPACE_BITS 36
83 #elif defined(TARGET_IA64)
84 #define TARGET_PHYS_ADDR_SPACE_BITS 36
85 #else
86 /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
87 #define TARGET_PHYS_ADDR_SPACE_BITS 32
88 #endif
90 static TranslationBlock *tbs;
91 int code_gen_max_blocks;
92 TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
93 static int nb_tbs;
94 /* any access to the tbs or the page table must use this lock */
95 spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
97 #if defined(__arm__) || defined(__sparc_v9__)
98 /* The prologue must be reachable with a direct jump. ARM and Sparc64
99 have limited branch ranges (possibly also PPC) so place it in a
100 section close to code segment. */
101 #define code_gen_section \
102 __attribute__((__section__(".gen_code"))) \
103 __attribute__((aligned (32)))
104 #elif defined(_WIN32)
105 /* Maximum alignment for Win32 is 16. */
106 #define code_gen_section \
107 __attribute__((aligned (16)))
108 #else
109 #define code_gen_section \
110 __attribute__((aligned (32)))
111 #endif
113 uint8_t code_gen_prologue[1024] code_gen_section;
114 static uint8_t *code_gen_buffer;
115 static unsigned long code_gen_buffer_size;
116 /* threshold to flush the translated code buffer */
117 static unsigned long code_gen_buffer_max_size;
118 uint8_t *code_gen_ptr;
120 #if !defined(CONFIG_USER_ONLY)
121 int phys_ram_fd;
122 uint8_t *phys_ram_dirty;
123 uint8_t *bios_mem;
124 static int in_migration;
126 typedef struct RAMBlock {
127 uint8_t *host;
128 ram_addr_t offset;
129 ram_addr_t length;
130 struct RAMBlock *next;
131 } RAMBlock;
133 static RAMBlock *ram_blocks;
134 /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
135 then we can no longer assume contiguous ram offsets, and external uses
136 of this variable will break. */
137 ram_addr_t last_ram_offset;
138 #endif
140 CPUState *first_cpu;
141 /* current CPU in the current thread. It is only valid inside
142 cpu_exec() */
143 CPUState *cpu_single_env;
144 /* 0 = Do not count executed instructions.
145 1 = Precise instruction counting.
146 2 = Adaptive rate instruction counting. */
147 int use_icount = 0;
148 /* Current instruction counter. While executing translated code this may
149 include some instructions that have not yet been executed. */
150 int64_t qemu_icount;
152 typedef struct PageDesc {
153 /* list of TBs intersecting this ram page */
154 TranslationBlock *first_tb;
155 /* in order to optimize self modifying code, we count the number
156 of lookups we do to a given page to use a bitmap */
157 unsigned int code_write_count;
158 uint8_t *code_bitmap;
159 #if defined(CONFIG_USER_ONLY)
160 unsigned long flags;
161 #endif
162 } PageDesc;
164 typedef struct PhysPageDesc {
165 /* offset in host memory of the page + io_index in the low bits */
166 ram_addr_t phys_offset;
167 ram_addr_t region_offset;
168 } PhysPageDesc;
170 #define L2_BITS 10
171 #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
172 /* XXX: this is a temporary hack for alpha target.
173 * In the future, this is to be replaced by a multi-level table
174 * to actually be able to handle the complete 64 bits address space.
176 #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
177 #else
178 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
179 #endif
181 #define L1_SIZE (1 << L1_BITS)
182 #define L2_SIZE (1 << L2_BITS)
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;
189 /* XXX: for system emulation, it could just be an array */
190 static PageDesc *l1_map[L1_SIZE];
191 static PhysPageDesc **l1_phys_map;
193 #if !defined(CONFIG_USER_ONLY)
194 static void io_mem_init(void);
196 /* io memory support */
197 CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
198 CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
199 void *io_mem_opaque[IO_MEM_NB_ENTRIES];
200 static char io_mem_used[IO_MEM_NB_ENTRIES];
201 static int io_mem_watch;
202 #endif
204 /* log support */
205 static const char *logfilename = "/tmp/qemu.log";
206 FILE *logfile;
207 int loglevel;
208 static int log_append = 0;
210 /* statistics */
211 static int tlb_flush_count;
212 static int tb_flush_count;
213 static int tb_phys_invalidate_count;
215 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
216 typedef struct subpage_t {
217 target_phys_addr_t base;
218 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
219 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
220 void *opaque[TARGET_PAGE_SIZE][2][4];
221 ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4];
222 } subpage_t;
224 #ifdef _WIN32
225 static void map_exec(void *addr, long size)
227 DWORD old_protect;
228 VirtualProtect(addr, size,
229 PAGE_EXECUTE_READWRITE, &old_protect);
232 #else
233 static void map_exec(void *addr, long size)
235 unsigned long start, end, page_size;
237 page_size = getpagesize();
238 start = (unsigned long)addr;
239 start &= ~(page_size - 1);
241 end = (unsigned long)addr + size;
242 end += page_size - 1;
243 end &= ~(page_size - 1);
245 mprotect((void *)start, end - start,
246 PROT_READ | PROT_WRITE | PROT_EXEC);
248 #endif
250 static void page_init(void)
252 /* NOTE: we can always suppose that qemu_host_page_size >=
253 TARGET_PAGE_SIZE */
254 #ifdef _WIN32
256 SYSTEM_INFO system_info;
258 GetSystemInfo(&system_info);
259 qemu_real_host_page_size = system_info.dwPageSize;
261 #else
262 qemu_real_host_page_size = getpagesize();
263 #endif
264 if (qemu_host_page_size == 0)
265 qemu_host_page_size = qemu_real_host_page_size;
266 if (qemu_host_page_size < TARGET_PAGE_SIZE)
267 qemu_host_page_size = TARGET_PAGE_SIZE;
268 qemu_host_page_bits = 0;
269 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
270 qemu_host_page_bits++;
271 qemu_host_page_mask = ~(qemu_host_page_size - 1);
272 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
273 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
275 #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
277 long long startaddr, endaddr;
278 FILE *f;
279 int n;
281 mmap_lock();
282 last_brk = (unsigned long)sbrk(0);
283 f = fopen("/proc/self/maps", "r");
284 if (f) {
285 do {
286 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
287 if (n == 2) {
288 startaddr = MIN(startaddr,
289 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
290 endaddr = MIN(endaddr,
291 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
292 page_set_flags(startaddr & TARGET_PAGE_MASK,
293 TARGET_PAGE_ALIGN(endaddr),
294 PAGE_RESERVED);
296 } while (!feof(f));
297 fclose(f);
299 mmap_unlock();
301 #endif
304 static inline PageDesc **page_l1_map(target_ulong index)
306 #if TARGET_LONG_BITS > 32
307 /* Host memory outside guest VM. For 32-bit targets we have already
308 excluded high addresses. */
309 if (index > ((target_ulong)L2_SIZE * L1_SIZE))
310 return NULL;
311 #endif
312 return &l1_map[index >> L2_BITS];
315 static inline PageDesc *page_find_alloc(target_ulong index)
317 PageDesc **lp, *p;
318 lp = page_l1_map(index);
319 if (!lp)
320 return NULL;
322 p = *lp;
323 if (!p) {
324 /* allocate if not found */
325 #if defined(CONFIG_USER_ONLY)
326 size_t len = sizeof(PageDesc) * L2_SIZE;
327 /* Don't use qemu_malloc because it may recurse. */
328 p = mmap(0, len, PROT_READ | PROT_WRITE,
329 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
330 *lp = p;
331 if (h2g_valid(p)) {
332 unsigned long addr = h2g(p);
333 page_set_flags(addr & TARGET_PAGE_MASK,
334 TARGET_PAGE_ALIGN(addr + len),
335 PAGE_RESERVED);
337 #else
338 p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
339 *lp = p;
340 #endif
342 return p + (index & (L2_SIZE - 1));
345 static inline PageDesc *page_find(target_ulong index)
347 PageDesc **lp, *p;
348 lp = page_l1_map(index);
349 if (!lp)
350 return NULL;
352 p = *lp;
353 if (!p)
354 return 0;
355 return p + (index & (L2_SIZE - 1));
358 static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
360 void **lp, **p;
361 PhysPageDesc *pd;
363 p = (void **)l1_phys_map;
364 #if TARGET_PHYS_ADDR_SPACE_BITS > 32
366 #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
367 #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
368 #endif
369 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
370 p = *lp;
371 if (!p) {
372 /* allocate if not found */
373 if (!alloc)
374 return NULL;
375 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
376 memset(p, 0, sizeof(void *) * L1_SIZE);
377 *lp = p;
379 #endif
380 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
381 pd = *lp;
382 if (!pd) {
383 int i;
384 /* allocate if not found */
385 if (!alloc)
386 return NULL;
387 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
388 *lp = pd;
389 for (i = 0; i < L2_SIZE; i++) {
390 pd[i].phys_offset = IO_MEM_UNASSIGNED;
391 pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
394 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
397 static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
399 return phys_page_find_alloc(index, 0);
402 #if !defined(CONFIG_USER_ONLY)
403 static void tlb_protect_code(ram_addr_t ram_addr);
404 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
405 target_ulong vaddr);
406 #define mmap_lock() do { } while(0)
407 #define mmap_unlock() do { } while(0)
408 #endif
410 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
412 #if defined(CONFIG_USER_ONLY)
413 /* Currently it is not recommended to allocate big chunks of data in
414 user mode. It will change when a dedicated libc will be used */
415 #define USE_STATIC_CODE_GEN_BUFFER
416 #endif
418 #ifdef USE_STATIC_CODE_GEN_BUFFER
419 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
420 #endif
422 static void code_gen_alloc(unsigned long tb_size)
424 if (kvm_enabled())
425 return;
427 #ifdef USE_STATIC_CODE_GEN_BUFFER
428 code_gen_buffer = static_code_gen_buffer;
429 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
430 map_exec(code_gen_buffer, code_gen_buffer_size);
431 #else
432 code_gen_buffer_size = tb_size;
433 if (code_gen_buffer_size == 0) {
434 #if defined(CONFIG_USER_ONLY)
435 /* in user mode, phys_ram_size is not meaningful */
436 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
437 #else
438 /* XXX: needs adjustments */
439 code_gen_buffer_size = (unsigned long)(ram_size / 4);
440 #endif
442 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
443 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
444 /* The code gen buffer location may have constraints depending on
445 the host cpu and OS */
446 #if defined(__linux__)
448 int flags;
449 void *start = NULL;
451 flags = MAP_PRIVATE | MAP_ANONYMOUS;
452 #if defined(__x86_64__)
453 flags |= MAP_32BIT;
454 /* Cannot map more than that */
455 if (code_gen_buffer_size > (800 * 1024 * 1024))
456 code_gen_buffer_size = (800 * 1024 * 1024);
457 #elif defined(__sparc_v9__)
458 // Map the buffer below 2G, so we can use direct calls and branches
459 flags |= MAP_FIXED;
460 start = (void *) 0x60000000UL;
461 if (code_gen_buffer_size > (512 * 1024 * 1024))
462 code_gen_buffer_size = (512 * 1024 * 1024);
463 #elif defined(__arm__)
464 /* Map the buffer below 32M, so we can use direct calls and branches */
465 flags |= MAP_FIXED;
466 start = (void *) 0x01000000UL;
467 if (code_gen_buffer_size > 16 * 1024 * 1024)
468 code_gen_buffer_size = 16 * 1024 * 1024;
469 #endif
470 code_gen_buffer = mmap(start, code_gen_buffer_size,
471 PROT_WRITE | PROT_READ | PROT_EXEC,
472 flags, -1, 0);
473 if (code_gen_buffer == MAP_FAILED) {
474 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
475 exit(1);
478 #elif defined(__FreeBSD__) || defined(__DragonFly__)
480 int flags;
481 void *addr = NULL;
482 flags = MAP_PRIVATE | MAP_ANONYMOUS;
483 #if defined(__x86_64__)
484 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
485 * 0x40000000 is free */
486 flags |= MAP_FIXED;
487 addr = (void *)0x40000000;
488 /* Cannot map more than that */
489 if (code_gen_buffer_size > (800 * 1024 * 1024))
490 code_gen_buffer_size = (800 * 1024 * 1024);
491 #endif
492 code_gen_buffer = mmap(addr, code_gen_buffer_size,
493 PROT_WRITE | PROT_READ | PROT_EXEC,
494 flags, -1, 0);
495 if (code_gen_buffer == MAP_FAILED) {
496 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
497 exit(1);
500 #else
501 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
502 map_exec(code_gen_buffer, code_gen_buffer_size);
503 #endif
504 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
505 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
506 code_gen_buffer_max_size = code_gen_buffer_size -
507 code_gen_max_block_size();
508 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
509 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
512 /* Must be called before using the QEMU cpus. 'tb_size' is the size
513 (in bytes) allocated to the translation buffer. Zero means default
514 size. */
515 void cpu_exec_init_all(unsigned long tb_size)
517 cpu_gen_init();
518 code_gen_alloc(tb_size);
519 code_gen_ptr = code_gen_buffer;
520 page_init();
521 #if !defined(CONFIG_USER_ONLY)
522 io_mem_init();
523 #endif
526 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
528 #define CPU_COMMON_SAVE_VERSION 1
530 static void cpu_common_save(QEMUFile *f, void *opaque)
532 CPUState *env = opaque;
534 cpu_synchronize_state(env, 0);
536 qemu_put_be32s(f, &env->halted);
537 qemu_put_be32s(f, &env->interrupt_request);
540 static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
542 CPUState *env = opaque;
544 if (version_id != CPU_COMMON_SAVE_VERSION)
545 return -EINVAL;
547 qemu_get_be32s(f, &env->halted);
548 qemu_get_be32s(f, &env->interrupt_request);
549 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
550 version_id is increased. */
551 env->interrupt_request &= ~0x01;
552 tlb_flush(env, 1);
553 cpu_synchronize_state(env, 1);
555 return 0;
557 #endif
559 CPUState *qemu_get_cpu(int cpu)
561 CPUState *env = first_cpu;
563 while (env) {
564 if (env->cpu_index == cpu)
565 break;
566 env = env->next_cpu;
569 return env;
572 void cpu_exec_init(CPUState *env)
574 CPUState **penv;
575 int cpu_index;
577 #if defined(CONFIG_USER_ONLY)
578 cpu_list_lock();
579 #endif
580 env->next_cpu = NULL;
581 penv = &first_cpu;
582 cpu_index = 0;
583 while (*penv != NULL) {
584 penv = &(*penv)->next_cpu;
585 cpu_index++;
587 env->cpu_index = cpu_index;
588 env->numa_node = 0;
589 TAILQ_INIT(&env->breakpoints);
590 TAILQ_INIT(&env->watchpoints);
591 #ifdef __WIN32
592 env->thread_id = GetCurrentProcessId();
593 #else
594 env->thread_id = getpid();
595 #endif
596 *penv = env;
597 #if defined(CONFIG_USER_ONLY)
598 cpu_list_unlock();
599 #endif
600 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
601 register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
602 cpu_common_save, cpu_common_load, env);
603 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
604 cpu_save, cpu_load, env);
605 #endif
608 static inline void invalidate_page_bitmap(PageDesc *p)
610 if (p->code_bitmap) {
611 qemu_free(p->code_bitmap);
612 p->code_bitmap = NULL;
614 p->code_write_count = 0;
617 /* set to NULL all the 'first_tb' fields in all PageDescs */
618 static void page_flush_tb(void)
620 int i, j;
621 PageDesc *p;
623 for(i = 0; i < L1_SIZE; i++) {
624 p = l1_map[i];
625 if (p) {
626 for(j = 0; j < L2_SIZE; j++) {
627 p->first_tb = NULL;
628 invalidate_page_bitmap(p);
629 p++;
635 /* flush all the translation blocks */
636 /* XXX: tb_flush is currently not thread safe */
637 void tb_flush(CPUState *env1)
639 CPUState *env;
640 #if defined(DEBUG_FLUSH)
641 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
642 (unsigned long)(code_gen_ptr - code_gen_buffer),
643 nb_tbs, nb_tbs > 0 ?
644 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
645 #endif
646 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
647 cpu_abort(env1, "Internal error: code buffer overflow\n");
649 nb_tbs = 0;
651 for(env = first_cpu; env != NULL; env = env->next_cpu) {
652 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
655 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
656 page_flush_tb();
658 code_gen_ptr = code_gen_buffer;
659 /* XXX: flush processor icache at this point if cache flush is
660 expensive */
661 tb_flush_count++;
664 #ifdef DEBUG_TB_CHECK
666 static void tb_invalidate_check(target_ulong address)
668 TranslationBlock *tb;
669 int i;
670 address &= TARGET_PAGE_MASK;
671 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
672 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
673 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
674 address >= tb->pc + tb->size)) {
675 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
676 address, (long)tb->pc, tb->size);
682 /* verify that all the pages have correct rights for code */
683 static void tb_page_check(void)
685 TranslationBlock *tb;
686 int i, flags1, flags2;
688 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
689 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
690 flags1 = page_get_flags(tb->pc);
691 flags2 = page_get_flags(tb->pc + tb->size - 1);
692 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
693 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
694 (long)tb->pc, tb->size, flags1, flags2);
700 static void tb_jmp_check(TranslationBlock *tb)
702 TranslationBlock *tb1;
703 unsigned int n1;
705 /* suppress any remaining jumps to this TB */
706 tb1 = tb->jmp_first;
707 for(;;) {
708 n1 = (long)tb1 & 3;
709 tb1 = (TranslationBlock *)((long)tb1 & ~3);
710 if (n1 == 2)
711 break;
712 tb1 = tb1->jmp_next[n1];
714 /* check end of list */
715 if (tb1 != tb) {
716 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
720 #endif
722 /* invalidate one TB */
723 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
724 int next_offset)
726 TranslationBlock *tb1;
727 for(;;) {
728 tb1 = *ptb;
729 if (tb1 == tb) {
730 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
731 break;
733 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
737 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
739 TranslationBlock *tb1;
740 unsigned int n1;
742 for(;;) {
743 tb1 = *ptb;
744 n1 = (long)tb1 & 3;
745 tb1 = (TranslationBlock *)((long)tb1 & ~3);
746 if (tb1 == tb) {
747 *ptb = tb1->page_next[n1];
748 break;
750 ptb = &tb1->page_next[n1];
754 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
756 TranslationBlock *tb1, **ptb;
757 unsigned int n1;
759 ptb = &tb->jmp_next[n];
760 tb1 = *ptb;
761 if (tb1) {
762 /* find tb(n) in circular list */
763 for(;;) {
764 tb1 = *ptb;
765 n1 = (long)tb1 & 3;
766 tb1 = (TranslationBlock *)((long)tb1 & ~3);
767 if (n1 == n && tb1 == tb)
768 break;
769 if (n1 == 2) {
770 ptb = &tb1->jmp_first;
771 } else {
772 ptb = &tb1->jmp_next[n1];
775 /* now we can suppress tb(n) from the list */
776 *ptb = tb->jmp_next[n];
778 tb->jmp_next[n] = NULL;
782 /* reset the jump entry 'n' of a TB so that it is not chained to
783 another TB */
784 static inline void tb_reset_jump(TranslationBlock *tb, int n)
786 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
789 void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
791 CPUState *env;
792 PageDesc *p;
793 unsigned int h, n1;
794 target_phys_addr_t phys_pc;
795 TranslationBlock *tb1, *tb2;
797 /* remove the TB from the hash list */
798 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
799 h = tb_phys_hash_func(phys_pc);
800 tb_remove(&tb_phys_hash[h], tb,
801 offsetof(TranslationBlock, phys_hash_next));
803 /* remove the TB from the page list */
804 if (tb->page_addr[0] != page_addr) {
805 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
806 tb_page_remove(&p->first_tb, tb);
807 invalidate_page_bitmap(p);
809 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
810 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
811 tb_page_remove(&p->first_tb, tb);
812 invalidate_page_bitmap(p);
815 tb_invalidated_flag = 1;
817 /* remove the TB from the hash list */
818 h = tb_jmp_cache_hash_func(tb->pc);
819 for(env = first_cpu; env != NULL; env = env->next_cpu) {
820 if (env->tb_jmp_cache[h] == tb)
821 env->tb_jmp_cache[h] = NULL;
824 /* suppress this TB from the two jump lists */
825 tb_jmp_remove(tb, 0);
826 tb_jmp_remove(tb, 1);
828 /* suppress any remaining jumps to this TB */
829 tb1 = tb->jmp_first;
830 for(;;) {
831 n1 = (long)tb1 & 3;
832 if (n1 == 2)
833 break;
834 tb1 = (TranslationBlock *)((long)tb1 & ~3);
835 tb2 = tb1->jmp_next[n1];
836 tb_reset_jump(tb1, n1);
837 tb1->jmp_next[n1] = NULL;
838 tb1 = tb2;
840 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
842 tb_phys_invalidate_count++;
845 static inline void set_bits(uint8_t *tab, int start, int len)
847 int end, mask, end1;
849 end = start + len;
850 tab += start >> 3;
851 mask = 0xff << (start & 7);
852 if ((start & ~7) == (end & ~7)) {
853 if (start < end) {
854 mask &= ~(0xff << (end & 7));
855 *tab |= mask;
857 } else {
858 *tab++ |= mask;
859 start = (start + 8) & ~7;
860 end1 = end & ~7;
861 while (start < end1) {
862 *tab++ = 0xff;
863 start += 8;
865 if (start < end) {
866 mask = ~(0xff << (end & 7));
867 *tab |= mask;
872 static void build_page_bitmap(PageDesc *p)
874 int n, tb_start, tb_end;
875 TranslationBlock *tb;
877 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
879 tb = p->first_tb;
880 while (tb != NULL) {
881 n = (long)tb & 3;
882 tb = (TranslationBlock *)((long)tb & ~3);
883 /* NOTE: this is subtle as a TB may span two physical pages */
884 if (n == 0) {
885 /* NOTE: tb_end may be after the end of the page, but
886 it is not a problem */
887 tb_start = tb->pc & ~TARGET_PAGE_MASK;
888 tb_end = tb_start + tb->size;
889 if (tb_end > TARGET_PAGE_SIZE)
890 tb_end = TARGET_PAGE_SIZE;
891 } else {
892 tb_start = 0;
893 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
895 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
896 tb = tb->page_next[n];
900 TranslationBlock *tb_gen_code(CPUState *env,
901 target_ulong pc, target_ulong cs_base,
902 int flags, int cflags)
904 TranslationBlock *tb;
905 uint8_t *tc_ptr;
906 target_ulong phys_pc, phys_page2, virt_page2;
907 int code_gen_size;
909 phys_pc = get_phys_addr_code(env, pc);
910 tb = tb_alloc(pc);
911 if (!tb) {
912 /* flush must be done */
913 tb_flush(env);
914 /* cannot fail at this point */
915 tb = tb_alloc(pc);
916 /* Don't forget to invalidate previous TB info. */
917 tb_invalidated_flag = 1;
919 tc_ptr = code_gen_ptr;
920 tb->tc_ptr = tc_ptr;
921 tb->cs_base = cs_base;
922 tb->flags = flags;
923 tb->cflags = cflags;
924 cpu_gen_code(env, tb, &code_gen_size);
925 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
927 /* check next page if needed */
928 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
929 phys_page2 = -1;
930 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
931 phys_page2 = get_phys_addr_code(env, virt_page2);
933 tb_link_phys(tb, phys_pc, phys_page2);
934 return tb;
937 /* invalidate all TBs which intersect with the target physical page
938 starting in range [start;end[. NOTE: start and end must refer to
939 the same physical page. 'is_cpu_write_access' should be true if called
940 from a real cpu write access: the virtual CPU will exit the current
941 TB if code is modified inside this TB. */
942 void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
943 int is_cpu_write_access)
945 TranslationBlock *tb, *tb_next, *saved_tb;
946 CPUState *env = cpu_single_env;
947 target_ulong tb_start, tb_end;
948 PageDesc *p;
949 int n;
950 #ifdef TARGET_HAS_PRECISE_SMC
951 int current_tb_not_found = is_cpu_write_access;
952 TranslationBlock *current_tb = NULL;
953 int current_tb_modified = 0;
954 target_ulong current_pc = 0;
955 target_ulong current_cs_base = 0;
956 int current_flags = 0;
957 #endif /* TARGET_HAS_PRECISE_SMC */
959 p = page_find(start >> TARGET_PAGE_BITS);
960 if (!p)
961 return;
962 if (!p->code_bitmap &&
963 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
964 is_cpu_write_access) {
965 /* build code bitmap */
966 build_page_bitmap(p);
969 /* we remove all the TBs in the range [start, end[ */
970 /* XXX: see if in some cases it could be faster to invalidate all the code */
971 tb = p->first_tb;
972 while (tb != NULL) {
973 n = (long)tb & 3;
974 tb = (TranslationBlock *)((long)tb & ~3);
975 tb_next = tb->page_next[n];
976 /* NOTE: this is subtle as a TB may span two physical pages */
977 if (n == 0) {
978 /* NOTE: tb_end may be after the end of the page, but
979 it is not a problem */
980 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
981 tb_end = tb_start + tb->size;
982 } else {
983 tb_start = tb->page_addr[1];
984 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
986 if (!(tb_end <= start || tb_start >= end)) {
987 #ifdef TARGET_HAS_PRECISE_SMC
988 if (current_tb_not_found) {
989 current_tb_not_found = 0;
990 current_tb = NULL;
991 if (env->mem_io_pc) {
992 /* now we have a real cpu fault */
993 current_tb = tb_find_pc(env->mem_io_pc);
996 if (current_tb == tb &&
997 (current_tb->cflags & CF_COUNT_MASK) != 1) {
998 /* If we are modifying the current TB, we must stop
999 its execution. We could be more precise by checking
1000 that the modification is after the current PC, but it
1001 would require a specialized function to partially
1002 restore the CPU state */
1004 current_tb_modified = 1;
1005 cpu_restore_state(current_tb, env,
1006 env->mem_io_pc, NULL);
1007 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1008 &current_flags);
1010 #endif /* TARGET_HAS_PRECISE_SMC */
1011 /* we need to do that to handle the case where a signal
1012 occurs while doing tb_phys_invalidate() */
1013 saved_tb = NULL;
1014 if (env) {
1015 saved_tb = env->current_tb;
1016 env->current_tb = NULL;
1018 tb_phys_invalidate(tb, -1);
1019 if (env) {
1020 env->current_tb = saved_tb;
1021 if (env->interrupt_request && env->current_tb)
1022 cpu_interrupt(env, env->interrupt_request);
1025 tb = tb_next;
1027 #if !defined(CONFIG_USER_ONLY)
1028 /* if no code remaining, no need to continue to use slow writes */
1029 if (!p->first_tb) {
1030 invalidate_page_bitmap(p);
1031 if (is_cpu_write_access) {
1032 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
1035 #endif
1036 #ifdef TARGET_HAS_PRECISE_SMC
1037 if (current_tb_modified) {
1038 /* we generate a block containing just the instruction
1039 modifying the memory. It will ensure that it cannot modify
1040 itself */
1041 env->current_tb = NULL;
1042 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1043 cpu_resume_from_signal(env, NULL);
1045 #endif
1048 /* len must be <= 8 and start must be a multiple of len */
1049 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
1051 PageDesc *p;
1052 int offset, b;
1053 #if 0
1054 if (1) {
1055 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1056 cpu_single_env->mem_io_vaddr, len,
1057 cpu_single_env->eip,
1058 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
1060 #endif
1061 p = page_find(start >> TARGET_PAGE_BITS);
1062 if (!p)
1063 return;
1064 if (p->code_bitmap) {
1065 offset = start & ~TARGET_PAGE_MASK;
1066 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1067 if (b & ((1 << len) - 1))
1068 goto do_invalidate;
1069 } else {
1070 do_invalidate:
1071 tb_invalidate_phys_page_range(start, start + len, 1);
1075 #if !defined(CONFIG_SOFTMMU)
1076 static void tb_invalidate_phys_page(target_phys_addr_t addr,
1077 unsigned long pc, void *puc)
1079 TranslationBlock *tb;
1080 PageDesc *p;
1081 int n;
1082 #ifdef TARGET_HAS_PRECISE_SMC
1083 TranslationBlock *current_tb = NULL;
1084 CPUState *env = cpu_single_env;
1085 int current_tb_modified = 0;
1086 target_ulong current_pc = 0;
1087 target_ulong current_cs_base = 0;
1088 int current_flags = 0;
1089 #endif
1091 addr &= TARGET_PAGE_MASK;
1092 p = page_find(addr >> TARGET_PAGE_BITS);
1093 if (!p)
1094 return;
1095 tb = p->first_tb;
1096 #ifdef TARGET_HAS_PRECISE_SMC
1097 if (tb && pc != 0) {
1098 current_tb = tb_find_pc(pc);
1100 #endif
1101 while (tb != NULL) {
1102 n = (long)tb & 3;
1103 tb = (TranslationBlock *)((long)tb & ~3);
1104 #ifdef TARGET_HAS_PRECISE_SMC
1105 if (current_tb == tb &&
1106 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1107 /* If we are modifying the current TB, we must stop
1108 its execution. We could be more precise by checking
1109 that the modification is after the current PC, but it
1110 would require a specialized function to partially
1111 restore the CPU state */
1113 current_tb_modified = 1;
1114 cpu_restore_state(current_tb, env, pc, puc);
1115 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1116 &current_flags);
1118 #endif /* TARGET_HAS_PRECISE_SMC */
1119 tb_phys_invalidate(tb, addr);
1120 tb = tb->page_next[n];
1122 p->first_tb = NULL;
1123 #ifdef TARGET_HAS_PRECISE_SMC
1124 if (current_tb_modified) {
1125 /* we generate a block containing just the instruction
1126 modifying the memory. It will ensure that it cannot modify
1127 itself */
1128 env->current_tb = NULL;
1129 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1130 cpu_resume_from_signal(env, puc);
1132 #endif
1134 #endif
1136 /* add the tb in the target page and protect it if necessary */
1137 static inline void tb_alloc_page(TranslationBlock *tb,
1138 unsigned int n, target_ulong page_addr)
1140 PageDesc *p;
1141 TranslationBlock *last_first_tb;
1143 tb->page_addr[n] = page_addr;
1144 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
1145 tb->page_next[n] = p->first_tb;
1146 last_first_tb = p->first_tb;
1147 p->first_tb = (TranslationBlock *)((long)tb | n);
1148 invalidate_page_bitmap(p);
1150 #if defined(TARGET_HAS_SMC) || 1
1152 #if defined(CONFIG_USER_ONLY)
1153 if (p->flags & PAGE_WRITE) {
1154 target_ulong addr;
1155 PageDesc *p2;
1156 int prot;
1158 /* force the host page as non writable (writes will have a
1159 page fault + mprotect overhead) */
1160 page_addr &= qemu_host_page_mask;
1161 prot = 0;
1162 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1163 addr += TARGET_PAGE_SIZE) {
1165 p2 = page_find (addr >> TARGET_PAGE_BITS);
1166 if (!p2)
1167 continue;
1168 prot |= p2->flags;
1169 p2->flags &= ~PAGE_WRITE;
1170 page_get_flags(addr);
1172 mprotect(g2h(page_addr), qemu_host_page_size,
1173 (prot & PAGE_BITS) & ~PAGE_WRITE);
1174 #ifdef DEBUG_TB_INVALIDATE
1175 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1176 page_addr);
1177 #endif
1179 #else
1180 /* if some code is already present, then the pages are already
1181 protected. So we handle the case where only the first TB is
1182 allocated in a physical page */
1183 if (!last_first_tb) {
1184 tlb_protect_code(page_addr);
1186 #endif
1188 #endif /* TARGET_HAS_SMC */
1191 /* Allocate a new translation block. Flush the translation buffer if
1192 too many translation blocks or too much generated code. */
1193 TranslationBlock *tb_alloc(target_ulong pc)
1195 TranslationBlock *tb;
1197 if (nb_tbs >= code_gen_max_blocks ||
1198 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
1199 return NULL;
1200 tb = &tbs[nb_tbs++];
1201 tb->pc = pc;
1202 tb->cflags = 0;
1203 return tb;
1206 void tb_free(TranslationBlock *tb)
1208 /* In practice this is mostly used for single use temporary TB
1209 Ignore the hard cases and just back up if this TB happens to
1210 be the last one generated. */
1211 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1212 code_gen_ptr = tb->tc_ptr;
1213 nb_tbs--;
1217 /* add a new TB and link it to the physical page tables. phys_page2 is
1218 (-1) to indicate that only one page contains the TB. */
1219 void tb_link_phys(TranslationBlock *tb,
1220 target_ulong phys_pc, target_ulong phys_page2)
1222 unsigned int h;
1223 TranslationBlock **ptb;
1225 /* Grab the mmap lock to stop another thread invalidating this TB
1226 before we are done. */
1227 mmap_lock();
1228 /* add in the physical hash table */
1229 h = tb_phys_hash_func(phys_pc);
1230 ptb = &tb_phys_hash[h];
1231 tb->phys_hash_next = *ptb;
1232 *ptb = tb;
1234 /* add in the page list */
1235 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1236 if (phys_page2 != -1)
1237 tb_alloc_page(tb, 1, phys_page2);
1238 else
1239 tb->page_addr[1] = -1;
1241 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1242 tb->jmp_next[0] = NULL;
1243 tb->jmp_next[1] = NULL;
1245 /* init original jump addresses */
1246 if (tb->tb_next_offset[0] != 0xffff)
1247 tb_reset_jump(tb, 0);
1248 if (tb->tb_next_offset[1] != 0xffff)
1249 tb_reset_jump(tb, 1);
1251 #ifdef DEBUG_TB_CHECK
1252 tb_page_check();
1253 #endif
1254 mmap_unlock();
1257 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1258 tb[1].tc_ptr. Return NULL if not found */
1259 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1261 int m_min, m_max, m;
1262 unsigned long v;
1263 TranslationBlock *tb;
1265 if (nb_tbs <= 0)
1266 return NULL;
1267 if (tc_ptr < (unsigned long)code_gen_buffer ||
1268 tc_ptr >= (unsigned long)code_gen_ptr)
1269 return NULL;
1270 /* binary search (cf Knuth) */
1271 m_min = 0;
1272 m_max = nb_tbs - 1;
1273 while (m_min <= m_max) {
1274 m = (m_min + m_max) >> 1;
1275 tb = &tbs[m];
1276 v = (unsigned long)tb->tc_ptr;
1277 if (v == tc_ptr)
1278 return tb;
1279 else if (tc_ptr < v) {
1280 m_max = m - 1;
1281 } else {
1282 m_min = m + 1;
1285 return &tbs[m_max];
1288 static void tb_reset_jump_recursive(TranslationBlock *tb);
1290 static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1292 TranslationBlock *tb1, *tb_next, **ptb;
1293 unsigned int n1;
1295 tb1 = tb->jmp_next[n];
1296 if (tb1 != NULL) {
1297 /* find head of list */
1298 for(;;) {
1299 n1 = (long)tb1 & 3;
1300 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1301 if (n1 == 2)
1302 break;
1303 tb1 = tb1->jmp_next[n1];
1305 /* we are now sure now that tb jumps to tb1 */
1306 tb_next = tb1;
1308 /* remove tb from the jmp_first list */
1309 ptb = &tb_next->jmp_first;
1310 for(;;) {
1311 tb1 = *ptb;
1312 n1 = (long)tb1 & 3;
1313 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1314 if (n1 == n && tb1 == tb)
1315 break;
1316 ptb = &tb1->jmp_next[n1];
1318 *ptb = tb->jmp_next[n];
1319 tb->jmp_next[n] = NULL;
1321 /* suppress the jump to next tb in generated code */
1322 tb_reset_jump(tb, n);
1324 /* suppress jumps in the tb on which we could have jumped */
1325 tb_reset_jump_recursive(tb_next);
1329 static void tb_reset_jump_recursive(TranslationBlock *tb)
1331 tb_reset_jump_recursive2(tb, 0);
1332 tb_reset_jump_recursive2(tb, 1);
1335 #if defined(TARGET_HAS_ICE)
1336 static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1338 target_phys_addr_t addr;
1339 target_ulong pd;
1340 ram_addr_t ram_addr;
1341 PhysPageDesc *p;
1343 addr = cpu_get_phys_page_debug(env, pc);
1344 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1345 if (!p) {
1346 pd = IO_MEM_UNASSIGNED;
1347 } else {
1348 pd = p->phys_offset;
1350 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
1351 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1353 #endif
1355 /* Add a watchpoint. */
1356 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1357 int flags, CPUWatchpoint **watchpoint)
1359 target_ulong len_mask = ~(len - 1);
1360 CPUWatchpoint *wp;
1362 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1363 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1364 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1365 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1366 return -EINVAL;
1368 wp = qemu_malloc(sizeof(*wp));
1370 wp->vaddr = addr;
1371 wp->len_mask = len_mask;
1372 wp->flags = flags;
1374 /* keep all GDB-injected watchpoints in front */
1375 if (flags & BP_GDB)
1376 TAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
1377 else
1378 TAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
1380 tlb_flush_page(env, addr);
1382 if (watchpoint)
1383 *watchpoint = wp;
1384 return 0;
1387 /* Remove a specific watchpoint. */
1388 int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1389 int flags)
1391 target_ulong len_mask = ~(len - 1);
1392 CPUWatchpoint *wp;
1394 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
1395 if (addr == wp->vaddr && len_mask == wp->len_mask
1396 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
1397 cpu_watchpoint_remove_by_ref(env, wp);
1398 return 0;
1401 return -ENOENT;
1404 /* Remove a specific watchpoint by reference. */
1405 void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1407 TAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
1409 tlb_flush_page(env, watchpoint->vaddr);
1411 qemu_free(watchpoint);
1414 /* Remove all matching watchpoints. */
1415 void cpu_watchpoint_remove_all(CPUState *env, int mask)
1417 CPUWatchpoint *wp, *next;
1419 TAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
1420 if (wp->flags & mask)
1421 cpu_watchpoint_remove_by_ref(env, wp);
1425 /* Add a breakpoint. */
1426 int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1427 CPUBreakpoint **breakpoint)
1429 #if defined(TARGET_HAS_ICE)
1430 CPUBreakpoint *bp;
1432 bp = qemu_malloc(sizeof(*bp));
1434 bp->pc = pc;
1435 bp->flags = flags;
1437 /* keep all GDB-injected breakpoints in front */
1438 if (flags & BP_GDB)
1439 TAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
1440 else
1441 TAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
1443 breakpoint_invalidate(env, pc);
1445 if (breakpoint)
1446 *breakpoint = bp;
1447 return 0;
1448 #else
1449 return -ENOSYS;
1450 #endif
1453 /* Remove a specific breakpoint. */
1454 int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1456 #if defined(TARGET_HAS_ICE)
1457 CPUBreakpoint *bp;
1459 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
1460 if (bp->pc == pc && bp->flags == flags) {
1461 cpu_breakpoint_remove_by_ref(env, bp);
1462 return 0;
1465 return -ENOENT;
1466 #else
1467 return -ENOSYS;
1468 #endif
1471 /* Remove a specific breakpoint by reference. */
1472 void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
1474 #if defined(TARGET_HAS_ICE)
1475 TAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
1477 breakpoint_invalidate(env, breakpoint->pc);
1479 qemu_free(breakpoint);
1480 #endif
1483 /* Remove all matching breakpoints. */
1484 void cpu_breakpoint_remove_all(CPUState *env, int mask)
1486 #if defined(TARGET_HAS_ICE)
1487 CPUBreakpoint *bp, *next;
1489 TAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
1490 if (bp->flags & mask)
1491 cpu_breakpoint_remove_by_ref(env, bp);
1493 #endif
1496 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1497 CPU loop after each instruction */
1498 void cpu_single_step(CPUState *env, int enabled)
1500 #if defined(TARGET_HAS_ICE)
1501 if (env->singlestep_enabled != enabled) {
1502 env->singlestep_enabled = enabled;
1503 if (kvm_enabled())
1504 kvm_update_guest_debug(env, 0);
1505 else {
1506 /* must flush all the translated code to avoid inconsistencies */
1507 /* XXX: only flush what is necessary */
1508 tb_flush(env);
1511 #endif
1514 /* enable or disable low levels log */
1515 void cpu_set_log(int log_flags)
1517 loglevel = log_flags;
1518 if (loglevel && !logfile) {
1519 logfile = fopen(logfilename, log_append ? "a" : "w");
1520 if (!logfile) {
1521 perror(logfilename);
1522 _exit(1);
1524 #if !defined(CONFIG_SOFTMMU)
1525 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1527 static char logfile_buf[4096];
1528 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1530 #else
1531 setvbuf(logfile, NULL, _IOLBF, 0);
1532 #endif
1533 log_append = 1;
1535 if (!loglevel && logfile) {
1536 fclose(logfile);
1537 logfile = NULL;
1541 void cpu_set_log_filename(const char *filename)
1543 logfilename = strdup(filename);
1544 if (logfile) {
1545 fclose(logfile);
1546 logfile = NULL;
1548 cpu_set_log(loglevel);
1551 static void cpu_unlink_tb(CPUState *env)
1553 #if defined(USE_NPTL)
1554 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1555 problem and hope the cpu will stop of its own accord. For userspace
1556 emulation this often isn't actually as bad as it sounds. Often
1557 signals are used primarily to interrupt blocking syscalls. */
1558 #else
1559 TranslationBlock *tb;
1560 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
1562 tb = env->current_tb;
1563 /* if the cpu is currently executing code, we must unlink it and
1564 all the potentially executing TB */
1565 if (tb && !testandset(&interrupt_lock)) {
1566 env->current_tb = NULL;
1567 tb_reset_jump_recursive(tb);
1568 resetlock(&interrupt_lock);
1570 #endif
1573 /* mask must never be zero, except for A20 change call */
1574 void cpu_interrupt(CPUState *env, int mask)
1576 int old_mask;
1578 old_mask = env->interrupt_request;
1579 env->interrupt_request |= mask;
1580 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
1581 kvm_update_interrupt_request(env);
1583 #ifndef CONFIG_USER_ONLY
1585 * If called from iothread context, wake the target cpu in
1586 * case its halted.
1588 if (!qemu_cpu_self(env)) {
1589 qemu_cpu_kick(env);
1590 return;
1592 #endif
1594 if (use_icount) {
1595 env->icount_decr.u16.high = 0xffff;
1596 #ifndef CONFIG_USER_ONLY
1597 if (!can_do_io(env)
1598 && (mask & ~old_mask) != 0) {
1599 cpu_abort(env, "Raised interrupt while not in I/O function");
1601 #endif
1602 } else {
1603 cpu_unlink_tb(env);
1607 void cpu_reset_interrupt(CPUState *env, int mask)
1609 env->interrupt_request &= ~mask;
1612 void cpu_exit(CPUState *env)
1614 env->exit_request = 1;
1615 cpu_unlink_tb(env);
1618 const CPULogItem cpu_log_items[] = {
1619 { CPU_LOG_TB_OUT_ASM, "out_asm",
1620 "show generated host assembly code for each compiled TB" },
1621 { CPU_LOG_TB_IN_ASM, "in_asm",
1622 "show target assembly code for each compiled TB" },
1623 { CPU_LOG_TB_OP, "op",
1624 "show micro ops for each compiled TB" },
1625 { CPU_LOG_TB_OP_OPT, "op_opt",
1626 "show micro ops "
1627 #ifdef TARGET_I386
1628 "before eflags optimization and "
1629 #endif
1630 "after liveness analysis" },
1631 { CPU_LOG_INT, "int",
1632 "show interrupts/exceptions in short format" },
1633 { CPU_LOG_EXEC, "exec",
1634 "show trace before each executed TB (lots of logs)" },
1635 { CPU_LOG_TB_CPU, "cpu",
1636 "show CPU state before block translation" },
1637 #ifdef TARGET_I386
1638 { CPU_LOG_PCALL, "pcall",
1639 "show protected mode far calls/returns/exceptions" },
1640 { CPU_LOG_RESET, "cpu_reset",
1641 "show CPU state before CPU resets" },
1642 #endif
1643 #ifdef DEBUG_IOPORT
1644 { CPU_LOG_IOPORT, "ioport",
1645 "show all i/o ports accesses" },
1646 #endif
1647 { 0, NULL, NULL },
1650 static int cmp1(const char *s1, int n, const char *s2)
1652 if (strlen(s2) != n)
1653 return 0;
1654 return memcmp(s1, s2, n) == 0;
1657 /* takes a comma separated list of log masks. Return 0 if error. */
1658 int cpu_str_to_log_mask(const char *str)
1660 const CPULogItem *item;
1661 int mask;
1662 const char *p, *p1;
1664 p = str;
1665 mask = 0;
1666 for(;;) {
1667 p1 = strchr(p, ',');
1668 if (!p1)
1669 p1 = p + strlen(p);
1670 if(cmp1(p,p1-p,"all")) {
1671 for(item = cpu_log_items; item->mask != 0; item++) {
1672 mask |= item->mask;
1674 } else {
1675 for(item = cpu_log_items; item->mask != 0; item++) {
1676 if (cmp1(p, p1 - p, item->name))
1677 goto found;
1679 return 0;
1681 found:
1682 mask |= item->mask;
1683 if (*p1 != ',')
1684 break;
1685 p = p1 + 1;
1687 return mask;
1690 void cpu_abort(CPUState *env, const char *fmt, ...)
1692 va_list ap;
1693 va_list ap2;
1695 va_start(ap, fmt);
1696 va_copy(ap2, ap);
1697 fprintf(stderr, "qemu: fatal: ");
1698 vfprintf(stderr, fmt, ap);
1699 fprintf(stderr, "\n");
1700 #ifdef TARGET_I386
1701 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1702 #else
1703 cpu_dump_state(env, stderr, fprintf, 0);
1704 #endif
1705 if (qemu_log_enabled()) {
1706 qemu_log("qemu: fatal: ");
1707 qemu_log_vprintf(fmt, ap2);
1708 qemu_log("\n");
1709 #ifdef TARGET_I386
1710 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
1711 #else
1712 log_cpu_state(env, 0);
1713 #endif
1714 qemu_log_flush();
1715 qemu_log_close();
1717 va_end(ap2);
1718 va_end(ap);
1719 abort();
1722 CPUState *cpu_copy(CPUState *env)
1724 CPUState *new_env = cpu_init(env->cpu_model_str);
1725 CPUState *next_cpu = new_env->next_cpu;
1726 int cpu_index = new_env->cpu_index;
1727 #if defined(TARGET_HAS_ICE)
1728 CPUBreakpoint *bp;
1729 CPUWatchpoint *wp;
1730 #endif
1732 memcpy(new_env, env, sizeof(CPUState));
1734 /* Preserve chaining and index. */
1735 new_env->next_cpu = next_cpu;
1736 new_env->cpu_index = cpu_index;
1738 /* Clone all break/watchpoints.
1739 Note: Once we support ptrace with hw-debug register access, make sure
1740 BP_CPU break/watchpoints are handled correctly on clone. */
1741 TAILQ_INIT(&env->breakpoints);
1742 TAILQ_INIT(&env->watchpoints);
1743 #if defined(TARGET_HAS_ICE)
1744 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
1745 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1747 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
1748 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1749 wp->flags, NULL);
1751 #endif
1753 return new_env;
1756 #if !defined(CONFIG_USER_ONLY)
1758 static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1760 unsigned int i;
1762 /* Discard jump cache entries for any tb which might potentially
1763 overlap the flushed page. */
1764 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1765 memset (&env->tb_jmp_cache[i], 0,
1766 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1768 i = tb_jmp_cache_hash_page(addr);
1769 memset (&env->tb_jmp_cache[i], 0,
1770 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1773 static CPUTLBEntry s_cputlb_empty_entry = {
1774 .addr_read = -1,
1775 .addr_write = -1,
1776 .addr_code = -1,
1777 .addend = -1,
1780 /* NOTE: if flush_global is true, also flush global entries (not
1781 implemented yet) */
1782 void tlb_flush(CPUState *env, int flush_global)
1784 int i;
1786 #if defined(DEBUG_TLB)
1787 printf("tlb_flush:\n");
1788 #endif
1789 /* must reset current TB so that interrupts cannot modify the
1790 links while we are modifying them */
1791 env->current_tb = NULL;
1793 for(i = 0; i < CPU_TLB_SIZE; i++) {
1794 int mmu_idx;
1795 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1796 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
1800 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
1802 #ifdef CONFIG_KQEMU
1803 if (env->kqemu_enabled) {
1804 kqemu_flush(env, flush_global);
1806 #endif
1807 tlb_flush_count++;
1810 static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
1812 if (addr == (tlb_entry->addr_read &
1813 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1814 addr == (tlb_entry->addr_write &
1815 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1816 addr == (tlb_entry->addr_code &
1817 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1818 *tlb_entry = s_cputlb_empty_entry;
1822 void tlb_flush_page(CPUState *env, target_ulong addr)
1824 int i;
1825 int mmu_idx;
1827 #if defined(DEBUG_TLB)
1828 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
1829 #endif
1830 /* must reset current TB so that interrupts cannot modify the
1831 links while we are modifying them */
1832 env->current_tb = NULL;
1834 addr &= TARGET_PAGE_MASK;
1835 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1836 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1837 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
1839 tlb_flush_jmp_cache(env, addr);
1841 #ifdef CONFIG_KQEMU
1842 if (env->kqemu_enabled) {
1843 kqemu_flush_page(env, addr);
1845 #endif
1848 /* update the TLBs so that writes to code in the virtual page 'addr'
1849 can be detected */
1850 static void tlb_protect_code(ram_addr_t ram_addr)
1852 cpu_physical_memory_reset_dirty(ram_addr,
1853 ram_addr + TARGET_PAGE_SIZE,
1854 CODE_DIRTY_FLAG);
1857 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1858 tested for self modifying code */
1859 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
1860 target_ulong vaddr)
1862 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
1865 static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
1866 unsigned long start, unsigned long length)
1868 unsigned long addr;
1869 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1870 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
1871 if ((addr - start) < length) {
1872 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
1877 /* Note: start and end must be within the same ram block. */
1878 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
1879 int dirty_flags)
1881 CPUState *env;
1882 unsigned long length, start1;
1883 int i, mask, len;
1884 uint8_t *p;
1886 start &= TARGET_PAGE_MASK;
1887 end = TARGET_PAGE_ALIGN(end);
1889 length = end - start;
1890 if (length == 0)
1891 return;
1892 len = length >> TARGET_PAGE_BITS;
1893 #ifdef CONFIG_KQEMU
1894 /* XXX: should not depend on cpu context */
1895 env = first_cpu;
1896 if (env->kqemu_enabled) {
1897 ram_addr_t addr;
1898 addr = start;
1899 for(i = 0; i < len; i++) {
1900 kqemu_set_notdirty(env, addr);
1901 addr += TARGET_PAGE_SIZE;
1904 #endif
1905 mask = ~dirty_flags;
1906 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1907 for(i = 0; i < len; i++)
1908 p[i] &= mask;
1910 /* we modify the TLB cache so that the dirty bit will be set again
1911 when accessing the range */
1912 start1 = (unsigned long)qemu_get_ram_ptr(start);
1913 /* Chek that we don't span multiple blocks - this breaks the
1914 address comparisons below. */
1915 if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1
1916 != (end - 1) - start) {
1917 abort();
1920 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1921 int mmu_idx;
1922 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1923 for(i = 0; i < CPU_TLB_SIZE; i++)
1924 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
1925 start1, length);
1930 int cpu_physical_memory_set_dirty_tracking(int enable)
1932 if (kvm_enabled()) {
1933 return kvm_set_migration_log(enable);
1935 return 0;
1938 int cpu_physical_memory_get_dirty_tracking(void)
1940 return in_migration;
1943 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1944 target_phys_addr_t end_addr)
1946 int ret = 0;
1948 if (kvm_enabled())
1949 ret = kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
1950 return ret;
1953 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1955 ram_addr_t ram_addr;
1956 void *p;
1958 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1959 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
1960 + tlb_entry->addend);
1961 ram_addr = qemu_ram_addr_from_host(p);
1962 if (!cpu_physical_memory_is_dirty(ram_addr)) {
1963 tlb_entry->addr_write |= TLB_NOTDIRTY;
1968 /* update the TLB according to the current state of the dirty bits */
1969 void cpu_tlb_update_dirty(CPUState *env)
1971 int i;
1972 int mmu_idx;
1973 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1974 for(i = 0; i < CPU_TLB_SIZE; i++)
1975 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
1979 static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
1981 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1982 tlb_entry->addr_write = vaddr;
1985 /* update the TLB corresponding to virtual page vaddr
1986 so that it is no longer dirty */
1987 static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
1989 int i;
1990 int mmu_idx;
1992 vaddr &= TARGET_PAGE_MASK;
1993 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1994 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1995 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
1998 /* add a new TLB entry. At most one entry for a given virtual address
1999 is permitted. Return 0 if OK or 2 if the page could not be mapped
2000 (can only happen in non SOFTMMU mode for I/O pages or pages
2001 conflicting with the host address space). */
2002 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
2003 target_phys_addr_t paddr, int prot,
2004 int mmu_idx, int is_softmmu)
2006 PhysPageDesc *p;
2007 unsigned long pd;
2008 unsigned int index;
2009 target_ulong address;
2010 target_ulong code_address;
2011 target_phys_addr_t addend;
2012 int ret;
2013 CPUTLBEntry *te;
2014 CPUWatchpoint *wp;
2015 target_phys_addr_t iotlb;
2017 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
2018 if (!p) {
2019 pd = IO_MEM_UNASSIGNED;
2020 } else {
2021 pd = p->phys_offset;
2023 #if defined(DEBUG_TLB)
2024 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
2025 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
2026 #endif
2028 ret = 0;
2029 address = vaddr;
2030 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2031 /* IO memory case (romd handled later) */
2032 address |= TLB_MMIO;
2034 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
2035 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2036 /* Normal RAM. */
2037 iotlb = pd & TARGET_PAGE_MASK;
2038 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2039 iotlb |= IO_MEM_NOTDIRTY;
2040 else
2041 iotlb |= IO_MEM_ROM;
2042 } else {
2043 /* IO handlers are currently passed a physical address.
2044 It would be nice to pass an offset from the base address
2045 of that region. This would avoid having to special case RAM,
2046 and avoid full address decoding in every device.
2047 We can't use the high bits of pd for this because
2048 IO_MEM_ROMD uses these as a ram address. */
2049 iotlb = (pd & ~TARGET_PAGE_MASK);
2050 if (p) {
2051 iotlb += p->region_offset;
2052 } else {
2053 iotlb += paddr;
2057 code_address = address;
2058 /* Make accesses to pages with watchpoints go via the
2059 watchpoint trap routines. */
2060 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
2061 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
2062 iotlb = io_mem_watch + paddr;
2063 /* TODO: The memory case can be optimized by not trapping
2064 reads of pages with a write breakpoint. */
2065 address |= TLB_MMIO;
2069 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2070 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2071 te = &env->tlb_table[mmu_idx][index];
2072 te->addend = addend - vaddr;
2073 if (prot & PAGE_READ) {
2074 te->addr_read = address;
2075 } else {
2076 te->addr_read = -1;
2079 if (prot & PAGE_EXEC) {
2080 te->addr_code = code_address;
2081 } else {
2082 te->addr_code = -1;
2084 if (prot & PAGE_WRITE) {
2085 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2086 (pd & IO_MEM_ROMD)) {
2087 /* Write access calls the I/O callback. */
2088 te->addr_write = address | TLB_MMIO;
2089 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2090 !cpu_physical_memory_is_dirty(pd)) {
2091 te->addr_write = address | TLB_NOTDIRTY;
2092 } else {
2093 te->addr_write = address;
2095 } else {
2096 te->addr_write = -1;
2098 return ret;
2101 #else
2103 void tlb_flush(CPUState *env, int flush_global)
2107 void tlb_flush_page(CPUState *env, target_ulong addr)
2111 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
2112 target_phys_addr_t paddr, int prot,
2113 int mmu_idx, int is_softmmu)
2115 return 0;
2119 * Walks guest process memory "regions" one by one
2120 * and calls callback function 'fn' for each region.
2122 int walk_memory_regions(void *priv,
2123 int (*fn)(void *, unsigned long, unsigned long, unsigned long))
2125 unsigned long start, end;
2126 PageDesc *p = NULL;
2127 int i, j, prot, prot1;
2128 int rc = 0;
2130 start = end = -1;
2131 prot = 0;
2133 for (i = 0; i <= L1_SIZE; i++) {
2134 p = (i < L1_SIZE) ? l1_map[i] : NULL;
2135 for (j = 0; j < L2_SIZE; j++) {
2136 prot1 = (p == NULL) ? 0 : p[j].flags;
2138 * "region" is one continuous chunk of memory
2139 * that has same protection flags set.
2141 if (prot1 != prot) {
2142 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
2143 if (start != -1) {
2144 rc = (*fn)(priv, start, end, prot);
2145 /* callback can stop iteration by returning != 0 */
2146 if (rc != 0)
2147 return (rc);
2149 if (prot1 != 0)
2150 start = end;
2151 else
2152 start = -1;
2153 prot = prot1;
2155 if (p == NULL)
2156 break;
2159 return (rc);
2162 static int dump_region(void *priv, unsigned long start,
2163 unsigned long end, unsigned long prot)
2165 FILE *f = (FILE *)priv;
2167 (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
2168 start, end, end - start,
2169 ((prot & PAGE_READ) ? 'r' : '-'),
2170 ((prot & PAGE_WRITE) ? 'w' : '-'),
2171 ((prot & PAGE_EXEC) ? 'x' : '-'));
2173 return (0);
2176 /* dump memory mappings */
2177 void page_dump(FILE *f)
2179 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2180 "start", "end", "size", "prot");
2181 walk_memory_regions(f, dump_region);
2184 int page_get_flags(target_ulong address)
2186 PageDesc *p;
2188 p = page_find(address >> TARGET_PAGE_BITS);
2189 if (!p)
2190 return 0;
2191 return p->flags;
2194 /* modify the flags of a page and invalidate the code if
2195 necessary. The flag PAGE_WRITE_ORG is positioned automatically
2196 depending on PAGE_WRITE */
2197 void page_set_flags(target_ulong start, target_ulong end, int flags)
2199 PageDesc *p;
2200 target_ulong addr;
2202 /* mmap_lock should already be held. */
2203 start = start & TARGET_PAGE_MASK;
2204 end = TARGET_PAGE_ALIGN(end);
2205 if (flags & PAGE_WRITE)
2206 flags |= PAGE_WRITE_ORG;
2207 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2208 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
2209 /* We may be called for host regions that are outside guest
2210 address space. */
2211 if (!p)
2212 return;
2213 /* if the write protection is set, then we invalidate the code
2214 inside */
2215 if (!(p->flags & PAGE_WRITE) &&
2216 (flags & PAGE_WRITE) &&
2217 p->first_tb) {
2218 tb_invalidate_phys_page(addr, 0, NULL);
2220 p->flags = flags;
2224 int page_check_range(target_ulong start, target_ulong len, int flags)
2226 PageDesc *p;
2227 target_ulong end;
2228 target_ulong addr;
2230 if (start + len < start)
2231 /* we've wrapped around */
2232 return -1;
2234 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2235 start = start & TARGET_PAGE_MASK;
2237 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2238 p = page_find(addr >> TARGET_PAGE_BITS);
2239 if( !p )
2240 return -1;
2241 if( !(p->flags & PAGE_VALID) )
2242 return -1;
2244 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
2245 return -1;
2246 if (flags & PAGE_WRITE) {
2247 if (!(p->flags & PAGE_WRITE_ORG))
2248 return -1;
2249 /* unprotect the page if it was put read-only because it
2250 contains translated code */
2251 if (!(p->flags & PAGE_WRITE)) {
2252 if (!page_unprotect(addr, 0, NULL))
2253 return -1;
2255 return 0;
2258 return 0;
2261 /* called from signal handler: invalidate the code and unprotect the
2262 page. Return TRUE if the fault was successfully handled. */
2263 int page_unprotect(target_ulong address, unsigned long pc, void *puc)
2265 unsigned int page_index, prot, pindex;
2266 PageDesc *p, *p1;
2267 target_ulong host_start, host_end, addr;
2269 /* Technically this isn't safe inside a signal handler. However we
2270 know this only ever happens in a synchronous SEGV handler, so in
2271 practice it seems to be ok. */
2272 mmap_lock();
2274 host_start = address & qemu_host_page_mask;
2275 page_index = host_start >> TARGET_PAGE_BITS;
2276 p1 = page_find(page_index);
2277 if (!p1) {
2278 mmap_unlock();
2279 return 0;
2281 host_end = host_start + qemu_host_page_size;
2282 p = p1;
2283 prot = 0;
2284 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2285 prot |= p->flags;
2286 p++;
2288 /* if the page was really writable, then we change its
2289 protection back to writable */
2290 if (prot & PAGE_WRITE_ORG) {
2291 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2292 if (!(p1[pindex].flags & PAGE_WRITE)) {
2293 mprotect((void *)g2h(host_start), qemu_host_page_size,
2294 (prot & PAGE_BITS) | PAGE_WRITE);
2295 p1[pindex].flags |= PAGE_WRITE;
2296 /* and since the content will be modified, we must invalidate
2297 the corresponding translated code. */
2298 tb_invalidate_phys_page(address, pc, puc);
2299 #ifdef DEBUG_TB_CHECK
2300 tb_invalidate_check(address);
2301 #endif
2302 mmap_unlock();
2303 return 1;
2306 mmap_unlock();
2307 return 0;
2310 static inline void tlb_set_dirty(CPUState *env,
2311 unsigned long addr, target_ulong vaddr)
2314 #endif /* defined(CONFIG_USER_ONLY) */
2316 #if !defined(CONFIG_USER_ONLY)
2318 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2319 ram_addr_t memory, ram_addr_t region_offset);
2320 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2321 ram_addr_t orig_memory, ram_addr_t region_offset);
2322 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2323 need_subpage) \
2324 do { \
2325 if (addr > start_addr) \
2326 start_addr2 = 0; \
2327 else { \
2328 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2329 if (start_addr2 > 0) \
2330 need_subpage = 1; \
2333 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2334 end_addr2 = TARGET_PAGE_SIZE - 1; \
2335 else { \
2336 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2337 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2338 need_subpage = 1; \
2340 } while (0)
2342 /* register physical memory. 'size' must be a multiple of the target
2343 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2344 io memory page. The address used when calling the IO function is
2345 the offset from the start of the region, plus region_offset. Both
2346 start_addr and region_offset are rounded down to a page boundary
2347 before calculating this offset. This should not be a problem unless
2348 the low bits of start_addr and region_offset differ. */
2349 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2350 ram_addr_t size,
2351 ram_addr_t phys_offset,
2352 ram_addr_t region_offset)
2354 target_phys_addr_t addr, end_addr;
2355 PhysPageDesc *p;
2356 CPUState *env;
2357 ram_addr_t orig_size = size;
2358 void *subpage;
2360 #ifdef CONFIG_KQEMU
2361 /* XXX: should not depend on cpu context */
2362 env = first_cpu;
2363 if (env->kqemu_enabled) {
2364 kqemu_set_phys_mem(start_addr, size, phys_offset);
2366 #endif
2367 if (kvm_enabled())
2368 kvm_set_phys_mem(start_addr, size, phys_offset);
2370 if (phys_offset == IO_MEM_UNASSIGNED) {
2371 region_offset = start_addr;
2373 region_offset &= TARGET_PAGE_MASK;
2374 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
2375 end_addr = start_addr + (target_phys_addr_t)size;
2376 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
2377 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2378 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2379 ram_addr_t orig_memory = p->phys_offset;
2380 target_phys_addr_t start_addr2, end_addr2;
2381 int need_subpage = 0;
2383 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2384 need_subpage);
2385 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2386 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2387 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2388 &p->phys_offset, orig_memory,
2389 p->region_offset);
2390 } else {
2391 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2392 >> IO_MEM_SHIFT];
2394 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2395 region_offset);
2396 p->region_offset = 0;
2397 } else {
2398 p->phys_offset = phys_offset;
2399 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2400 (phys_offset & IO_MEM_ROMD))
2401 phys_offset += TARGET_PAGE_SIZE;
2403 } else {
2404 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2405 p->phys_offset = phys_offset;
2406 p->region_offset = region_offset;
2407 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2408 (phys_offset & IO_MEM_ROMD)) {
2409 phys_offset += TARGET_PAGE_SIZE;
2410 } else {
2411 target_phys_addr_t start_addr2, end_addr2;
2412 int need_subpage = 0;
2414 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2415 end_addr2, need_subpage);
2417 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2418 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2419 &p->phys_offset, IO_MEM_UNASSIGNED,
2420 addr & TARGET_PAGE_MASK);
2421 subpage_register(subpage, start_addr2, end_addr2,
2422 phys_offset, region_offset);
2423 p->region_offset = 0;
2427 region_offset += TARGET_PAGE_SIZE;
2430 /* since each CPU stores ram addresses in its TLB cache, we must
2431 reset the modified entries */
2432 /* XXX: slow ! */
2433 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2434 tlb_flush(env, 1);
2438 /* XXX: temporary until new memory mapping API */
2439 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2441 PhysPageDesc *p;
2443 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2444 if (!p)
2445 return IO_MEM_UNASSIGNED;
2446 return p->phys_offset;
2449 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2451 if (kvm_enabled())
2452 kvm_coalesce_mmio_region(addr, size);
2455 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2457 if (kvm_enabled())
2458 kvm_uncoalesce_mmio_region(addr, size);
2461 #ifdef CONFIG_KQEMU
2462 /* XXX: better than nothing */
2463 static ram_addr_t kqemu_ram_alloc(ram_addr_t size)
2465 ram_addr_t addr;
2466 if ((last_ram_offset + size) > kqemu_phys_ram_size) {
2467 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
2468 (uint64_t)size, (uint64_t)kqemu_phys_ram_size);
2469 abort();
2471 addr = last_ram_offset;
2472 last_ram_offset = TARGET_PAGE_ALIGN(last_ram_offset + size);
2473 return addr;
2475 #endif
2477 #ifdef __linux__
2479 #include <sys/vfs.h>
2481 #define HUGETLBFS_MAGIC 0x958458f6
2483 static long gethugepagesize(const char *path)
2485 struct statfs fs;
2486 int ret;
2488 do {
2489 ret = statfs(path, &fs);
2490 } while (ret != 0 && errno == EINTR);
2492 if (ret != 0) {
2493 perror("statfs");
2494 return 0;
2497 if (fs.f_type != HUGETLBFS_MAGIC)
2498 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
2500 return fs.f_bsize;
2503 static void *file_ram_alloc(ram_addr_t memory, const char *path)
2505 char *filename;
2506 void *area;
2507 int fd;
2508 #ifdef MAP_POPULATE
2509 int flags;
2510 #endif
2511 unsigned long hpagesize;
2512 extern int mem_prealloc;
2514 if (!path) {
2515 return NULL;
2518 hpagesize = gethugepagesize(path);
2519 if (!hpagesize) {
2520 return NULL;
2523 if (memory < hpagesize) {
2524 return NULL;
2527 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2528 fprintf(stderr, "host lacks mmu notifiers, disabling --mem-path\n");
2529 return NULL;
2532 if (asprintf(&filename, "%s/kvm.XXXXXX", path) == -1) {
2533 return NULL;
2536 fd = mkstemp(filename);
2537 if (fd < 0) {
2538 perror("mkstemp");
2539 free(filename);
2540 return NULL;
2542 unlink(filename);
2543 free(filename);
2545 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2548 * ftruncate is not supported by hugetlbfs in older
2549 * hosts, so don't bother checking for errors.
2550 * If anything goes wrong with it under other filesystems,
2551 * mmap will fail.
2553 ftruncate(fd, memory);
2555 #ifdef MAP_POPULATE
2556 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2557 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2558 * to sidestep this quirk.
2560 flags = mem_prealloc ? MAP_POPULATE|MAP_SHARED : MAP_PRIVATE;
2561 area = mmap(0, memory, PROT_READ|PROT_WRITE, flags, fd, 0);
2562 #else
2563 area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
2564 #endif
2565 if (area == MAP_FAILED) {
2566 perror("alloc_mem_area: can't mmap hugetlbfs pages");
2567 close(fd);
2568 return (NULL);
2570 return area;
2573 #else
2575 static void *file_ram_alloc(ram_addr_t memory, const char *path)
2577 return NULL;
2580 #endif
2582 extern const char *mem_path;
2584 ram_addr_t qemu_ram_alloc(ram_addr_t size)
2586 RAMBlock *new_block;
2588 #ifdef CONFIG_KQEMU
2589 if (kqemu_phys_ram_base) {
2590 return kqemu_ram_alloc(size);
2592 #endif
2594 size = TARGET_PAGE_ALIGN(size);
2595 new_block = qemu_malloc(sizeof(*new_block));
2597 new_block->host = file_ram_alloc(size, mem_path);
2598 if (!new_block->host) {
2599 new_block->host = qemu_vmalloc(size);
2601 new_block->offset = last_ram_offset;
2602 new_block->length = size;
2604 new_block->next = ram_blocks;
2605 ram_blocks = new_block;
2607 phys_ram_dirty = qemu_realloc(phys_ram_dirty,
2608 (last_ram_offset + size) >> TARGET_PAGE_BITS);
2609 memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
2610 0xff, size >> TARGET_PAGE_BITS);
2612 last_ram_offset += size;
2614 if (kvm_enabled())
2615 kvm_setup_guest_memory(new_block->host, size);
2617 return new_block->offset;
2620 void qemu_ram_free(ram_addr_t addr)
2622 /* TODO: implement this. */
2625 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2626 With the exception of the softmmu code in this file, this should
2627 only be used for local memory (e.g. video ram) that the device owns,
2628 and knows it isn't going to access beyond the end of the block.
2630 It should not be used for general purpose DMA.
2631 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2633 void *qemu_get_ram_ptr(ram_addr_t addr)
2635 RAMBlock *prev;
2636 RAMBlock **prevp;
2637 RAMBlock *block;
2639 #ifdef CONFIG_KQEMU
2640 if (kqemu_phys_ram_base) {
2641 return kqemu_phys_ram_base + addr;
2643 #endif
2645 prev = NULL;
2646 prevp = &ram_blocks;
2647 block = ram_blocks;
2648 while (block && (block->offset > addr
2649 || block->offset + block->length <= addr)) {
2650 if (prev)
2651 prevp = &prev->next;
2652 prev = block;
2653 block = block->next;
2655 if (!block) {
2656 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2657 abort();
2659 /* Move this entry to to start of the list. */
2660 if (prev) {
2661 prev->next = block->next;
2662 block->next = *prevp;
2663 *prevp = block;
2665 return block->host + (addr - block->offset);
2668 /* Some of the softmmu routines need to translate from a host pointer
2669 (typically a TLB entry) back to a ram offset. */
2670 ram_addr_t qemu_ram_addr_from_host(void *ptr)
2672 RAMBlock *prev;
2673 RAMBlock **prevp;
2674 RAMBlock *block;
2675 uint8_t *host = ptr;
2677 #ifdef CONFIG_KQEMU
2678 if (kqemu_phys_ram_base) {
2679 return host - kqemu_phys_ram_base;
2681 #endif
2683 prev = NULL;
2684 prevp = &ram_blocks;
2685 block = ram_blocks;
2686 while (block && (block->host > host
2687 || block->host + block->length <= host)) {
2688 if (prev)
2689 prevp = &prev->next;
2690 prev = block;
2691 block = block->next;
2693 if (!block) {
2694 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2695 abort();
2697 return block->offset + (host - block->host);
2700 static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
2702 #ifdef DEBUG_UNASSIGNED
2703 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2704 #endif
2705 #if defined(TARGET_SPARC)
2706 do_unassigned_access(addr, 0, 0, 0, 1);
2707 #endif
2708 return 0;
2711 static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
2713 #ifdef DEBUG_UNASSIGNED
2714 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2715 #endif
2716 #if defined(TARGET_SPARC)
2717 do_unassigned_access(addr, 0, 0, 0, 2);
2718 #endif
2719 return 0;
2722 static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
2724 #ifdef DEBUG_UNASSIGNED
2725 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2726 #endif
2727 #if defined(TARGET_SPARC)
2728 do_unassigned_access(addr, 0, 0, 0, 4);
2729 #endif
2730 return 0;
2733 static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
2735 #ifdef DEBUG_UNASSIGNED
2736 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2737 #endif
2738 #if defined(TARGET_SPARC)
2739 do_unassigned_access(addr, 1, 0, 0, 1);
2740 #endif
2743 static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2745 #ifdef DEBUG_UNASSIGNED
2746 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2747 #endif
2748 #if defined(TARGET_SPARC)
2749 do_unassigned_access(addr, 1, 0, 0, 2);
2750 #endif
2753 static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2755 #ifdef DEBUG_UNASSIGNED
2756 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2757 #endif
2758 #if defined(TARGET_SPARC)
2759 do_unassigned_access(addr, 1, 0, 0, 4);
2760 #endif
2763 static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2764 unassigned_mem_readb,
2765 unassigned_mem_readw,
2766 unassigned_mem_readl,
2769 static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2770 unassigned_mem_writeb,
2771 unassigned_mem_writew,
2772 unassigned_mem_writel,
2775 static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2776 uint32_t val)
2778 int dirty_flags;
2779 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2780 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2781 #if !defined(CONFIG_USER_ONLY)
2782 tb_invalidate_phys_page_fast(ram_addr, 1);
2783 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2784 #endif
2786 stb_p(qemu_get_ram_ptr(ram_addr), val);
2787 #ifdef CONFIG_KQEMU
2788 if (cpu_single_env->kqemu_enabled &&
2789 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2790 kqemu_modify_page(cpu_single_env, ram_addr);
2791 #endif
2792 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2793 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2794 /* we remove the notdirty callback only if the code has been
2795 flushed */
2796 if (dirty_flags == 0xff)
2797 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2800 static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2801 uint32_t val)
2803 int dirty_flags;
2804 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2805 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2806 #if !defined(CONFIG_USER_ONLY)
2807 tb_invalidate_phys_page_fast(ram_addr, 2);
2808 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2809 #endif
2811 stw_p(qemu_get_ram_ptr(ram_addr), val);
2812 #ifdef CONFIG_KQEMU
2813 if (cpu_single_env->kqemu_enabled &&
2814 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2815 kqemu_modify_page(cpu_single_env, ram_addr);
2816 #endif
2817 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2818 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2819 /* we remove the notdirty callback only if the code has been
2820 flushed */
2821 if (dirty_flags == 0xff)
2822 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2825 static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2826 uint32_t val)
2828 int dirty_flags;
2829 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2830 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2831 #if !defined(CONFIG_USER_ONLY)
2832 tb_invalidate_phys_page_fast(ram_addr, 4);
2833 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2834 #endif
2836 stl_p(qemu_get_ram_ptr(ram_addr), val);
2837 #ifdef CONFIG_KQEMU
2838 if (cpu_single_env->kqemu_enabled &&
2839 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2840 kqemu_modify_page(cpu_single_env, ram_addr);
2841 #endif
2842 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2843 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2844 /* we remove the notdirty callback only if the code has been
2845 flushed */
2846 if (dirty_flags == 0xff)
2847 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2850 static CPUReadMemoryFunc *error_mem_read[3] = {
2851 NULL, /* never used */
2852 NULL, /* never used */
2853 NULL, /* never used */
2856 static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2857 notdirty_mem_writeb,
2858 notdirty_mem_writew,
2859 notdirty_mem_writel,
2862 /* Generate a debug exception if a watchpoint has been hit. */
2863 static void check_watchpoint(int offset, int len_mask, int flags)
2865 CPUState *env = cpu_single_env;
2866 target_ulong pc, cs_base;
2867 TranslationBlock *tb;
2868 target_ulong vaddr;
2869 CPUWatchpoint *wp;
2870 int cpu_flags;
2872 if (env->watchpoint_hit) {
2873 /* We re-entered the check after replacing the TB. Now raise
2874 * the debug interrupt so that is will trigger after the
2875 * current instruction. */
2876 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2877 return;
2879 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
2880 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
2881 if ((vaddr == (wp->vaddr & len_mask) ||
2882 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
2883 wp->flags |= BP_WATCHPOINT_HIT;
2884 if (!env->watchpoint_hit) {
2885 env->watchpoint_hit = wp;
2886 tb = tb_find_pc(env->mem_io_pc);
2887 if (!tb) {
2888 cpu_abort(env, "check_watchpoint: could not find TB for "
2889 "pc=%p", (void *)env->mem_io_pc);
2891 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
2892 tb_phys_invalidate(tb, -1);
2893 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2894 env->exception_index = EXCP_DEBUG;
2895 } else {
2896 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2897 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
2899 cpu_resume_from_signal(env, NULL);
2901 } else {
2902 wp->flags &= ~BP_WATCHPOINT_HIT;
2907 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2908 so these check for a hit then pass through to the normal out-of-line
2909 phys routines. */
2910 static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2912 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
2913 return ldub_phys(addr);
2916 static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2918 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
2919 return lduw_phys(addr);
2922 static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2924 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
2925 return ldl_phys(addr);
2928 static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2929 uint32_t val)
2931 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
2932 stb_phys(addr, val);
2935 static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2936 uint32_t val)
2938 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
2939 stw_phys(addr, val);
2942 static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2943 uint32_t val)
2945 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
2946 stl_phys(addr, val);
2949 static CPUReadMemoryFunc *watch_mem_read[3] = {
2950 watch_mem_readb,
2951 watch_mem_readw,
2952 watch_mem_readl,
2955 static CPUWriteMemoryFunc *watch_mem_write[3] = {
2956 watch_mem_writeb,
2957 watch_mem_writew,
2958 watch_mem_writel,
2961 static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2962 unsigned int len)
2964 uint32_t ret;
2965 unsigned int idx;
2967 idx = SUBPAGE_IDX(addr);
2968 #if defined(DEBUG_SUBPAGE)
2969 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2970 mmio, len, addr, idx);
2971 #endif
2972 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
2973 addr + mmio->region_offset[idx][0][len]);
2975 return ret;
2978 static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2979 uint32_t value, unsigned int len)
2981 unsigned int idx;
2983 idx = SUBPAGE_IDX(addr);
2984 #if defined(DEBUG_SUBPAGE)
2985 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2986 mmio, len, addr, idx, value);
2987 #endif
2988 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
2989 addr + mmio->region_offset[idx][1][len],
2990 value);
2993 static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2995 #if defined(DEBUG_SUBPAGE)
2996 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2997 #endif
2999 return subpage_readlen(opaque, addr, 0);
3002 static void subpage_writeb (void *opaque, target_phys_addr_t addr,
3003 uint32_t value)
3005 #if defined(DEBUG_SUBPAGE)
3006 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3007 #endif
3008 subpage_writelen(opaque, addr, value, 0);
3011 static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
3013 #if defined(DEBUG_SUBPAGE)
3014 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3015 #endif
3017 return subpage_readlen(opaque, addr, 1);
3020 static void subpage_writew (void *opaque, target_phys_addr_t addr,
3021 uint32_t value)
3023 #if defined(DEBUG_SUBPAGE)
3024 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3025 #endif
3026 subpage_writelen(opaque, addr, value, 1);
3029 static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
3031 #if defined(DEBUG_SUBPAGE)
3032 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3033 #endif
3035 return subpage_readlen(opaque, addr, 2);
3038 static void subpage_writel (void *opaque,
3039 target_phys_addr_t addr, uint32_t value)
3041 #if defined(DEBUG_SUBPAGE)
3042 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3043 #endif
3044 subpage_writelen(opaque, addr, value, 2);
3047 static CPUReadMemoryFunc *subpage_read[] = {
3048 &subpage_readb,
3049 &subpage_readw,
3050 &subpage_readl,
3053 static CPUWriteMemoryFunc *subpage_write[] = {
3054 &subpage_writeb,
3055 &subpage_writew,
3056 &subpage_writel,
3059 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3060 ram_addr_t memory, ram_addr_t region_offset)
3062 int idx, eidx;
3063 unsigned int i;
3065 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3066 return -1;
3067 idx = SUBPAGE_IDX(start);
3068 eidx = SUBPAGE_IDX(end);
3069 #if defined(DEBUG_SUBPAGE)
3070 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
3071 mmio, start, end, idx, eidx, memory);
3072 #endif
3073 memory >>= IO_MEM_SHIFT;
3074 for (; idx <= eidx; idx++) {
3075 for (i = 0; i < 4; i++) {
3076 if (io_mem_read[memory][i]) {
3077 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
3078 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
3079 mmio->region_offset[idx][0][i] = region_offset;
3081 if (io_mem_write[memory][i]) {
3082 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
3083 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
3084 mmio->region_offset[idx][1][i] = region_offset;
3089 return 0;
3092 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3093 ram_addr_t orig_memory, ram_addr_t region_offset)
3095 subpage_t *mmio;
3096 int subpage_memory;
3098 mmio = qemu_mallocz(sizeof(subpage_t));
3100 mmio->base = base;
3101 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio);
3102 #if defined(DEBUG_SUBPAGE)
3103 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3104 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
3105 #endif
3106 *phys = subpage_memory | IO_MEM_SUBPAGE;
3107 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory,
3108 region_offset);
3110 return mmio;
3113 static int get_free_io_mem_idx(void)
3115 int i;
3117 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3118 if (!io_mem_used[i]) {
3119 io_mem_used[i] = 1;
3120 return i;
3123 return -1;
3126 /* mem_read and mem_write are arrays of functions containing the
3127 function to access byte (index 0), word (index 1) and dword (index
3128 2). Functions can be omitted with a NULL function pointer.
3129 If io_index is non zero, the corresponding io zone is
3130 modified. If it is zero, a new io zone is allocated. The return
3131 value can be used with cpu_register_physical_memory(). (-1) is
3132 returned if error. */
3133 static int cpu_register_io_memory_fixed(int io_index,
3134 CPUReadMemoryFunc **mem_read,
3135 CPUWriteMemoryFunc **mem_write,
3136 void *opaque)
3138 int i, subwidth = 0;
3140 if (io_index <= 0) {
3141 io_index = get_free_io_mem_idx();
3142 if (io_index == -1)
3143 return io_index;
3144 } else {
3145 io_index >>= IO_MEM_SHIFT;
3146 if (io_index >= IO_MEM_NB_ENTRIES)
3147 return -1;
3150 for(i = 0;i < 3; i++) {
3151 if (!mem_read[i] || !mem_write[i])
3152 subwidth = IO_MEM_SUBWIDTH;
3153 io_mem_read[io_index][i] = mem_read[i];
3154 io_mem_write[io_index][i] = mem_write[i];
3156 io_mem_opaque[io_index] = opaque;
3157 return (io_index << IO_MEM_SHIFT) | subwidth;
3160 int cpu_register_io_memory(CPUReadMemoryFunc **mem_read,
3161 CPUWriteMemoryFunc **mem_write,
3162 void *opaque)
3164 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
3167 void cpu_unregister_io_memory(int io_table_address)
3169 int i;
3170 int io_index = io_table_address >> IO_MEM_SHIFT;
3172 for (i=0;i < 3; i++) {
3173 io_mem_read[io_index][i] = unassigned_mem_read[i];
3174 io_mem_write[io_index][i] = unassigned_mem_write[i];
3176 io_mem_opaque[io_index] = NULL;
3177 io_mem_used[io_index] = 0;
3180 static void io_mem_init(void)
3182 int i;
3184 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
3185 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
3186 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
3187 for (i=0; i<5; i++)
3188 io_mem_used[i] = 1;
3190 io_mem_watch = cpu_register_io_memory(watch_mem_read,
3191 watch_mem_write, NULL);
3192 #ifdef CONFIG_KQEMU
3193 if (kqemu_phys_ram_base) {
3194 /* alloc dirty bits array */
3195 phys_ram_dirty = qemu_vmalloc(kqemu_phys_ram_size >> TARGET_PAGE_BITS);
3196 memset(phys_ram_dirty, 0xff, kqemu_phys_ram_size >> TARGET_PAGE_BITS);
3198 #endif
3201 #endif /* !defined(CONFIG_USER_ONLY) */
3203 /* physical memory access (slow version, mainly for debug) */
3204 #if defined(CONFIG_USER_ONLY)
3205 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
3206 int len, int is_write)
3208 int l, flags;
3209 target_ulong page;
3210 void * p;
3212 while (len > 0) {
3213 page = addr & TARGET_PAGE_MASK;
3214 l = (page + TARGET_PAGE_SIZE) - addr;
3215 if (l > len)
3216 l = len;
3217 flags = page_get_flags(page);
3218 if (!(flags & PAGE_VALID))
3219 return;
3220 if (is_write) {
3221 if (!(flags & PAGE_WRITE))
3222 return;
3223 /* XXX: this code should not depend on lock_user */
3224 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
3225 /* FIXME - should this return an error rather than just fail? */
3226 return;
3227 memcpy(p, buf, l);
3228 unlock_user(p, addr, l);
3229 } else {
3230 if (!(flags & PAGE_READ))
3231 return;
3232 /* XXX: this code should not depend on lock_user */
3233 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
3234 /* FIXME - should this return an error rather than just fail? */
3235 return;
3236 memcpy(buf, p, l);
3237 unlock_user(p, addr, 0);
3239 len -= l;
3240 buf += l;
3241 addr += l;
3245 #else
3246 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
3247 int len, int is_write)
3249 int l, io_index;
3250 uint8_t *ptr;
3251 uint32_t val;
3252 target_phys_addr_t page;
3253 unsigned long pd;
3254 PhysPageDesc *p;
3256 while (len > 0) {
3257 page = addr & TARGET_PAGE_MASK;
3258 l = (page + TARGET_PAGE_SIZE) - addr;
3259 if (l > len)
3260 l = len;
3261 p = phys_page_find(page >> TARGET_PAGE_BITS);
3262 if (!p) {
3263 pd = IO_MEM_UNASSIGNED;
3264 } else {
3265 pd = p->phys_offset;
3268 if (is_write) {
3269 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3270 target_phys_addr_t addr1 = addr;
3271 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3272 if (p)
3273 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3274 /* XXX: could force cpu_single_env to NULL to avoid
3275 potential bugs */
3276 if (l >= 4 && ((addr1 & 3) == 0)) {
3277 /* 32 bit write access */
3278 val = ldl_p(buf);
3279 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
3280 l = 4;
3281 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3282 /* 16 bit write access */
3283 val = lduw_p(buf);
3284 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
3285 l = 2;
3286 } else {
3287 /* 8 bit write access */
3288 val = ldub_p(buf);
3289 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
3290 l = 1;
3292 } else {
3293 unsigned long addr1;
3294 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3295 /* RAM case */
3296 ptr = qemu_get_ram_ptr(addr1);
3297 memcpy(ptr, buf, l);
3298 if (!cpu_physical_memory_is_dirty(addr1)) {
3299 /* invalidate code */
3300 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3301 /* set dirty bit */
3302 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3303 (0xff & ~CODE_DIRTY_FLAG);
3305 /* qemu doesn't execute guest code directly, but kvm does
3306 therefore flush instruction caches */
3307 if (kvm_enabled())
3308 flush_icache_range((unsigned long)ptr,
3309 ((unsigned long)ptr)+l);
3311 } else {
3312 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3313 !(pd & IO_MEM_ROMD)) {
3314 target_phys_addr_t addr1 = addr;
3315 /* I/O case */
3316 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3317 if (p)
3318 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3319 if (l >= 4 && ((addr1 & 3) == 0)) {
3320 /* 32 bit read access */
3321 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
3322 stl_p(buf, val);
3323 l = 4;
3324 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3325 /* 16 bit read access */
3326 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
3327 stw_p(buf, val);
3328 l = 2;
3329 } else {
3330 /* 8 bit read access */
3331 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
3332 stb_p(buf, val);
3333 l = 1;
3335 } else {
3336 /* RAM case */
3337 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3338 (addr & ~TARGET_PAGE_MASK);
3339 memcpy(buf, ptr, l);
3342 len -= l;
3343 buf += l;
3344 addr += l;
3348 /* used for ROM loading : can write in RAM and ROM */
3349 void cpu_physical_memory_write_rom(target_phys_addr_t addr,
3350 const uint8_t *buf, int len)
3352 int l;
3353 uint8_t *ptr;
3354 target_phys_addr_t page;
3355 unsigned long pd;
3356 PhysPageDesc *p;
3358 while (len > 0) {
3359 page = addr & TARGET_PAGE_MASK;
3360 l = (page + TARGET_PAGE_SIZE) - addr;
3361 if (l > len)
3362 l = len;
3363 p = phys_page_find(page >> TARGET_PAGE_BITS);
3364 if (!p) {
3365 pd = IO_MEM_UNASSIGNED;
3366 } else {
3367 pd = p->phys_offset;
3370 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
3371 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3372 !(pd & IO_MEM_ROMD)) {
3373 /* do nothing */
3374 } else {
3375 unsigned long addr1;
3376 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3377 /* ROM/RAM case */
3378 ptr = qemu_get_ram_ptr(addr1);
3379 memcpy(ptr, buf, l);
3381 len -= l;
3382 buf += l;
3383 addr += l;
3387 typedef struct {
3388 void *buffer;
3389 target_phys_addr_t addr;
3390 target_phys_addr_t len;
3391 } BounceBuffer;
3393 static BounceBuffer bounce;
3395 typedef struct MapClient {
3396 void *opaque;
3397 void (*callback)(void *opaque);
3398 LIST_ENTRY(MapClient) link;
3399 } MapClient;
3401 static LIST_HEAD(map_client_list, MapClient) map_client_list
3402 = LIST_HEAD_INITIALIZER(map_client_list);
3404 void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3406 MapClient *client = qemu_malloc(sizeof(*client));
3408 client->opaque = opaque;
3409 client->callback = callback;
3410 LIST_INSERT_HEAD(&map_client_list, client, link);
3411 return client;
3414 void cpu_unregister_map_client(void *_client)
3416 MapClient *client = (MapClient *)_client;
3418 LIST_REMOVE(client, link);
3419 qemu_free(client);
3422 static void cpu_notify_map_clients(void)
3424 MapClient *client;
3426 while (!LIST_EMPTY(&map_client_list)) {
3427 client = LIST_FIRST(&map_client_list);
3428 client->callback(client->opaque);
3429 cpu_unregister_map_client(client);
3433 /* Map a physical memory region into a host virtual address.
3434 * May map a subset of the requested range, given by and returned in *plen.
3435 * May return NULL if resources needed to perform the mapping are exhausted.
3436 * Use only for reads OR writes - not for read-modify-write operations.
3437 * Use cpu_register_map_client() to know when retrying the map operation is
3438 * likely to succeed.
3440 void *cpu_physical_memory_map(target_phys_addr_t addr,
3441 target_phys_addr_t *plen,
3442 int is_write)
3444 target_phys_addr_t len = *plen;
3445 target_phys_addr_t done = 0;
3446 int l;
3447 uint8_t *ret = NULL;
3448 uint8_t *ptr;
3449 target_phys_addr_t page;
3450 unsigned long pd;
3451 PhysPageDesc *p;
3452 unsigned long addr1;
3454 while (len > 0) {
3455 page = addr & TARGET_PAGE_MASK;
3456 l = (page + TARGET_PAGE_SIZE) - addr;
3457 if (l > len)
3458 l = len;
3459 p = phys_page_find(page >> TARGET_PAGE_BITS);
3460 if (!p) {
3461 pd = IO_MEM_UNASSIGNED;
3462 } else {
3463 pd = p->phys_offset;
3466 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3467 if (done || bounce.buffer) {
3468 break;
3470 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3471 bounce.addr = addr;
3472 bounce.len = l;
3473 if (!is_write) {
3474 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3476 ptr = bounce.buffer;
3477 } else {
3478 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3479 ptr = qemu_get_ram_ptr(addr1);
3481 if (!done) {
3482 ret = ptr;
3483 } else if (ret + done != ptr) {
3484 break;
3487 len -= l;
3488 addr += l;
3489 done += l;
3491 *plen = done;
3492 return ret;
3495 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3496 * Will also mark the memory as dirty if is_write == 1. access_len gives
3497 * the amount of memory that was actually read or written by the caller.
3499 void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3500 int is_write, target_phys_addr_t access_len)
3502 unsigned long flush_len = (unsigned long)access_len;
3504 if (buffer != bounce.buffer) {
3505 if (is_write) {
3506 ram_addr_t addr1 = qemu_ram_addr_from_host(buffer);
3507 while (access_len) {
3508 unsigned l;
3509 l = TARGET_PAGE_SIZE;
3510 if (l > access_len)
3511 l = access_len;
3512 if (!cpu_physical_memory_is_dirty(addr1)) {
3513 /* invalidate code */
3514 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3515 /* set dirty bit */
3516 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3517 (0xff & ~CODE_DIRTY_FLAG);
3519 addr1 += l;
3520 access_len -= l;
3522 dma_flush_range((unsigned long)buffer,
3523 (unsigned long)buffer + flush_len);
3525 return;
3527 if (is_write) {
3528 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3530 qemu_free(bounce.buffer);
3531 bounce.buffer = NULL;
3532 cpu_notify_map_clients();
3535 /* warning: addr must be aligned */
3536 uint32_t ldl_phys(target_phys_addr_t addr)
3538 int io_index;
3539 uint8_t *ptr;
3540 uint32_t val;
3541 unsigned long pd;
3542 PhysPageDesc *p;
3544 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3545 if (!p) {
3546 pd = IO_MEM_UNASSIGNED;
3547 } else {
3548 pd = p->phys_offset;
3551 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3552 !(pd & IO_MEM_ROMD)) {
3553 /* I/O case */
3554 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3555 if (p)
3556 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3557 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3558 } else {
3559 /* RAM case */
3560 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3561 (addr & ~TARGET_PAGE_MASK);
3562 val = ldl_p(ptr);
3564 return val;
3567 /* warning: addr must be aligned */
3568 uint64_t ldq_phys(target_phys_addr_t addr)
3570 int io_index;
3571 uint8_t *ptr;
3572 uint64_t val;
3573 unsigned long pd;
3574 PhysPageDesc *p;
3576 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3577 if (!p) {
3578 pd = IO_MEM_UNASSIGNED;
3579 } else {
3580 pd = p->phys_offset;
3583 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3584 !(pd & IO_MEM_ROMD)) {
3585 /* I/O case */
3586 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3587 if (p)
3588 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3589 #ifdef TARGET_WORDS_BIGENDIAN
3590 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3591 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3592 #else
3593 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3594 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3595 #endif
3596 } else {
3597 /* RAM case */
3598 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3599 (addr & ~TARGET_PAGE_MASK);
3600 val = ldq_p(ptr);
3602 return val;
3605 /* XXX: optimize */
3606 uint32_t ldub_phys(target_phys_addr_t addr)
3608 uint8_t val;
3609 cpu_physical_memory_read(addr, &val, 1);
3610 return val;
3613 /* XXX: optimize */
3614 uint32_t lduw_phys(target_phys_addr_t addr)
3616 uint16_t val;
3617 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
3618 return tswap16(val);
3621 /* warning: addr must be aligned. The ram page is not masked as dirty
3622 and the code inside is not invalidated. It is useful if the dirty
3623 bits are used to track modified PTEs */
3624 void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
3626 int io_index;
3627 uint8_t *ptr;
3628 unsigned long pd;
3629 PhysPageDesc *p;
3631 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3632 if (!p) {
3633 pd = IO_MEM_UNASSIGNED;
3634 } else {
3635 pd = p->phys_offset;
3638 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3639 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3640 if (p)
3641 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3642 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3643 } else {
3644 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3645 ptr = qemu_get_ram_ptr(addr1);
3646 stl_p(ptr, val);
3648 if (unlikely(in_migration)) {
3649 if (!cpu_physical_memory_is_dirty(addr1)) {
3650 /* invalidate code */
3651 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3652 /* set dirty bit */
3653 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3654 (0xff & ~CODE_DIRTY_FLAG);
3660 void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
3662 int io_index;
3663 uint8_t *ptr;
3664 unsigned long pd;
3665 PhysPageDesc *p;
3667 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3668 if (!p) {
3669 pd = IO_MEM_UNASSIGNED;
3670 } else {
3671 pd = p->phys_offset;
3674 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3675 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3676 if (p)
3677 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3678 #ifdef TARGET_WORDS_BIGENDIAN
3679 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3680 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3681 #else
3682 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3683 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3684 #endif
3685 } else {
3686 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3687 (addr & ~TARGET_PAGE_MASK);
3688 stq_p(ptr, val);
3692 /* warning: addr must be aligned */
3693 void stl_phys(target_phys_addr_t addr, uint32_t val)
3695 int io_index;
3696 uint8_t *ptr;
3697 unsigned long pd;
3698 PhysPageDesc *p;
3700 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3701 if (!p) {
3702 pd = IO_MEM_UNASSIGNED;
3703 } else {
3704 pd = p->phys_offset;
3707 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3708 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3709 if (p)
3710 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3711 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3712 } else {
3713 unsigned long addr1;
3714 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3715 /* RAM case */
3716 ptr = qemu_get_ram_ptr(addr1);
3717 stl_p(ptr, val);
3718 if (!cpu_physical_memory_is_dirty(addr1)) {
3719 /* invalidate code */
3720 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3721 /* set dirty bit */
3722 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3723 (0xff & ~CODE_DIRTY_FLAG);
3728 /* XXX: optimize */
3729 void stb_phys(target_phys_addr_t addr, uint32_t val)
3731 uint8_t v = val;
3732 cpu_physical_memory_write(addr, &v, 1);
3735 /* XXX: optimize */
3736 void stw_phys(target_phys_addr_t addr, uint32_t val)
3738 uint16_t v = tswap16(val);
3739 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3742 /* XXX: optimize */
3743 void stq_phys(target_phys_addr_t addr, uint64_t val)
3745 val = tswap64(val);
3746 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3749 #endif
3751 /* virtual memory access for debug (includes writing to ROM) */
3752 int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3753 uint8_t *buf, int len, int is_write)
3755 int l;
3756 target_phys_addr_t phys_addr;
3757 target_ulong page;
3759 while (len > 0) {
3760 page = addr & TARGET_PAGE_MASK;
3761 phys_addr = cpu_get_phys_page_debug(env, page);
3762 /* if no physical page mapped, return an error */
3763 if (phys_addr == -1)
3764 return -1;
3765 l = (page + TARGET_PAGE_SIZE) - addr;
3766 if (l > len)
3767 l = len;
3768 phys_addr += (addr & ~TARGET_PAGE_MASK);
3769 #if !defined(CONFIG_USER_ONLY)
3770 if (is_write)
3771 cpu_physical_memory_write_rom(phys_addr, buf, l);
3772 else
3773 #endif
3774 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
3775 len -= l;
3776 buf += l;
3777 addr += l;
3779 return 0;
3782 /* in deterministic execution mode, instructions doing device I/Os
3783 must be at the end of the TB */
3784 void cpu_io_recompile(CPUState *env, void *retaddr)
3786 TranslationBlock *tb;
3787 uint32_t n, cflags;
3788 target_ulong pc, cs_base;
3789 uint64_t flags;
3791 tb = tb_find_pc((unsigned long)retaddr);
3792 if (!tb) {
3793 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3794 retaddr);
3796 n = env->icount_decr.u16.low + tb->icount;
3797 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3798 /* Calculate how many instructions had been executed before the fault
3799 occurred. */
3800 n = n - env->icount_decr.u16.low;
3801 /* Generate a new TB ending on the I/O insn. */
3802 n++;
3803 /* On MIPS and SH, delay slot instructions can only be restarted if
3804 they were already the first instruction in the TB. If this is not
3805 the first instruction in a TB then re-execute the preceding
3806 branch. */
3807 #if defined(TARGET_MIPS)
3808 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3809 env->active_tc.PC -= 4;
3810 env->icount_decr.u16.low++;
3811 env->hflags &= ~MIPS_HFLAG_BMASK;
3813 #elif defined(TARGET_SH4)
3814 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3815 && n > 1) {
3816 env->pc -= 2;
3817 env->icount_decr.u16.low++;
3818 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3820 #endif
3821 /* This should never happen. */
3822 if (n > CF_COUNT_MASK)
3823 cpu_abort(env, "TB too big during recompile");
3825 cflags = n | CF_LAST_IO;
3826 pc = tb->pc;
3827 cs_base = tb->cs_base;
3828 flags = tb->flags;
3829 tb_phys_invalidate(tb, -1);
3830 /* FIXME: In theory this could raise an exception. In practice
3831 we have already translated the block once so it's probably ok. */
3832 tb_gen_code(env, pc, cs_base, flags, cflags);
3833 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3834 the first in the TB) then we end up generating a whole new TB and
3835 repeating the fault, which is horribly inefficient.
3836 Better would be to execute just this insn uncached, or generate a
3837 second new TB. */
3838 cpu_resume_from_signal(env, NULL);
3841 void dump_exec_info(FILE *f,
3842 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3844 int i, target_code_size, max_target_code_size;
3845 int direct_jmp_count, direct_jmp2_count, cross_page;
3846 TranslationBlock *tb;
3848 target_code_size = 0;
3849 max_target_code_size = 0;
3850 cross_page = 0;
3851 direct_jmp_count = 0;
3852 direct_jmp2_count = 0;
3853 for(i = 0; i < nb_tbs; i++) {
3854 tb = &tbs[i];
3855 target_code_size += tb->size;
3856 if (tb->size > max_target_code_size)
3857 max_target_code_size = tb->size;
3858 if (tb->page_addr[1] != -1)
3859 cross_page++;
3860 if (tb->tb_next_offset[0] != 0xffff) {
3861 direct_jmp_count++;
3862 if (tb->tb_next_offset[1] != 0xffff) {
3863 direct_jmp2_count++;
3867 /* XXX: avoid using doubles ? */
3868 cpu_fprintf(f, "Translation buffer state:\n");
3869 cpu_fprintf(f, "gen code size %ld/%ld\n",
3870 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3871 cpu_fprintf(f, "TB count %d/%d\n",
3872 nb_tbs, code_gen_max_blocks);
3873 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
3874 nb_tbs ? target_code_size / nb_tbs : 0,
3875 max_target_code_size);
3876 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3877 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3878 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
3879 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3880 cross_page,
3881 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3882 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3883 direct_jmp_count,
3884 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3885 direct_jmp2_count,
3886 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
3887 cpu_fprintf(f, "\nStatistics:\n");
3888 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3889 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3890 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
3891 tcg_dump_info(f, cpu_fprintf);
3894 #if !defined(CONFIG_USER_ONLY)
3896 #define MMUSUFFIX _cmmu
3897 #define GETPC() NULL
3898 #define env cpu_single_env
3899 #define SOFTMMU_CODE_ACCESS
3901 #define SHIFT 0
3902 #include "softmmu_template.h"
3904 #define SHIFT 1
3905 #include "softmmu_template.h"
3907 #define SHIFT 2
3908 #include "softmmu_template.h"
3910 #define SHIFT 3
3911 #include "softmmu_template.h"
3913 #undef env
3915 #endif