Workaround segfault when qemu-kvm runs inside a VM (RHBZ#516543).
[qemu-kvm/fedora.git] / exec.c
blob721fcffd2cf3f93117d321125e6245019e25812b
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 #ifdef USE_STATIC_CODE_GEN_BUFFER
425 code_gen_buffer = static_code_gen_buffer;
426 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
427 map_exec(code_gen_buffer, code_gen_buffer_size);
428 #else
429 code_gen_buffer_size = tb_size;
430 if (code_gen_buffer_size == 0) {
431 #if defined(CONFIG_USER_ONLY)
432 /* in user mode, phys_ram_size is not meaningful */
433 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
434 #else
435 /* XXX: needs adjustments */
436 code_gen_buffer_size = (unsigned long)(ram_size / 4);
437 #endif
439 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
440 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
441 /* The code gen buffer location may have constraints depending on
442 the host cpu and OS */
443 #if defined(__linux__)
445 int flags;
446 void *start = NULL;
448 flags = MAP_PRIVATE | MAP_ANONYMOUS;
449 #if defined(__x86_64__)
450 flags |= MAP_32BIT;
451 /* Cannot map more than that */
452 if (code_gen_buffer_size > (800 * 1024 * 1024))
453 code_gen_buffer_size = (800 * 1024 * 1024);
454 #elif defined(__sparc_v9__)
455 // Map the buffer below 2G, so we can use direct calls and branches
456 flags |= MAP_FIXED;
457 start = (void *) 0x60000000UL;
458 if (code_gen_buffer_size > (512 * 1024 * 1024))
459 code_gen_buffer_size = (512 * 1024 * 1024);
460 #elif defined(__arm__)
461 /* Map the buffer below 32M, so we can use direct calls and branches */
462 flags |= MAP_FIXED;
463 start = (void *) 0x01000000UL;
464 if (code_gen_buffer_size > 16 * 1024 * 1024)
465 code_gen_buffer_size = 16 * 1024 * 1024;
466 #endif
467 code_gen_buffer = mmap(start, code_gen_buffer_size,
468 PROT_WRITE | PROT_READ | PROT_EXEC,
469 flags, -1, 0);
470 if (code_gen_buffer == MAP_FAILED) {
471 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
472 exit(1);
475 #elif defined(__FreeBSD__) || defined(__DragonFly__)
477 int flags;
478 void *addr = NULL;
479 flags = MAP_PRIVATE | MAP_ANONYMOUS;
480 #if defined(__x86_64__)
481 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
482 * 0x40000000 is free */
483 flags |= MAP_FIXED;
484 addr = (void *)0x40000000;
485 /* Cannot map more than that */
486 if (code_gen_buffer_size > (800 * 1024 * 1024))
487 code_gen_buffer_size = (800 * 1024 * 1024);
488 #endif
489 code_gen_buffer = mmap(addr, code_gen_buffer_size,
490 PROT_WRITE | PROT_READ | PROT_EXEC,
491 flags, -1, 0);
492 if (code_gen_buffer == MAP_FAILED) {
493 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
494 exit(1);
497 #else
498 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
499 map_exec(code_gen_buffer, code_gen_buffer_size);
500 #endif
501 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
502 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
503 code_gen_buffer_max_size = code_gen_buffer_size -
504 code_gen_max_block_size();
505 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
506 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
509 /* Must be called before using the QEMU cpus. 'tb_size' is the size
510 (in bytes) allocated to the translation buffer. Zero means default
511 size. */
512 void cpu_exec_init_all(unsigned long tb_size)
514 cpu_gen_init();
515 code_gen_alloc(tb_size);
516 code_gen_ptr = code_gen_buffer;
517 page_init();
518 #if !defined(CONFIG_USER_ONLY)
519 io_mem_init();
520 #endif
523 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
525 #define CPU_COMMON_SAVE_VERSION 1
527 static void cpu_common_save(QEMUFile *f, void *opaque)
529 CPUState *env = opaque;
531 cpu_synchronize_state(env, 0);
533 qemu_put_be32s(f, &env->halted);
534 qemu_put_be32s(f, &env->interrupt_request);
537 static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
539 CPUState *env = opaque;
541 if (version_id != CPU_COMMON_SAVE_VERSION)
542 return -EINVAL;
544 qemu_get_be32s(f, &env->halted);
545 qemu_get_be32s(f, &env->interrupt_request);
546 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
547 version_id is increased. */
548 env->interrupt_request &= ~0x01;
549 tlb_flush(env, 1);
550 cpu_synchronize_state(env, 1);
552 return 0;
554 #endif
556 CPUState *qemu_get_cpu(int cpu)
558 CPUState *env = first_cpu;
560 while (env) {
561 if (env->cpu_index == cpu)
562 break;
563 env = env->next_cpu;
566 return env;
569 void cpu_exec_init(CPUState *env)
571 CPUState **penv;
572 int cpu_index;
574 #if defined(CONFIG_USER_ONLY)
575 cpu_list_lock();
576 #endif
577 env->next_cpu = NULL;
578 penv = &first_cpu;
579 cpu_index = 0;
580 while (*penv != NULL) {
581 penv = &(*penv)->next_cpu;
582 cpu_index++;
584 env->cpu_index = cpu_index;
585 env->numa_node = 0;
586 TAILQ_INIT(&env->breakpoints);
587 TAILQ_INIT(&env->watchpoints);
588 #ifdef __WIN32
589 env->thread_id = GetCurrentProcessId();
590 #else
591 env->thread_id = getpid();
592 #endif
593 *penv = env;
594 #if defined(CONFIG_USER_ONLY)
595 cpu_list_unlock();
596 #endif
597 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
598 register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
599 cpu_common_save, cpu_common_load, env);
600 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
601 cpu_save, cpu_load, env);
602 #endif
605 static inline void invalidate_page_bitmap(PageDesc *p)
607 if (p->code_bitmap) {
608 qemu_free(p->code_bitmap);
609 p->code_bitmap = NULL;
611 p->code_write_count = 0;
614 /* set to NULL all the 'first_tb' fields in all PageDescs */
615 static void page_flush_tb(void)
617 int i, j;
618 PageDesc *p;
620 for(i = 0; i < L1_SIZE; i++) {
621 p = l1_map[i];
622 if (p) {
623 for(j = 0; j < L2_SIZE; j++) {
624 p->first_tb = NULL;
625 invalidate_page_bitmap(p);
626 p++;
632 /* flush all the translation blocks */
633 /* XXX: tb_flush is currently not thread safe */
634 void tb_flush(CPUState *env1)
636 CPUState *env;
637 #if defined(DEBUG_FLUSH)
638 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
639 (unsigned long)(code_gen_ptr - code_gen_buffer),
640 nb_tbs, nb_tbs > 0 ?
641 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
642 #endif
643 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
644 cpu_abort(env1, "Internal error: code buffer overflow\n");
646 nb_tbs = 0;
648 for(env = first_cpu; env != NULL; env = env->next_cpu) {
649 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
652 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
653 page_flush_tb();
655 code_gen_ptr = code_gen_buffer;
656 /* XXX: flush processor icache at this point if cache flush is
657 expensive */
658 tb_flush_count++;
661 #ifdef DEBUG_TB_CHECK
663 static void tb_invalidate_check(target_ulong address)
665 TranslationBlock *tb;
666 int i;
667 address &= TARGET_PAGE_MASK;
668 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
669 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
670 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
671 address >= tb->pc + tb->size)) {
672 printf("ERROR invalidate: address=" TARGET_FMT_lx
673 " PC=%08lx size=%04x\n",
674 address, (long)tb->pc, tb->size);
680 /* verify that all the pages have correct rights for code */
681 static void tb_page_check(void)
683 TranslationBlock *tb;
684 int i, flags1, flags2;
686 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
687 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
688 flags1 = page_get_flags(tb->pc);
689 flags2 = page_get_flags(tb->pc + tb->size - 1);
690 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
691 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
692 (long)tb->pc, tb->size, flags1, flags2);
698 #endif
700 /* invalidate one TB */
701 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
702 int next_offset)
704 TranslationBlock *tb1;
705 for(;;) {
706 tb1 = *ptb;
707 if (tb1 == tb) {
708 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
709 break;
711 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
715 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
717 TranslationBlock *tb1;
718 unsigned int n1;
720 for(;;) {
721 tb1 = *ptb;
722 n1 = (long)tb1 & 3;
723 tb1 = (TranslationBlock *)((long)tb1 & ~3);
724 if (tb1 == tb) {
725 *ptb = tb1->page_next[n1];
726 break;
728 ptb = &tb1->page_next[n1];
732 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
734 TranslationBlock *tb1, **ptb;
735 unsigned int n1;
737 ptb = &tb->jmp_next[n];
738 tb1 = *ptb;
739 if (tb1) {
740 /* find tb(n) in circular list */
741 for(;;) {
742 tb1 = *ptb;
743 n1 = (long)tb1 & 3;
744 tb1 = (TranslationBlock *)((long)tb1 & ~3);
745 if (n1 == n && tb1 == tb)
746 break;
747 if (n1 == 2) {
748 ptb = &tb1->jmp_first;
749 } else {
750 ptb = &tb1->jmp_next[n1];
753 /* now we can suppress tb(n) from the list */
754 *ptb = tb->jmp_next[n];
756 tb->jmp_next[n] = NULL;
760 /* reset the jump entry 'n' of a TB so that it is not chained to
761 another TB */
762 static inline void tb_reset_jump(TranslationBlock *tb, int n)
764 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
767 void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
769 CPUState *env;
770 PageDesc *p;
771 unsigned int h, n1;
772 target_phys_addr_t phys_pc;
773 TranslationBlock *tb1, *tb2;
775 /* remove the TB from the hash list */
776 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
777 h = tb_phys_hash_func(phys_pc);
778 tb_remove(&tb_phys_hash[h], tb,
779 offsetof(TranslationBlock, phys_hash_next));
781 /* remove the TB from the page list */
782 if (tb->page_addr[0] != page_addr) {
783 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
784 tb_page_remove(&p->first_tb, tb);
785 invalidate_page_bitmap(p);
787 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
788 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
789 tb_page_remove(&p->first_tb, tb);
790 invalidate_page_bitmap(p);
793 tb_invalidated_flag = 1;
795 /* remove the TB from the hash list */
796 h = tb_jmp_cache_hash_func(tb->pc);
797 for(env = first_cpu; env != NULL; env = env->next_cpu) {
798 if (env->tb_jmp_cache[h] == tb)
799 env->tb_jmp_cache[h] = NULL;
802 /* suppress this TB from the two jump lists */
803 tb_jmp_remove(tb, 0);
804 tb_jmp_remove(tb, 1);
806 /* suppress any remaining jumps to this TB */
807 tb1 = tb->jmp_first;
808 for(;;) {
809 n1 = (long)tb1 & 3;
810 if (n1 == 2)
811 break;
812 tb1 = (TranslationBlock *)((long)tb1 & ~3);
813 tb2 = tb1->jmp_next[n1];
814 tb_reset_jump(tb1, n1);
815 tb1->jmp_next[n1] = NULL;
816 tb1 = tb2;
818 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
820 tb_phys_invalidate_count++;
823 static inline void set_bits(uint8_t *tab, int start, int len)
825 int end, mask, end1;
827 end = start + len;
828 tab += start >> 3;
829 mask = 0xff << (start & 7);
830 if ((start & ~7) == (end & ~7)) {
831 if (start < end) {
832 mask &= ~(0xff << (end & 7));
833 *tab |= mask;
835 } else {
836 *tab++ |= mask;
837 start = (start + 8) & ~7;
838 end1 = end & ~7;
839 while (start < end1) {
840 *tab++ = 0xff;
841 start += 8;
843 if (start < end) {
844 mask = ~(0xff << (end & 7));
845 *tab |= mask;
850 static void build_page_bitmap(PageDesc *p)
852 int n, tb_start, tb_end;
853 TranslationBlock *tb;
855 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
857 tb = p->first_tb;
858 while (tb != NULL) {
859 n = (long)tb & 3;
860 tb = (TranslationBlock *)((long)tb & ~3);
861 /* NOTE: this is subtle as a TB may span two physical pages */
862 if (n == 0) {
863 /* NOTE: tb_end may be after the end of the page, but
864 it is not a problem */
865 tb_start = tb->pc & ~TARGET_PAGE_MASK;
866 tb_end = tb_start + tb->size;
867 if (tb_end > TARGET_PAGE_SIZE)
868 tb_end = TARGET_PAGE_SIZE;
869 } else {
870 tb_start = 0;
871 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
873 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
874 tb = tb->page_next[n];
878 TranslationBlock *tb_gen_code(CPUState *env,
879 target_ulong pc, target_ulong cs_base,
880 int flags, int cflags)
882 TranslationBlock *tb;
883 uint8_t *tc_ptr;
884 target_ulong phys_pc, phys_page2, virt_page2;
885 int code_gen_size;
887 phys_pc = get_phys_addr_code(env, pc);
888 tb = tb_alloc(pc);
889 if (!tb) {
890 /* flush must be done */
891 tb_flush(env);
892 /* cannot fail at this point */
893 tb = tb_alloc(pc);
894 /* Don't forget to invalidate previous TB info. */
895 tb_invalidated_flag = 1;
897 tc_ptr = code_gen_ptr;
898 tb->tc_ptr = tc_ptr;
899 tb->cs_base = cs_base;
900 tb->flags = flags;
901 tb->cflags = cflags;
902 cpu_gen_code(env, tb, &code_gen_size);
903 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
905 /* check next page if needed */
906 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
907 phys_page2 = -1;
908 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
909 phys_page2 = get_phys_addr_code(env, virt_page2);
911 tb_link_phys(tb, phys_pc, phys_page2);
912 return tb;
915 /* invalidate all TBs which intersect with the target physical page
916 starting in range [start;end[. NOTE: start and end must refer to
917 the same physical page. 'is_cpu_write_access' should be true if called
918 from a real cpu write access: the virtual CPU will exit the current
919 TB if code is modified inside this TB. */
920 void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
921 int is_cpu_write_access)
923 TranslationBlock *tb, *tb_next, *saved_tb;
924 CPUState *env = cpu_single_env;
925 target_ulong tb_start, tb_end;
926 PageDesc *p;
927 int n;
928 #ifdef TARGET_HAS_PRECISE_SMC
929 int current_tb_not_found = is_cpu_write_access;
930 TranslationBlock *current_tb = NULL;
931 int current_tb_modified = 0;
932 target_ulong current_pc = 0;
933 target_ulong current_cs_base = 0;
934 int current_flags = 0;
935 #endif /* TARGET_HAS_PRECISE_SMC */
937 p = page_find(start >> TARGET_PAGE_BITS);
938 if (!p)
939 return;
940 if (!p->code_bitmap &&
941 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
942 is_cpu_write_access) {
943 /* build code bitmap */
944 build_page_bitmap(p);
947 /* we remove all the TBs in the range [start, end[ */
948 /* XXX: see if in some cases it could be faster to invalidate all the code */
949 tb = p->first_tb;
950 while (tb != NULL) {
951 n = (long)tb & 3;
952 tb = (TranslationBlock *)((long)tb & ~3);
953 tb_next = tb->page_next[n];
954 /* NOTE: this is subtle as a TB may span two physical pages */
955 if (n == 0) {
956 /* NOTE: tb_end may be after the end of the page, but
957 it is not a problem */
958 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
959 tb_end = tb_start + tb->size;
960 } else {
961 tb_start = tb->page_addr[1];
962 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
964 if (!(tb_end <= start || tb_start >= end)) {
965 #ifdef TARGET_HAS_PRECISE_SMC
966 if (current_tb_not_found) {
967 current_tb_not_found = 0;
968 current_tb = NULL;
969 if (env->mem_io_pc) {
970 /* now we have a real cpu fault */
971 current_tb = tb_find_pc(env->mem_io_pc);
974 if (current_tb == tb &&
975 (current_tb->cflags & CF_COUNT_MASK) != 1) {
976 /* If we are modifying the current TB, we must stop
977 its execution. We could be more precise by checking
978 that the modification is after the current PC, but it
979 would require a specialized function to partially
980 restore the CPU state */
982 current_tb_modified = 1;
983 cpu_restore_state(current_tb, env,
984 env->mem_io_pc, NULL);
985 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
986 &current_flags);
988 #endif /* TARGET_HAS_PRECISE_SMC */
989 /* we need to do that to handle the case where a signal
990 occurs while doing tb_phys_invalidate() */
991 saved_tb = NULL;
992 if (env) {
993 saved_tb = env->current_tb;
994 env->current_tb = NULL;
996 tb_phys_invalidate(tb, -1);
997 if (env) {
998 env->current_tb = saved_tb;
999 if (env->interrupt_request && env->current_tb)
1000 cpu_interrupt(env, env->interrupt_request);
1003 tb = tb_next;
1005 #if !defined(CONFIG_USER_ONLY)
1006 /* if no code remaining, no need to continue to use slow writes */
1007 if (!p->first_tb) {
1008 invalidate_page_bitmap(p);
1009 if (is_cpu_write_access) {
1010 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
1013 #endif
1014 #ifdef TARGET_HAS_PRECISE_SMC
1015 if (current_tb_modified) {
1016 /* we generate a block containing just the instruction
1017 modifying the memory. It will ensure that it cannot modify
1018 itself */
1019 env->current_tb = NULL;
1020 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1021 cpu_resume_from_signal(env, NULL);
1023 #endif
1026 /* len must be <= 8 and start must be a multiple of len */
1027 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
1029 PageDesc *p;
1030 int offset, b;
1031 #if 0
1032 if (1) {
1033 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1034 cpu_single_env->mem_io_vaddr, len,
1035 cpu_single_env->eip,
1036 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
1038 #endif
1039 p = page_find(start >> TARGET_PAGE_BITS);
1040 if (!p)
1041 return;
1042 if (p->code_bitmap) {
1043 offset = start & ~TARGET_PAGE_MASK;
1044 b = p->code_bitmap[offset >> 3] >> (offset & 7);
1045 if (b & ((1 << len) - 1))
1046 goto do_invalidate;
1047 } else {
1048 do_invalidate:
1049 tb_invalidate_phys_page_range(start, start + len, 1);
1053 #if !defined(CONFIG_SOFTMMU)
1054 static void tb_invalidate_phys_page(target_phys_addr_t addr,
1055 unsigned long pc, void *puc)
1057 TranslationBlock *tb;
1058 PageDesc *p;
1059 int n;
1060 #ifdef TARGET_HAS_PRECISE_SMC
1061 TranslationBlock *current_tb = NULL;
1062 CPUState *env = cpu_single_env;
1063 int current_tb_modified = 0;
1064 target_ulong current_pc = 0;
1065 target_ulong current_cs_base = 0;
1066 int current_flags = 0;
1067 #endif
1069 addr &= TARGET_PAGE_MASK;
1070 p = page_find(addr >> TARGET_PAGE_BITS);
1071 if (!p)
1072 return;
1073 tb = p->first_tb;
1074 #ifdef TARGET_HAS_PRECISE_SMC
1075 if (tb && pc != 0) {
1076 current_tb = tb_find_pc(pc);
1078 #endif
1079 while (tb != NULL) {
1080 n = (long)tb & 3;
1081 tb = (TranslationBlock *)((long)tb & ~3);
1082 #ifdef TARGET_HAS_PRECISE_SMC
1083 if (current_tb == tb &&
1084 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1085 /* If we are modifying the current TB, we must stop
1086 its execution. We could be more precise by checking
1087 that the modification is after the current PC, but it
1088 would require a specialized function to partially
1089 restore the CPU state */
1091 current_tb_modified = 1;
1092 cpu_restore_state(current_tb, env, pc, puc);
1093 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1094 &current_flags);
1096 #endif /* TARGET_HAS_PRECISE_SMC */
1097 tb_phys_invalidate(tb, addr);
1098 tb = tb->page_next[n];
1100 p->first_tb = NULL;
1101 #ifdef TARGET_HAS_PRECISE_SMC
1102 if (current_tb_modified) {
1103 /* we generate a block containing just the instruction
1104 modifying the memory. It will ensure that it cannot modify
1105 itself */
1106 env->current_tb = NULL;
1107 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1108 cpu_resume_from_signal(env, puc);
1110 #endif
1112 #endif
1114 /* add the tb in the target page and protect it if necessary */
1115 static inline void tb_alloc_page(TranslationBlock *tb,
1116 unsigned int n, target_ulong page_addr)
1118 PageDesc *p;
1119 TranslationBlock *last_first_tb;
1121 tb->page_addr[n] = page_addr;
1122 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
1123 tb->page_next[n] = p->first_tb;
1124 last_first_tb = p->first_tb;
1125 p->first_tb = (TranslationBlock *)((long)tb | n);
1126 invalidate_page_bitmap(p);
1128 #if defined(TARGET_HAS_SMC) || 1
1130 #if defined(CONFIG_USER_ONLY)
1131 if (p->flags & PAGE_WRITE) {
1132 target_ulong addr;
1133 PageDesc *p2;
1134 int prot;
1136 /* force the host page as non writable (writes will have a
1137 page fault + mprotect overhead) */
1138 page_addr &= qemu_host_page_mask;
1139 prot = 0;
1140 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1141 addr += TARGET_PAGE_SIZE) {
1143 p2 = page_find (addr >> TARGET_PAGE_BITS);
1144 if (!p2)
1145 continue;
1146 prot |= p2->flags;
1147 p2->flags &= ~PAGE_WRITE;
1148 page_get_flags(addr);
1150 mprotect(g2h(page_addr), qemu_host_page_size,
1151 (prot & PAGE_BITS) & ~PAGE_WRITE);
1152 #ifdef DEBUG_TB_INVALIDATE
1153 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1154 page_addr);
1155 #endif
1157 #else
1158 /* if some code is already present, then the pages are already
1159 protected. So we handle the case where only the first TB is
1160 allocated in a physical page */
1161 if (!last_first_tb) {
1162 tlb_protect_code(page_addr);
1164 #endif
1166 #endif /* TARGET_HAS_SMC */
1169 /* Allocate a new translation block. Flush the translation buffer if
1170 too many translation blocks or too much generated code. */
1171 TranslationBlock *tb_alloc(target_ulong pc)
1173 TranslationBlock *tb;
1175 if (nb_tbs >= code_gen_max_blocks ||
1176 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
1177 return NULL;
1178 tb = &tbs[nb_tbs++];
1179 tb->pc = pc;
1180 tb->cflags = 0;
1181 return tb;
1184 void tb_free(TranslationBlock *tb)
1186 /* In practice this is mostly used for single use temporary TB
1187 Ignore the hard cases and just back up if this TB happens to
1188 be the last one generated. */
1189 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1190 code_gen_ptr = tb->tc_ptr;
1191 nb_tbs--;
1195 /* add a new TB and link it to the physical page tables. phys_page2 is
1196 (-1) to indicate that only one page contains the TB. */
1197 void tb_link_phys(TranslationBlock *tb,
1198 target_ulong phys_pc, target_ulong phys_page2)
1200 unsigned int h;
1201 TranslationBlock **ptb;
1203 /* Grab the mmap lock to stop another thread invalidating this TB
1204 before we are done. */
1205 mmap_lock();
1206 /* add in the physical hash table */
1207 h = tb_phys_hash_func(phys_pc);
1208 ptb = &tb_phys_hash[h];
1209 tb->phys_hash_next = *ptb;
1210 *ptb = tb;
1212 /* add in the page list */
1213 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1214 if (phys_page2 != -1)
1215 tb_alloc_page(tb, 1, phys_page2);
1216 else
1217 tb->page_addr[1] = -1;
1219 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1220 tb->jmp_next[0] = NULL;
1221 tb->jmp_next[1] = NULL;
1223 /* init original jump addresses */
1224 if (tb->tb_next_offset[0] != 0xffff)
1225 tb_reset_jump(tb, 0);
1226 if (tb->tb_next_offset[1] != 0xffff)
1227 tb_reset_jump(tb, 1);
1229 #ifdef DEBUG_TB_CHECK
1230 tb_page_check();
1231 #endif
1232 mmap_unlock();
1235 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1236 tb[1].tc_ptr. Return NULL if not found */
1237 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1239 int m_min, m_max, m;
1240 unsigned long v;
1241 TranslationBlock *tb;
1243 if (nb_tbs <= 0)
1244 return NULL;
1245 if (tc_ptr < (unsigned long)code_gen_buffer ||
1246 tc_ptr >= (unsigned long)code_gen_ptr)
1247 return NULL;
1248 /* binary search (cf Knuth) */
1249 m_min = 0;
1250 m_max = nb_tbs - 1;
1251 while (m_min <= m_max) {
1252 m = (m_min + m_max) >> 1;
1253 tb = &tbs[m];
1254 v = (unsigned long)tb->tc_ptr;
1255 if (v == tc_ptr)
1256 return tb;
1257 else if (tc_ptr < v) {
1258 m_max = m - 1;
1259 } else {
1260 m_min = m + 1;
1263 return &tbs[m_max];
1266 static void tb_reset_jump_recursive(TranslationBlock *tb);
1268 static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1270 TranslationBlock *tb1, *tb_next, **ptb;
1271 unsigned int n1;
1273 tb1 = tb->jmp_next[n];
1274 if (tb1 != NULL) {
1275 /* find head of list */
1276 for(;;) {
1277 n1 = (long)tb1 & 3;
1278 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1279 if (n1 == 2)
1280 break;
1281 tb1 = tb1->jmp_next[n1];
1283 /* we are now sure now that tb jumps to tb1 */
1284 tb_next = tb1;
1286 /* remove tb from the jmp_first list */
1287 ptb = &tb_next->jmp_first;
1288 for(;;) {
1289 tb1 = *ptb;
1290 n1 = (long)tb1 & 3;
1291 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1292 if (n1 == n && tb1 == tb)
1293 break;
1294 ptb = &tb1->jmp_next[n1];
1296 *ptb = tb->jmp_next[n];
1297 tb->jmp_next[n] = NULL;
1299 /* suppress the jump to next tb in generated code */
1300 tb_reset_jump(tb, n);
1302 /* suppress jumps in the tb on which we could have jumped */
1303 tb_reset_jump_recursive(tb_next);
1307 static void tb_reset_jump_recursive(TranslationBlock *tb)
1309 tb_reset_jump_recursive2(tb, 0);
1310 tb_reset_jump_recursive2(tb, 1);
1313 #if defined(TARGET_HAS_ICE)
1314 static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1316 target_phys_addr_t addr;
1317 target_ulong pd;
1318 ram_addr_t ram_addr;
1319 PhysPageDesc *p;
1321 addr = cpu_get_phys_page_debug(env, pc);
1322 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1323 if (!p) {
1324 pd = IO_MEM_UNASSIGNED;
1325 } else {
1326 pd = p->phys_offset;
1328 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
1329 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1331 #endif
1333 /* Add a watchpoint. */
1334 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
1335 int flags, CPUWatchpoint **watchpoint)
1337 target_ulong len_mask = ~(len - 1);
1338 CPUWatchpoint *wp;
1340 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1341 if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
1342 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
1343 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
1344 return -EINVAL;
1346 wp = qemu_malloc(sizeof(*wp));
1348 wp->vaddr = addr;
1349 wp->len_mask = len_mask;
1350 wp->flags = flags;
1352 /* keep all GDB-injected watchpoints in front */
1353 if (flags & BP_GDB)
1354 TAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
1355 else
1356 TAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
1358 tlb_flush_page(env, addr);
1360 if (watchpoint)
1361 *watchpoint = wp;
1362 return 0;
1365 /* Remove a specific watchpoint. */
1366 int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
1367 int flags)
1369 target_ulong len_mask = ~(len - 1);
1370 CPUWatchpoint *wp;
1372 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
1373 if (addr == wp->vaddr && len_mask == wp->len_mask
1374 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
1375 cpu_watchpoint_remove_by_ref(env, wp);
1376 return 0;
1379 return -ENOENT;
1382 /* Remove a specific watchpoint by reference. */
1383 void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
1385 TAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
1387 tlb_flush_page(env, watchpoint->vaddr);
1389 qemu_free(watchpoint);
1392 /* Remove all matching watchpoints. */
1393 void cpu_watchpoint_remove_all(CPUState *env, int mask)
1395 CPUWatchpoint *wp, *next;
1397 TAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
1398 if (wp->flags & mask)
1399 cpu_watchpoint_remove_by_ref(env, wp);
1403 /* Add a breakpoint. */
1404 int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
1405 CPUBreakpoint **breakpoint)
1407 #if defined(TARGET_HAS_ICE)
1408 CPUBreakpoint *bp;
1410 bp = qemu_malloc(sizeof(*bp));
1412 bp->pc = pc;
1413 bp->flags = flags;
1415 /* keep all GDB-injected breakpoints in front */
1416 if (flags & BP_GDB)
1417 TAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
1418 else
1419 TAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
1421 breakpoint_invalidate(env, pc);
1423 if (breakpoint)
1424 *breakpoint = bp;
1425 return 0;
1426 #else
1427 return -ENOSYS;
1428 #endif
1431 /* Remove a specific breakpoint. */
1432 int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
1434 #if defined(TARGET_HAS_ICE)
1435 CPUBreakpoint *bp;
1437 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
1438 if (bp->pc == pc && bp->flags == flags) {
1439 cpu_breakpoint_remove_by_ref(env, bp);
1440 return 0;
1443 return -ENOENT;
1444 #else
1445 return -ENOSYS;
1446 #endif
1449 /* Remove a specific breakpoint by reference. */
1450 void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
1452 #if defined(TARGET_HAS_ICE)
1453 TAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
1455 breakpoint_invalidate(env, breakpoint->pc);
1457 qemu_free(breakpoint);
1458 #endif
1461 /* Remove all matching breakpoints. */
1462 void cpu_breakpoint_remove_all(CPUState *env, int mask)
1464 #if defined(TARGET_HAS_ICE)
1465 CPUBreakpoint *bp, *next;
1467 TAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
1468 if (bp->flags & mask)
1469 cpu_breakpoint_remove_by_ref(env, bp);
1471 #endif
1474 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1475 CPU loop after each instruction */
1476 void cpu_single_step(CPUState *env, int enabled)
1478 #if defined(TARGET_HAS_ICE)
1479 if (env->singlestep_enabled != enabled) {
1480 env->singlestep_enabled = enabled;
1481 if (kvm_enabled())
1482 kvm_update_guest_debug(env, 0);
1483 else {
1484 /* must flush all the translated code to avoid inconsistencies */
1485 /* XXX: only flush what is necessary */
1486 tb_flush(env);
1489 #endif
1492 /* enable or disable low levels log */
1493 void cpu_set_log(int log_flags)
1495 loglevel = log_flags;
1496 if (loglevel && !logfile) {
1497 logfile = fopen(logfilename, log_append ? "a" : "w");
1498 if (!logfile) {
1499 perror(logfilename);
1500 _exit(1);
1502 #if !defined(CONFIG_SOFTMMU)
1503 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1505 static char logfile_buf[4096];
1506 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1508 #else
1509 setvbuf(logfile, NULL, _IOLBF, 0);
1510 #endif
1511 log_append = 1;
1513 if (!loglevel && logfile) {
1514 fclose(logfile);
1515 logfile = NULL;
1519 void cpu_set_log_filename(const char *filename)
1521 logfilename = strdup(filename);
1522 if (logfile) {
1523 fclose(logfile);
1524 logfile = NULL;
1526 cpu_set_log(loglevel);
1529 static void cpu_unlink_tb(CPUState *env)
1531 #if defined(USE_NPTL)
1532 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1533 problem and hope the cpu will stop of its own accord. For userspace
1534 emulation this often isn't actually as bad as it sounds. Often
1535 signals are used primarily to interrupt blocking syscalls. */
1536 #else
1537 TranslationBlock *tb;
1538 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
1540 tb = env->current_tb;
1541 /* if the cpu is currently executing code, we must unlink it and
1542 all the potentially executing TB */
1543 if (tb && !testandset(&interrupt_lock)) {
1544 env->current_tb = NULL;
1545 tb_reset_jump_recursive(tb);
1546 resetlock(&interrupt_lock);
1548 #endif
1551 /* mask must never be zero, except for A20 change call */
1552 void cpu_interrupt(CPUState *env, int mask)
1554 int old_mask;
1556 old_mask = env->interrupt_request;
1557 env->interrupt_request |= mask;
1558 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
1559 kvm_update_interrupt_request(env);
1561 #ifndef CONFIG_USER_ONLY
1563 * If called from iothread context, wake the target cpu in
1564 * case its halted.
1566 if (!qemu_cpu_self(env)) {
1567 qemu_cpu_kick(env);
1568 return;
1570 #endif
1572 if (use_icount) {
1573 env->icount_decr.u16.high = 0xffff;
1574 #ifndef CONFIG_USER_ONLY
1575 if (!can_do_io(env)
1576 && (mask & ~old_mask) != 0) {
1577 cpu_abort(env, "Raised interrupt while not in I/O function");
1579 #endif
1580 } else {
1581 cpu_unlink_tb(env);
1585 void cpu_reset_interrupt(CPUState *env, int mask)
1587 env->interrupt_request &= ~mask;
1590 void cpu_exit(CPUState *env)
1592 env->exit_request = 1;
1593 cpu_unlink_tb(env);
1596 const CPULogItem cpu_log_items[] = {
1597 { CPU_LOG_TB_OUT_ASM, "out_asm",
1598 "show generated host assembly code for each compiled TB" },
1599 { CPU_LOG_TB_IN_ASM, "in_asm",
1600 "show target assembly code for each compiled TB" },
1601 { CPU_LOG_TB_OP, "op",
1602 "show micro ops for each compiled TB" },
1603 { CPU_LOG_TB_OP_OPT, "op_opt",
1604 "show micro ops "
1605 #ifdef TARGET_I386
1606 "before eflags optimization and "
1607 #endif
1608 "after liveness analysis" },
1609 { CPU_LOG_INT, "int",
1610 "show interrupts/exceptions in short format" },
1611 { CPU_LOG_EXEC, "exec",
1612 "show trace before each executed TB (lots of logs)" },
1613 { CPU_LOG_TB_CPU, "cpu",
1614 "show CPU state before block translation" },
1615 #ifdef TARGET_I386
1616 { CPU_LOG_PCALL, "pcall",
1617 "show protected mode far calls/returns/exceptions" },
1618 { CPU_LOG_RESET, "cpu_reset",
1619 "show CPU state before CPU resets" },
1620 #endif
1621 #ifdef DEBUG_IOPORT
1622 { CPU_LOG_IOPORT, "ioport",
1623 "show all i/o ports accesses" },
1624 #endif
1625 { 0, NULL, NULL },
1628 static int cmp1(const char *s1, int n, const char *s2)
1630 if (strlen(s2) != n)
1631 return 0;
1632 return memcmp(s1, s2, n) == 0;
1635 /* takes a comma separated list of log masks. Return 0 if error. */
1636 int cpu_str_to_log_mask(const char *str)
1638 const CPULogItem *item;
1639 int mask;
1640 const char *p, *p1;
1642 p = str;
1643 mask = 0;
1644 for(;;) {
1645 p1 = strchr(p, ',');
1646 if (!p1)
1647 p1 = p + strlen(p);
1648 if(cmp1(p,p1-p,"all")) {
1649 for(item = cpu_log_items; item->mask != 0; item++) {
1650 mask |= item->mask;
1652 } else {
1653 for(item = cpu_log_items; item->mask != 0; item++) {
1654 if (cmp1(p, p1 - p, item->name))
1655 goto found;
1657 return 0;
1659 found:
1660 mask |= item->mask;
1661 if (*p1 != ',')
1662 break;
1663 p = p1 + 1;
1665 return mask;
1668 void cpu_abort(CPUState *env, const char *fmt, ...)
1670 va_list ap;
1671 va_list ap2;
1673 va_start(ap, fmt);
1674 va_copy(ap2, ap);
1675 fprintf(stderr, "qemu: fatal: ");
1676 vfprintf(stderr, fmt, ap);
1677 fprintf(stderr, "\n");
1678 #ifdef TARGET_I386
1679 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1680 #else
1681 cpu_dump_state(env, stderr, fprintf, 0);
1682 #endif
1683 if (qemu_log_enabled()) {
1684 qemu_log("qemu: fatal: ");
1685 qemu_log_vprintf(fmt, ap2);
1686 qemu_log("\n");
1687 #ifdef TARGET_I386
1688 log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
1689 #else
1690 log_cpu_state(env, 0);
1691 #endif
1692 qemu_log_flush();
1693 qemu_log_close();
1695 va_end(ap2);
1696 va_end(ap);
1697 abort();
1700 CPUState *cpu_copy(CPUState *env)
1702 CPUState *new_env = cpu_init(env->cpu_model_str);
1703 CPUState *next_cpu = new_env->next_cpu;
1704 int cpu_index = new_env->cpu_index;
1705 #if defined(TARGET_HAS_ICE)
1706 CPUBreakpoint *bp;
1707 CPUWatchpoint *wp;
1708 #endif
1710 memcpy(new_env, env, sizeof(CPUState));
1712 /* Preserve chaining and index. */
1713 new_env->next_cpu = next_cpu;
1714 new_env->cpu_index = cpu_index;
1716 /* Clone all break/watchpoints.
1717 Note: Once we support ptrace with hw-debug register access, make sure
1718 BP_CPU break/watchpoints are handled correctly on clone. */
1719 TAILQ_INIT(&env->breakpoints);
1720 TAILQ_INIT(&env->watchpoints);
1721 #if defined(TARGET_HAS_ICE)
1722 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
1723 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
1725 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
1726 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
1727 wp->flags, NULL);
1729 #endif
1731 return new_env;
1734 #if !defined(CONFIG_USER_ONLY)
1736 static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1738 unsigned int i;
1740 /* Discard jump cache entries for any tb which might potentially
1741 overlap the flushed page. */
1742 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1743 memset (&env->tb_jmp_cache[i], 0,
1744 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1746 i = tb_jmp_cache_hash_page(addr);
1747 memset (&env->tb_jmp_cache[i], 0,
1748 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1751 static CPUTLBEntry s_cputlb_empty_entry = {
1752 .addr_read = -1,
1753 .addr_write = -1,
1754 .addr_code = -1,
1755 .addend = -1,
1758 /* NOTE: if flush_global is true, also flush global entries (not
1759 implemented yet) */
1760 void tlb_flush(CPUState *env, int flush_global)
1762 int i;
1764 #if defined(DEBUG_TLB)
1765 printf("tlb_flush:\n");
1766 #endif
1767 /* must reset current TB so that interrupts cannot modify the
1768 links while we are modifying them */
1769 env->current_tb = NULL;
1771 for(i = 0; i < CPU_TLB_SIZE; i++) {
1772 int mmu_idx;
1773 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1774 env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
1778 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
1780 #ifdef CONFIG_KQEMU
1781 if (env->kqemu_enabled) {
1782 kqemu_flush(env, flush_global);
1784 #endif
1785 tlb_flush_count++;
1788 static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
1790 if (addr == (tlb_entry->addr_read &
1791 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1792 addr == (tlb_entry->addr_write &
1793 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1794 addr == (tlb_entry->addr_code &
1795 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1796 *tlb_entry = s_cputlb_empty_entry;
1800 void tlb_flush_page(CPUState *env, target_ulong addr)
1802 int i;
1803 int mmu_idx;
1805 #if defined(DEBUG_TLB)
1806 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
1807 #endif
1808 /* must reset current TB so that interrupts cannot modify the
1809 links while we are modifying them */
1810 env->current_tb = NULL;
1812 addr &= TARGET_PAGE_MASK;
1813 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1814 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1815 tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
1817 tlb_flush_jmp_cache(env, addr);
1819 #ifdef CONFIG_KQEMU
1820 if (env->kqemu_enabled) {
1821 kqemu_flush_page(env, addr);
1823 #endif
1826 /* update the TLBs so that writes to code in the virtual page 'addr'
1827 can be detected */
1828 static void tlb_protect_code(ram_addr_t ram_addr)
1830 cpu_physical_memory_reset_dirty(ram_addr,
1831 ram_addr + TARGET_PAGE_SIZE,
1832 CODE_DIRTY_FLAG);
1835 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1836 tested for self modifying code */
1837 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
1838 target_ulong vaddr)
1840 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
1843 static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
1844 unsigned long start, unsigned long length)
1846 unsigned long addr;
1847 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1848 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
1849 if ((addr - start) < length) {
1850 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
1855 /* Note: start and end must be within the same ram block. */
1856 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
1857 int dirty_flags)
1859 CPUState *env;
1860 unsigned long length, start1;
1861 int i, mask, len;
1862 uint8_t *p;
1864 start &= TARGET_PAGE_MASK;
1865 end = TARGET_PAGE_ALIGN(end);
1867 length = end - start;
1868 if (length == 0)
1869 return;
1870 len = length >> TARGET_PAGE_BITS;
1871 #ifdef CONFIG_KQEMU
1872 /* XXX: should not depend on cpu context */
1873 env = first_cpu;
1874 if (env->kqemu_enabled) {
1875 ram_addr_t addr;
1876 addr = start;
1877 for(i = 0; i < len; i++) {
1878 kqemu_set_notdirty(env, addr);
1879 addr += TARGET_PAGE_SIZE;
1882 #endif
1883 mask = ~dirty_flags;
1884 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1885 for(i = 0; i < len; i++)
1886 p[i] &= mask;
1888 /* we modify the TLB cache so that the dirty bit will be set again
1889 when accessing the range */
1890 start1 = (unsigned long)qemu_get_ram_ptr(start);
1891 /* Chek that we don't span multiple blocks - this breaks the
1892 address comparisons below. */
1893 if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1
1894 != (end - 1) - start) {
1895 abort();
1898 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1899 int mmu_idx;
1900 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1901 for(i = 0; i < CPU_TLB_SIZE; i++)
1902 tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
1903 start1, length);
1908 int cpu_physical_memory_set_dirty_tracking(int enable)
1910 if (kvm_enabled()) {
1911 return kvm_set_migration_log(enable);
1913 return 0;
1916 int cpu_physical_memory_get_dirty_tracking(void)
1918 return in_migration;
1921 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1922 target_phys_addr_t end_addr)
1924 int ret = 0;
1926 if (kvm_enabled())
1927 ret = kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
1928 return ret;
1931 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1933 ram_addr_t ram_addr;
1934 void *p;
1936 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1937 p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
1938 + tlb_entry->addend);
1939 ram_addr = qemu_ram_addr_from_host(p);
1940 if (!cpu_physical_memory_is_dirty(ram_addr)) {
1941 tlb_entry->addr_write |= TLB_NOTDIRTY;
1946 /* update the TLB according to the current state of the dirty bits */
1947 void cpu_tlb_update_dirty(CPUState *env)
1949 int i;
1950 int mmu_idx;
1951 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
1952 for(i = 0; i < CPU_TLB_SIZE; i++)
1953 tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
1957 static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
1959 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1960 tlb_entry->addr_write = vaddr;
1963 /* update the TLB corresponding to virtual page vaddr
1964 so that it is no longer dirty */
1965 static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
1967 int i;
1968 int mmu_idx;
1970 vaddr &= TARGET_PAGE_MASK;
1971 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1972 for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
1973 tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
1976 /* add a new TLB entry. At most one entry for a given virtual address
1977 is permitted. Return 0 if OK or 2 if the page could not be mapped
1978 (can only happen in non SOFTMMU mode for I/O pages or pages
1979 conflicting with the host address space). */
1980 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1981 target_phys_addr_t paddr, int prot,
1982 int mmu_idx, int is_softmmu)
1984 PhysPageDesc *p;
1985 unsigned long pd;
1986 unsigned int index;
1987 target_ulong address;
1988 target_ulong code_address;
1989 target_phys_addr_t addend;
1990 int ret;
1991 CPUTLBEntry *te;
1992 CPUWatchpoint *wp;
1993 target_phys_addr_t iotlb;
1995 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
1996 if (!p) {
1997 pd = IO_MEM_UNASSIGNED;
1998 } else {
1999 pd = p->phys_offset;
2001 #if defined(DEBUG_TLB)
2002 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
2003 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
2004 #endif
2006 ret = 0;
2007 address = vaddr;
2008 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
2009 /* IO memory case (romd handled later) */
2010 address |= TLB_MMIO;
2012 addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
2013 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
2014 /* Normal RAM. */
2015 iotlb = pd & TARGET_PAGE_MASK;
2016 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
2017 iotlb |= IO_MEM_NOTDIRTY;
2018 else
2019 iotlb |= IO_MEM_ROM;
2020 } else {
2021 /* IO handlers are currently passed a physical address.
2022 It would be nice to pass an offset from the base address
2023 of that region. This would avoid having to special case RAM,
2024 and avoid full address decoding in every device.
2025 We can't use the high bits of pd for this because
2026 IO_MEM_ROMD uses these as a ram address. */
2027 iotlb = (pd & ~TARGET_PAGE_MASK);
2028 if (p) {
2029 iotlb += p->region_offset;
2030 } else {
2031 iotlb += paddr;
2035 code_address = address;
2036 /* Make accesses to pages with watchpoints go via the
2037 watchpoint trap routines. */
2038 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
2039 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
2040 iotlb = io_mem_watch + paddr;
2041 /* TODO: The memory case can be optimized by not trapping
2042 reads of pages with a write breakpoint. */
2043 address |= TLB_MMIO;
2047 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
2048 env->iotlb[mmu_idx][index] = iotlb - vaddr;
2049 te = &env->tlb_table[mmu_idx][index];
2050 te->addend = addend - vaddr;
2051 if (prot & PAGE_READ) {
2052 te->addr_read = address;
2053 } else {
2054 te->addr_read = -1;
2057 if (prot & PAGE_EXEC) {
2058 te->addr_code = code_address;
2059 } else {
2060 te->addr_code = -1;
2062 if (prot & PAGE_WRITE) {
2063 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
2064 (pd & IO_MEM_ROMD)) {
2065 /* Write access calls the I/O callback. */
2066 te->addr_write = address | TLB_MMIO;
2067 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
2068 !cpu_physical_memory_is_dirty(pd)) {
2069 te->addr_write = address | TLB_NOTDIRTY;
2070 } else {
2071 te->addr_write = address;
2073 } else {
2074 te->addr_write = -1;
2076 return ret;
2079 #else
2081 void tlb_flush(CPUState *env, int flush_global)
2085 void tlb_flush_page(CPUState *env, target_ulong addr)
2089 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
2090 target_phys_addr_t paddr, int prot,
2091 int mmu_idx, int is_softmmu)
2093 return 0;
2097 * Walks guest process memory "regions" one by one
2098 * and calls callback function 'fn' for each region.
2100 int walk_memory_regions(void *priv,
2101 int (*fn)(void *, unsigned long, unsigned long, unsigned long))
2103 unsigned long start, end;
2104 PageDesc *p = NULL;
2105 int i, j, prot, prot1;
2106 int rc = 0;
2108 start = end = -1;
2109 prot = 0;
2111 for (i = 0; i <= L1_SIZE; i++) {
2112 p = (i < L1_SIZE) ? l1_map[i] : NULL;
2113 for (j = 0; j < L2_SIZE; j++) {
2114 prot1 = (p == NULL) ? 0 : p[j].flags;
2116 * "region" is one continuous chunk of memory
2117 * that has same protection flags set.
2119 if (prot1 != prot) {
2120 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
2121 if (start != -1) {
2122 rc = (*fn)(priv, start, end, prot);
2123 /* callback can stop iteration by returning != 0 */
2124 if (rc != 0)
2125 return (rc);
2127 if (prot1 != 0)
2128 start = end;
2129 else
2130 start = -1;
2131 prot = prot1;
2133 if (p == NULL)
2134 break;
2137 return (rc);
2140 static int dump_region(void *priv, unsigned long start,
2141 unsigned long end, unsigned long prot)
2143 FILE *f = (FILE *)priv;
2145 (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
2146 start, end, end - start,
2147 ((prot & PAGE_READ) ? 'r' : '-'),
2148 ((prot & PAGE_WRITE) ? 'w' : '-'),
2149 ((prot & PAGE_EXEC) ? 'x' : '-'));
2151 return (0);
2154 /* dump memory mappings */
2155 void page_dump(FILE *f)
2157 (void) fprintf(f, "%-8s %-8s %-8s %s\n",
2158 "start", "end", "size", "prot");
2159 walk_memory_regions(f, dump_region);
2162 int page_get_flags(target_ulong address)
2164 PageDesc *p;
2166 p = page_find(address >> TARGET_PAGE_BITS);
2167 if (!p)
2168 return 0;
2169 return p->flags;
2172 /* modify the flags of a page and invalidate the code if
2173 necessary. The flag PAGE_WRITE_ORG is positioned automatically
2174 depending on PAGE_WRITE */
2175 void page_set_flags(target_ulong start, target_ulong end, int flags)
2177 PageDesc *p;
2178 target_ulong addr;
2180 /* mmap_lock should already be held. */
2181 start = start & TARGET_PAGE_MASK;
2182 end = TARGET_PAGE_ALIGN(end);
2183 if (flags & PAGE_WRITE)
2184 flags |= PAGE_WRITE_ORG;
2185 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2186 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
2187 /* We may be called for host regions that are outside guest
2188 address space. */
2189 if (!p)
2190 return;
2191 /* if the write protection is set, then we invalidate the code
2192 inside */
2193 if (!(p->flags & PAGE_WRITE) &&
2194 (flags & PAGE_WRITE) &&
2195 p->first_tb) {
2196 tb_invalidate_phys_page(addr, 0, NULL);
2198 p->flags = flags;
2202 int page_check_range(target_ulong start, target_ulong len, int flags)
2204 PageDesc *p;
2205 target_ulong end;
2206 target_ulong addr;
2208 if (start + len < start)
2209 /* we've wrapped around */
2210 return -1;
2212 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2213 start = start & TARGET_PAGE_MASK;
2215 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2216 p = page_find(addr >> TARGET_PAGE_BITS);
2217 if( !p )
2218 return -1;
2219 if( !(p->flags & PAGE_VALID) )
2220 return -1;
2222 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
2223 return -1;
2224 if (flags & PAGE_WRITE) {
2225 if (!(p->flags & PAGE_WRITE_ORG))
2226 return -1;
2227 /* unprotect the page if it was put read-only because it
2228 contains translated code */
2229 if (!(p->flags & PAGE_WRITE)) {
2230 if (!page_unprotect(addr, 0, NULL))
2231 return -1;
2233 return 0;
2236 return 0;
2239 /* called from signal handler: invalidate the code and unprotect the
2240 page. Return TRUE if the fault was successfully handled. */
2241 int page_unprotect(target_ulong address, unsigned long pc, void *puc)
2243 unsigned int page_index, prot, pindex;
2244 PageDesc *p, *p1;
2245 target_ulong host_start, host_end, addr;
2247 /* Technically this isn't safe inside a signal handler. However we
2248 know this only ever happens in a synchronous SEGV handler, so in
2249 practice it seems to be ok. */
2250 mmap_lock();
2252 host_start = address & qemu_host_page_mask;
2253 page_index = host_start >> TARGET_PAGE_BITS;
2254 p1 = page_find(page_index);
2255 if (!p1) {
2256 mmap_unlock();
2257 return 0;
2259 host_end = host_start + qemu_host_page_size;
2260 p = p1;
2261 prot = 0;
2262 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2263 prot |= p->flags;
2264 p++;
2266 /* if the page was really writable, then we change its
2267 protection back to writable */
2268 if (prot & PAGE_WRITE_ORG) {
2269 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2270 if (!(p1[pindex].flags & PAGE_WRITE)) {
2271 mprotect((void *)g2h(host_start), qemu_host_page_size,
2272 (prot & PAGE_BITS) | PAGE_WRITE);
2273 p1[pindex].flags |= PAGE_WRITE;
2274 /* and since the content will be modified, we must invalidate
2275 the corresponding translated code. */
2276 tb_invalidate_phys_page(address, pc, puc);
2277 #ifdef DEBUG_TB_CHECK
2278 tb_invalidate_check(address);
2279 #endif
2280 mmap_unlock();
2281 return 1;
2284 mmap_unlock();
2285 return 0;
2288 static inline void tlb_set_dirty(CPUState *env,
2289 unsigned long addr, target_ulong vaddr)
2292 #endif /* defined(CONFIG_USER_ONLY) */
2294 #if !defined(CONFIG_USER_ONLY)
2296 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2297 ram_addr_t memory, ram_addr_t region_offset);
2298 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2299 ram_addr_t orig_memory, ram_addr_t region_offset);
2300 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2301 need_subpage) \
2302 do { \
2303 if (addr > start_addr) \
2304 start_addr2 = 0; \
2305 else { \
2306 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2307 if (start_addr2 > 0) \
2308 need_subpage = 1; \
2311 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2312 end_addr2 = TARGET_PAGE_SIZE - 1; \
2313 else { \
2314 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2315 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2316 need_subpage = 1; \
2318 } while (0)
2320 /* register physical memory. 'size' must be a multiple of the target
2321 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2322 io memory page. The address used when calling the IO function is
2323 the offset from the start of the region, plus region_offset. Both
2324 start_addr and region_offset are rounded down to a page boundary
2325 before calculating this offset. This should not be a problem unless
2326 the low bits of start_addr and region_offset differ. */
2327 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
2328 ram_addr_t size,
2329 ram_addr_t phys_offset,
2330 ram_addr_t region_offset)
2332 target_phys_addr_t addr, end_addr;
2333 PhysPageDesc *p;
2334 CPUState *env;
2335 ram_addr_t orig_size = size;
2336 void *subpage;
2338 #ifdef CONFIG_KQEMU
2339 /* XXX: should not depend on cpu context */
2340 env = first_cpu;
2341 if (env->kqemu_enabled) {
2342 kqemu_set_phys_mem(start_addr, size, phys_offset);
2344 #endif
2345 if (kvm_enabled())
2346 kvm_set_phys_mem(start_addr, size, phys_offset);
2348 if (phys_offset == IO_MEM_UNASSIGNED) {
2349 region_offset = start_addr;
2351 region_offset &= TARGET_PAGE_MASK;
2352 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
2353 end_addr = start_addr + (target_phys_addr_t)size;
2354 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
2355 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2356 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2357 ram_addr_t orig_memory = p->phys_offset;
2358 target_phys_addr_t start_addr2, end_addr2;
2359 int need_subpage = 0;
2361 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2362 need_subpage);
2363 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2364 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2365 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2366 &p->phys_offset, orig_memory,
2367 p->region_offset);
2368 } else {
2369 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2370 >> IO_MEM_SHIFT];
2372 subpage_register(subpage, start_addr2, end_addr2, phys_offset,
2373 region_offset);
2374 p->region_offset = 0;
2375 } else {
2376 p->phys_offset = phys_offset;
2377 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2378 (phys_offset & IO_MEM_ROMD))
2379 phys_offset += TARGET_PAGE_SIZE;
2381 } else {
2382 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2383 p->phys_offset = phys_offset;
2384 p->region_offset = region_offset;
2385 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2386 (phys_offset & IO_MEM_ROMD)) {
2387 phys_offset += TARGET_PAGE_SIZE;
2388 } else {
2389 target_phys_addr_t start_addr2, end_addr2;
2390 int need_subpage = 0;
2392 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2393 end_addr2, need_subpage);
2395 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2396 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2397 &p->phys_offset, IO_MEM_UNASSIGNED,
2398 addr & TARGET_PAGE_MASK);
2399 subpage_register(subpage, start_addr2, end_addr2,
2400 phys_offset, region_offset);
2401 p->region_offset = 0;
2405 region_offset += TARGET_PAGE_SIZE;
2408 /* since each CPU stores ram addresses in its TLB cache, we must
2409 reset the modified entries */
2410 /* XXX: slow ! */
2411 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2412 tlb_flush(env, 1);
2416 /* XXX: temporary until new memory mapping API */
2417 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2419 PhysPageDesc *p;
2421 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2422 if (!p)
2423 return IO_MEM_UNASSIGNED;
2424 return p->phys_offset;
2427 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2429 if (kvm_enabled())
2430 kvm_coalesce_mmio_region(addr, size);
2433 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
2435 if (kvm_enabled())
2436 kvm_uncoalesce_mmio_region(addr, size);
2439 #ifdef CONFIG_KQEMU
2440 /* XXX: better than nothing */
2441 static ram_addr_t kqemu_ram_alloc(ram_addr_t size)
2443 ram_addr_t addr;
2444 if ((last_ram_offset + size) > kqemu_phys_ram_size) {
2445 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
2446 (uint64_t)size, (uint64_t)kqemu_phys_ram_size);
2447 abort();
2449 addr = last_ram_offset;
2450 last_ram_offset = TARGET_PAGE_ALIGN(last_ram_offset + size);
2451 return addr;
2453 #endif
2455 #ifdef __linux__
2457 #include <sys/vfs.h>
2459 #define HUGETLBFS_MAGIC 0x958458f6
2461 static long gethugepagesize(const char *path)
2463 struct statfs fs;
2464 int ret;
2466 do {
2467 ret = statfs(path, &fs);
2468 } while (ret != 0 && errno == EINTR);
2470 if (ret != 0) {
2471 perror("statfs");
2472 return 0;
2475 if (fs.f_type != HUGETLBFS_MAGIC)
2476 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
2478 return fs.f_bsize;
2481 static void *file_ram_alloc(ram_addr_t memory, const char *path)
2483 char *filename;
2484 void *area;
2485 int fd;
2486 #ifdef MAP_POPULATE
2487 int flags;
2488 #endif
2489 unsigned long hpagesize;
2490 extern int mem_prealloc;
2492 if (!path) {
2493 return NULL;
2496 hpagesize = gethugepagesize(path);
2497 if (!hpagesize) {
2498 return NULL;
2501 if (memory < hpagesize) {
2502 return NULL;
2505 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2506 fprintf(stderr, "host lacks mmu notifiers, disabling --mem-path\n");
2507 return NULL;
2510 if (asprintf(&filename, "%s/kvm.XXXXXX", path) == -1) {
2511 return NULL;
2514 fd = mkstemp(filename);
2515 if (fd < 0) {
2516 perror("mkstemp");
2517 free(filename);
2518 return NULL;
2520 unlink(filename);
2521 free(filename);
2523 memory = (memory+hpagesize-1) & ~(hpagesize-1);
2526 * ftruncate is not supported by hugetlbfs in older
2527 * hosts, so don't bother checking for errors.
2528 * If anything goes wrong with it under other filesystems,
2529 * mmap will fail.
2531 ftruncate(fd, memory);
2533 #ifdef MAP_POPULATE
2534 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2535 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2536 * to sidestep this quirk.
2538 flags = mem_prealloc ? MAP_POPULATE|MAP_SHARED : MAP_PRIVATE;
2539 area = mmap(0, memory, PROT_READ|PROT_WRITE, flags, fd, 0);
2540 #else
2541 area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
2542 #endif
2543 if (area == MAP_FAILED) {
2544 perror("alloc_mem_area: can't mmap hugetlbfs pages");
2545 close(fd);
2546 return (NULL);
2548 return area;
2551 #else
2553 static void *file_ram_alloc(ram_addr_t memory, const char *path)
2555 return NULL;
2558 #endif
2560 extern const char *mem_path;
2562 ram_addr_t qemu_ram_alloc(ram_addr_t size)
2564 RAMBlock *new_block;
2566 #ifdef CONFIG_KQEMU
2567 if (kqemu_phys_ram_base) {
2568 return kqemu_ram_alloc(size);
2570 #endif
2572 size = TARGET_PAGE_ALIGN(size);
2573 new_block = qemu_malloc(sizeof(*new_block));
2575 new_block->host = file_ram_alloc(size, mem_path);
2576 if (!new_block->host) {
2577 new_block->host = qemu_vmalloc(size);
2578 #ifdef MADV_MERGEABLE
2579 madvise(new_block->host, size, MADV_MERGEABLE);
2580 #endif
2582 new_block->offset = last_ram_offset;
2583 new_block->length = size;
2585 new_block->next = ram_blocks;
2586 ram_blocks = new_block;
2588 phys_ram_dirty = qemu_realloc(phys_ram_dirty,
2589 (last_ram_offset + size) >> TARGET_PAGE_BITS);
2590 memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
2591 0xff, size >> TARGET_PAGE_BITS);
2593 last_ram_offset += size;
2595 if (kvm_enabled())
2596 kvm_setup_guest_memory(new_block->host, size);
2598 return new_block->offset;
2601 void qemu_ram_free(ram_addr_t addr)
2603 /* TODO: implement this. */
2606 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2607 With the exception of the softmmu code in this file, this should
2608 only be used for local memory (e.g. video ram) that the device owns,
2609 and knows it isn't going to access beyond the end of the block.
2611 It should not be used for general purpose DMA.
2612 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2614 void *qemu_get_ram_ptr(ram_addr_t addr)
2616 RAMBlock *prev;
2617 RAMBlock **prevp;
2618 RAMBlock *block;
2620 #ifdef CONFIG_KQEMU
2621 if (kqemu_phys_ram_base) {
2622 return kqemu_phys_ram_base + addr;
2624 #endif
2626 prev = NULL;
2627 prevp = &ram_blocks;
2628 block = ram_blocks;
2629 while (block && (block->offset > addr
2630 || block->offset + block->length <= addr)) {
2631 if (prev)
2632 prevp = &prev->next;
2633 prev = block;
2634 block = block->next;
2636 if (!block) {
2637 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
2638 abort();
2640 /* Move this entry to to start of the list. */
2641 if (prev) {
2642 prev->next = block->next;
2643 block->next = *prevp;
2644 *prevp = block;
2646 return block->host + (addr - block->offset);
2649 /* Some of the softmmu routines need to translate from a host pointer
2650 (typically a TLB entry) back to a ram offset. */
2651 ram_addr_t qemu_ram_addr_from_host(void *ptr)
2653 RAMBlock *prev;
2654 RAMBlock **prevp;
2655 RAMBlock *block;
2656 uint8_t *host = ptr;
2658 #ifdef CONFIG_KQEMU
2659 if (kqemu_phys_ram_base) {
2660 return host - kqemu_phys_ram_base;
2662 #endif
2664 prev = NULL;
2665 prevp = &ram_blocks;
2666 block = ram_blocks;
2667 while (block && (block->host > host
2668 || block->host + block->length <= host)) {
2669 if (prev)
2670 prevp = &prev->next;
2671 prev = block;
2672 block = block->next;
2674 if (!block) {
2675 fprintf(stderr, "Bad ram pointer %p\n", ptr);
2676 abort();
2678 return block->offset + (host - block->host);
2681 static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
2683 #ifdef DEBUG_UNASSIGNED
2684 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2685 #endif
2686 #if defined(TARGET_SPARC)
2687 do_unassigned_access(addr, 0, 0, 0, 1);
2688 #endif
2689 return 0;
2692 static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
2694 #ifdef DEBUG_UNASSIGNED
2695 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2696 #endif
2697 #if defined(TARGET_SPARC)
2698 do_unassigned_access(addr, 0, 0, 0, 2);
2699 #endif
2700 return 0;
2703 static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
2705 #ifdef DEBUG_UNASSIGNED
2706 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2707 #endif
2708 #if defined(TARGET_SPARC)
2709 do_unassigned_access(addr, 0, 0, 0, 4);
2710 #endif
2711 return 0;
2714 static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
2716 #ifdef DEBUG_UNASSIGNED
2717 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2718 #endif
2719 #if defined(TARGET_SPARC)
2720 do_unassigned_access(addr, 1, 0, 0, 1);
2721 #endif
2724 static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2726 #ifdef DEBUG_UNASSIGNED
2727 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2728 #endif
2729 #if defined(TARGET_SPARC)
2730 do_unassigned_access(addr, 1, 0, 0, 2);
2731 #endif
2734 static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2736 #ifdef DEBUG_UNASSIGNED
2737 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2738 #endif
2739 #if defined(TARGET_SPARC)
2740 do_unassigned_access(addr, 1, 0, 0, 4);
2741 #endif
2744 static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2745 unassigned_mem_readb,
2746 unassigned_mem_readw,
2747 unassigned_mem_readl,
2750 static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2751 unassigned_mem_writeb,
2752 unassigned_mem_writew,
2753 unassigned_mem_writel,
2756 static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2757 uint32_t val)
2759 int dirty_flags;
2760 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2761 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2762 #if !defined(CONFIG_USER_ONLY)
2763 tb_invalidate_phys_page_fast(ram_addr, 1);
2764 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2765 #endif
2767 stb_p(qemu_get_ram_ptr(ram_addr), val);
2768 #ifdef CONFIG_KQEMU
2769 if (cpu_single_env->kqemu_enabled &&
2770 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2771 kqemu_modify_page(cpu_single_env, ram_addr);
2772 #endif
2773 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2774 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2775 /* we remove the notdirty callback only if the code has been
2776 flushed */
2777 if (dirty_flags == 0xff)
2778 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2781 static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2782 uint32_t val)
2784 int dirty_flags;
2785 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2786 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2787 #if !defined(CONFIG_USER_ONLY)
2788 tb_invalidate_phys_page_fast(ram_addr, 2);
2789 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2790 #endif
2792 stw_p(qemu_get_ram_ptr(ram_addr), val);
2793 #ifdef CONFIG_KQEMU
2794 if (cpu_single_env->kqemu_enabled &&
2795 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2796 kqemu_modify_page(cpu_single_env, ram_addr);
2797 #endif
2798 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2799 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2800 /* we remove the notdirty callback only if the code has been
2801 flushed */
2802 if (dirty_flags == 0xff)
2803 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2806 static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2807 uint32_t val)
2809 int dirty_flags;
2810 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2811 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2812 #if !defined(CONFIG_USER_ONLY)
2813 tb_invalidate_phys_page_fast(ram_addr, 4);
2814 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2815 #endif
2817 stl_p(qemu_get_ram_ptr(ram_addr), val);
2818 #ifdef CONFIG_KQEMU
2819 if (cpu_single_env->kqemu_enabled &&
2820 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2821 kqemu_modify_page(cpu_single_env, ram_addr);
2822 #endif
2823 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2824 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2825 /* we remove the notdirty callback only if the code has been
2826 flushed */
2827 if (dirty_flags == 0xff)
2828 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2831 static CPUReadMemoryFunc *error_mem_read[3] = {
2832 NULL, /* never used */
2833 NULL, /* never used */
2834 NULL, /* never used */
2837 static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2838 notdirty_mem_writeb,
2839 notdirty_mem_writew,
2840 notdirty_mem_writel,
2843 /* Generate a debug exception if a watchpoint has been hit. */
2844 static void check_watchpoint(int offset, int len_mask, int flags)
2846 CPUState *env = cpu_single_env;
2847 target_ulong pc, cs_base;
2848 TranslationBlock *tb;
2849 target_ulong vaddr;
2850 CPUWatchpoint *wp;
2851 int cpu_flags;
2853 if (env->watchpoint_hit) {
2854 /* We re-entered the check after replacing the TB. Now raise
2855 * the debug interrupt so that is will trigger after the
2856 * current instruction. */
2857 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2858 return;
2860 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
2861 TAILQ_FOREACH(wp, &env->watchpoints, entry) {
2862 if ((vaddr == (wp->vaddr & len_mask) ||
2863 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
2864 wp->flags |= BP_WATCHPOINT_HIT;
2865 if (!env->watchpoint_hit) {
2866 env->watchpoint_hit = wp;
2867 tb = tb_find_pc(env->mem_io_pc);
2868 if (!tb) {
2869 cpu_abort(env, "check_watchpoint: could not find TB for "
2870 "pc=%p", (void *)env->mem_io_pc);
2872 cpu_restore_state(tb, env, env->mem_io_pc, NULL);
2873 tb_phys_invalidate(tb, -1);
2874 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2875 env->exception_index = EXCP_DEBUG;
2876 } else {
2877 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2878 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
2880 cpu_resume_from_signal(env, NULL);
2882 } else {
2883 wp->flags &= ~BP_WATCHPOINT_HIT;
2888 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2889 so these check for a hit then pass through to the normal out-of-line
2890 phys routines. */
2891 static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2893 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
2894 return ldub_phys(addr);
2897 static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2899 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
2900 return lduw_phys(addr);
2903 static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2905 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
2906 return ldl_phys(addr);
2909 static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2910 uint32_t val)
2912 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
2913 stb_phys(addr, val);
2916 static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2917 uint32_t val)
2919 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
2920 stw_phys(addr, val);
2923 static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2924 uint32_t val)
2926 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
2927 stl_phys(addr, val);
2930 static CPUReadMemoryFunc *watch_mem_read[3] = {
2931 watch_mem_readb,
2932 watch_mem_readw,
2933 watch_mem_readl,
2936 static CPUWriteMemoryFunc *watch_mem_write[3] = {
2937 watch_mem_writeb,
2938 watch_mem_writew,
2939 watch_mem_writel,
2942 static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2943 unsigned int len)
2945 uint32_t ret;
2946 unsigned int idx;
2948 idx = SUBPAGE_IDX(addr);
2949 #if defined(DEBUG_SUBPAGE)
2950 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2951 mmio, len, addr, idx);
2952 #endif
2953 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
2954 addr + mmio->region_offset[idx][0][len]);
2956 return ret;
2959 static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2960 uint32_t value, unsigned int len)
2962 unsigned int idx;
2964 idx = SUBPAGE_IDX(addr);
2965 #if defined(DEBUG_SUBPAGE)
2966 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2967 mmio, len, addr, idx, value);
2968 #endif
2969 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
2970 addr + mmio->region_offset[idx][1][len],
2971 value);
2974 static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2976 #if defined(DEBUG_SUBPAGE)
2977 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2978 #endif
2980 return subpage_readlen(opaque, addr, 0);
2983 static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2984 uint32_t value)
2986 #if defined(DEBUG_SUBPAGE)
2987 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2988 #endif
2989 subpage_writelen(opaque, addr, value, 0);
2992 static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2994 #if defined(DEBUG_SUBPAGE)
2995 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2996 #endif
2998 return subpage_readlen(opaque, addr, 1);
3001 static void subpage_writew (void *opaque, target_phys_addr_t addr,
3002 uint32_t value)
3004 #if defined(DEBUG_SUBPAGE)
3005 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3006 #endif
3007 subpage_writelen(opaque, addr, value, 1);
3010 static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
3012 #if defined(DEBUG_SUBPAGE)
3013 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
3014 #endif
3016 return subpage_readlen(opaque, addr, 2);
3019 static void subpage_writel (void *opaque,
3020 target_phys_addr_t addr, uint32_t value)
3022 #if defined(DEBUG_SUBPAGE)
3023 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
3024 #endif
3025 subpage_writelen(opaque, addr, value, 2);
3028 static CPUReadMemoryFunc *subpage_read[] = {
3029 &subpage_readb,
3030 &subpage_readw,
3031 &subpage_readl,
3034 static CPUWriteMemoryFunc *subpage_write[] = {
3035 &subpage_writeb,
3036 &subpage_writew,
3037 &subpage_writel,
3040 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
3041 ram_addr_t memory, ram_addr_t region_offset)
3043 int idx, eidx;
3044 unsigned int i;
3046 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
3047 return -1;
3048 idx = SUBPAGE_IDX(start);
3049 eidx = SUBPAGE_IDX(end);
3050 #if defined(DEBUG_SUBPAGE)
3051 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
3052 mmio, start, end, idx, eidx, memory);
3053 #endif
3054 memory >>= IO_MEM_SHIFT;
3055 for (; idx <= eidx; idx++) {
3056 for (i = 0; i < 4; i++) {
3057 if (io_mem_read[memory][i]) {
3058 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
3059 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
3060 mmio->region_offset[idx][0][i] = region_offset;
3062 if (io_mem_write[memory][i]) {
3063 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
3064 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
3065 mmio->region_offset[idx][1][i] = region_offset;
3070 return 0;
3073 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
3074 ram_addr_t orig_memory, ram_addr_t region_offset)
3076 subpage_t *mmio;
3077 int subpage_memory;
3079 mmio = qemu_mallocz(sizeof(subpage_t));
3081 mmio->base = base;
3082 subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio);
3083 #if defined(DEBUG_SUBPAGE)
3084 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
3085 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
3086 #endif
3087 *phys = subpage_memory | IO_MEM_SUBPAGE;
3088 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory,
3089 region_offset);
3091 return mmio;
3094 static int get_free_io_mem_idx(void)
3096 int i;
3098 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
3099 if (!io_mem_used[i]) {
3100 io_mem_used[i] = 1;
3101 return i;
3104 return -1;
3107 /* mem_read and mem_write are arrays of functions containing the
3108 function to access byte (index 0), word (index 1) and dword (index
3109 2). Functions can be omitted with a NULL function pointer.
3110 If io_index is non zero, the corresponding io zone is
3111 modified. If it is zero, a new io zone is allocated. The return
3112 value can be used with cpu_register_physical_memory(). (-1) is
3113 returned if error. */
3114 static int cpu_register_io_memory_fixed(int io_index,
3115 CPUReadMemoryFunc **mem_read,
3116 CPUWriteMemoryFunc **mem_write,
3117 void *opaque)
3119 int i, subwidth = 0;
3121 if (io_index <= 0) {
3122 io_index = get_free_io_mem_idx();
3123 if (io_index == -1)
3124 return io_index;
3125 } else {
3126 io_index >>= IO_MEM_SHIFT;
3127 if (io_index >= IO_MEM_NB_ENTRIES)
3128 return -1;
3131 for(i = 0;i < 3; i++) {
3132 if (!mem_read[i] || !mem_write[i])
3133 subwidth = IO_MEM_SUBWIDTH;
3134 io_mem_read[io_index][i] = mem_read[i];
3135 io_mem_write[io_index][i] = mem_write[i];
3137 io_mem_opaque[io_index] = opaque;
3138 return (io_index << IO_MEM_SHIFT) | subwidth;
3141 int cpu_register_io_memory(CPUReadMemoryFunc **mem_read,
3142 CPUWriteMemoryFunc **mem_write,
3143 void *opaque)
3145 return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque);
3148 void cpu_unregister_io_memory(int io_table_address)
3150 int i;
3151 int io_index = io_table_address >> IO_MEM_SHIFT;
3153 for (i=0;i < 3; i++) {
3154 io_mem_read[io_index][i] = unassigned_mem_read[i];
3155 io_mem_write[io_index][i] = unassigned_mem_write[i];
3157 io_mem_opaque[io_index] = NULL;
3158 io_mem_used[io_index] = 0;
3161 static void io_mem_init(void)
3163 int i;
3165 cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
3166 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
3167 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
3168 for (i=0; i<5; i++)
3169 io_mem_used[i] = 1;
3171 io_mem_watch = cpu_register_io_memory(watch_mem_read,
3172 watch_mem_write, NULL);
3173 #ifdef CONFIG_KQEMU
3174 if (kqemu_phys_ram_base) {
3175 /* alloc dirty bits array */
3176 phys_ram_dirty = qemu_vmalloc(kqemu_phys_ram_size >> TARGET_PAGE_BITS);
3177 memset(phys_ram_dirty, 0xff, kqemu_phys_ram_size >> TARGET_PAGE_BITS);
3179 #endif
3182 #endif /* !defined(CONFIG_USER_ONLY) */
3184 /* physical memory access (slow version, mainly for debug) */
3185 #if defined(CONFIG_USER_ONLY)
3186 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
3187 int len, int is_write)
3189 int l, flags;
3190 target_ulong page;
3191 void * p;
3193 while (len > 0) {
3194 page = addr & TARGET_PAGE_MASK;
3195 l = (page + TARGET_PAGE_SIZE) - addr;
3196 if (l > len)
3197 l = len;
3198 flags = page_get_flags(page);
3199 if (!(flags & PAGE_VALID))
3200 return;
3201 if (is_write) {
3202 if (!(flags & PAGE_WRITE))
3203 return;
3204 /* XXX: this code should not depend on lock_user */
3205 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
3206 /* FIXME - should this return an error rather than just fail? */
3207 return;
3208 memcpy(p, buf, l);
3209 unlock_user(p, addr, l);
3210 } else {
3211 if (!(flags & PAGE_READ))
3212 return;
3213 /* XXX: this code should not depend on lock_user */
3214 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
3215 /* FIXME - should this return an error rather than just fail? */
3216 return;
3217 memcpy(buf, p, l);
3218 unlock_user(p, addr, 0);
3220 len -= l;
3221 buf += l;
3222 addr += l;
3226 #else
3227 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
3228 int len, int is_write)
3230 int l, io_index;
3231 uint8_t *ptr;
3232 uint32_t val;
3233 target_phys_addr_t page;
3234 unsigned long pd;
3235 PhysPageDesc *p;
3237 while (len > 0) {
3238 page = addr & TARGET_PAGE_MASK;
3239 l = (page + TARGET_PAGE_SIZE) - addr;
3240 if (l > len)
3241 l = len;
3242 p = phys_page_find(page >> TARGET_PAGE_BITS);
3243 if (!p) {
3244 pd = IO_MEM_UNASSIGNED;
3245 } else {
3246 pd = p->phys_offset;
3249 if (is_write) {
3250 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3251 target_phys_addr_t addr1 = addr;
3252 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3253 if (p)
3254 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3255 /* XXX: could force cpu_single_env to NULL to avoid
3256 potential bugs */
3257 if (l >= 4 && ((addr1 & 3) == 0)) {
3258 /* 32 bit write access */
3259 val = ldl_p(buf);
3260 io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
3261 l = 4;
3262 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3263 /* 16 bit write access */
3264 val = lduw_p(buf);
3265 io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
3266 l = 2;
3267 } else {
3268 /* 8 bit write access */
3269 val = ldub_p(buf);
3270 io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
3271 l = 1;
3273 } else {
3274 unsigned long addr1;
3275 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3276 /* RAM case */
3277 ptr = qemu_get_ram_ptr(addr1);
3278 memcpy(ptr, buf, l);
3279 if (!cpu_physical_memory_is_dirty(addr1)) {
3280 /* invalidate code */
3281 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3282 /* set dirty bit */
3283 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3284 (0xff & ~CODE_DIRTY_FLAG);
3286 /* qemu doesn't execute guest code directly, but kvm does
3287 therefore flush instruction caches */
3288 if (kvm_enabled())
3289 flush_icache_range((unsigned long)ptr,
3290 ((unsigned long)ptr)+l);
3292 } else {
3293 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3294 !(pd & IO_MEM_ROMD)) {
3295 target_phys_addr_t addr1 = addr;
3296 /* I/O case */
3297 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3298 if (p)
3299 addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3300 if (l >= 4 && ((addr1 & 3) == 0)) {
3301 /* 32 bit read access */
3302 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
3303 stl_p(buf, val);
3304 l = 4;
3305 } else if (l >= 2 && ((addr1 & 1) == 0)) {
3306 /* 16 bit read access */
3307 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
3308 stw_p(buf, val);
3309 l = 2;
3310 } else {
3311 /* 8 bit read access */
3312 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
3313 stb_p(buf, val);
3314 l = 1;
3316 } else {
3317 /* RAM case */
3318 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3319 (addr & ~TARGET_PAGE_MASK);
3320 memcpy(buf, ptr, l);
3323 len -= l;
3324 buf += l;
3325 addr += l;
3329 /* used for ROM loading : can write in RAM and ROM */
3330 void cpu_physical_memory_write_rom(target_phys_addr_t addr,
3331 const uint8_t *buf, int len)
3333 int l;
3334 uint8_t *ptr;
3335 target_phys_addr_t page;
3336 unsigned long pd;
3337 PhysPageDesc *p;
3339 while (len > 0) {
3340 page = addr & TARGET_PAGE_MASK;
3341 l = (page + TARGET_PAGE_SIZE) - addr;
3342 if (l > len)
3343 l = len;
3344 p = phys_page_find(page >> TARGET_PAGE_BITS);
3345 if (!p) {
3346 pd = IO_MEM_UNASSIGNED;
3347 } else {
3348 pd = p->phys_offset;
3351 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
3352 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
3353 !(pd & IO_MEM_ROMD)) {
3354 /* do nothing */
3355 } else {
3356 unsigned long addr1;
3357 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3358 /* ROM/RAM case */
3359 ptr = qemu_get_ram_ptr(addr1);
3360 memcpy(ptr, buf, l);
3362 len -= l;
3363 buf += l;
3364 addr += l;
3368 typedef struct {
3369 void *buffer;
3370 target_phys_addr_t addr;
3371 target_phys_addr_t len;
3372 } BounceBuffer;
3374 static BounceBuffer bounce;
3376 typedef struct MapClient {
3377 void *opaque;
3378 void (*callback)(void *opaque);
3379 LIST_ENTRY(MapClient) link;
3380 } MapClient;
3382 static LIST_HEAD(map_client_list, MapClient) map_client_list
3383 = LIST_HEAD_INITIALIZER(map_client_list);
3385 void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
3387 MapClient *client = qemu_malloc(sizeof(*client));
3389 client->opaque = opaque;
3390 client->callback = callback;
3391 LIST_INSERT_HEAD(&map_client_list, client, link);
3392 return client;
3395 void cpu_unregister_map_client(void *_client)
3397 MapClient *client = (MapClient *)_client;
3399 LIST_REMOVE(client, link);
3400 qemu_free(client);
3403 static void cpu_notify_map_clients(void)
3405 MapClient *client;
3407 while (!LIST_EMPTY(&map_client_list)) {
3408 client = LIST_FIRST(&map_client_list);
3409 client->callback(client->opaque);
3410 cpu_unregister_map_client(client);
3414 /* Map a physical memory region into a host virtual address.
3415 * May map a subset of the requested range, given by and returned in *plen.
3416 * May return NULL if resources needed to perform the mapping are exhausted.
3417 * Use only for reads OR writes - not for read-modify-write operations.
3418 * Use cpu_register_map_client() to know when retrying the map operation is
3419 * likely to succeed.
3421 void *cpu_physical_memory_map(target_phys_addr_t addr,
3422 target_phys_addr_t *plen,
3423 int is_write)
3425 target_phys_addr_t len = *plen;
3426 target_phys_addr_t done = 0;
3427 int l;
3428 uint8_t *ret = NULL;
3429 uint8_t *ptr;
3430 target_phys_addr_t page;
3431 unsigned long pd;
3432 PhysPageDesc *p;
3433 unsigned long addr1;
3435 while (len > 0) {
3436 page = addr & TARGET_PAGE_MASK;
3437 l = (page + TARGET_PAGE_SIZE) - addr;
3438 if (l > len)
3439 l = len;
3440 p = phys_page_find(page >> TARGET_PAGE_BITS);
3441 if (!p) {
3442 pd = IO_MEM_UNASSIGNED;
3443 } else {
3444 pd = p->phys_offset;
3447 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3448 if (done || bounce.buffer) {
3449 break;
3451 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
3452 bounce.addr = addr;
3453 bounce.len = l;
3454 if (!is_write) {
3455 cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
3457 ptr = bounce.buffer;
3458 } else {
3459 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3460 ptr = qemu_get_ram_ptr(addr1);
3462 if (!done) {
3463 ret = ptr;
3464 } else if (ret + done != ptr) {
3465 break;
3468 len -= l;
3469 addr += l;
3470 done += l;
3472 *plen = done;
3473 return ret;
3476 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3477 * Will also mark the memory as dirty if is_write == 1. access_len gives
3478 * the amount of memory that was actually read or written by the caller.
3480 void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
3481 int is_write, target_phys_addr_t access_len)
3483 unsigned long flush_len = (unsigned long)access_len;
3485 if (buffer != bounce.buffer) {
3486 if (is_write) {
3487 ram_addr_t addr1 = qemu_ram_addr_from_host(buffer);
3488 while (access_len) {
3489 unsigned l;
3490 l = TARGET_PAGE_SIZE;
3491 if (l > access_len)
3492 l = access_len;
3493 if (!cpu_physical_memory_is_dirty(addr1)) {
3494 /* invalidate code */
3495 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
3496 /* set dirty bit */
3497 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3498 (0xff & ~CODE_DIRTY_FLAG);
3500 addr1 += l;
3501 access_len -= l;
3503 dma_flush_range((unsigned long)buffer,
3504 (unsigned long)buffer + flush_len);
3506 return;
3508 if (is_write) {
3509 cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
3511 qemu_free(bounce.buffer);
3512 bounce.buffer = NULL;
3513 cpu_notify_map_clients();
3516 /* warning: addr must be aligned */
3517 uint32_t ldl_phys(target_phys_addr_t addr)
3519 int io_index;
3520 uint8_t *ptr;
3521 uint32_t val;
3522 unsigned long pd;
3523 PhysPageDesc *p;
3525 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3526 if (!p) {
3527 pd = IO_MEM_UNASSIGNED;
3528 } else {
3529 pd = p->phys_offset;
3532 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3533 !(pd & IO_MEM_ROMD)) {
3534 /* I/O case */
3535 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3536 if (p)
3537 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3538 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3539 } else {
3540 /* RAM case */
3541 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3542 (addr & ~TARGET_PAGE_MASK);
3543 val = ldl_p(ptr);
3545 return val;
3548 /* warning: addr must be aligned */
3549 uint64_t ldq_phys(target_phys_addr_t addr)
3551 int io_index;
3552 uint8_t *ptr;
3553 uint64_t val;
3554 unsigned long pd;
3555 PhysPageDesc *p;
3557 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3558 if (!p) {
3559 pd = IO_MEM_UNASSIGNED;
3560 } else {
3561 pd = p->phys_offset;
3564 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
3565 !(pd & IO_MEM_ROMD)) {
3566 /* I/O case */
3567 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3568 if (p)
3569 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3570 #ifdef TARGET_WORDS_BIGENDIAN
3571 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
3572 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
3573 #else
3574 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
3575 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
3576 #endif
3577 } else {
3578 /* RAM case */
3579 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3580 (addr & ~TARGET_PAGE_MASK);
3581 val = ldq_p(ptr);
3583 return val;
3586 /* XXX: optimize */
3587 uint32_t ldub_phys(target_phys_addr_t addr)
3589 uint8_t val;
3590 cpu_physical_memory_read(addr, &val, 1);
3591 return val;
3594 /* XXX: optimize */
3595 uint32_t lduw_phys(target_phys_addr_t addr)
3597 uint16_t val;
3598 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
3599 return tswap16(val);
3602 /* warning: addr must be aligned. The ram page is not masked as dirty
3603 and the code inside is not invalidated. It is useful if the dirty
3604 bits are used to track modified PTEs */
3605 void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
3607 int io_index;
3608 uint8_t *ptr;
3609 unsigned long pd;
3610 PhysPageDesc *p;
3612 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3613 if (!p) {
3614 pd = IO_MEM_UNASSIGNED;
3615 } else {
3616 pd = p->phys_offset;
3619 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3620 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3621 if (p)
3622 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3623 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3624 } else {
3625 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3626 ptr = qemu_get_ram_ptr(addr1);
3627 stl_p(ptr, val);
3629 if (unlikely(in_migration)) {
3630 if (!cpu_physical_memory_is_dirty(addr1)) {
3631 /* invalidate code */
3632 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3633 /* set dirty bit */
3634 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3635 (0xff & ~CODE_DIRTY_FLAG);
3641 void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
3643 int io_index;
3644 uint8_t *ptr;
3645 unsigned long pd;
3646 PhysPageDesc *p;
3648 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3649 if (!p) {
3650 pd = IO_MEM_UNASSIGNED;
3651 } else {
3652 pd = p->phys_offset;
3655 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3656 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3657 if (p)
3658 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3659 #ifdef TARGET_WORDS_BIGENDIAN
3660 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3661 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3662 #else
3663 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3664 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3665 #endif
3666 } else {
3667 ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
3668 (addr & ~TARGET_PAGE_MASK);
3669 stq_p(ptr, val);
3673 /* warning: addr must be aligned */
3674 void stl_phys(target_phys_addr_t addr, uint32_t val)
3676 int io_index;
3677 uint8_t *ptr;
3678 unsigned long pd;
3679 PhysPageDesc *p;
3681 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3682 if (!p) {
3683 pd = IO_MEM_UNASSIGNED;
3684 } else {
3685 pd = p->phys_offset;
3688 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3689 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3690 if (p)
3691 addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
3692 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3693 } else {
3694 unsigned long addr1;
3695 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3696 /* RAM case */
3697 ptr = qemu_get_ram_ptr(addr1);
3698 stl_p(ptr, val);
3699 if (!cpu_physical_memory_is_dirty(addr1)) {
3700 /* invalidate code */
3701 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3702 /* set dirty bit */
3703 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3704 (0xff & ~CODE_DIRTY_FLAG);
3709 /* XXX: optimize */
3710 void stb_phys(target_phys_addr_t addr, uint32_t val)
3712 uint8_t v = val;
3713 cpu_physical_memory_write(addr, &v, 1);
3716 /* XXX: optimize */
3717 void stw_phys(target_phys_addr_t addr, uint32_t val)
3719 uint16_t v = tswap16(val);
3720 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3723 /* XXX: optimize */
3724 void stq_phys(target_phys_addr_t addr, uint64_t val)
3726 val = tswap64(val);
3727 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3730 #endif
3732 /* virtual memory access for debug (includes writing to ROM) */
3733 int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3734 uint8_t *buf, int len, int is_write)
3736 int l;
3737 target_phys_addr_t phys_addr;
3738 target_ulong page;
3740 while (len > 0) {
3741 page = addr & TARGET_PAGE_MASK;
3742 phys_addr = cpu_get_phys_page_debug(env, page);
3743 /* if no physical page mapped, return an error */
3744 if (phys_addr == -1)
3745 return -1;
3746 l = (page + TARGET_PAGE_SIZE) - addr;
3747 if (l > len)
3748 l = len;
3749 phys_addr += (addr & ~TARGET_PAGE_MASK);
3750 #if !defined(CONFIG_USER_ONLY)
3751 if (is_write)
3752 cpu_physical_memory_write_rom(phys_addr, buf, l);
3753 else
3754 #endif
3755 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
3756 len -= l;
3757 buf += l;
3758 addr += l;
3760 return 0;
3763 /* in deterministic execution mode, instructions doing device I/Os
3764 must be at the end of the TB */
3765 void cpu_io_recompile(CPUState *env, void *retaddr)
3767 TranslationBlock *tb;
3768 uint32_t n, cflags;
3769 target_ulong pc, cs_base;
3770 uint64_t flags;
3772 tb = tb_find_pc((unsigned long)retaddr);
3773 if (!tb) {
3774 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3775 retaddr);
3777 n = env->icount_decr.u16.low + tb->icount;
3778 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3779 /* Calculate how many instructions had been executed before the fault
3780 occurred. */
3781 n = n - env->icount_decr.u16.low;
3782 /* Generate a new TB ending on the I/O insn. */
3783 n++;
3784 /* On MIPS and SH, delay slot instructions can only be restarted if
3785 they were already the first instruction in the TB. If this is not
3786 the first instruction in a TB then re-execute the preceding
3787 branch. */
3788 #if defined(TARGET_MIPS)
3789 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3790 env->active_tc.PC -= 4;
3791 env->icount_decr.u16.low++;
3792 env->hflags &= ~MIPS_HFLAG_BMASK;
3794 #elif defined(TARGET_SH4)
3795 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3796 && n > 1) {
3797 env->pc -= 2;
3798 env->icount_decr.u16.low++;
3799 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3801 #endif
3802 /* This should never happen. */
3803 if (n > CF_COUNT_MASK)
3804 cpu_abort(env, "TB too big during recompile");
3806 cflags = n | CF_LAST_IO;
3807 pc = tb->pc;
3808 cs_base = tb->cs_base;
3809 flags = tb->flags;
3810 tb_phys_invalidate(tb, -1);
3811 /* FIXME: In theory this could raise an exception. In practice
3812 we have already translated the block once so it's probably ok. */
3813 tb_gen_code(env, pc, cs_base, flags, cflags);
3814 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3815 the first in the TB) then we end up generating a whole new TB and
3816 repeating the fault, which is horribly inefficient.
3817 Better would be to execute just this insn uncached, or generate a
3818 second new TB. */
3819 cpu_resume_from_signal(env, NULL);
3822 void dump_exec_info(FILE *f,
3823 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3825 int i, target_code_size, max_target_code_size;
3826 int direct_jmp_count, direct_jmp2_count, cross_page;
3827 TranslationBlock *tb;
3829 target_code_size = 0;
3830 max_target_code_size = 0;
3831 cross_page = 0;
3832 direct_jmp_count = 0;
3833 direct_jmp2_count = 0;
3834 for(i = 0; i < nb_tbs; i++) {
3835 tb = &tbs[i];
3836 target_code_size += tb->size;
3837 if (tb->size > max_target_code_size)
3838 max_target_code_size = tb->size;
3839 if (tb->page_addr[1] != -1)
3840 cross_page++;
3841 if (tb->tb_next_offset[0] != 0xffff) {
3842 direct_jmp_count++;
3843 if (tb->tb_next_offset[1] != 0xffff) {
3844 direct_jmp2_count++;
3848 /* XXX: avoid using doubles ? */
3849 cpu_fprintf(f, "Translation buffer state:\n");
3850 cpu_fprintf(f, "gen code size %ld/%ld\n",
3851 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3852 cpu_fprintf(f, "TB count %d/%d\n",
3853 nb_tbs, code_gen_max_blocks);
3854 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
3855 nb_tbs ? target_code_size / nb_tbs : 0,
3856 max_target_code_size);
3857 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3858 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3859 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
3860 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3861 cross_page,
3862 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3863 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3864 direct_jmp_count,
3865 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3866 direct_jmp2_count,
3867 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
3868 cpu_fprintf(f, "\nStatistics:\n");
3869 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3870 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3871 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
3872 tcg_dump_info(f, cpu_fprintf);
3875 #if !defined(CONFIG_USER_ONLY)
3877 #define MMUSUFFIX _cmmu
3878 #define GETPC() NULL
3879 #define env cpu_single_env
3880 #define SOFTMMU_CODE_ACCESS
3882 #define SHIFT 0
3883 #include "softmmu_template.h"
3885 #define SHIFT 1
3886 #include "softmmu_template.h"
3888 #define SHIFT 2
3889 #include "softmmu_template.h"
3891 #define SHIFT 3
3892 #include "softmmu_template.h"
3894 #undef env
3896 #endif