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