Don't require all drivers to use virtio_net_hdr
[qemu-kvm/markmc.git] / exec.c
blob7a37dfdb16d86e0acbafba0d89d6a655bd1a7171
1 /*
2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "config.h"
21 #ifdef _WIN32
22 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h>
24 #else
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 #endif
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <inttypes.h>
36 #include "cpu.h"
37 #include "exec-all.h"
38 #include "qemu-common.h"
40 #if !defined(TARGET_IA64)
41 #include "tcg.h"
42 #endif
43 #include "qemu-kvm.h"
45 #include "hw/hw.h"
46 #if defined(CONFIG_USER_ONLY)
47 #include <qemu.h>
48 #endif
50 //#define DEBUG_TB_INVALIDATE
51 //#define DEBUG_FLUSH
52 //#define DEBUG_TLB
53 //#define DEBUG_UNASSIGNED
55 /* make various TB consistency checks */
56 //#define DEBUG_TB_CHECK
57 //#define DEBUG_TLB_CHECK
59 //#define DEBUG_IOPORT
60 //#define DEBUG_SUBPAGE
62 #if !defined(CONFIG_USER_ONLY)
63 /* TB consistency checks only implemented for usermode emulation. */
64 #undef DEBUG_TB_CHECK
65 #endif
67 #define SMC_BITMAP_USE_THRESHOLD 10
69 #define MMAP_AREA_START 0x00000000
70 #define MMAP_AREA_END 0xa8000000
72 #if defined(TARGET_SPARC64)
73 #define TARGET_PHYS_ADDR_SPACE_BITS 41
74 #elif defined(TARGET_SPARC)
75 #define TARGET_PHYS_ADDR_SPACE_BITS 36
76 #elif defined(TARGET_ALPHA)
77 #define TARGET_PHYS_ADDR_SPACE_BITS 42
78 #define TARGET_VIRT_ADDR_SPACE_BITS 42
79 #elif defined(TARGET_PPC64)
80 #define TARGET_PHYS_ADDR_SPACE_BITS 42
81 #elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
82 #define TARGET_PHYS_ADDR_SPACE_BITS 42
83 #elif defined(TARGET_I386) && !defined(USE_KQEMU)
84 #define TARGET_PHYS_ADDR_SPACE_BITS 36
85 #else
86 /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
87 #define TARGET_PHYS_ADDR_SPACE_BITS 32
88 #endif
90 TranslationBlock *tbs;
91 int code_gen_max_blocks;
92 TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
93 int nb_tbs;
94 /* any access to the tbs or the page table must use this lock */
95 spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
97 #if defined(__arm__) || defined(__sparc_v9__)
98 /* The prologue must be reachable with a direct jump. ARM and Sparc64
99 have limited branch ranges (possibly also PPC) so place it in a
100 section close to code segment. */
101 #define code_gen_section \
102 __attribute__((__section__(".gen_code"))) \
103 __attribute__((aligned (32)))
104 #else
105 #define code_gen_section \
106 __attribute__((aligned (32)))
107 #endif
109 uint8_t code_gen_prologue[1024] code_gen_section;
110 uint8_t *code_gen_buffer;
111 unsigned long code_gen_buffer_size;
112 /* threshold to flush the translated code buffer */
113 unsigned long code_gen_buffer_max_size;
114 uint8_t *code_gen_ptr;
116 #if !defined(CONFIG_USER_ONLY)
117 ram_addr_t phys_ram_size;
118 int phys_ram_fd;
119 uint8_t *phys_ram_base;
120 uint8_t *phys_ram_dirty;
121 uint8_t *bios_mem;
122 static int in_migration;
123 static ram_addr_t phys_ram_alloc_offset = 0;
124 #endif
126 CPUState *first_cpu;
127 /* current CPU in the current thread. It is only valid inside
128 cpu_exec() */
129 CPUState *cpu_single_env;
130 /* 0 = Do not count executed instructions.
131 1 = Precise instruction counting.
132 2 = Adaptive rate instruction counting. */
133 int use_icount = 0;
134 /* Current instruction counter. While executing translated code this may
135 include some instructions that have not yet been executed. */
136 int64_t qemu_icount;
138 typedef struct PageDesc {
139 /* list of TBs intersecting this ram page */
140 TranslationBlock *first_tb;
141 /* in order to optimize self modifying code, we count the number
142 of lookups we do to a given page to use a bitmap */
143 unsigned int code_write_count;
144 uint8_t *code_bitmap;
145 #if defined(CONFIG_USER_ONLY)
146 unsigned long flags;
147 #endif
148 } PageDesc;
150 typedef struct PhysPageDesc {
151 /* offset in host memory of the page + io_index in the low bits */
152 ram_addr_t phys_offset;
153 } PhysPageDesc;
155 #define L2_BITS 10
156 #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
157 /* XXX: this is a temporary hack for alpha target.
158 * In the future, this is to be replaced by a multi-level table
159 * to actually be able to handle the complete 64 bits address space.
161 #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
162 #else
163 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
164 #endif
166 #define L1_SIZE (1 << L1_BITS)
167 #define L2_SIZE (1 << L2_BITS)
169 unsigned long qemu_real_host_page_size;
170 unsigned long qemu_host_page_bits;
171 unsigned long qemu_host_page_size;
172 unsigned long qemu_host_page_mask;
174 /* XXX: for system emulation, it could just be an array */
175 static PageDesc *l1_map[L1_SIZE];
176 PhysPageDesc **l1_phys_map;
178 #if !defined(CONFIG_USER_ONLY)
179 static void io_mem_init(void);
181 /* io memory support */
182 CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
183 CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
184 void *io_mem_opaque[IO_MEM_NB_ENTRIES];
185 static int io_mem_nb;
186 char io_mem_used[IO_MEM_NB_ENTRIES];
187 static int io_mem_watch;
188 #endif
190 /* log support */
191 char *logfilename = "/tmp/qemu.log";
192 FILE *logfile;
193 int loglevel;
194 static int log_append = 0;
196 /* statistics */
197 static int tlb_flush_count;
198 static int tb_flush_count;
199 static int tb_phys_invalidate_count;
201 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
202 typedef struct subpage_t {
203 target_phys_addr_t base;
204 CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
205 CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
206 void *opaque[TARGET_PAGE_SIZE][2][4];
207 } subpage_t;
209 #ifdef _WIN32
210 static void map_exec(void *addr, long size)
212 DWORD old_protect;
213 VirtualProtect(addr, size,
214 PAGE_EXECUTE_READWRITE, &old_protect);
217 #else
218 static void map_exec(void *addr, long size)
220 unsigned long start, end, page_size;
222 page_size = getpagesize();
223 start = (unsigned long)addr;
224 start &= ~(page_size - 1);
226 end = (unsigned long)addr + size;
227 end += page_size - 1;
228 end &= ~(page_size - 1);
230 mprotect((void *)start, end - start,
231 PROT_READ | PROT_WRITE | PROT_EXEC);
233 #endif
235 static void page_init(void)
237 /* NOTE: we can always suppose that qemu_host_page_size >=
238 TARGET_PAGE_SIZE */
239 #ifdef _WIN32
241 SYSTEM_INFO system_info;
242 DWORD old_protect;
244 GetSystemInfo(&system_info);
245 qemu_real_host_page_size = system_info.dwPageSize;
247 #else
248 qemu_real_host_page_size = getpagesize();
249 #endif
250 if (qemu_host_page_size == 0)
251 qemu_host_page_size = qemu_real_host_page_size;
252 if (qemu_host_page_size < TARGET_PAGE_SIZE)
253 qemu_host_page_size = TARGET_PAGE_SIZE;
254 qemu_host_page_bits = 0;
255 while ((1 << qemu_host_page_bits) < qemu_host_page_size)
256 qemu_host_page_bits++;
257 qemu_host_page_mask = ~(qemu_host_page_size - 1);
258 l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
259 memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
261 #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
263 long long startaddr, endaddr;
264 FILE *f;
265 int n;
267 mmap_lock();
268 last_brk = (unsigned long)sbrk(0);
269 f = fopen("/proc/self/maps", "r");
270 if (f) {
271 do {
272 n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
273 if (n == 2) {
274 startaddr = MIN(startaddr,
275 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
276 endaddr = MIN(endaddr,
277 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
278 page_set_flags(startaddr & TARGET_PAGE_MASK,
279 TARGET_PAGE_ALIGN(endaddr),
280 PAGE_RESERVED);
282 } while (!feof(f));
283 fclose(f);
285 mmap_unlock();
287 #endif
290 static inline PageDesc *page_find_alloc(target_ulong index)
292 PageDesc **lp, *p;
294 #if TARGET_LONG_BITS > 32
295 /* Host memory outside guest VM. For 32-bit targets we have already
296 excluded high addresses. */
297 if (index > ((target_ulong)L2_SIZE * L1_SIZE * TARGET_PAGE_SIZE))
298 return NULL;
299 #endif
300 lp = &l1_map[index >> L2_BITS];
301 p = *lp;
302 if (!p) {
303 /* allocate if not found */
304 #if defined(CONFIG_USER_ONLY)
305 unsigned long addr;
306 size_t len = sizeof(PageDesc) * L2_SIZE;
307 /* Don't use qemu_malloc because it may recurse. */
308 p = mmap(0, len, PROT_READ | PROT_WRITE,
309 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
310 *lp = p;
311 addr = h2g(p);
312 if (addr == (target_ulong)addr) {
313 page_set_flags(addr & TARGET_PAGE_MASK,
314 TARGET_PAGE_ALIGN(addr + len),
315 PAGE_RESERVED);
317 #else
318 p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
319 *lp = p;
320 #endif
322 return p + (index & (L2_SIZE - 1));
325 static inline PageDesc *page_find(target_ulong index)
327 PageDesc *p;
329 p = l1_map[index >> L2_BITS];
330 if (!p)
331 return 0;
332 return p + (index & (L2_SIZE - 1));
335 static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
337 void **lp, **p;
338 PhysPageDesc *pd;
340 p = (void **)l1_phys_map;
341 #if TARGET_PHYS_ADDR_SPACE_BITS > 32
343 #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
344 #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
345 #endif
346 lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
347 p = *lp;
348 if (!p) {
349 /* allocate if not found */
350 if (!alloc)
351 return NULL;
352 p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
353 memset(p, 0, sizeof(void *) * L1_SIZE);
354 *lp = p;
356 #endif
357 lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
358 pd = *lp;
359 if (!pd) {
360 int i;
361 /* allocate if not found */
362 if (!alloc)
363 return NULL;
364 pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
365 *lp = pd;
366 for (i = 0; i < L2_SIZE; i++)
367 pd[i].phys_offset = IO_MEM_UNASSIGNED;
369 return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
372 static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
374 return phys_page_find_alloc(index, 0);
377 #if !defined(CONFIG_USER_ONLY)
378 static void tlb_protect_code(ram_addr_t ram_addr);
379 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
380 target_ulong vaddr);
381 #define mmap_lock() do { } while(0)
382 #define mmap_unlock() do { } while(0)
383 #endif
385 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
387 #if defined(CONFIG_USER_ONLY)
388 /* Currently it is not recommanded to allocate big chunks of data in
389 user mode. It will change when a dedicated libc will be used */
390 #define USE_STATIC_CODE_GEN_BUFFER
391 #endif
393 #ifdef USE_STATIC_CODE_GEN_BUFFER
394 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
395 #endif
397 void code_gen_alloc(unsigned long tb_size)
399 #ifdef USE_STATIC_CODE_GEN_BUFFER
400 code_gen_buffer = static_code_gen_buffer;
401 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
402 map_exec(code_gen_buffer, code_gen_buffer_size);
403 #else
404 code_gen_buffer_size = tb_size;
405 if (code_gen_buffer_size == 0) {
406 #if defined(CONFIG_USER_ONLY)
407 /* in user mode, phys_ram_size is not meaningful */
408 code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
409 #else
410 /* XXX: needs ajustments */
411 code_gen_buffer_size = (int)(phys_ram_size / 4);
412 #endif
414 if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
415 code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
416 /* The code gen buffer location may have constraints depending on
417 the host cpu and OS */
418 #if defined(__linux__)
420 int flags;
421 void *start = NULL;
423 flags = MAP_PRIVATE | MAP_ANONYMOUS;
424 #if defined(__x86_64__)
425 flags |= MAP_32BIT;
426 /* Cannot map more than that */
427 if (code_gen_buffer_size > (800 * 1024 * 1024))
428 code_gen_buffer_size = (800 * 1024 * 1024);
429 #elif defined(__sparc_v9__)
430 // Map the buffer below 2G, so we can use direct calls and branches
431 flags |= MAP_FIXED;
432 start = (void *) 0x60000000UL;
433 if (code_gen_buffer_size > (512 * 1024 * 1024))
434 code_gen_buffer_size = (512 * 1024 * 1024);
435 #endif
436 code_gen_buffer = mmap(start, code_gen_buffer_size,
437 PROT_WRITE | PROT_READ | PROT_EXEC,
438 flags, -1, 0);
439 if (code_gen_buffer == MAP_FAILED) {
440 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
441 exit(1);
444 #else
445 code_gen_buffer = qemu_malloc(code_gen_buffer_size);
446 if (!code_gen_buffer) {
447 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
448 exit(1);
450 map_exec(code_gen_buffer, code_gen_buffer_size);
451 #endif
452 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
453 map_exec(code_gen_prologue, sizeof(code_gen_prologue));
454 code_gen_buffer_max_size = code_gen_buffer_size -
455 code_gen_max_block_size();
456 code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
457 tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
460 /* Must be called before using the QEMU cpus. 'tb_size' is the size
461 (in bytes) allocated to the translation buffer. Zero means default
462 size. */
463 void cpu_exec_init_all(unsigned long tb_size)
465 cpu_gen_init();
466 code_gen_alloc(tb_size);
467 code_gen_ptr = code_gen_buffer;
468 page_init();
469 #if !defined(CONFIG_USER_ONLY)
470 io_mem_init();
471 #endif
474 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
476 #define CPU_COMMON_SAVE_VERSION 1
478 static void cpu_common_save(QEMUFile *f, void *opaque)
480 CPUState *env = opaque;
482 qemu_put_be32s(f, &env->halted);
483 qemu_put_be32s(f, &env->interrupt_request);
486 static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
488 CPUState *env = opaque;
490 if (version_id != CPU_COMMON_SAVE_VERSION)
491 return -EINVAL;
493 qemu_get_be32s(f, &env->halted);
494 qemu_get_be32s(f, &env->interrupt_request);
495 tlb_flush(env, 1);
497 return 0;
499 #endif
501 void cpu_exec_init(CPUState *env)
503 CPUState **penv;
504 int cpu_index;
506 env->next_cpu = NULL;
507 penv = &first_cpu;
508 cpu_index = 0;
509 while (*penv != NULL) {
510 penv = (CPUState **)&(*penv)->next_cpu;
511 cpu_index++;
513 env->cpu_index = cpu_index;
514 env->nb_watchpoints = 0;
515 #ifdef __WIN32
516 env->thread_id = GetCurrentProcessId();
517 #else
518 env->thread_id = getpid();
519 #endif
520 *penv = env;
521 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
522 register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
523 cpu_common_save, cpu_common_load, env);
524 register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
525 cpu_save, cpu_load, env);
526 #endif
529 static inline void invalidate_page_bitmap(PageDesc *p)
531 if (p->code_bitmap) {
532 qemu_free(p->code_bitmap);
533 p->code_bitmap = NULL;
535 p->code_write_count = 0;
538 /* set to NULL all the 'first_tb' fields in all PageDescs */
539 static void page_flush_tb(void)
541 int i, j;
542 PageDesc *p;
544 for(i = 0; i < L1_SIZE; i++) {
545 p = l1_map[i];
546 if (p) {
547 for(j = 0; j < L2_SIZE; j++) {
548 p->first_tb = NULL;
549 invalidate_page_bitmap(p);
550 p++;
556 /* flush all the translation blocks */
557 /* XXX: tb_flush is currently not thread safe */
558 void tb_flush(CPUState *env1)
560 CPUState *env;
561 #if defined(DEBUG_FLUSH)
562 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
563 (unsigned long)(code_gen_ptr - code_gen_buffer),
564 nb_tbs, nb_tbs > 0 ?
565 ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
566 #endif
567 if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
568 cpu_abort(env1, "Internal error: code buffer overflow\n");
570 nb_tbs = 0;
572 for(env = first_cpu; env != NULL; env = env->next_cpu) {
573 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
576 memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
577 page_flush_tb();
579 code_gen_ptr = code_gen_buffer;
580 /* XXX: flush processor icache at this point if cache flush is
581 expensive */
582 tb_flush_count++;
585 #ifdef DEBUG_TB_CHECK
587 static void tb_invalidate_check(target_ulong address)
589 TranslationBlock *tb;
590 int i;
591 address &= TARGET_PAGE_MASK;
592 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
593 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
594 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
595 address >= tb->pc + tb->size)) {
596 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
597 address, (long)tb->pc, tb->size);
603 /* verify that all the pages have correct rights for code */
604 static void tb_page_check(void)
606 TranslationBlock *tb;
607 int i, flags1, flags2;
609 for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
610 for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
611 flags1 = page_get_flags(tb->pc);
612 flags2 = page_get_flags(tb->pc + tb->size - 1);
613 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
614 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
615 (long)tb->pc, tb->size, flags1, flags2);
621 void tb_jmp_check(TranslationBlock *tb)
623 TranslationBlock *tb1;
624 unsigned int n1;
626 /* suppress any remaining jumps to this TB */
627 tb1 = tb->jmp_first;
628 for(;;) {
629 n1 = (long)tb1 & 3;
630 tb1 = (TranslationBlock *)((long)tb1 & ~3);
631 if (n1 == 2)
632 break;
633 tb1 = tb1->jmp_next[n1];
635 /* check end of list */
636 if (tb1 != tb) {
637 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
641 #endif
643 /* invalidate one TB */
644 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
645 int next_offset)
647 TranslationBlock *tb1;
648 for(;;) {
649 tb1 = *ptb;
650 if (tb1 == tb) {
651 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
652 break;
654 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
658 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
660 TranslationBlock *tb1;
661 unsigned int n1;
663 for(;;) {
664 tb1 = *ptb;
665 n1 = (long)tb1 & 3;
666 tb1 = (TranslationBlock *)((long)tb1 & ~3);
667 if (tb1 == tb) {
668 *ptb = tb1->page_next[n1];
669 break;
671 ptb = &tb1->page_next[n1];
675 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
677 TranslationBlock *tb1, **ptb;
678 unsigned int n1;
680 ptb = &tb->jmp_next[n];
681 tb1 = *ptb;
682 if (tb1) {
683 /* find tb(n) in circular list */
684 for(;;) {
685 tb1 = *ptb;
686 n1 = (long)tb1 & 3;
687 tb1 = (TranslationBlock *)((long)tb1 & ~3);
688 if (n1 == n && tb1 == tb)
689 break;
690 if (n1 == 2) {
691 ptb = &tb1->jmp_first;
692 } else {
693 ptb = &tb1->jmp_next[n1];
696 /* now we can suppress tb(n) from the list */
697 *ptb = tb->jmp_next[n];
699 tb->jmp_next[n] = NULL;
703 /* reset the jump entry 'n' of a TB so that it is not chained to
704 another TB */
705 static inline void tb_reset_jump(TranslationBlock *tb, int n)
707 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
710 void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
712 CPUState *env;
713 PageDesc *p;
714 unsigned int h, n1;
715 target_phys_addr_t phys_pc;
716 TranslationBlock *tb1, *tb2;
718 /* remove the TB from the hash list */
719 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
720 h = tb_phys_hash_func(phys_pc);
721 tb_remove(&tb_phys_hash[h], tb,
722 offsetof(TranslationBlock, phys_hash_next));
724 /* remove the TB from the page list */
725 if (tb->page_addr[0] != page_addr) {
726 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
727 tb_page_remove(&p->first_tb, tb);
728 invalidate_page_bitmap(p);
730 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
731 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
732 tb_page_remove(&p->first_tb, tb);
733 invalidate_page_bitmap(p);
736 tb_invalidated_flag = 1;
738 /* remove the TB from the hash list */
739 h = tb_jmp_cache_hash_func(tb->pc);
740 for(env = first_cpu; env != NULL; env = env->next_cpu) {
741 if (env->tb_jmp_cache[h] == tb)
742 env->tb_jmp_cache[h] = NULL;
745 /* suppress this TB from the two jump lists */
746 tb_jmp_remove(tb, 0);
747 tb_jmp_remove(tb, 1);
749 /* suppress any remaining jumps to this TB */
750 tb1 = tb->jmp_first;
751 for(;;) {
752 n1 = (long)tb1 & 3;
753 if (n1 == 2)
754 break;
755 tb1 = (TranslationBlock *)((long)tb1 & ~3);
756 tb2 = tb1->jmp_next[n1];
757 tb_reset_jump(tb1, n1);
758 tb1->jmp_next[n1] = NULL;
759 tb1 = tb2;
761 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
763 tb_phys_invalidate_count++;
766 static inline void set_bits(uint8_t *tab, int start, int len)
768 int end, mask, end1;
770 end = start + len;
771 tab += start >> 3;
772 mask = 0xff << (start & 7);
773 if ((start & ~7) == (end & ~7)) {
774 if (start < end) {
775 mask &= ~(0xff << (end & 7));
776 *tab |= mask;
778 } else {
779 *tab++ |= mask;
780 start = (start + 8) & ~7;
781 end1 = end & ~7;
782 while (start < end1) {
783 *tab++ = 0xff;
784 start += 8;
786 if (start < end) {
787 mask = ~(0xff << (end & 7));
788 *tab |= mask;
793 static void build_page_bitmap(PageDesc *p)
795 int n, tb_start, tb_end;
796 TranslationBlock *tb;
798 p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
799 if (!p->code_bitmap)
800 return;
802 tb = p->first_tb;
803 while (tb != NULL) {
804 n = (long)tb & 3;
805 tb = (TranslationBlock *)((long)tb & ~3);
806 /* NOTE: this is subtle as a TB may span two physical pages */
807 if (n == 0) {
808 /* NOTE: tb_end may be after the end of the page, but
809 it is not a problem */
810 tb_start = tb->pc & ~TARGET_PAGE_MASK;
811 tb_end = tb_start + tb->size;
812 if (tb_end > TARGET_PAGE_SIZE)
813 tb_end = TARGET_PAGE_SIZE;
814 } else {
815 tb_start = 0;
816 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
818 set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
819 tb = tb->page_next[n];
823 TranslationBlock *tb_gen_code(CPUState *env,
824 target_ulong pc, target_ulong cs_base,
825 int flags, int cflags)
827 TranslationBlock *tb;
828 uint8_t *tc_ptr;
829 target_ulong phys_pc, phys_page2, virt_page2;
830 int code_gen_size;
832 phys_pc = get_phys_addr_code(env, pc);
833 tb = tb_alloc(pc);
834 if (!tb) {
835 /* flush must be done */
836 tb_flush(env);
837 /* cannot fail at this point */
838 tb = tb_alloc(pc);
839 /* Don't forget to invalidate previous TB info. */
840 tb_invalidated_flag = 1;
842 tc_ptr = code_gen_ptr;
843 tb->tc_ptr = tc_ptr;
844 tb->cs_base = cs_base;
845 tb->flags = flags;
846 tb->cflags = cflags;
847 cpu_gen_code(env, tb, &code_gen_size);
848 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
850 /* check next page if needed */
851 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
852 phys_page2 = -1;
853 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
854 phys_page2 = get_phys_addr_code(env, virt_page2);
856 tb_link_phys(tb, phys_pc, phys_page2);
857 return tb;
860 /* invalidate all TBs which intersect with the target physical page
861 starting in range [start;end[. NOTE: start and end must refer to
862 the same physical page. 'is_cpu_write_access' should be true if called
863 from a real cpu write access: the virtual CPU will exit the current
864 TB if code is modified inside this TB. */
865 void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
866 int is_cpu_write_access)
868 int n, current_tb_modified, current_tb_not_found, current_flags;
869 CPUState *env = cpu_single_env;
870 PageDesc *p;
871 TranslationBlock *tb, *tb_next, *current_tb, *saved_tb;
872 target_ulong tb_start, tb_end;
873 target_ulong current_pc, current_cs_base;
875 p = page_find(start >> TARGET_PAGE_BITS);
876 if (!p)
877 return;
878 if (!p->code_bitmap &&
879 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
880 is_cpu_write_access) {
881 /* build code bitmap */
882 build_page_bitmap(p);
885 /* we remove all the TBs in the range [start, end[ */
886 /* XXX: see if in some cases it could be faster to invalidate all the code */
887 current_tb_not_found = is_cpu_write_access;
888 current_tb_modified = 0;
889 current_tb = NULL; /* avoid warning */
890 current_pc = 0; /* avoid warning */
891 current_cs_base = 0; /* avoid warning */
892 current_flags = 0; /* avoid warning */
893 tb = p->first_tb;
894 while (tb != NULL) {
895 n = (long)tb & 3;
896 tb = (TranslationBlock *)((long)tb & ~3);
897 tb_next = tb->page_next[n];
898 /* NOTE: this is subtle as a TB may span two physical pages */
899 if (n == 0) {
900 /* NOTE: tb_end may be after the end of the page, but
901 it is not a problem */
902 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
903 tb_end = tb_start + tb->size;
904 } else {
905 tb_start = tb->page_addr[1];
906 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
908 if (!(tb_end <= start || tb_start >= end)) {
909 #ifdef TARGET_HAS_PRECISE_SMC
910 if (current_tb_not_found) {
911 current_tb_not_found = 0;
912 current_tb = NULL;
913 if (env->mem_io_pc) {
914 /* now we have a real cpu fault */
915 current_tb = tb_find_pc(env->mem_io_pc);
918 if (current_tb == tb &&
919 (current_tb->cflags & CF_COUNT_MASK) != 1) {
920 /* If we are modifying the current TB, we must stop
921 its execution. We could be more precise by checking
922 that the modification is after the current PC, but it
923 would require a specialized function to partially
924 restore the CPU state */
926 current_tb_modified = 1;
927 cpu_restore_state(current_tb, env,
928 env->mem_io_pc, NULL);
929 #if defined(TARGET_I386)
930 current_flags = env->hflags;
931 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
932 current_cs_base = (target_ulong)env->segs[R_CS].base;
933 current_pc = current_cs_base + env->eip;
934 #else
935 #error unsupported CPU
936 #endif
938 #endif /* TARGET_HAS_PRECISE_SMC */
939 /* we need to do that to handle the case where a signal
940 occurs while doing tb_phys_invalidate() */
941 saved_tb = NULL;
942 if (env) {
943 saved_tb = env->current_tb;
944 env->current_tb = NULL;
946 tb_phys_invalidate(tb, -1);
947 if (env) {
948 env->current_tb = saved_tb;
949 if (env->interrupt_request && env->current_tb)
950 cpu_interrupt(env, env->interrupt_request);
953 tb = tb_next;
955 #if !defined(CONFIG_USER_ONLY)
956 /* if no code remaining, no need to continue to use slow writes */
957 if (!p->first_tb) {
958 invalidate_page_bitmap(p);
959 if (is_cpu_write_access) {
960 tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
963 #endif
964 #ifdef TARGET_HAS_PRECISE_SMC
965 if (current_tb_modified) {
966 /* we generate a block containing just the instruction
967 modifying the memory. It will ensure that it cannot modify
968 itself */
969 env->current_tb = NULL;
970 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
971 cpu_resume_from_signal(env, NULL);
973 #endif
976 /* len must be <= 8 and start must be a multiple of len */
977 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
979 PageDesc *p;
980 int offset, b;
981 #if 0
982 if (1) {
983 if (loglevel) {
984 fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
985 cpu_single_env->mem_io_vaddr, len,
986 cpu_single_env->eip,
987 cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
990 #endif
991 p = page_find(start >> TARGET_PAGE_BITS);
992 if (!p)
993 return;
994 if (p->code_bitmap) {
995 offset = start & ~TARGET_PAGE_MASK;
996 b = p->code_bitmap[offset >> 3] >> (offset & 7);
997 if (b & ((1 << len) - 1))
998 goto do_invalidate;
999 } else {
1000 do_invalidate:
1001 tb_invalidate_phys_page_range(start, start + len, 1);
1005 #if !defined(CONFIG_SOFTMMU)
1006 static void tb_invalidate_phys_page(target_phys_addr_t addr,
1007 unsigned long pc, void *puc)
1009 int n, current_flags, current_tb_modified;
1010 target_ulong current_pc, current_cs_base;
1011 PageDesc *p;
1012 TranslationBlock *tb, *current_tb;
1013 #ifdef TARGET_HAS_PRECISE_SMC
1014 CPUState *env = cpu_single_env;
1015 #endif
1017 addr &= TARGET_PAGE_MASK;
1018 p = page_find(addr >> TARGET_PAGE_BITS);
1019 if (!p)
1020 return;
1021 tb = p->first_tb;
1022 current_tb_modified = 0;
1023 current_tb = NULL;
1024 current_pc = 0; /* avoid warning */
1025 current_cs_base = 0; /* avoid warning */
1026 current_flags = 0; /* avoid warning */
1027 #ifdef TARGET_HAS_PRECISE_SMC
1028 if (tb && pc != 0) {
1029 current_tb = tb_find_pc(pc);
1031 #endif
1032 while (tb != NULL) {
1033 n = (long)tb & 3;
1034 tb = (TranslationBlock *)((long)tb & ~3);
1035 #ifdef TARGET_HAS_PRECISE_SMC
1036 if (current_tb == tb &&
1037 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1038 /* If we are modifying the current TB, we must stop
1039 its execution. We could be more precise by checking
1040 that the modification is after the current PC, but it
1041 would require a specialized function to partially
1042 restore the CPU state */
1044 current_tb_modified = 1;
1045 cpu_restore_state(current_tb, env, pc, puc);
1046 #if defined(TARGET_I386)
1047 current_flags = env->hflags;
1048 current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
1049 current_cs_base = (target_ulong)env->segs[R_CS].base;
1050 current_pc = current_cs_base + env->eip;
1051 #else
1052 #error unsupported CPU
1053 #endif
1055 #endif /* TARGET_HAS_PRECISE_SMC */
1056 tb_phys_invalidate(tb, addr);
1057 tb = tb->page_next[n];
1059 p->first_tb = NULL;
1060 #ifdef TARGET_HAS_PRECISE_SMC
1061 if (current_tb_modified) {
1062 /* we generate a block containing just the instruction
1063 modifying the memory. It will ensure that it cannot modify
1064 itself */
1065 env->current_tb = NULL;
1066 tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
1067 cpu_resume_from_signal(env, puc);
1069 #endif
1071 #endif
1073 /* add the tb in the target page and protect it if necessary */
1074 static inline void tb_alloc_page(TranslationBlock *tb,
1075 unsigned int n, target_ulong page_addr)
1077 PageDesc *p;
1078 TranslationBlock *last_first_tb;
1080 tb->page_addr[n] = page_addr;
1081 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
1082 tb->page_next[n] = p->first_tb;
1083 last_first_tb = p->first_tb;
1084 p->first_tb = (TranslationBlock *)((long)tb | n);
1085 invalidate_page_bitmap(p);
1087 #if defined(TARGET_HAS_SMC) || 1
1089 #if defined(CONFIG_USER_ONLY)
1090 if (p->flags & PAGE_WRITE) {
1091 target_ulong addr;
1092 PageDesc *p2;
1093 int prot;
1095 /* force the host page as non writable (writes will have a
1096 page fault + mprotect overhead) */
1097 page_addr &= qemu_host_page_mask;
1098 prot = 0;
1099 for(addr = page_addr; addr < page_addr + qemu_host_page_size;
1100 addr += TARGET_PAGE_SIZE) {
1102 p2 = page_find (addr >> TARGET_PAGE_BITS);
1103 if (!p2)
1104 continue;
1105 prot |= p2->flags;
1106 p2->flags &= ~PAGE_WRITE;
1107 page_get_flags(addr);
1109 mprotect(g2h(page_addr), qemu_host_page_size,
1110 (prot & PAGE_BITS) & ~PAGE_WRITE);
1111 #ifdef DEBUG_TB_INVALIDATE
1112 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1113 page_addr);
1114 #endif
1116 #else
1117 /* if some code is already present, then the pages are already
1118 protected. So we handle the case where only the first TB is
1119 allocated in a physical page */
1120 if (!last_first_tb) {
1121 tlb_protect_code(page_addr);
1123 #endif
1125 #endif /* TARGET_HAS_SMC */
1128 /* Allocate a new translation block. Flush the translation buffer if
1129 too many translation blocks or too much generated code. */
1130 TranslationBlock *tb_alloc(target_ulong pc)
1132 TranslationBlock *tb;
1134 if (nb_tbs >= code_gen_max_blocks ||
1135 (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
1136 return NULL;
1137 tb = &tbs[nb_tbs++];
1138 tb->pc = pc;
1139 tb->cflags = 0;
1140 return tb;
1143 void tb_free(TranslationBlock *tb)
1145 /* In practice this is mostly used for single use temporary TB
1146 Ignore the hard cases and just back up if this TB happens to
1147 be the last one generated. */
1148 if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1149 code_gen_ptr = tb->tc_ptr;
1150 nb_tbs--;
1154 /* add a new TB and link it to the physical page tables. phys_page2 is
1155 (-1) to indicate that only one page contains the TB. */
1156 void tb_link_phys(TranslationBlock *tb,
1157 target_ulong phys_pc, target_ulong phys_page2)
1159 unsigned int h;
1160 TranslationBlock **ptb;
1162 /* Grab the mmap lock to stop another thread invalidating this TB
1163 before we are done. */
1164 mmap_lock();
1165 /* add in the physical hash table */
1166 h = tb_phys_hash_func(phys_pc);
1167 ptb = &tb_phys_hash[h];
1168 tb->phys_hash_next = *ptb;
1169 *ptb = tb;
1171 /* add in the page list */
1172 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1173 if (phys_page2 != -1)
1174 tb_alloc_page(tb, 1, phys_page2);
1175 else
1176 tb->page_addr[1] = -1;
1178 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
1179 tb->jmp_next[0] = NULL;
1180 tb->jmp_next[1] = NULL;
1182 /* init original jump addresses */
1183 if (tb->tb_next_offset[0] != 0xffff)
1184 tb_reset_jump(tb, 0);
1185 if (tb->tb_next_offset[1] != 0xffff)
1186 tb_reset_jump(tb, 1);
1188 #ifdef DEBUG_TB_CHECK
1189 tb_page_check();
1190 #endif
1191 mmap_unlock();
1194 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1195 tb[1].tc_ptr. Return NULL if not found */
1196 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
1198 int m_min, m_max, m;
1199 unsigned long v;
1200 TranslationBlock *tb;
1202 if (nb_tbs <= 0)
1203 return NULL;
1204 if (tc_ptr < (unsigned long)code_gen_buffer ||
1205 tc_ptr >= (unsigned long)code_gen_ptr)
1206 return NULL;
1207 /* binary search (cf Knuth) */
1208 m_min = 0;
1209 m_max = nb_tbs - 1;
1210 while (m_min <= m_max) {
1211 m = (m_min + m_max) >> 1;
1212 tb = &tbs[m];
1213 v = (unsigned long)tb->tc_ptr;
1214 if (v == tc_ptr)
1215 return tb;
1216 else if (tc_ptr < v) {
1217 m_max = m - 1;
1218 } else {
1219 m_min = m + 1;
1222 return &tbs[m_max];
1225 static void tb_reset_jump_recursive(TranslationBlock *tb);
1227 static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
1229 TranslationBlock *tb1, *tb_next, **ptb;
1230 unsigned int n1;
1232 tb1 = tb->jmp_next[n];
1233 if (tb1 != NULL) {
1234 /* find head of list */
1235 for(;;) {
1236 n1 = (long)tb1 & 3;
1237 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1238 if (n1 == 2)
1239 break;
1240 tb1 = tb1->jmp_next[n1];
1242 /* we are now sure now that tb jumps to tb1 */
1243 tb_next = tb1;
1245 /* remove tb from the jmp_first list */
1246 ptb = &tb_next->jmp_first;
1247 for(;;) {
1248 tb1 = *ptb;
1249 n1 = (long)tb1 & 3;
1250 tb1 = (TranslationBlock *)((long)tb1 & ~3);
1251 if (n1 == n && tb1 == tb)
1252 break;
1253 ptb = &tb1->jmp_next[n1];
1255 *ptb = tb->jmp_next[n];
1256 tb->jmp_next[n] = NULL;
1258 /* suppress the jump to next tb in generated code */
1259 tb_reset_jump(tb, n);
1261 /* suppress jumps in the tb on which we could have jumped */
1262 tb_reset_jump_recursive(tb_next);
1266 static void tb_reset_jump_recursive(TranslationBlock *tb)
1268 tb_reset_jump_recursive2(tb, 0);
1269 tb_reset_jump_recursive2(tb, 1);
1272 #if defined(TARGET_HAS_ICE)
1273 static void breakpoint_invalidate(CPUState *env, target_ulong pc)
1275 target_phys_addr_t addr;
1276 target_ulong pd;
1277 ram_addr_t ram_addr;
1278 PhysPageDesc *p;
1280 addr = cpu_get_phys_page_debug(env, pc);
1281 p = phys_page_find(addr >> TARGET_PAGE_BITS);
1282 if (!p) {
1283 pd = IO_MEM_UNASSIGNED;
1284 } else {
1285 pd = p->phys_offset;
1287 ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
1288 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1290 #endif
1292 /* Add a watchpoint. */
1293 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type)
1295 int i;
1297 for (i = 0; i < env->nb_watchpoints; i++) {
1298 if (addr == env->watchpoint[i].vaddr)
1299 return 0;
1301 if (env->nb_watchpoints >= MAX_WATCHPOINTS)
1302 return -1;
1304 i = env->nb_watchpoints++;
1305 env->watchpoint[i].vaddr = addr;
1306 env->watchpoint[i].type = type;
1307 tlb_flush_page(env, addr);
1308 /* FIXME: This flush is needed because of the hack to make memory ops
1309 terminate the TB. It can be removed once the proper IO trap and
1310 re-execute bits are in. */
1311 tb_flush(env);
1312 return i;
1315 /* Remove a watchpoint. */
1316 int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
1318 int i;
1320 for (i = 0; i < env->nb_watchpoints; i++) {
1321 if (addr == env->watchpoint[i].vaddr) {
1322 env->nb_watchpoints--;
1323 env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
1324 tlb_flush_page(env, addr);
1325 return 0;
1328 return -1;
1331 /* Remove all watchpoints. */
1332 void cpu_watchpoint_remove_all(CPUState *env) {
1333 int i;
1335 for (i = 0; i < env->nb_watchpoints; i++) {
1336 tlb_flush_page(env, env->watchpoint[i].vaddr);
1338 env->nb_watchpoints = 0;
1341 /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1342 breakpoint is reached */
1343 int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
1345 #if defined(TARGET_HAS_ICE)
1346 int i;
1348 for(i = 0; i < env->nb_breakpoints; i++) {
1349 if (env->breakpoints[i] == pc)
1350 return 0;
1353 if (env->nb_breakpoints >= MAX_BREAKPOINTS)
1354 return -1;
1355 env->breakpoints[env->nb_breakpoints++] = pc;
1357 if (kvm_enabled())
1358 kvm_update_debugger(env);
1360 breakpoint_invalidate(env, pc);
1361 return 0;
1362 #else
1363 return -1;
1364 #endif
1367 /* remove all breakpoints */
1368 void cpu_breakpoint_remove_all(CPUState *env) {
1369 #if defined(TARGET_HAS_ICE)
1370 int i;
1371 for(i = 0; i < env->nb_breakpoints; i++) {
1372 breakpoint_invalidate(env, env->breakpoints[i]);
1374 env->nb_breakpoints = 0;
1375 #endif
1378 /* remove a breakpoint */
1379 int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
1381 #if defined(TARGET_HAS_ICE)
1382 int i;
1383 for(i = 0; i < env->nb_breakpoints; i++) {
1384 if (env->breakpoints[i] == pc)
1385 goto found;
1387 return -1;
1388 found:
1389 env->nb_breakpoints--;
1390 if (i < env->nb_breakpoints)
1391 env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
1393 if (kvm_enabled())
1394 kvm_update_debugger(env);
1396 breakpoint_invalidate(env, pc);
1397 return 0;
1398 #else
1399 return -1;
1400 #endif
1403 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1404 CPU loop after each instruction */
1405 void cpu_single_step(CPUState *env, int enabled)
1407 #if defined(TARGET_HAS_ICE)
1408 if (env->singlestep_enabled != enabled) {
1409 env->singlestep_enabled = enabled;
1410 /* must flush all the translated code to avoid inconsistancies */
1411 /* XXX: only flush what is necessary */
1412 tb_flush(env);
1414 if (kvm_enabled())
1415 kvm_update_debugger(env);
1416 #endif
1419 /* enable or disable low levels log */
1420 void cpu_set_log(int log_flags)
1422 loglevel = log_flags;
1423 if (loglevel && !logfile) {
1424 logfile = fopen(logfilename, log_append ? "a" : "w");
1425 if (!logfile) {
1426 perror(logfilename);
1427 _exit(1);
1429 #if !defined(CONFIG_SOFTMMU)
1430 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1432 static uint8_t logfile_buf[4096];
1433 setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1435 #else
1436 setvbuf(logfile, NULL, _IOLBF, 0);
1437 #endif
1438 log_append = 1;
1440 if (!loglevel && logfile) {
1441 fclose(logfile);
1442 logfile = NULL;
1446 void cpu_set_log_filename(const char *filename)
1448 logfilename = strdup(filename);
1449 if (logfile) {
1450 fclose(logfile);
1451 logfile = NULL;
1453 cpu_set_log(loglevel);
1456 /* mask must never be zero, except for A20 change call */
1457 void cpu_interrupt(CPUState *env, int mask)
1459 #if !defined(USE_NPTL)
1460 TranslationBlock *tb;
1461 static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
1462 #endif
1463 int old_mask;
1465 old_mask = env->interrupt_request;
1466 /* FIXME: This is probably not threadsafe. A different thread could
1467 be in the middle of a read-modify-write operation. */
1468 env->interrupt_request |= mask;
1469 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
1470 kvm_update_interrupt_request(env);
1471 #if defined(USE_NPTL)
1472 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1473 problem and hope the cpu will stop of its own accord. For userspace
1474 emulation this often isn't actually as bad as it sounds. Often
1475 signals are used primarily to interrupt blocking syscalls. */
1476 #else
1477 if (use_icount) {
1478 env->icount_decr.u16.high = 0xffff;
1479 #ifndef CONFIG_USER_ONLY
1480 /* CPU_INTERRUPT_EXIT isn't a real interrupt. It just means
1481 an async event happened and we need to process it. */
1482 if (!can_do_io(env)
1483 && (mask & ~(old_mask | CPU_INTERRUPT_EXIT)) != 0) {
1484 cpu_abort(env, "Raised interrupt while not in I/O function");
1486 #endif
1487 } else {
1488 tb = env->current_tb;
1489 /* if the cpu is currently executing code, we must unlink it and
1490 all the potentially executing TB */
1491 if (tb && !testandset(&interrupt_lock)) {
1492 env->current_tb = NULL;
1493 tb_reset_jump_recursive(tb);
1494 resetlock(&interrupt_lock);
1497 #endif
1500 void cpu_reset_interrupt(CPUState *env, int mask)
1502 env->interrupt_request &= ~mask;
1505 CPULogItem cpu_log_items[] = {
1506 { CPU_LOG_TB_OUT_ASM, "out_asm",
1507 "show generated host assembly code for each compiled TB" },
1508 { CPU_LOG_TB_IN_ASM, "in_asm",
1509 "show target assembly code for each compiled TB" },
1510 { CPU_LOG_TB_OP, "op",
1511 "show micro ops for each compiled TB" },
1512 { CPU_LOG_TB_OP_OPT, "op_opt",
1513 "show micro ops "
1514 #ifdef TARGET_I386
1515 "before eflags optimization and "
1516 #endif
1517 "after liveness analysis" },
1518 { CPU_LOG_INT, "int",
1519 "show interrupts/exceptions in short format" },
1520 { CPU_LOG_EXEC, "exec",
1521 "show trace before each executed TB (lots of logs)" },
1522 { CPU_LOG_TB_CPU, "cpu",
1523 "show CPU state before block translation" },
1524 #ifdef TARGET_I386
1525 { CPU_LOG_PCALL, "pcall",
1526 "show protected mode far calls/returns/exceptions" },
1527 #endif
1528 #ifdef DEBUG_IOPORT
1529 { CPU_LOG_IOPORT, "ioport",
1530 "show all i/o ports accesses" },
1531 #endif
1532 { 0, NULL, NULL },
1535 static int cmp1(const char *s1, int n, const char *s2)
1537 if (strlen(s2) != n)
1538 return 0;
1539 return memcmp(s1, s2, n) == 0;
1542 /* takes a comma separated list of log masks. Return 0 if error. */
1543 int cpu_str_to_log_mask(const char *str)
1545 CPULogItem *item;
1546 int mask;
1547 const char *p, *p1;
1549 p = str;
1550 mask = 0;
1551 for(;;) {
1552 p1 = strchr(p, ',');
1553 if (!p1)
1554 p1 = p + strlen(p);
1555 if(cmp1(p,p1-p,"all")) {
1556 for(item = cpu_log_items; item->mask != 0; item++) {
1557 mask |= item->mask;
1559 } else {
1560 for(item = cpu_log_items; item->mask != 0; item++) {
1561 if (cmp1(p, p1 - p, item->name))
1562 goto found;
1564 return 0;
1566 found:
1567 mask |= item->mask;
1568 if (*p1 != ',')
1569 break;
1570 p = p1 + 1;
1572 return mask;
1575 void cpu_abort(CPUState *env, const char *fmt, ...)
1577 va_list ap;
1578 va_list ap2;
1580 va_start(ap, fmt);
1581 va_copy(ap2, ap);
1582 fprintf(stderr, "qemu: fatal: ");
1583 vfprintf(stderr, fmt, ap);
1584 fprintf(stderr, "\n");
1585 #ifdef TARGET_I386
1586 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1587 #else
1588 cpu_dump_state(env, stderr, fprintf, 0);
1589 #endif
1590 if (logfile) {
1591 fprintf(logfile, "qemu: fatal: ");
1592 vfprintf(logfile, fmt, ap2);
1593 fprintf(logfile, "\n");
1594 #ifdef TARGET_I386
1595 cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1596 #else
1597 cpu_dump_state(env, logfile, fprintf, 0);
1598 #endif
1599 fflush(logfile);
1600 fclose(logfile);
1602 va_end(ap2);
1603 va_end(ap);
1604 abort();
1607 CPUState *cpu_copy(CPUState *env)
1609 CPUState *new_env = cpu_init(env->cpu_model_str);
1610 /* preserve chaining and index */
1611 CPUState *next_cpu = new_env->next_cpu;
1612 int cpu_index = new_env->cpu_index;
1613 memcpy(new_env, env, sizeof(CPUState));
1614 new_env->next_cpu = next_cpu;
1615 new_env->cpu_index = cpu_index;
1616 return new_env;
1619 #if !defined(CONFIG_USER_ONLY)
1621 static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1623 unsigned int i;
1625 /* Discard jump cache entries for any tb which might potentially
1626 overlap the flushed page. */
1627 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1628 memset (&env->tb_jmp_cache[i], 0,
1629 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1631 i = tb_jmp_cache_hash_page(addr);
1632 memset (&env->tb_jmp_cache[i], 0,
1633 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1636 /* NOTE: if flush_global is true, also flush global entries (not
1637 implemented yet) */
1638 void tlb_flush(CPUState *env, int flush_global)
1640 int i;
1642 #if defined(DEBUG_TLB)
1643 printf("tlb_flush:\n");
1644 #endif
1645 /* must reset current TB so that interrupts cannot modify the
1646 links while we are modifying them */
1647 env->current_tb = NULL;
1649 for(i = 0; i < CPU_TLB_SIZE; i++) {
1650 env->tlb_table[0][i].addr_read = -1;
1651 env->tlb_table[0][i].addr_write = -1;
1652 env->tlb_table[0][i].addr_code = -1;
1653 env->tlb_table[1][i].addr_read = -1;
1654 env->tlb_table[1][i].addr_write = -1;
1655 env->tlb_table[1][i].addr_code = -1;
1656 #if (NB_MMU_MODES >= 3)
1657 env->tlb_table[2][i].addr_read = -1;
1658 env->tlb_table[2][i].addr_write = -1;
1659 env->tlb_table[2][i].addr_code = -1;
1660 #if (NB_MMU_MODES == 4)
1661 env->tlb_table[3][i].addr_read = -1;
1662 env->tlb_table[3][i].addr_write = -1;
1663 env->tlb_table[3][i].addr_code = -1;
1664 #endif
1665 #endif
1668 memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
1670 #ifdef USE_KQEMU
1671 if (env->kqemu_enabled) {
1672 kqemu_flush(env, flush_global);
1674 #endif
1675 tlb_flush_count++;
1678 static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
1680 if (addr == (tlb_entry->addr_read &
1681 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1682 addr == (tlb_entry->addr_write &
1683 (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
1684 addr == (tlb_entry->addr_code &
1685 (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1686 tlb_entry->addr_read = -1;
1687 tlb_entry->addr_write = -1;
1688 tlb_entry->addr_code = -1;
1692 void tlb_flush_page(CPUState *env, target_ulong addr)
1694 int i;
1696 #if defined(DEBUG_TLB)
1697 printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
1698 #endif
1699 /* must reset current TB so that interrupts cannot modify the
1700 links while we are modifying them */
1701 env->current_tb = NULL;
1703 addr &= TARGET_PAGE_MASK;
1704 i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1705 tlb_flush_entry(&env->tlb_table[0][i], addr);
1706 tlb_flush_entry(&env->tlb_table[1][i], addr);
1707 #if (NB_MMU_MODES >= 3)
1708 tlb_flush_entry(&env->tlb_table[2][i], addr);
1709 #if (NB_MMU_MODES == 4)
1710 tlb_flush_entry(&env->tlb_table[3][i], addr);
1711 #endif
1712 #endif
1714 tlb_flush_jmp_cache(env, addr);
1716 #ifdef USE_KQEMU
1717 if (env->kqemu_enabled) {
1718 kqemu_flush_page(env, addr);
1720 #endif
1723 /* update the TLBs so that writes to code in the virtual page 'addr'
1724 can be detected */
1725 static void tlb_protect_code(ram_addr_t ram_addr)
1727 cpu_physical_memory_reset_dirty(ram_addr,
1728 ram_addr + TARGET_PAGE_SIZE,
1729 CODE_DIRTY_FLAG);
1732 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1733 tested for self modifying code */
1734 static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
1735 target_ulong vaddr)
1737 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
1740 static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
1741 unsigned long start, unsigned long length)
1743 unsigned long addr;
1744 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1745 addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
1746 if ((addr - start) < length) {
1747 tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
1752 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
1753 int dirty_flags)
1755 CPUState *env;
1756 unsigned long length, start1;
1757 int i, mask, len;
1758 uint8_t *p;
1760 start &= TARGET_PAGE_MASK;
1761 end = TARGET_PAGE_ALIGN(end);
1763 length = end - start;
1764 if (length == 0)
1765 return;
1766 len = length >> TARGET_PAGE_BITS;
1767 #ifdef USE_KQEMU
1768 /* XXX: should not depend on cpu context */
1769 env = first_cpu;
1770 if (env->kqemu_enabled) {
1771 ram_addr_t addr;
1772 addr = start;
1773 for(i = 0; i < len; i++) {
1774 kqemu_set_notdirty(env, addr);
1775 addr += TARGET_PAGE_SIZE;
1778 #endif
1779 mask = ~dirty_flags;
1780 p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
1781 for(i = 0; i < len; i++)
1782 p[i] &= mask;
1784 /* we modify the TLB cache so that the dirty bit will be set again
1785 when accessing the range */
1786 start1 = start + (unsigned long)phys_ram_base;
1787 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1788 for(i = 0; i < CPU_TLB_SIZE; i++)
1789 tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
1790 for(i = 0; i < CPU_TLB_SIZE; i++)
1791 tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
1792 #if (NB_MMU_MODES >= 3)
1793 for(i = 0; i < CPU_TLB_SIZE; i++)
1794 tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
1795 #if (NB_MMU_MODES == 4)
1796 for(i = 0; i < CPU_TLB_SIZE; i++)
1797 tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
1798 #endif
1799 #endif
1803 int cpu_physical_memory_set_dirty_tracking(int enable)
1805 int r=0;
1807 if (kvm_enabled())
1808 r = kvm_physical_memory_set_dirty_tracking(enable);
1809 in_migration = enable;
1810 return r;
1813 int cpu_physical_memory_get_dirty_tracking(void)
1815 return in_migration;
1818 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1820 ram_addr_t ram_addr;
1822 if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1823 ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
1824 tlb_entry->addend - (unsigned long)phys_ram_base;
1825 if (!cpu_physical_memory_is_dirty(ram_addr)) {
1826 tlb_entry->addr_write |= TLB_NOTDIRTY;
1831 /* update the TLB according to the current state of the dirty bits */
1832 void cpu_tlb_update_dirty(CPUState *env)
1834 int i;
1835 for(i = 0; i < CPU_TLB_SIZE; i++)
1836 tlb_update_dirty(&env->tlb_table[0][i]);
1837 for(i = 0; i < CPU_TLB_SIZE; i++)
1838 tlb_update_dirty(&env->tlb_table[1][i]);
1839 #if (NB_MMU_MODES >= 3)
1840 for(i = 0; i < CPU_TLB_SIZE; i++)
1841 tlb_update_dirty(&env->tlb_table[2][i]);
1842 #if (NB_MMU_MODES == 4)
1843 for(i = 0; i < CPU_TLB_SIZE; i++)
1844 tlb_update_dirty(&env->tlb_table[3][i]);
1845 #endif
1846 #endif
1849 static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
1851 if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1852 tlb_entry->addr_write = vaddr;
1855 /* update the TLB corresponding to virtual page vaddr
1856 so that it is no longer dirty */
1857 static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
1859 int i;
1861 vaddr &= TARGET_PAGE_MASK;
1862 i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1863 tlb_set_dirty1(&env->tlb_table[0][i], vaddr);
1864 tlb_set_dirty1(&env->tlb_table[1][i], vaddr);
1865 #if (NB_MMU_MODES >= 3)
1866 tlb_set_dirty1(&env->tlb_table[2][i], vaddr);
1867 #if (NB_MMU_MODES == 4)
1868 tlb_set_dirty1(&env->tlb_table[3][i], vaddr);
1869 #endif
1870 #endif
1873 /* add a new TLB entry. At most one entry for a given virtual address
1874 is permitted. Return 0 if OK or 2 if the page could not be mapped
1875 (can only happen in non SOFTMMU mode for I/O pages or pages
1876 conflicting with the host address space). */
1877 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1878 target_phys_addr_t paddr, int prot,
1879 int mmu_idx, int is_softmmu)
1881 PhysPageDesc *p;
1882 unsigned long pd;
1883 unsigned int index;
1884 target_ulong address;
1885 target_ulong code_address;
1886 target_phys_addr_t addend;
1887 int ret;
1888 CPUTLBEntry *te;
1889 int i;
1890 target_phys_addr_t iotlb;
1892 p = phys_page_find(paddr >> TARGET_PAGE_BITS);
1893 if (!p) {
1894 pd = IO_MEM_UNASSIGNED;
1895 } else {
1896 pd = p->phys_offset;
1898 #if defined(DEBUG_TLB)
1899 printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1900 vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
1901 #endif
1903 ret = 0;
1904 address = vaddr;
1905 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
1906 /* IO memory case (romd handled later) */
1907 address |= TLB_MMIO;
1909 addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1910 if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
1911 /* Normal RAM. */
1912 iotlb = pd & TARGET_PAGE_MASK;
1913 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
1914 iotlb |= IO_MEM_NOTDIRTY;
1915 else
1916 iotlb |= IO_MEM_ROM;
1917 } else {
1918 /* IO handlers are currently passed a phsical address.
1919 It would be nice to pass an offset from the base address
1920 of that region. This would avoid having to special case RAM,
1921 and avoid full address decoding in every device.
1922 We can't use the high bits of pd for this because
1923 IO_MEM_ROMD uses these as a ram address. */
1924 iotlb = (pd & ~TARGET_PAGE_MASK) + paddr;
1927 code_address = address;
1928 /* Make accesses to pages with watchpoints go via the
1929 watchpoint trap routines. */
1930 for (i = 0; i < env->nb_watchpoints; i++) {
1931 if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1932 iotlb = io_mem_watch + paddr;
1933 /* TODO: The memory case can be optimized by not trapping
1934 reads of pages with a write breakpoint. */
1935 address |= TLB_MMIO;
1939 index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1940 env->iotlb[mmu_idx][index] = iotlb - vaddr;
1941 te = &env->tlb_table[mmu_idx][index];
1942 te->addend = addend - vaddr;
1943 if (prot & PAGE_READ) {
1944 te->addr_read = address;
1945 } else {
1946 te->addr_read = -1;
1949 if (prot & PAGE_EXEC) {
1950 te->addr_code = code_address;
1951 } else {
1952 te->addr_code = -1;
1954 if (prot & PAGE_WRITE) {
1955 if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
1956 (pd & IO_MEM_ROMD)) {
1957 /* Write access calls the I/O callback. */
1958 te->addr_write = address | TLB_MMIO;
1959 } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
1960 !cpu_physical_memory_is_dirty(pd)) {
1961 te->addr_write = address | TLB_NOTDIRTY;
1962 } else {
1963 te->addr_write = address;
1965 } else {
1966 te->addr_write = -1;
1968 return ret;
1971 #else
1973 void tlb_flush(CPUState *env, int flush_global)
1977 void tlb_flush_page(CPUState *env, target_ulong addr)
1981 int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
1982 target_phys_addr_t paddr, int prot,
1983 int mmu_idx, int is_softmmu)
1985 return 0;
1988 /* dump memory mappings */
1989 void page_dump(FILE *f)
1991 unsigned long start, end;
1992 int i, j, prot, prot1;
1993 PageDesc *p;
1995 fprintf(f, "%-8s %-8s %-8s %s\n",
1996 "start", "end", "size", "prot");
1997 start = -1;
1998 end = -1;
1999 prot = 0;
2000 for(i = 0; i <= L1_SIZE; i++) {
2001 if (i < L1_SIZE)
2002 p = l1_map[i];
2003 else
2004 p = NULL;
2005 for(j = 0;j < L2_SIZE; j++) {
2006 if (!p)
2007 prot1 = 0;
2008 else
2009 prot1 = p[j].flags;
2010 if (prot1 != prot) {
2011 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
2012 if (start != -1) {
2013 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
2014 start, end, end - start,
2015 prot & PAGE_READ ? 'r' : '-',
2016 prot & PAGE_WRITE ? 'w' : '-',
2017 prot & PAGE_EXEC ? 'x' : '-');
2019 if (prot1 != 0)
2020 start = end;
2021 else
2022 start = -1;
2023 prot = prot1;
2025 if (!p)
2026 break;
2031 int page_get_flags(target_ulong address)
2033 PageDesc *p;
2035 p = page_find(address >> TARGET_PAGE_BITS);
2036 if (!p)
2037 return 0;
2038 return p->flags;
2041 /* modify the flags of a page and invalidate the code if
2042 necessary. The flag PAGE_WRITE_ORG is positionned automatically
2043 depending on PAGE_WRITE */
2044 void page_set_flags(target_ulong start, target_ulong end, int flags)
2046 PageDesc *p;
2047 target_ulong addr;
2049 /* mmap_lock should already be held. */
2050 start = start & TARGET_PAGE_MASK;
2051 end = TARGET_PAGE_ALIGN(end);
2052 if (flags & PAGE_WRITE)
2053 flags |= PAGE_WRITE_ORG;
2054 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2055 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
2056 /* We may be called for host regions that are outside guest
2057 address space. */
2058 if (!p)
2059 return;
2060 /* if the write protection is set, then we invalidate the code
2061 inside */
2062 if (!(p->flags & PAGE_WRITE) &&
2063 (flags & PAGE_WRITE) &&
2064 p->first_tb) {
2065 tb_invalidate_phys_page(addr, 0, NULL);
2067 p->flags = flags;
2071 int page_check_range(target_ulong start, target_ulong len, int flags)
2073 PageDesc *p;
2074 target_ulong end;
2075 target_ulong addr;
2077 end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2078 start = start & TARGET_PAGE_MASK;
2080 if( end < start )
2081 /* we've wrapped around */
2082 return -1;
2083 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2084 p = page_find(addr >> TARGET_PAGE_BITS);
2085 if( !p )
2086 return -1;
2087 if( !(p->flags & PAGE_VALID) )
2088 return -1;
2090 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
2091 return -1;
2092 if (flags & PAGE_WRITE) {
2093 if (!(p->flags & PAGE_WRITE_ORG))
2094 return -1;
2095 /* unprotect the page if it was put read-only because it
2096 contains translated code */
2097 if (!(p->flags & PAGE_WRITE)) {
2098 if (!page_unprotect(addr, 0, NULL))
2099 return -1;
2101 return 0;
2104 return 0;
2107 /* called from signal handler: invalidate the code and unprotect the
2108 page. Return TRUE if the fault was succesfully handled. */
2109 int page_unprotect(target_ulong address, unsigned long pc, void *puc)
2111 unsigned int page_index, prot, pindex;
2112 PageDesc *p, *p1;
2113 target_ulong host_start, host_end, addr;
2115 /* Technically this isn't safe inside a signal handler. However we
2116 know this only ever happens in a synchronous SEGV handler, so in
2117 practice it seems to be ok. */
2118 mmap_lock();
2120 host_start = address & qemu_host_page_mask;
2121 page_index = host_start >> TARGET_PAGE_BITS;
2122 p1 = page_find(page_index);
2123 if (!p1) {
2124 mmap_unlock();
2125 return 0;
2127 host_end = host_start + qemu_host_page_size;
2128 p = p1;
2129 prot = 0;
2130 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
2131 prot |= p->flags;
2132 p++;
2134 /* if the page was really writable, then we change its
2135 protection back to writable */
2136 if (prot & PAGE_WRITE_ORG) {
2137 pindex = (address - host_start) >> TARGET_PAGE_BITS;
2138 if (!(p1[pindex].flags & PAGE_WRITE)) {
2139 mprotect((void *)g2h(host_start), qemu_host_page_size,
2140 (prot & PAGE_BITS) | PAGE_WRITE);
2141 p1[pindex].flags |= PAGE_WRITE;
2142 /* and since the content will be modified, we must invalidate
2143 the corresponding translated code. */
2144 tb_invalidate_phys_page(address, pc, puc);
2145 #ifdef DEBUG_TB_CHECK
2146 tb_invalidate_check(address);
2147 #endif
2148 mmap_unlock();
2149 return 1;
2152 mmap_unlock();
2153 return 0;
2156 static inline void tlb_set_dirty(CPUState *env,
2157 unsigned long addr, target_ulong vaddr)
2160 #endif /* defined(CONFIG_USER_ONLY) */
2162 #if !defined(CONFIG_USER_ONLY)
2163 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2164 ram_addr_t memory);
2165 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2166 ram_addr_t orig_memory);
2167 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2168 need_subpage) \
2169 do { \
2170 if (addr > start_addr) \
2171 start_addr2 = 0; \
2172 else { \
2173 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2174 if (start_addr2 > 0) \
2175 need_subpage = 1; \
2178 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2179 end_addr2 = TARGET_PAGE_SIZE - 1; \
2180 else { \
2181 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2182 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2183 need_subpage = 1; \
2185 } while (0)
2187 /* register physical memory. 'size' must be a multiple of the target
2188 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2189 io memory page */
2190 void cpu_register_physical_memory(target_phys_addr_t start_addr,
2191 ram_addr_t size,
2192 ram_addr_t phys_offset)
2194 target_phys_addr_t addr, end_addr;
2195 PhysPageDesc *p;
2196 CPUState *env;
2197 ram_addr_t orig_size = size;
2198 void *subpage;
2200 #ifdef USE_KQEMU
2201 /* XXX: should not depend on cpu context */
2202 env = first_cpu;
2203 if (env->kqemu_enabled) {
2204 kqemu_set_phys_mem(start_addr, size, phys_offset);
2206 #endif
2207 size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
2208 end_addr = start_addr + (target_phys_addr_t)size;
2209 for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
2210 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2211 if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2212 ram_addr_t orig_memory = p->phys_offset;
2213 target_phys_addr_t start_addr2, end_addr2;
2214 int need_subpage = 0;
2216 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2217 need_subpage);
2218 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2219 if (!(orig_memory & IO_MEM_SUBPAGE)) {
2220 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2221 &p->phys_offset, orig_memory);
2222 } else {
2223 subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
2224 >> IO_MEM_SHIFT];
2226 subpage_register(subpage, start_addr2, end_addr2, phys_offset);
2227 } else {
2228 p->phys_offset = phys_offset;
2229 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2230 (phys_offset & IO_MEM_ROMD))
2231 phys_offset += TARGET_PAGE_SIZE;
2233 } else {
2234 p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2235 p->phys_offset = phys_offset;
2236 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
2237 (phys_offset & IO_MEM_ROMD))
2238 phys_offset += TARGET_PAGE_SIZE;
2239 else {
2240 target_phys_addr_t start_addr2, end_addr2;
2241 int need_subpage = 0;
2243 CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2244 end_addr2, need_subpage);
2246 if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
2247 subpage = subpage_init((addr & TARGET_PAGE_MASK),
2248 &p->phys_offset, IO_MEM_UNASSIGNED);
2249 subpage_register(subpage, start_addr2, end_addr2,
2250 phys_offset);
2256 /* since each CPU stores ram addresses in its TLB cache, we must
2257 reset the modified entries */
2258 /* XXX: slow ! */
2259 for(env = first_cpu; env != NULL; env = env->next_cpu) {
2260 tlb_flush(env, 1);
2264 /* XXX: temporary until new memory mapping API */
2265 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2267 PhysPageDesc *p;
2269 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2270 if (!p)
2271 return IO_MEM_UNASSIGNED;
2272 return p->phys_offset;
2275 /* XXX: better than nothing */
2276 ram_addr_t qemu_ram_alloc(ram_addr_t size)
2278 ram_addr_t addr;
2279 if ((phys_ram_alloc_offset + size) > phys_ram_size) {
2280 fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 "\n",
2281 (uint64_t)size, (uint64_t)phys_ram_size);
2282 abort();
2284 addr = phys_ram_alloc_offset;
2285 phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
2286 return addr;
2289 void qemu_ram_free(ram_addr_t addr)
2293 static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
2295 #ifdef DEBUG_UNASSIGNED
2296 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2297 #endif
2298 #ifdef TARGET_SPARC
2299 do_unassigned_access(addr, 0, 0, 0);
2300 #elif TARGET_CRIS
2301 do_unassigned_access(addr, 0, 0, 0);
2302 #endif
2303 return 0;
2306 static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
2308 #ifdef DEBUG_UNASSIGNED
2309 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2310 #endif
2311 #ifdef TARGET_SPARC
2312 do_unassigned_access(addr, 1, 0, 0);
2313 #elif TARGET_CRIS
2314 do_unassigned_access(addr, 1, 0, 0);
2315 #endif
2318 static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2319 unassigned_mem_readb,
2320 unassigned_mem_readb,
2321 unassigned_mem_readb,
2324 static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2325 unassigned_mem_writeb,
2326 unassigned_mem_writeb,
2327 unassigned_mem_writeb,
2330 static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2331 uint32_t val)
2333 int dirty_flags;
2334 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2335 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2336 #if !defined(CONFIG_USER_ONLY)
2337 tb_invalidate_phys_page_fast(ram_addr, 1);
2338 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2339 #endif
2341 stb_p(phys_ram_base + ram_addr, val);
2342 #ifdef USE_KQEMU
2343 if (cpu_single_env->kqemu_enabled &&
2344 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2345 kqemu_modify_page(cpu_single_env, ram_addr);
2346 #endif
2347 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2348 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2349 /* we remove the notdirty callback only if the code has been
2350 flushed */
2351 if (dirty_flags == 0xff)
2352 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2355 static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2356 uint32_t val)
2358 int dirty_flags;
2359 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2360 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2361 #if !defined(CONFIG_USER_ONLY)
2362 tb_invalidate_phys_page_fast(ram_addr, 2);
2363 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2364 #endif
2366 stw_p(phys_ram_base + ram_addr, val);
2367 #ifdef USE_KQEMU
2368 if (cpu_single_env->kqemu_enabled &&
2369 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2370 kqemu_modify_page(cpu_single_env, ram_addr);
2371 #endif
2372 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2373 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2374 /* we remove the notdirty callback only if the code has been
2375 flushed */
2376 if (dirty_flags == 0xff)
2377 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2380 static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2381 uint32_t val)
2383 int dirty_flags;
2384 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2385 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2386 #if !defined(CONFIG_USER_ONLY)
2387 tb_invalidate_phys_page_fast(ram_addr, 4);
2388 dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2389 #endif
2391 stl_p(phys_ram_base + ram_addr, val);
2392 #ifdef USE_KQEMU
2393 if (cpu_single_env->kqemu_enabled &&
2394 (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2395 kqemu_modify_page(cpu_single_env, ram_addr);
2396 #endif
2397 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
2398 phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
2399 /* we remove the notdirty callback only if the code has been
2400 flushed */
2401 if (dirty_flags == 0xff)
2402 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
2405 static CPUReadMemoryFunc *error_mem_read[3] = {
2406 NULL, /* never used */
2407 NULL, /* never used */
2408 NULL, /* never used */
2411 static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
2412 notdirty_mem_writeb,
2413 notdirty_mem_writew,
2414 notdirty_mem_writel,
2417 /* Generate a debug exception if a watchpoint has been hit. */
2418 static void check_watchpoint(int offset, int flags)
2420 CPUState *env = cpu_single_env;
2421 target_ulong vaddr;
2422 int i;
2424 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
2425 for (i = 0; i < env->nb_watchpoints; i++) {
2426 if (vaddr == env->watchpoint[i].vaddr
2427 && (env->watchpoint[i].type & flags)) {
2428 env->watchpoint_hit = i + 1;
2429 cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2430 break;
2435 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2436 so these check for a hit then pass through to the normal out-of-line
2437 phys routines. */
2438 static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2440 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2441 return ldub_phys(addr);
2444 static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2446 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2447 return lduw_phys(addr);
2450 static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2452 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2453 return ldl_phys(addr);
2456 static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2457 uint32_t val)
2459 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
2460 stb_phys(addr, val);
2463 static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2464 uint32_t val)
2466 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
2467 stw_phys(addr, val);
2470 static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2471 uint32_t val)
2473 check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
2474 stl_phys(addr, val);
2477 static CPUReadMemoryFunc *watch_mem_read[3] = {
2478 watch_mem_readb,
2479 watch_mem_readw,
2480 watch_mem_readl,
2483 static CPUWriteMemoryFunc *watch_mem_write[3] = {
2484 watch_mem_writeb,
2485 watch_mem_writew,
2486 watch_mem_writel,
2489 static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2490 unsigned int len)
2492 uint32_t ret;
2493 unsigned int idx;
2495 idx = SUBPAGE_IDX(addr - mmio->base);
2496 #if defined(DEBUG_SUBPAGE)
2497 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2498 mmio, len, addr, idx);
2499 #endif
2500 ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
2502 return ret;
2505 static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2506 uint32_t value, unsigned int len)
2508 unsigned int idx;
2510 idx = SUBPAGE_IDX(addr - mmio->base);
2511 #if defined(DEBUG_SUBPAGE)
2512 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2513 mmio, len, addr, idx, value);
2514 #endif
2515 (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
2518 static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2520 #if defined(DEBUG_SUBPAGE)
2521 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2522 #endif
2524 return subpage_readlen(opaque, addr, 0);
2527 static void subpage_writeb (void *opaque, target_phys_addr_t addr,
2528 uint32_t value)
2530 #if defined(DEBUG_SUBPAGE)
2531 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2532 #endif
2533 subpage_writelen(opaque, addr, value, 0);
2536 static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
2538 #if defined(DEBUG_SUBPAGE)
2539 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2540 #endif
2542 return subpage_readlen(opaque, addr, 1);
2545 static void subpage_writew (void *opaque, target_phys_addr_t addr,
2546 uint32_t value)
2548 #if defined(DEBUG_SUBPAGE)
2549 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2550 #endif
2551 subpage_writelen(opaque, addr, value, 1);
2554 static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
2556 #if defined(DEBUG_SUBPAGE)
2557 printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
2558 #endif
2560 return subpage_readlen(opaque, addr, 2);
2563 static void subpage_writel (void *opaque,
2564 target_phys_addr_t addr, uint32_t value)
2566 #if defined(DEBUG_SUBPAGE)
2567 printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
2568 #endif
2569 subpage_writelen(opaque, addr, value, 2);
2572 static CPUReadMemoryFunc *subpage_read[] = {
2573 &subpage_readb,
2574 &subpage_readw,
2575 &subpage_readl,
2578 static CPUWriteMemoryFunc *subpage_write[] = {
2579 &subpage_writeb,
2580 &subpage_writew,
2581 &subpage_writel,
2584 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2585 ram_addr_t memory)
2587 int idx, eidx;
2588 unsigned int i;
2590 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2591 return -1;
2592 idx = SUBPAGE_IDX(start);
2593 eidx = SUBPAGE_IDX(end);
2594 #if defined(DEBUG_SUBPAGE)
2595 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
2596 mmio, start, end, idx, eidx, memory);
2597 #endif
2598 memory >>= IO_MEM_SHIFT;
2599 for (; idx <= eidx; idx++) {
2600 for (i = 0; i < 4; i++) {
2601 if (io_mem_read[memory][i]) {
2602 mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2603 mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2605 if (io_mem_write[memory][i]) {
2606 mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2607 mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2612 return 0;
2615 static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2616 ram_addr_t orig_memory)
2618 subpage_t *mmio;
2619 int subpage_memory;
2621 mmio = qemu_mallocz(sizeof(subpage_t));
2622 if (mmio != NULL) {
2623 mmio->base = base;
2624 subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
2625 #if defined(DEBUG_SUBPAGE)
2626 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
2627 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
2628 #endif
2629 *phys = subpage_memory | IO_MEM_SUBPAGE;
2630 subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory);
2633 return mmio;
2636 static int get_free_io_mem_idx(void)
2638 int i;
2640 for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
2641 if (!io_mem_used[i]) {
2642 io_mem_used[i] = 1;
2643 return i;
2646 return -1;
2649 static void io_mem_init(void)
2651 int i;
2653 cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
2654 cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
2655 cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
2656 for (i=0; i<5; i++)
2657 io_mem_used[i] = 1;
2659 io_mem_watch = cpu_register_io_memory(0, watch_mem_read,
2660 watch_mem_write, NULL);
2661 /* alloc dirty bits array */
2662 phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
2663 memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
2666 /* mem_read and mem_write are arrays of functions containing the
2667 function to access byte (index 0), word (index 1) and dword (index
2668 2). Functions can be omitted with a NULL function pointer. The
2669 registered functions may be modified dynamically later.
2670 If io_index is non zero, the corresponding io zone is
2671 modified. If it is zero, a new io zone is allocated. The return
2672 value can be used with cpu_register_physical_memory(). (-1) is
2673 returned if error. */
2674 int cpu_register_io_memory(int io_index,
2675 CPUReadMemoryFunc **mem_read,
2676 CPUWriteMemoryFunc **mem_write,
2677 void *opaque)
2679 int i, subwidth = 0;
2681 if (io_index <= 0) {
2682 io_index = get_free_io_mem_idx();
2683 if (io_index == -1)
2684 return io_index;
2685 } else {
2686 if (io_index >= IO_MEM_NB_ENTRIES)
2687 return -1;
2690 for(i = 0;i < 3; i++) {
2691 if (!mem_read[i] || !mem_write[i])
2692 subwidth = IO_MEM_SUBWIDTH;
2693 io_mem_read[io_index][i] = mem_read[i];
2694 io_mem_write[io_index][i] = mem_write[i];
2696 io_mem_opaque[io_index] = opaque;
2697 return (io_index << IO_MEM_SHIFT) | subwidth;
2700 void cpu_unregister_io_memory(int io_table_address)
2702 int i;
2703 int io_index = io_table_address >> IO_MEM_SHIFT;
2705 for (i=0;i < 3; i++) {
2706 io_mem_read[io_index][i] = unassigned_mem_read[i];
2707 io_mem_write[io_index][i] = unassigned_mem_write[i];
2709 io_mem_opaque[io_index] = NULL;
2710 io_mem_used[io_index] = 0;
2713 CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2715 return io_mem_write[io_index >> IO_MEM_SHIFT];
2718 CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
2720 return io_mem_read[io_index >> IO_MEM_SHIFT];
2723 #endif /* !defined(CONFIG_USER_ONLY) */
2725 /* physical memory access (slow version, mainly for debug) */
2726 #if defined(CONFIG_USER_ONLY)
2727 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
2728 int len, int is_write)
2730 int l, flags;
2731 target_ulong page;
2732 void * p;
2734 while (len > 0) {
2735 page = addr & TARGET_PAGE_MASK;
2736 l = (page + TARGET_PAGE_SIZE) - addr;
2737 if (l > len)
2738 l = len;
2739 flags = page_get_flags(page);
2740 if (!(flags & PAGE_VALID))
2741 return;
2742 if (is_write) {
2743 if (!(flags & PAGE_WRITE))
2744 return;
2745 /* XXX: this code should not depend on lock_user */
2746 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
2747 /* FIXME - should this return an error rather than just fail? */
2748 return;
2749 memcpy(p, buf, l);
2750 unlock_user(p, addr, l);
2751 } else {
2752 if (!(flags & PAGE_READ))
2753 return;
2754 /* XXX: this code should not depend on lock_user */
2755 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
2756 /* FIXME - should this return an error rather than just fail? */
2757 return;
2758 memcpy(buf, p, l);
2759 unlock_user(p, addr, 0);
2761 len -= l;
2762 buf += l;
2763 addr += l;
2767 #else
2768 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
2769 int len, int is_write)
2771 int l, io_index;
2772 uint8_t *ptr;
2773 uint32_t val;
2774 target_phys_addr_t page;
2775 unsigned long pd;
2776 PhysPageDesc *p;
2778 while (len > 0) {
2779 page = addr & TARGET_PAGE_MASK;
2780 l = (page + TARGET_PAGE_SIZE) - addr;
2781 if (l > len)
2782 l = len;
2783 p = phys_page_find(page >> TARGET_PAGE_BITS);
2784 if (!p) {
2785 pd = IO_MEM_UNASSIGNED;
2786 } else {
2787 pd = p->phys_offset;
2790 if (is_write) {
2791 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
2792 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2793 /* XXX: could force cpu_single_env to NULL to avoid
2794 potential bugs */
2795 if (l >= 4 && ((addr & 3) == 0)) {
2796 /* 32 bit write access */
2797 val = ldl_p(buf);
2798 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
2799 l = 4;
2800 } else if (l >= 2 && ((addr & 1) == 0)) {
2801 /* 16 bit write access */
2802 val = lduw_p(buf);
2803 io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
2804 l = 2;
2805 } else {
2806 /* 8 bit write access */
2807 val = ldub_p(buf);
2808 io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val);
2809 l = 1;
2811 } else {
2812 unsigned long addr1;
2813 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2814 /* RAM case */
2815 ptr = phys_ram_base + addr1;
2816 memcpy(ptr, buf, l);
2817 if (!cpu_physical_memory_is_dirty(addr1)) {
2818 /* invalidate code */
2819 tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
2820 /* set dirty bit */
2821 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
2822 (0xff & ~CODE_DIRTY_FLAG);
2824 /* qemu doesn't execute guest code directly, but kvm does
2825 therefore fluch instruction caches */
2826 if (kvm_enabled())
2827 flush_icache_range((unsigned long)ptr,
2828 ((unsigned long)ptr)+l);
2830 } else {
2831 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2832 !(pd & IO_MEM_ROMD)) {
2833 /* I/O case */
2834 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2835 if (l >= 4 && ((addr & 3) == 0)) {
2836 /* 32 bit read access */
2837 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2838 stl_p(buf, val);
2839 l = 4;
2840 } else if (l >= 2 && ((addr & 1) == 0)) {
2841 /* 16 bit read access */
2842 val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
2843 stw_p(buf, val);
2844 l = 2;
2845 } else {
2846 /* 8 bit read access */
2847 val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr);
2848 stb_p(buf, val);
2849 l = 1;
2851 } else {
2852 /* RAM case */
2853 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2854 (addr & ~TARGET_PAGE_MASK);
2855 memcpy(buf, ptr, l);
2858 len -= l;
2859 buf += l;
2860 addr += l;
2864 /* used for ROM loading : can write in RAM and ROM */
2865 void cpu_physical_memory_write_rom(target_phys_addr_t addr,
2866 const uint8_t *buf, int len)
2868 int l;
2869 uint8_t *ptr;
2870 target_phys_addr_t page;
2871 unsigned long pd;
2872 PhysPageDesc *p;
2874 while (len > 0) {
2875 page = addr & TARGET_PAGE_MASK;
2876 l = (page + TARGET_PAGE_SIZE) - addr;
2877 if (l > len)
2878 l = len;
2879 p = phys_page_find(page >> TARGET_PAGE_BITS);
2880 if (!p) {
2881 pd = IO_MEM_UNASSIGNED;
2882 } else {
2883 pd = p->phys_offset;
2886 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
2887 (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
2888 !(pd & IO_MEM_ROMD)) {
2889 /* do nothing */
2890 } else {
2891 unsigned long addr1;
2892 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
2893 /* ROM/RAM case */
2894 ptr = phys_ram_base + addr1;
2895 memcpy(ptr, buf, l);
2897 len -= l;
2898 buf += l;
2899 addr += l;
2904 /* warning: addr must be aligned */
2905 uint32_t ldl_phys(target_phys_addr_t addr)
2907 int io_index;
2908 uint8_t *ptr;
2909 uint32_t val;
2910 unsigned long pd;
2911 PhysPageDesc *p;
2913 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2914 if (!p) {
2915 pd = IO_MEM_UNASSIGNED;
2916 } else {
2917 pd = p->phys_offset;
2920 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2921 !(pd & IO_MEM_ROMD)) {
2922 /* I/O case */
2923 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2924 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2925 } else {
2926 /* RAM case */
2927 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2928 (addr & ~TARGET_PAGE_MASK);
2929 val = ldl_p(ptr);
2931 return val;
2934 /* warning: addr must be aligned */
2935 uint64_t ldq_phys(target_phys_addr_t addr)
2937 int io_index;
2938 uint8_t *ptr;
2939 uint64_t val;
2940 unsigned long pd;
2941 PhysPageDesc *p;
2943 p = phys_page_find(addr >> TARGET_PAGE_BITS);
2944 if (!p) {
2945 pd = IO_MEM_UNASSIGNED;
2946 } else {
2947 pd = p->phys_offset;
2950 if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
2951 !(pd & IO_MEM_ROMD)) {
2952 /* I/O case */
2953 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
2954 #ifdef TARGET_WORDS_BIGENDIAN
2955 val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
2956 val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
2957 #else
2958 val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
2959 val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
2960 #endif
2961 } else {
2962 /* RAM case */
2963 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
2964 (addr & ~TARGET_PAGE_MASK);
2965 val = ldq_p(ptr);
2967 return val;
2970 /* XXX: optimize */
2971 uint32_t ldub_phys(target_phys_addr_t addr)
2973 uint8_t val;
2974 cpu_physical_memory_read(addr, &val, 1);
2975 return val;
2978 /* XXX: optimize */
2979 uint32_t lduw_phys(target_phys_addr_t addr)
2981 uint16_t val;
2982 cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
2983 return tswap16(val);
2986 #ifdef __GNUC__
2987 #define likely(x) __builtin_expect(!!(x), 1)
2988 #define unlikely(x) __builtin_expect(!!(x), 0)
2989 #else
2990 #define likely(x) x
2991 #define unlikely(x) x
2992 #endif
2994 /* warning: addr must be aligned. The ram page is not masked as dirty
2995 and the code inside is not invalidated. It is useful if the dirty
2996 bits are used to track modified PTEs */
2997 void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
2999 int io_index;
3000 uint8_t *ptr;
3001 unsigned long pd;
3002 PhysPageDesc *p;
3004 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3005 if (!p) {
3006 pd = IO_MEM_UNASSIGNED;
3007 } else {
3008 pd = p->phys_offset;
3011 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3012 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3013 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3014 } else {
3015 unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3016 ptr = phys_ram_base + addr1;
3017 stl_p(ptr, val);
3019 if (unlikely(in_migration)) {
3020 if (!cpu_physical_memory_is_dirty(addr1)) {
3021 /* invalidate code */
3022 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3023 /* set dirty bit */
3024 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3025 (0xff & ~CODE_DIRTY_FLAG);
3031 void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
3033 int io_index;
3034 uint8_t *ptr;
3035 unsigned long pd;
3036 PhysPageDesc *p;
3038 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3039 if (!p) {
3040 pd = IO_MEM_UNASSIGNED;
3041 } else {
3042 pd = p->phys_offset;
3045 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3046 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3047 #ifdef TARGET_WORDS_BIGENDIAN
3048 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
3049 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
3050 #else
3051 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3052 io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
3053 #endif
3054 } else {
3055 ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
3056 (addr & ~TARGET_PAGE_MASK);
3057 stq_p(ptr, val);
3061 /* warning: addr must be aligned */
3062 void stl_phys(target_phys_addr_t addr, uint32_t val)
3064 int io_index;
3065 uint8_t *ptr;
3066 unsigned long pd;
3067 PhysPageDesc *p;
3069 p = phys_page_find(addr >> TARGET_PAGE_BITS);
3070 if (!p) {
3071 pd = IO_MEM_UNASSIGNED;
3072 } else {
3073 pd = p->phys_offset;
3076 if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
3077 io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3078 io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3079 } else {
3080 unsigned long addr1;
3081 addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3082 /* RAM case */
3083 ptr = phys_ram_base + addr1;
3084 stl_p(ptr, val);
3085 if (!cpu_physical_memory_is_dirty(addr1)) {
3086 /* invalidate code */
3087 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3088 /* set dirty bit */
3089 phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3090 (0xff & ~CODE_DIRTY_FLAG);
3095 /* XXX: optimize */
3096 void stb_phys(target_phys_addr_t addr, uint32_t val)
3098 uint8_t v = val;
3099 cpu_physical_memory_write(addr, &v, 1);
3102 /* XXX: optimize */
3103 void stw_phys(target_phys_addr_t addr, uint32_t val)
3105 uint16_t v = tswap16(val);
3106 cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
3109 /* XXX: optimize */
3110 void stq_phys(target_phys_addr_t addr, uint64_t val)
3112 val = tswap64(val);
3113 cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
3116 #endif
3118 /* virtual memory access for debug */
3119 int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
3120 uint8_t *buf, int len, int is_write)
3122 int l;
3123 target_phys_addr_t phys_addr;
3124 target_ulong page;
3126 while (len > 0) {
3127 page = addr & TARGET_PAGE_MASK;
3128 phys_addr = cpu_get_phys_page_debug(env, page);
3129 /* if no physical page mapped, return an error */
3130 if (phys_addr == -1)
3131 return -1;
3132 l = (page + TARGET_PAGE_SIZE) - addr;
3133 if (l > len)
3134 l = len;
3135 cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
3136 buf, l, is_write);
3137 len -= l;
3138 buf += l;
3139 addr += l;
3141 return 0;
3144 /* in deterministic execution mode, instructions doing device I/Os
3145 must be at the end of the TB */
3146 void cpu_io_recompile(CPUState *env, void *retaddr)
3148 TranslationBlock *tb;
3149 uint32_t n, cflags;
3150 target_ulong pc, cs_base;
3151 uint64_t flags;
3153 tb = tb_find_pc((unsigned long)retaddr);
3154 if (!tb) {
3155 cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
3156 retaddr);
3158 n = env->icount_decr.u16.low + tb->icount;
3159 cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3160 /* Calculate how many instructions had been executed before the fault
3161 occurred. */
3162 n = n - env->icount_decr.u16.low;
3163 /* Generate a new TB ending on the I/O insn. */
3164 n++;
3165 /* On MIPS and SH, delay slot instructions can only be restarted if
3166 they were already the first instruction in the TB. If this is not
3167 the first instruction in a TB then re-execute the preceding
3168 branch. */
3169 #if defined(TARGET_MIPS)
3170 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3171 env->active_tc.PC -= 4;
3172 env->icount_decr.u16.low++;
3173 env->hflags &= ~MIPS_HFLAG_BMASK;
3175 #elif defined(TARGET_SH4)
3176 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3177 && n > 1) {
3178 env->pc -= 2;
3179 env->icount_decr.u16.low++;
3180 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3182 #endif
3183 /* This should never happen. */
3184 if (n > CF_COUNT_MASK)
3185 cpu_abort(env, "TB too big during recompile");
3187 cflags = n | CF_LAST_IO;
3188 pc = tb->pc;
3189 cs_base = tb->cs_base;
3190 flags = tb->flags;
3191 tb_phys_invalidate(tb, -1);
3192 /* FIXME: In theory this could raise an exception. In practice
3193 we have already translated the block once so it's probably ok. */
3194 tb_gen_code(env, pc, cs_base, flags, cflags);
3195 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3196 the first in the TB) then we end up generating a whole new TB and
3197 repeating the fault, which is horribly inefficient.
3198 Better would be to execute just this insn uncached, or generate a
3199 second new TB. */
3200 cpu_resume_from_signal(env, NULL);
3203 void dump_exec_info(FILE *f,
3204 int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3206 int i, target_code_size, max_target_code_size;
3207 int direct_jmp_count, direct_jmp2_count, cross_page;
3208 TranslationBlock *tb;
3210 target_code_size = 0;
3211 max_target_code_size = 0;
3212 cross_page = 0;
3213 direct_jmp_count = 0;
3214 direct_jmp2_count = 0;
3215 for(i = 0; i < nb_tbs; i++) {
3216 tb = &tbs[i];
3217 target_code_size += tb->size;
3218 if (tb->size > max_target_code_size)
3219 max_target_code_size = tb->size;
3220 if (tb->page_addr[1] != -1)
3221 cross_page++;
3222 if (tb->tb_next_offset[0] != 0xffff) {
3223 direct_jmp_count++;
3224 if (tb->tb_next_offset[1] != 0xffff) {
3225 direct_jmp2_count++;
3229 /* XXX: avoid using doubles ? */
3230 cpu_fprintf(f, "Translation buffer state:\n");
3231 cpu_fprintf(f, "gen code size %ld/%ld\n",
3232 code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3233 cpu_fprintf(f, "TB count %d/%d\n",
3234 nb_tbs, code_gen_max_blocks);
3235 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
3236 nb_tbs ? target_code_size / nb_tbs : 0,
3237 max_target_code_size);
3238 cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3239 nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
3240 target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
3241 cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
3242 cross_page,
3243 nb_tbs ? (cross_page * 100) / nb_tbs : 0);
3244 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3245 direct_jmp_count,
3246 nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3247 direct_jmp2_count,
3248 nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
3249 cpu_fprintf(f, "\nStatistics:\n");
3250 cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
3251 cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3252 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
3253 tcg_dump_info(f, cpu_fprintf);
3256 #if !defined(CONFIG_USER_ONLY)
3258 #define MMUSUFFIX _cmmu
3259 #define GETPC() NULL
3260 #define env cpu_single_env
3261 #define SOFTMMU_CODE_ACCESS
3263 #define SHIFT 0
3264 #include "softmmu_template.h"
3266 #define SHIFT 1
3267 #include "softmmu_template.h"
3269 #define SHIFT 2
3270 #include "softmmu_template.h"
3272 #define SHIFT 3
3273 #include "softmmu_template.h"
3275 #undef env
3277 #endif