2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
36 #include "qemu-common.h"
37 #include "cache-utils.h"
39 #if !defined(TARGET_IA64)
47 #if defined(CONFIG_USER_ONLY)
51 //#define DEBUG_TB_INVALIDATE
54 //#define DEBUG_UNASSIGNED
56 /* make various TB consistency checks */
57 //#define DEBUG_TB_CHECK
58 //#define DEBUG_TLB_CHECK
60 //#define DEBUG_IOPORT
61 //#define DEBUG_SUBPAGE
63 #if !defined(CONFIG_USER_ONLY)
64 /* TB consistency checks only implemented for usermode emulation. */
68 #define SMC_BITMAP_USE_THRESHOLD 10
70 #if defined(TARGET_SPARC64)
71 #define TARGET_PHYS_ADDR_SPACE_BITS 41
72 #elif defined(TARGET_SPARC)
73 #define TARGET_PHYS_ADDR_SPACE_BITS 36
74 #elif defined(TARGET_ALPHA)
75 #define TARGET_PHYS_ADDR_SPACE_BITS 42
76 #define TARGET_VIRT_ADDR_SPACE_BITS 42
77 #elif defined(TARGET_PPC64)
78 #define TARGET_PHYS_ADDR_SPACE_BITS 42
79 #elif defined(TARGET_X86_64) && !defined(CONFIG_KQEMU)
80 #define TARGET_PHYS_ADDR_SPACE_BITS 42
81 #elif defined(TARGET_I386) && !defined(CONFIG_KQEMU)
82 #define TARGET_PHYS_ADDR_SPACE_BITS 36
83 #elif defined(TARGET_IA64)
84 #define TARGET_PHYS_ADDR_SPACE_BITS 36
86 /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
87 #define TARGET_PHYS_ADDR_SPACE_BITS 32
90 static TranslationBlock
*tbs
;
91 int code_gen_max_blocks
;
92 TranslationBlock
*tb_phys_hash
[CODE_GEN_PHYS_HASH_SIZE
];
94 /* any access to the tbs or the page table must use this lock */
95 spinlock_t tb_lock
= SPIN_LOCK_UNLOCKED
;
97 #if defined(__arm__) || defined(__sparc_v9__)
98 /* The prologue must be reachable with a direct jump. ARM and Sparc64
99 have limited branch ranges (possibly also PPC) so place it in a
100 section close to code segment. */
101 #define code_gen_section \
102 __attribute__((__section__(".gen_code"))) \
103 __attribute__((aligned (32)))
104 #elif defined(_WIN32)
105 /* Maximum alignment for Win32 is 16. */
106 #define code_gen_section \
107 __attribute__((aligned (16)))
109 #define code_gen_section \
110 __attribute__((aligned (32)))
113 uint8_t code_gen_prologue
[1024] code_gen_section
;
114 static uint8_t *code_gen_buffer
;
115 static unsigned long code_gen_buffer_size
;
116 /* threshold to flush the translated code buffer */
117 static unsigned long code_gen_buffer_max_size
;
118 uint8_t *code_gen_ptr
;
120 #if !defined(CONFIG_USER_ONLY)
122 uint8_t *phys_ram_dirty
;
124 static int in_migration
;
126 typedef struct RAMBlock
{
130 struct RAMBlock
*next
;
133 static RAMBlock
*ram_blocks
;
134 /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
135 then we can no longer assume contiguous ram offsets, and external uses
136 of this variable will break. */
137 ram_addr_t last_ram_offset
;
141 /* current CPU in the current thread. It is only valid inside
143 CPUState
*cpu_single_env
;
144 /* 0 = Do not count executed instructions.
145 1 = Precise instruction counting.
146 2 = Adaptive rate instruction counting. */
148 /* Current instruction counter. While executing translated code this may
149 include some instructions that have not yet been executed. */
152 typedef struct PageDesc
{
153 /* list of TBs intersecting this ram page */
154 TranslationBlock
*first_tb
;
155 /* in order to optimize self modifying code, we count the number
156 of lookups we do to a given page to use a bitmap */
157 unsigned int code_write_count
;
158 uint8_t *code_bitmap
;
159 #if defined(CONFIG_USER_ONLY)
164 typedef struct PhysPageDesc
{
165 /* offset in host memory of the page + io_index in the low bits */
166 ram_addr_t phys_offset
;
167 ram_addr_t region_offset
;
171 #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
172 /* XXX: this is a temporary hack for alpha target.
173 * In the future, this is to be replaced by a multi-level table
174 * to actually be able to handle the complete 64 bits address space.
176 #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
178 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
181 #define L1_SIZE (1 << L1_BITS)
182 #define L2_SIZE (1 << L2_BITS)
184 unsigned long qemu_real_host_page_size
;
185 unsigned long qemu_host_page_bits
;
186 unsigned long qemu_host_page_size
;
187 unsigned long qemu_host_page_mask
;
189 /* XXX: for system emulation, it could just be an array */
190 static PageDesc
*l1_map
[L1_SIZE
];
191 static PhysPageDesc
**l1_phys_map
;
193 #if !defined(CONFIG_USER_ONLY)
194 static void io_mem_init(void);
196 /* io memory support */
197 CPUWriteMemoryFunc
*io_mem_write
[IO_MEM_NB_ENTRIES
][4];
198 CPUReadMemoryFunc
*io_mem_read
[IO_MEM_NB_ENTRIES
][4];
199 void *io_mem_opaque
[IO_MEM_NB_ENTRIES
];
200 static char io_mem_used
[IO_MEM_NB_ENTRIES
];
201 static int io_mem_watch
;
205 static const char *logfilename
= "/tmp/qemu.log";
208 static int log_append
= 0;
211 static int tlb_flush_count
;
212 static int tb_flush_count
;
213 static int tb_phys_invalidate_count
;
215 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
216 typedef struct subpage_t
{
217 target_phys_addr_t base
;
218 CPUReadMemoryFunc
**mem_read
[TARGET_PAGE_SIZE
][4];
219 CPUWriteMemoryFunc
**mem_write
[TARGET_PAGE_SIZE
][4];
220 void *opaque
[TARGET_PAGE_SIZE
][2][4];
221 ram_addr_t region_offset
[TARGET_PAGE_SIZE
][2][4];
225 static void map_exec(void *addr
, long size
)
228 VirtualProtect(addr
, size
,
229 PAGE_EXECUTE_READWRITE
, &old_protect
);
233 static void map_exec(void *addr
, long size
)
235 unsigned long start
, end
, page_size
;
237 page_size
= getpagesize();
238 start
= (unsigned long)addr
;
239 start
&= ~(page_size
- 1);
241 end
= (unsigned long)addr
+ size
;
242 end
+= page_size
- 1;
243 end
&= ~(page_size
- 1);
245 mprotect((void *)start
, end
- start
,
246 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
250 static void page_init(void)
252 /* NOTE: we can always suppose that qemu_host_page_size >=
256 SYSTEM_INFO system_info
;
258 GetSystemInfo(&system_info
);
259 qemu_real_host_page_size
= system_info
.dwPageSize
;
262 qemu_real_host_page_size
= getpagesize();
264 if (qemu_host_page_size
== 0)
265 qemu_host_page_size
= qemu_real_host_page_size
;
266 if (qemu_host_page_size
< TARGET_PAGE_SIZE
)
267 qemu_host_page_size
= TARGET_PAGE_SIZE
;
268 qemu_host_page_bits
= 0;
269 while ((1 << qemu_host_page_bits
) < qemu_host_page_size
)
270 qemu_host_page_bits
++;
271 qemu_host_page_mask
= ~(qemu_host_page_size
- 1);
272 l1_phys_map
= qemu_vmalloc(L1_SIZE
* sizeof(void *));
273 memset(l1_phys_map
, 0, L1_SIZE
* sizeof(void *));
275 #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
277 long long startaddr
, endaddr
;
282 last_brk
= (unsigned long)sbrk(0);
283 f
= fopen("/proc/self/maps", "r");
286 n
= fscanf (f
, "%llx-%llx %*[^\n]\n", &startaddr
, &endaddr
);
288 startaddr
= MIN(startaddr
,
289 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
290 endaddr
= MIN(endaddr
,
291 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
292 page_set_flags(startaddr
& TARGET_PAGE_MASK
,
293 TARGET_PAGE_ALIGN(endaddr
),
304 static inline PageDesc
**page_l1_map(target_ulong index
)
306 #if TARGET_LONG_BITS > 32
307 /* Host memory outside guest VM. For 32-bit targets we have already
308 excluded high addresses. */
309 if (index
> ((target_ulong
)L2_SIZE
* L1_SIZE
))
312 return &l1_map
[index
>> L2_BITS
];
315 static inline PageDesc
*page_find_alloc(target_ulong index
)
318 lp
= page_l1_map(index
);
324 /* allocate if not found */
325 #if defined(CONFIG_USER_ONLY)
326 size_t len
= sizeof(PageDesc
) * L2_SIZE
;
327 /* Don't use qemu_malloc because it may recurse. */
328 p
= mmap(0, len
, PROT_READ
| PROT_WRITE
,
329 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
332 unsigned long addr
= h2g(p
);
333 page_set_flags(addr
& TARGET_PAGE_MASK
,
334 TARGET_PAGE_ALIGN(addr
+ len
),
338 p
= qemu_mallocz(sizeof(PageDesc
) * L2_SIZE
);
342 return p
+ (index
& (L2_SIZE
- 1));
345 static inline PageDesc
*page_find(target_ulong index
)
348 lp
= page_l1_map(index
);
355 return p
+ (index
& (L2_SIZE
- 1));
358 static PhysPageDesc
*phys_page_find_alloc(target_phys_addr_t index
, int alloc
)
363 p
= (void **)l1_phys_map
;
364 #if TARGET_PHYS_ADDR_SPACE_BITS > 32
366 #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
367 #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
369 lp
= p
+ ((index
>> (L1_BITS
+ L2_BITS
)) & (L1_SIZE
- 1));
372 /* allocate if not found */
375 p
= qemu_vmalloc(sizeof(void *) * L1_SIZE
);
376 memset(p
, 0, sizeof(void *) * L1_SIZE
);
380 lp
= p
+ ((index
>> L2_BITS
) & (L1_SIZE
- 1));
384 /* allocate if not found */
387 pd
= qemu_vmalloc(sizeof(PhysPageDesc
) * L2_SIZE
);
389 for (i
= 0; i
< L2_SIZE
; i
++) {
390 pd
[i
].phys_offset
= IO_MEM_UNASSIGNED
;
391 pd
[i
].region_offset
= (index
+ i
) << TARGET_PAGE_BITS
;
394 return ((PhysPageDesc
*)pd
) + (index
& (L2_SIZE
- 1));
397 static inline PhysPageDesc
*phys_page_find(target_phys_addr_t index
)
399 return phys_page_find_alloc(index
, 0);
402 #if !defined(CONFIG_USER_ONLY)
403 static void tlb_protect_code(ram_addr_t ram_addr
);
404 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
406 #define mmap_lock() do { } while(0)
407 #define mmap_unlock() do { } while(0)
410 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
412 #if defined(CONFIG_USER_ONLY)
413 /* Currently it is not recommended to allocate big chunks of data in
414 user mode. It will change when a dedicated libc will be used */
415 #define USE_STATIC_CODE_GEN_BUFFER
418 #ifdef USE_STATIC_CODE_GEN_BUFFER
419 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
];
422 static void code_gen_alloc(unsigned long tb_size
)
427 #ifdef USE_STATIC_CODE_GEN_BUFFER
428 code_gen_buffer
= static_code_gen_buffer
;
429 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
430 map_exec(code_gen_buffer
, code_gen_buffer_size
);
432 code_gen_buffer_size
= tb_size
;
433 if (code_gen_buffer_size
== 0) {
434 #if defined(CONFIG_USER_ONLY)
435 /* in user mode, phys_ram_size is not meaningful */
436 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
438 /* XXX: needs adjustments */
439 code_gen_buffer_size
= (unsigned long)(ram_size
/ 4);
442 if (code_gen_buffer_size
< MIN_CODE_GEN_BUFFER_SIZE
)
443 code_gen_buffer_size
= MIN_CODE_GEN_BUFFER_SIZE
;
444 /* The code gen buffer location may have constraints depending on
445 the host cpu and OS */
446 #if defined(__linux__)
451 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
452 #if defined(__x86_64__)
454 /* Cannot map more than that */
455 if (code_gen_buffer_size
> (800 * 1024 * 1024))
456 code_gen_buffer_size
= (800 * 1024 * 1024);
457 #elif defined(__sparc_v9__)
458 // Map the buffer below 2G, so we can use direct calls and branches
460 start
= (void *) 0x60000000UL
;
461 if (code_gen_buffer_size
> (512 * 1024 * 1024))
462 code_gen_buffer_size
= (512 * 1024 * 1024);
463 #elif defined(__arm__)
464 /* Map the buffer below 32M, so we can use direct calls and branches */
466 start
= (void *) 0x01000000UL
;
467 if (code_gen_buffer_size
> 16 * 1024 * 1024)
468 code_gen_buffer_size
= 16 * 1024 * 1024;
470 code_gen_buffer
= mmap(start
, code_gen_buffer_size
,
471 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
473 if (code_gen_buffer
== MAP_FAILED
) {
474 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
478 #elif defined(__FreeBSD__) || defined(__DragonFly__)
482 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
483 #if defined(__x86_64__)
484 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
485 * 0x40000000 is free */
487 addr
= (void *)0x40000000;
488 /* Cannot map more than that */
489 if (code_gen_buffer_size
> (800 * 1024 * 1024))
490 code_gen_buffer_size
= (800 * 1024 * 1024);
492 code_gen_buffer
= mmap(addr
, code_gen_buffer_size
,
493 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
495 if (code_gen_buffer
== MAP_FAILED
) {
496 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
501 code_gen_buffer
= qemu_malloc(code_gen_buffer_size
);
502 map_exec(code_gen_buffer
, code_gen_buffer_size
);
504 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
505 map_exec(code_gen_prologue
, sizeof(code_gen_prologue
));
506 code_gen_buffer_max_size
= code_gen_buffer_size
-
507 code_gen_max_block_size();
508 code_gen_max_blocks
= code_gen_buffer_size
/ CODE_GEN_AVG_BLOCK_SIZE
;
509 tbs
= qemu_malloc(code_gen_max_blocks
* sizeof(TranslationBlock
));
512 /* Must be called before using the QEMU cpus. 'tb_size' is the size
513 (in bytes) allocated to the translation buffer. Zero means default
515 void cpu_exec_init_all(unsigned long tb_size
)
518 code_gen_alloc(tb_size
);
519 code_gen_ptr
= code_gen_buffer
;
521 #if !defined(CONFIG_USER_ONLY)
526 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
528 #define CPU_COMMON_SAVE_VERSION 1
530 static void cpu_common_save(QEMUFile
*f
, void *opaque
)
532 CPUState
*env
= opaque
;
534 cpu_synchronize_state(env
, 0);
536 qemu_put_be32s(f
, &env
->halted
);
537 qemu_put_be32s(f
, &env
->interrupt_request
);
540 static int cpu_common_load(QEMUFile
*f
, void *opaque
, int version_id
)
542 CPUState
*env
= opaque
;
544 if (version_id
!= CPU_COMMON_SAVE_VERSION
)
547 qemu_get_be32s(f
, &env
->halted
);
548 qemu_get_be32s(f
, &env
->interrupt_request
);
549 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
550 version_id is increased. */
551 env
->interrupt_request
&= ~0x01;
553 cpu_synchronize_state(env
, 1);
559 CPUState
*qemu_get_cpu(int cpu
)
561 CPUState
*env
= first_cpu
;
564 if (env
->cpu_index
== cpu
)
572 void cpu_exec_init(CPUState
*env
)
577 #if defined(CONFIG_USER_ONLY)
580 env
->next_cpu
= NULL
;
583 while (*penv
!= NULL
) {
584 penv
= &(*penv
)->next_cpu
;
587 env
->cpu_index
= cpu_index
;
589 TAILQ_INIT(&env
->breakpoints
);
590 TAILQ_INIT(&env
->watchpoints
);
592 env
->thread_id
= GetCurrentProcessId();
594 env
->thread_id
= getpid();
597 #if defined(CONFIG_USER_ONLY)
600 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
601 register_savevm("cpu_common", cpu_index
, CPU_COMMON_SAVE_VERSION
,
602 cpu_common_save
, cpu_common_load
, env
);
603 register_savevm("cpu", cpu_index
, CPU_SAVE_VERSION
,
604 cpu_save
, cpu_load
, env
);
608 static inline void invalidate_page_bitmap(PageDesc
*p
)
610 if (p
->code_bitmap
) {
611 qemu_free(p
->code_bitmap
);
612 p
->code_bitmap
= NULL
;
614 p
->code_write_count
= 0;
617 /* set to NULL all the 'first_tb' fields in all PageDescs */
618 static void page_flush_tb(void)
623 for(i
= 0; i
< L1_SIZE
; i
++) {
626 for(j
= 0; j
< L2_SIZE
; j
++) {
628 invalidate_page_bitmap(p
);
635 /* flush all the translation blocks */
636 /* XXX: tb_flush is currently not thread safe */
637 void tb_flush(CPUState
*env1
)
640 #if defined(DEBUG_FLUSH)
641 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
642 (unsigned long)(code_gen_ptr
- code_gen_buffer
),
644 ((unsigned long)(code_gen_ptr
- code_gen_buffer
)) / nb_tbs
: 0);
646 if ((unsigned long)(code_gen_ptr
- code_gen_buffer
) > code_gen_buffer_size
)
647 cpu_abort(env1
, "Internal error: code buffer overflow\n");
651 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
652 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
655 memset (tb_phys_hash
, 0, CODE_GEN_PHYS_HASH_SIZE
* sizeof (void *));
658 code_gen_ptr
= code_gen_buffer
;
659 /* XXX: flush processor icache at this point if cache flush is
664 #ifdef DEBUG_TB_CHECK
666 static void tb_invalidate_check(target_ulong address
)
668 TranslationBlock
*tb
;
670 address
&= TARGET_PAGE_MASK
;
671 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
672 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
673 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
674 address
>= tb
->pc
+ tb
->size
)) {
675 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
676 address
, (long)tb
->pc
, tb
->size
);
682 /* verify that all the pages have correct rights for code */
683 static void tb_page_check(void)
685 TranslationBlock
*tb
;
686 int i
, flags1
, flags2
;
688 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
689 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
690 flags1
= page_get_flags(tb
->pc
);
691 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
692 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
693 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
694 (long)tb
->pc
, tb
->size
, flags1
, flags2
);
700 static void tb_jmp_check(TranslationBlock
*tb
)
702 TranslationBlock
*tb1
;
705 /* suppress any remaining jumps to this TB */
709 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
712 tb1
= tb1
->jmp_next
[n1
];
714 /* check end of list */
716 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb
);
722 /* invalidate one TB */
723 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
726 TranslationBlock
*tb1
;
730 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
733 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
737 static inline void tb_page_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
739 TranslationBlock
*tb1
;
745 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
747 *ptb
= tb1
->page_next
[n1
];
750 ptb
= &tb1
->page_next
[n1
];
754 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
756 TranslationBlock
*tb1
, **ptb
;
759 ptb
= &tb
->jmp_next
[n
];
762 /* find tb(n) in circular list */
766 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
767 if (n1
== n
&& tb1
== tb
)
770 ptb
= &tb1
->jmp_first
;
772 ptb
= &tb1
->jmp_next
[n1
];
775 /* now we can suppress tb(n) from the list */
776 *ptb
= tb
->jmp_next
[n
];
778 tb
->jmp_next
[n
] = NULL
;
782 /* reset the jump entry 'n' of a TB so that it is not chained to
784 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
786 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
789 void tb_phys_invalidate(TranslationBlock
*tb
, target_ulong page_addr
)
794 target_phys_addr_t phys_pc
;
795 TranslationBlock
*tb1
, *tb2
;
797 /* remove the TB from the hash list */
798 phys_pc
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
799 h
= tb_phys_hash_func(phys_pc
);
800 tb_remove(&tb_phys_hash
[h
], tb
,
801 offsetof(TranslationBlock
, phys_hash_next
));
803 /* remove the TB from the page list */
804 if (tb
->page_addr
[0] != page_addr
) {
805 p
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
806 tb_page_remove(&p
->first_tb
, tb
);
807 invalidate_page_bitmap(p
);
809 if (tb
->page_addr
[1] != -1 && tb
->page_addr
[1] != page_addr
) {
810 p
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
811 tb_page_remove(&p
->first_tb
, tb
);
812 invalidate_page_bitmap(p
);
815 tb_invalidated_flag
= 1;
817 /* remove the TB from the hash list */
818 h
= tb_jmp_cache_hash_func(tb
->pc
);
819 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
820 if (env
->tb_jmp_cache
[h
] == tb
)
821 env
->tb_jmp_cache
[h
] = NULL
;
824 /* suppress this TB from the two jump lists */
825 tb_jmp_remove(tb
, 0);
826 tb_jmp_remove(tb
, 1);
828 /* suppress any remaining jumps to this TB */
834 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
835 tb2
= tb1
->jmp_next
[n1
];
836 tb_reset_jump(tb1
, n1
);
837 tb1
->jmp_next
[n1
] = NULL
;
840 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
842 tb_phys_invalidate_count
++;
845 static inline void set_bits(uint8_t *tab
, int start
, int len
)
851 mask
= 0xff << (start
& 7);
852 if ((start
& ~7) == (end
& ~7)) {
854 mask
&= ~(0xff << (end
& 7));
859 start
= (start
+ 8) & ~7;
861 while (start
< end1
) {
866 mask
= ~(0xff << (end
& 7));
872 static void build_page_bitmap(PageDesc
*p
)
874 int n
, tb_start
, tb_end
;
875 TranslationBlock
*tb
;
877 p
->code_bitmap
= qemu_mallocz(TARGET_PAGE_SIZE
/ 8);
882 tb
= (TranslationBlock
*)((long)tb
& ~3);
883 /* NOTE: this is subtle as a TB may span two physical pages */
885 /* NOTE: tb_end may be after the end of the page, but
886 it is not a problem */
887 tb_start
= tb
->pc
& ~TARGET_PAGE_MASK
;
888 tb_end
= tb_start
+ tb
->size
;
889 if (tb_end
> TARGET_PAGE_SIZE
)
890 tb_end
= TARGET_PAGE_SIZE
;
893 tb_end
= ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
895 set_bits(p
->code_bitmap
, tb_start
, tb_end
- tb_start
);
896 tb
= tb
->page_next
[n
];
900 TranslationBlock
*tb_gen_code(CPUState
*env
,
901 target_ulong pc
, target_ulong cs_base
,
902 int flags
, int cflags
)
904 TranslationBlock
*tb
;
906 target_ulong phys_pc
, phys_page2
, virt_page2
;
909 phys_pc
= get_phys_addr_code(env
, pc
);
912 /* flush must be done */
914 /* cannot fail at this point */
916 /* Don't forget to invalidate previous TB info. */
917 tb_invalidated_flag
= 1;
919 tc_ptr
= code_gen_ptr
;
921 tb
->cs_base
= cs_base
;
924 cpu_gen_code(env
, tb
, &code_gen_size
);
925 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
927 /* check next page if needed */
928 virt_page2
= (pc
+ tb
->size
- 1) & TARGET_PAGE_MASK
;
930 if ((pc
& TARGET_PAGE_MASK
) != virt_page2
) {
931 phys_page2
= get_phys_addr_code(env
, virt_page2
);
933 tb_link_phys(tb
, phys_pc
, phys_page2
);
937 /* invalidate all TBs which intersect with the target physical page
938 starting in range [start;end[. NOTE: start and end must refer to
939 the same physical page. 'is_cpu_write_access' should be true if called
940 from a real cpu write access: the virtual CPU will exit the current
941 TB if code is modified inside this TB. */
942 void tb_invalidate_phys_page_range(target_phys_addr_t start
, target_phys_addr_t end
,
943 int is_cpu_write_access
)
945 TranslationBlock
*tb
, *tb_next
, *saved_tb
;
946 CPUState
*env
= cpu_single_env
;
947 target_ulong tb_start
, tb_end
;
950 #ifdef TARGET_HAS_PRECISE_SMC
951 int current_tb_not_found
= is_cpu_write_access
;
952 TranslationBlock
*current_tb
= NULL
;
953 int current_tb_modified
= 0;
954 target_ulong current_pc
= 0;
955 target_ulong current_cs_base
= 0;
956 int current_flags
= 0;
957 #endif /* TARGET_HAS_PRECISE_SMC */
959 p
= page_find(start
>> TARGET_PAGE_BITS
);
962 if (!p
->code_bitmap
&&
963 ++p
->code_write_count
>= SMC_BITMAP_USE_THRESHOLD
&&
964 is_cpu_write_access
) {
965 /* build code bitmap */
966 build_page_bitmap(p
);
969 /* we remove all the TBs in the range [start, end[ */
970 /* XXX: see if in some cases it could be faster to invalidate all the code */
974 tb
= (TranslationBlock
*)((long)tb
& ~3);
975 tb_next
= tb
->page_next
[n
];
976 /* NOTE: this is subtle as a TB may span two physical pages */
978 /* NOTE: tb_end may be after the end of the page, but
979 it is not a problem */
980 tb_start
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
981 tb_end
= tb_start
+ tb
->size
;
983 tb_start
= tb
->page_addr
[1];
984 tb_end
= tb_start
+ ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
986 if (!(tb_end
<= start
|| tb_start
>= end
)) {
987 #ifdef TARGET_HAS_PRECISE_SMC
988 if (current_tb_not_found
) {
989 current_tb_not_found
= 0;
991 if (env
->mem_io_pc
) {
992 /* now we have a real cpu fault */
993 current_tb
= tb_find_pc(env
->mem_io_pc
);
996 if (current_tb
== tb
&&
997 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
998 /* If we are modifying the current TB, we must stop
999 its execution. We could be more precise by checking
1000 that the modification is after the current PC, but it
1001 would require a specialized function to partially
1002 restore the CPU state */
1004 current_tb_modified
= 1;
1005 cpu_restore_state(current_tb
, env
,
1006 env
->mem_io_pc
, NULL
);
1007 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1010 #endif /* TARGET_HAS_PRECISE_SMC */
1011 /* we need to do that to handle the case where a signal
1012 occurs while doing tb_phys_invalidate() */
1015 saved_tb
= env
->current_tb
;
1016 env
->current_tb
= NULL
;
1018 tb_phys_invalidate(tb
, -1);
1020 env
->current_tb
= saved_tb
;
1021 if (env
->interrupt_request
&& env
->current_tb
)
1022 cpu_interrupt(env
, env
->interrupt_request
);
1027 #if !defined(CONFIG_USER_ONLY)
1028 /* if no code remaining, no need to continue to use slow writes */
1030 invalidate_page_bitmap(p
);
1031 if (is_cpu_write_access
) {
1032 tlb_unprotect_code_phys(env
, start
, env
->mem_io_vaddr
);
1036 #ifdef TARGET_HAS_PRECISE_SMC
1037 if (current_tb_modified
) {
1038 /* we generate a block containing just the instruction
1039 modifying the memory. It will ensure that it cannot modify
1041 env
->current_tb
= NULL
;
1042 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1043 cpu_resume_from_signal(env
, NULL
);
1048 /* len must be <= 8 and start must be a multiple of len */
1049 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start
, int len
)
1055 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1056 cpu_single_env
->mem_io_vaddr
, len
,
1057 cpu_single_env
->eip
,
1058 cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
);
1061 p
= page_find(start
>> TARGET_PAGE_BITS
);
1064 if (p
->code_bitmap
) {
1065 offset
= start
& ~TARGET_PAGE_MASK
;
1066 b
= p
->code_bitmap
[offset
>> 3] >> (offset
& 7);
1067 if (b
& ((1 << len
) - 1))
1071 tb_invalidate_phys_page_range(start
, start
+ len
, 1);
1075 #if !defined(CONFIG_SOFTMMU)
1076 static void tb_invalidate_phys_page(target_phys_addr_t addr
,
1077 unsigned long pc
, void *puc
)
1079 TranslationBlock
*tb
;
1082 #ifdef TARGET_HAS_PRECISE_SMC
1083 TranslationBlock
*current_tb
= NULL
;
1084 CPUState
*env
= cpu_single_env
;
1085 int current_tb_modified
= 0;
1086 target_ulong current_pc
= 0;
1087 target_ulong current_cs_base
= 0;
1088 int current_flags
= 0;
1091 addr
&= TARGET_PAGE_MASK
;
1092 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1096 #ifdef TARGET_HAS_PRECISE_SMC
1097 if (tb
&& pc
!= 0) {
1098 current_tb
= tb_find_pc(pc
);
1101 while (tb
!= NULL
) {
1103 tb
= (TranslationBlock
*)((long)tb
& ~3);
1104 #ifdef TARGET_HAS_PRECISE_SMC
1105 if (current_tb
== tb
&&
1106 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1107 /* If we are modifying the current TB, we must stop
1108 its execution. We could be more precise by checking
1109 that the modification is after the current PC, but it
1110 would require a specialized function to partially
1111 restore the CPU state */
1113 current_tb_modified
= 1;
1114 cpu_restore_state(current_tb
, env
, pc
, puc
);
1115 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1118 #endif /* TARGET_HAS_PRECISE_SMC */
1119 tb_phys_invalidate(tb
, addr
);
1120 tb
= tb
->page_next
[n
];
1123 #ifdef TARGET_HAS_PRECISE_SMC
1124 if (current_tb_modified
) {
1125 /* we generate a block containing just the instruction
1126 modifying the memory. It will ensure that it cannot modify
1128 env
->current_tb
= NULL
;
1129 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1130 cpu_resume_from_signal(env
, puc
);
1136 /* add the tb in the target page and protect it if necessary */
1137 static inline void tb_alloc_page(TranslationBlock
*tb
,
1138 unsigned int n
, target_ulong page_addr
)
1141 TranslationBlock
*last_first_tb
;
1143 tb
->page_addr
[n
] = page_addr
;
1144 p
= page_find_alloc(page_addr
>> TARGET_PAGE_BITS
);
1145 tb
->page_next
[n
] = p
->first_tb
;
1146 last_first_tb
= p
->first_tb
;
1147 p
->first_tb
= (TranslationBlock
*)((long)tb
| n
);
1148 invalidate_page_bitmap(p
);
1150 #if defined(TARGET_HAS_SMC) || 1
1152 #if defined(CONFIG_USER_ONLY)
1153 if (p
->flags
& PAGE_WRITE
) {
1158 /* force the host page as non writable (writes will have a
1159 page fault + mprotect overhead) */
1160 page_addr
&= qemu_host_page_mask
;
1162 for(addr
= page_addr
; addr
< page_addr
+ qemu_host_page_size
;
1163 addr
+= TARGET_PAGE_SIZE
) {
1165 p2
= page_find (addr
>> TARGET_PAGE_BITS
);
1169 p2
->flags
&= ~PAGE_WRITE
;
1170 page_get_flags(addr
);
1172 mprotect(g2h(page_addr
), qemu_host_page_size
,
1173 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
1174 #ifdef DEBUG_TB_INVALIDATE
1175 printf("protecting code page: 0x" TARGET_FMT_lx
"\n",
1180 /* if some code is already present, then the pages are already
1181 protected. So we handle the case where only the first TB is
1182 allocated in a physical page */
1183 if (!last_first_tb
) {
1184 tlb_protect_code(page_addr
);
1188 #endif /* TARGET_HAS_SMC */
1191 /* Allocate a new translation block. Flush the translation buffer if
1192 too many translation blocks or too much generated code. */
1193 TranslationBlock
*tb_alloc(target_ulong pc
)
1195 TranslationBlock
*tb
;
1197 if (nb_tbs
>= code_gen_max_blocks
||
1198 (code_gen_ptr
- code_gen_buffer
) >= code_gen_buffer_max_size
)
1200 tb
= &tbs
[nb_tbs
++];
1206 void tb_free(TranslationBlock
*tb
)
1208 /* In practice this is mostly used for single use temporary TB
1209 Ignore the hard cases and just back up if this TB happens to
1210 be the last one generated. */
1211 if (nb_tbs
> 0 && tb
== &tbs
[nb_tbs
- 1]) {
1212 code_gen_ptr
= tb
->tc_ptr
;
1217 /* add a new TB and link it to the physical page tables. phys_page2 is
1218 (-1) to indicate that only one page contains the TB. */
1219 void tb_link_phys(TranslationBlock
*tb
,
1220 target_ulong phys_pc
, target_ulong phys_page2
)
1223 TranslationBlock
**ptb
;
1225 /* Grab the mmap lock to stop another thread invalidating this TB
1226 before we are done. */
1228 /* add in the physical hash table */
1229 h
= tb_phys_hash_func(phys_pc
);
1230 ptb
= &tb_phys_hash
[h
];
1231 tb
->phys_hash_next
= *ptb
;
1234 /* add in the page list */
1235 tb_alloc_page(tb
, 0, phys_pc
& TARGET_PAGE_MASK
);
1236 if (phys_page2
!= -1)
1237 tb_alloc_page(tb
, 1, phys_page2
);
1239 tb
->page_addr
[1] = -1;
1241 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
1242 tb
->jmp_next
[0] = NULL
;
1243 tb
->jmp_next
[1] = NULL
;
1245 /* init original jump addresses */
1246 if (tb
->tb_next_offset
[0] != 0xffff)
1247 tb_reset_jump(tb
, 0);
1248 if (tb
->tb_next_offset
[1] != 0xffff)
1249 tb_reset_jump(tb
, 1);
1251 #ifdef DEBUG_TB_CHECK
1257 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1258 tb[1].tc_ptr. Return NULL if not found */
1259 TranslationBlock
*tb_find_pc(unsigned long tc_ptr
)
1261 int m_min
, m_max
, m
;
1263 TranslationBlock
*tb
;
1267 if (tc_ptr
< (unsigned long)code_gen_buffer
||
1268 tc_ptr
>= (unsigned long)code_gen_ptr
)
1270 /* binary search (cf Knuth) */
1273 while (m_min
<= m_max
) {
1274 m
= (m_min
+ m_max
) >> 1;
1276 v
= (unsigned long)tb
->tc_ptr
;
1279 else if (tc_ptr
< v
) {
1288 static void tb_reset_jump_recursive(TranslationBlock
*tb
);
1290 static inline void tb_reset_jump_recursive2(TranslationBlock
*tb
, int n
)
1292 TranslationBlock
*tb1
, *tb_next
, **ptb
;
1295 tb1
= tb
->jmp_next
[n
];
1297 /* find head of list */
1300 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1303 tb1
= tb1
->jmp_next
[n1
];
1305 /* we are now sure now that tb jumps to tb1 */
1308 /* remove tb from the jmp_first list */
1309 ptb
= &tb_next
->jmp_first
;
1313 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1314 if (n1
== n
&& tb1
== tb
)
1316 ptb
= &tb1
->jmp_next
[n1
];
1318 *ptb
= tb
->jmp_next
[n
];
1319 tb
->jmp_next
[n
] = NULL
;
1321 /* suppress the jump to next tb in generated code */
1322 tb_reset_jump(tb
, n
);
1324 /* suppress jumps in the tb on which we could have jumped */
1325 tb_reset_jump_recursive(tb_next
);
1329 static void tb_reset_jump_recursive(TranslationBlock
*tb
)
1331 tb_reset_jump_recursive2(tb
, 0);
1332 tb_reset_jump_recursive2(tb
, 1);
1335 #if defined(TARGET_HAS_ICE)
1336 static void breakpoint_invalidate(CPUState
*env
, target_ulong pc
)
1338 target_phys_addr_t addr
;
1340 ram_addr_t ram_addr
;
1343 addr
= cpu_get_phys_page_debug(env
, pc
);
1344 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
1346 pd
= IO_MEM_UNASSIGNED
;
1348 pd
= p
->phys_offset
;
1350 ram_addr
= (pd
& TARGET_PAGE_MASK
) | (pc
& ~TARGET_PAGE_MASK
);
1351 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1, 0);
1355 /* Add a watchpoint. */
1356 int cpu_watchpoint_insert(CPUState
*env
, target_ulong addr
, target_ulong len
,
1357 int flags
, CPUWatchpoint
**watchpoint
)
1359 target_ulong len_mask
= ~(len
- 1);
1362 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1363 if ((len
!= 1 && len
!= 2 && len
!= 4 && len
!= 8) || (addr
& ~len_mask
)) {
1364 fprintf(stderr
, "qemu: tried to set invalid watchpoint at "
1365 TARGET_FMT_lx
", len=" TARGET_FMT_lu
"\n", addr
, len
);
1368 wp
= qemu_malloc(sizeof(*wp
));
1371 wp
->len_mask
= len_mask
;
1374 /* keep all GDB-injected watchpoints in front */
1376 TAILQ_INSERT_HEAD(&env
->watchpoints
, wp
, entry
);
1378 TAILQ_INSERT_TAIL(&env
->watchpoints
, wp
, entry
);
1380 tlb_flush_page(env
, addr
);
1387 /* Remove a specific watchpoint. */
1388 int cpu_watchpoint_remove(CPUState
*env
, target_ulong addr
, target_ulong len
,
1391 target_ulong len_mask
= ~(len
- 1);
1394 TAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1395 if (addr
== wp
->vaddr
&& len_mask
== wp
->len_mask
1396 && flags
== (wp
->flags
& ~BP_WATCHPOINT_HIT
)) {
1397 cpu_watchpoint_remove_by_ref(env
, wp
);
1404 /* Remove a specific watchpoint by reference. */
1405 void cpu_watchpoint_remove_by_ref(CPUState
*env
, CPUWatchpoint
*watchpoint
)
1407 TAILQ_REMOVE(&env
->watchpoints
, watchpoint
, entry
);
1409 tlb_flush_page(env
, watchpoint
->vaddr
);
1411 qemu_free(watchpoint
);
1414 /* Remove all matching watchpoints. */
1415 void cpu_watchpoint_remove_all(CPUState
*env
, int mask
)
1417 CPUWatchpoint
*wp
, *next
;
1419 TAILQ_FOREACH_SAFE(wp
, &env
->watchpoints
, entry
, next
) {
1420 if (wp
->flags
& mask
)
1421 cpu_watchpoint_remove_by_ref(env
, wp
);
1425 /* Add a breakpoint. */
1426 int cpu_breakpoint_insert(CPUState
*env
, target_ulong pc
, int flags
,
1427 CPUBreakpoint
**breakpoint
)
1429 #if defined(TARGET_HAS_ICE)
1432 bp
= qemu_malloc(sizeof(*bp
));
1437 /* keep all GDB-injected breakpoints in front */
1439 TAILQ_INSERT_HEAD(&env
->breakpoints
, bp
, entry
);
1441 TAILQ_INSERT_TAIL(&env
->breakpoints
, bp
, entry
);
1443 breakpoint_invalidate(env
, pc
);
1453 /* Remove a specific breakpoint. */
1454 int cpu_breakpoint_remove(CPUState
*env
, target_ulong pc
, int flags
)
1456 #if defined(TARGET_HAS_ICE)
1459 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1460 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
1461 cpu_breakpoint_remove_by_ref(env
, bp
);
1471 /* Remove a specific breakpoint by reference. */
1472 void cpu_breakpoint_remove_by_ref(CPUState
*env
, CPUBreakpoint
*breakpoint
)
1474 #if defined(TARGET_HAS_ICE)
1475 TAILQ_REMOVE(&env
->breakpoints
, breakpoint
, entry
);
1477 breakpoint_invalidate(env
, breakpoint
->pc
);
1479 qemu_free(breakpoint
);
1483 /* Remove all matching breakpoints. */
1484 void cpu_breakpoint_remove_all(CPUState
*env
, int mask
)
1486 #if defined(TARGET_HAS_ICE)
1487 CPUBreakpoint
*bp
, *next
;
1489 TAILQ_FOREACH_SAFE(bp
, &env
->breakpoints
, entry
, next
) {
1490 if (bp
->flags
& mask
)
1491 cpu_breakpoint_remove_by_ref(env
, bp
);
1496 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1497 CPU loop after each instruction */
1498 void cpu_single_step(CPUState
*env
, int enabled
)
1500 #if defined(TARGET_HAS_ICE)
1501 if (env
->singlestep_enabled
!= enabled
) {
1502 env
->singlestep_enabled
= enabled
;
1504 kvm_update_guest_debug(env
, 0);
1506 /* must flush all the translated code to avoid inconsistencies */
1507 /* XXX: only flush what is necessary */
1514 /* enable or disable low levels log */
1515 void cpu_set_log(int log_flags
)
1517 loglevel
= log_flags
;
1518 if (loglevel
&& !logfile
) {
1519 logfile
= fopen(logfilename
, log_append
? "a" : "w");
1521 perror(logfilename
);
1524 #if !defined(CONFIG_SOFTMMU)
1525 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1527 static char logfile_buf
[4096];
1528 setvbuf(logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
1531 setvbuf(logfile
, NULL
, _IOLBF
, 0);
1535 if (!loglevel
&& logfile
) {
1541 void cpu_set_log_filename(const char *filename
)
1543 logfilename
= strdup(filename
);
1548 cpu_set_log(loglevel
);
1551 static void cpu_unlink_tb(CPUState
*env
)
1553 #if defined(USE_NPTL)
1554 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1555 problem and hope the cpu will stop of its own accord. For userspace
1556 emulation this often isn't actually as bad as it sounds. Often
1557 signals are used primarily to interrupt blocking syscalls. */
1559 TranslationBlock
*tb
;
1560 static spinlock_t interrupt_lock
= SPIN_LOCK_UNLOCKED
;
1562 tb
= env
->current_tb
;
1563 /* if the cpu is currently executing code, we must unlink it and
1564 all the potentially executing TB */
1565 if (tb
&& !testandset(&interrupt_lock
)) {
1566 env
->current_tb
= NULL
;
1567 tb_reset_jump_recursive(tb
);
1568 resetlock(&interrupt_lock
);
1573 /* mask must never be zero, except for A20 change call */
1574 void cpu_interrupt(CPUState
*env
, int mask
)
1578 old_mask
= env
->interrupt_request
;
1579 env
->interrupt_request
|= mask
;
1580 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
1581 kvm_update_interrupt_request(env
);
1583 #ifndef CONFIG_USER_ONLY
1585 * If called from iothread context, wake the target cpu in
1588 if (!qemu_cpu_self(env
)) {
1595 env
->icount_decr
.u16
.high
= 0xffff;
1596 #ifndef CONFIG_USER_ONLY
1598 && (mask
& ~old_mask
) != 0) {
1599 cpu_abort(env
, "Raised interrupt while not in I/O function");
1607 void cpu_reset_interrupt(CPUState
*env
, int mask
)
1609 env
->interrupt_request
&= ~mask
;
1612 void cpu_exit(CPUState
*env
)
1614 env
->exit_request
= 1;
1618 const CPULogItem cpu_log_items
[] = {
1619 { CPU_LOG_TB_OUT_ASM
, "out_asm",
1620 "show generated host assembly code for each compiled TB" },
1621 { CPU_LOG_TB_IN_ASM
, "in_asm",
1622 "show target assembly code for each compiled TB" },
1623 { CPU_LOG_TB_OP
, "op",
1624 "show micro ops for each compiled TB" },
1625 { CPU_LOG_TB_OP_OPT
, "op_opt",
1628 "before eflags optimization and "
1630 "after liveness analysis" },
1631 { CPU_LOG_INT
, "int",
1632 "show interrupts/exceptions in short format" },
1633 { CPU_LOG_EXEC
, "exec",
1634 "show trace before each executed TB (lots of logs)" },
1635 { CPU_LOG_TB_CPU
, "cpu",
1636 "show CPU state before block translation" },
1638 { CPU_LOG_PCALL
, "pcall",
1639 "show protected mode far calls/returns/exceptions" },
1640 { CPU_LOG_RESET
, "cpu_reset",
1641 "show CPU state before CPU resets" },
1644 { CPU_LOG_IOPORT
, "ioport",
1645 "show all i/o ports accesses" },
1650 static int cmp1(const char *s1
, int n
, const char *s2
)
1652 if (strlen(s2
) != n
)
1654 return memcmp(s1
, s2
, n
) == 0;
1657 /* takes a comma separated list of log masks. Return 0 if error. */
1658 int cpu_str_to_log_mask(const char *str
)
1660 const CPULogItem
*item
;
1667 p1
= strchr(p
, ',');
1670 if(cmp1(p
,p1
-p
,"all")) {
1671 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1675 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1676 if (cmp1(p
, p1
- p
, item
->name
))
1690 void cpu_abort(CPUState
*env
, const char *fmt
, ...)
1697 fprintf(stderr
, "qemu: fatal: ");
1698 vfprintf(stderr
, fmt
, ap
);
1699 fprintf(stderr
, "\n");
1701 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1703 cpu_dump_state(env
, stderr
, fprintf
, 0);
1705 if (qemu_log_enabled()) {
1706 qemu_log("qemu: fatal: ");
1707 qemu_log_vprintf(fmt
, ap2
);
1710 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1712 log_cpu_state(env
, 0);
1722 CPUState
*cpu_copy(CPUState
*env
)
1724 CPUState
*new_env
= cpu_init(env
->cpu_model_str
);
1725 CPUState
*next_cpu
= new_env
->next_cpu
;
1726 int cpu_index
= new_env
->cpu_index
;
1727 #if defined(TARGET_HAS_ICE)
1732 memcpy(new_env
, env
, sizeof(CPUState
));
1734 /* Preserve chaining and index. */
1735 new_env
->next_cpu
= next_cpu
;
1736 new_env
->cpu_index
= cpu_index
;
1738 /* Clone all break/watchpoints.
1739 Note: Once we support ptrace with hw-debug register access, make sure
1740 BP_CPU break/watchpoints are handled correctly on clone. */
1741 TAILQ_INIT(&env
->breakpoints
);
1742 TAILQ_INIT(&env
->watchpoints
);
1743 #if defined(TARGET_HAS_ICE)
1744 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1745 cpu_breakpoint_insert(new_env
, bp
->pc
, bp
->flags
, NULL
);
1747 TAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1748 cpu_watchpoint_insert(new_env
, wp
->vaddr
, (~wp
->len_mask
) + 1,
1756 #if !defined(CONFIG_USER_ONLY)
1758 static inline void tlb_flush_jmp_cache(CPUState
*env
, target_ulong addr
)
1762 /* Discard jump cache entries for any tb which might potentially
1763 overlap the flushed page. */
1764 i
= tb_jmp_cache_hash_page(addr
- TARGET_PAGE_SIZE
);
1765 memset (&env
->tb_jmp_cache
[i
], 0,
1766 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1768 i
= tb_jmp_cache_hash_page(addr
);
1769 memset (&env
->tb_jmp_cache
[i
], 0,
1770 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1773 static CPUTLBEntry s_cputlb_empty_entry
= {
1780 /* NOTE: if flush_global is true, also flush global entries (not
1782 void tlb_flush(CPUState
*env
, int flush_global
)
1786 #if defined(DEBUG_TLB)
1787 printf("tlb_flush:\n");
1789 /* must reset current TB so that interrupts cannot modify the
1790 links while we are modifying them */
1791 env
->current_tb
= NULL
;
1793 for(i
= 0; i
< CPU_TLB_SIZE
; i
++) {
1795 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1796 env
->tlb_table
[mmu_idx
][i
] = s_cputlb_empty_entry
;
1800 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
1803 if (env
->kqemu_enabled
) {
1804 kqemu_flush(env
, flush_global
);
1810 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, target_ulong addr
)
1812 if (addr
== (tlb_entry
->addr_read
&
1813 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1814 addr
== (tlb_entry
->addr_write
&
1815 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1816 addr
== (tlb_entry
->addr_code
&
1817 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1818 *tlb_entry
= s_cputlb_empty_entry
;
1822 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
1827 #if defined(DEBUG_TLB)
1828 printf("tlb_flush_page: " TARGET_FMT_lx
"\n", addr
);
1830 /* must reset current TB so that interrupts cannot modify the
1831 links while we are modifying them */
1832 env
->current_tb
= NULL
;
1834 addr
&= TARGET_PAGE_MASK
;
1835 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1836 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
1837 tlb_flush_entry(&env
->tlb_table
[mmu_idx
][i
], addr
);
1839 tlb_flush_jmp_cache(env
, addr
);
1842 if (env
->kqemu_enabled
) {
1843 kqemu_flush_page(env
, addr
);
1848 /* update the TLBs so that writes to code in the virtual page 'addr'
1850 static void tlb_protect_code(ram_addr_t ram_addr
)
1852 cpu_physical_memory_reset_dirty(ram_addr
,
1853 ram_addr
+ TARGET_PAGE_SIZE
,
1857 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1858 tested for self modifying code */
1859 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
1862 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] |= CODE_DIRTY_FLAG
;
1865 static inline void tlb_reset_dirty_range(CPUTLBEntry
*tlb_entry
,
1866 unsigned long start
, unsigned long length
)
1869 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1870 addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) + tlb_entry
->addend
;
1871 if ((addr
- start
) < length
) {
1872 tlb_entry
->addr_write
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) | TLB_NOTDIRTY
;
1877 /* Note: start and end must be within the same ram block. */
1878 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t end
,
1882 unsigned long length
, start1
;
1886 start
&= TARGET_PAGE_MASK
;
1887 end
= TARGET_PAGE_ALIGN(end
);
1889 length
= end
- start
;
1892 len
= length
>> TARGET_PAGE_BITS
;
1894 /* XXX: should not depend on cpu context */
1896 if (env
->kqemu_enabled
) {
1899 for(i
= 0; i
< len
; i
++) {
1900 kqemu_set_notdirty(env
, addr
);
1901 addr
+= TARGET_PAGE_SIZE
;
1905 mask
= ~dirty_flags
;
1906 p
= phys_ram_dirty
+ (start
>> TARGET_PAGE_BITS
);
1907 for(i
= 0; i
< len
; i
++)
1910 /* we modify the TLB cache so that the dirty bit will be set again
1911 when accessing the range */
1912 start1
= (unsigned long)qemu_get_ram_ptr(start
);
1913 /* Chek that we don't span multiple blocks - this breaks the
1914 address comparisons below. */
1915 if ((unsigned long)qemu_get_ram_ptr(end
- 1) - start1
1916 != (end
- 1) - start
) {
1920 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1922 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1923 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1924 tlb_reset_dirty_range(&env
->tlb_table
[mmu_idx
][i
],
1930 int cpu_physical_memory_set_dirty_tracking(int enable
)
1932 if (kvm_enabled()) {
1933 return kvm_set_migration_log(enable
);
1938 int cpu_physical_memory_get_dirty_tracking(void)
1940 return in_migration
;
1943 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
1944 target_phys_addr_t end_addr
)
1949 ret
= kvm_physical_sync_dirty_bitmap(start_addr
, end_addr
);
1953 static inline void tlb_update_dirty(CPUTLBEntry
*tlb_entry
)
1955 ram_addr_t ram_addr
;
1958 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1959 p
= (void *)(unsigned long)((tlb_entry
->addr_write
& TARGET_PAGE_MASK
)
1960 + tlb_entry
->addend
);
1961 ram_addr
= qemu_ram_addr_from_host(p
);
1962 if (!cpu_physical_memory_is_dirty(ram_addr
)) {
1963 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
1968 /* update the TLB according to the current state of the dirty bits */
1969 void cpu_tlb_update_dirty(CPUState
*env
)
1973 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1974 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1975 tlb_update_dirty(&env
->tlb_table
[mmu_idx
][i
]);
1979 static inline void tlb_set_dirty1(CPUTLBEntry
*tlb_entry
, target_ulong vaddr
)
1981 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
))
1982 tlb_entry
->addr_write
= vaddr
;
1985 /* update the TLB corresponding to virtual page vaddr
1986 so that it is no longer dirty */
1987 static inline void tlb_set_dirty(CPUState
*env
, target_ulong vaddr
)
1992 vaddr
&= TARGET_PAGE_MASK
;
1993 i
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1994 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
1995 tlb_set_dirty1(&env
->tlb_table
[mmu_idx
][i
], vaddr
);
1998 /* add a new TLB entry. At most one entry for a given virtual address
1999 is permitted. Return 0 if OK or 2 if the page could not be mapped
2000 (can only happen in non SOFTMMU mode for I/O pages or pages
2001 conflicting with the host address space). */
2002 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
2003 target_phys_addr_t paddr
, int prot
,
2004 int mmu_idx
, int is_softmmu
)
2009 target_ulong address
;
2010 target_ulong code_address
;
2011 target_phys_addr_t addend
;
2015 target_phys_addr_t iotlb
;
2017 p
= phys_page_find(paddr
>> TARGET_PAGE_BITS
);
2019 pd
= IO_MEM_UNASSIGNED
;
2021 pd
= p
->phys_offset
;
2023 #if defined(DEBUG_TLB)
2024 printf("tlb_set_page: vaddr=" TARGET_FMT_lx
" paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
2025 vaddr
, (int)paddr
, prot
, mmu_idx
, is_softmmu
, pd
);
2030 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&& !(pd
& IO_MEM_ROMD
)) {
2031 /* IO memory case (romd handled later) */
2032 address
|= TLB_MMIO
;
2034 addend
= (unsigned long)qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
);
2035 if ((pd
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
2037 iotlb
= pd
& TARGET_PAGE_MASK
;
2038 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
)
2039 iotlb
|= IO_MEM_NOTDIRTY
;
2041 iotlb
|= IO_MEM_ROM
;
2043 /* IO handlers are currently passed a physical address.
2044 It would be nice to pass an offset from the base address
2045 of that region. This would avoid having to special case RAM,
2046 and avoid full address decoding in every device.
2047 We can't use the high bits of pd for this because
2048 IO_MEM_ROMD uses these as a ram address. */
2049 iotlb
= (pd
& ~TARGET_PAGE_MASK
);
2051 iotlb
+= p
->region_offset
;
2057 code_address
= address
;
2058 /* Make accesses to pages with watchpoints go via the
2059 watchpoint trap routines. */
2060 TAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
2061 if (vaddr
== (wp
->vaddr
& TARGET_PAGE_MASK
)) {
2062 iotlb
= io_mem_watch
+ paddr
;
2063 /* TODO: The memory case can be optimized by not trapping
2064 reads of pages with a write breakpoint. */
2065 address
|= TLB_MMIO
;
2069 index
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
2070 env
->iotlb
[mmu_idx
][index
] = iotlb
- vaddr
;
2071 te
= &env
->tlb_table
[mmu_idx
][index
];
2072 te
->addend
= addend
- vaddr
;
2073 if (prot
& PAGE_READ
) {
2074 te
->addr_read
= address
;
2079 if (prot
& PAGE_EXEC
) {
2080 te
->addr_code
= code_address
;
2084 if (prot
& PAGE_WRITE
) {
2085 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_ROM
||
2086 (pd
& IO_MEM_ROMD
)) {
2087 /* Write access calls the I/O callback. */
2088 te
->addr_write
= address
| TLB_MMIO
;
2089 } else if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
&&
2090 !cpu_physical_memory_is_dirty(pd
)) {
2091 te
->addr_write
= address
| TLB_NOTDIRTY
;
2093 te
->addr_write
= address
;
2096 te
->addr_write
= -1;
2103 void tlb_flush(CPUState
*env
, int flush_global
)
2107 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
2111 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
2112 target_phys_addr_t paddr
, int prot
,
2113 int mmu_idx
, int is_softmmu
)
2119 * Walks guest process memory "regions" one by one
2120 * and calls callback function 'fn' for each region.
2122 int walk_memory_regions(void *priv
,
2123 int (*fn
)(void *, unsigned long, unsigned long, unsigned long))
2125 unsigned long start
, end
;
2127 int i
, j
, prot
, prot1
;
2133 for (i
= 0; i
<= L1_SIZE
; i
++) {
2134 p
= (i
< L1_SIZE
) ? l1_map
[i
] : NULL
;
2135 for (j
= 0; j
< L2_SIZE
; j
++) {
2136 prot1
= (p
== NULL
) ? 0 : p
[j
].flags
;
2138 * "region" is one continuous chunk of memory
2139 * that has same protection flags set.
2141 if (prot1
!= prot
) {
2142 end
= (i
<< (32 - L1_BITS
)) | (j
<< TARGET_PAGE_BITS
);
2144 rc
= (*fn
)(priv
, start
, end
, prot
);
2145 /* callback can stop iteration by returning != 0 */
2162 static int dump_region(void *priv
, unsigned long start
,
2163 unsigned long end
, unsigned long prot
)
2165 FILE *f
= (FILE *)priv
;
2167 (void) fprintf(f
, "%08lx-%08lx %08lx %c%c%c\n",
2168 start
, end
, end
- start
,
2169 ((prot
& PAGE_READ
) ? 'r' : '-'),
2170 ((prot
& PAGE_WRITE
) ? 'w' : '-'),
2171 ((prot
& PAGE_EXEC
) ? 'x' : '-'));
2176 /* dump memory mappings */
2177 void page_dump(FILE *f
)
2179 (void) fprintf(f
, "%-8s %-8s %-8s %s\n",
2180 "start", "end", "size", "prot");
2181 walk_memory_regions(f
, dump_region
);
2184 int page_get_flags(target_ulong address
)
2188 p
= page_find(address
>> TARGET_PAGE_BITS
);
2194 /* modify the flags of a page and invalidate the code if
2195 necessary. The flag PAGE_WRITE_ORG is positioned automatically
2196 depending on PAGE_WRITE */
2197 void page_set_flags(target_ulong start
, target_ulong end
, int flags
)
2202 /* mmap_lock should already be held. */
2203 start
= start
& TARGET_PAGE_MASK
;
2204 end
= TARGET_PAGE_ALIGN(end
);
2205 if (flags
& PAGE_WRITE
)
2206 flags
|= PAGE_WRITE_ORG
;
2207 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2208 p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
);
2209 /* We may be called for host regions that are outside guest
2213 /* if the write protection is set, then we invalidate the code
2215 if (!(p
->flags
& PAGE_WRITE
) &&
2216 (flags
& PAGE_WRITE
) &&
2218 tb_invalidate_phys_page(addr
, 0, NULL
);
2224 int page_check_range(target_ulong start
, target_ulong len
, int flags
)
2230 if (start
+ len
< start
)
2231 /* we've wrapped around */
2234 end
= TARGET_PAGE_ALIGN(start
+len
); /* must do before we loose bits in the next step */
2235 start
= start
& TARGET_PAGE_MASK
;
2237 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2238 p
= page_find(addr
>> TARGET_PAGE_BITS
);
2241 if( !(p
->flags
& PAGE_VALID
) )
2244 if ((flags
& PAGE_READ
) && !(p
->flags
& PAGE_READ
))
2246 if (flags
& PAGE_WRITE
) {
2247 if (!(p
->flags
& PAGE_WRITE_ORG
))
2249 /* unprotect the page if it was put read-only because it
2250 contains translated code */
2251 if (!(p
->flags
& PAGE_WRITE
)) {
2252 if (!page_unprotect(addr
, 0, NULL
))
2261 /* called from signal handler: invalidate the code and unprotect the
2262 page. Return TRUE if the fault was successfully handled. */
2263 int page_unprotect(target_ulong address
, unsigned long pc
, void *puc
)
2265 unsigned int page_index
, prot
, pindex
;
2267 target_ulong host_start
, host_end
, addr
;
2269 /* Technically this isn't safe inside a signal handler. However we
2270 know this only ever happens in a synchronous SEGV handler, so in
2271 practice it seems to be ok. */
2274 host_start
= address
& qemu_host_page_mask
;
2275 page_index
= host_start
>> TARGET_PAGE_BITS
;
2276 p1
= page_find(page_index
);
2281 host_end
= host_start
+ qemu_host_page_size
;
2284 for(addr
= host_start
;addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
2288 /* if the page was really writable, then we change its
2289 protection back to writable */
2290 if (prot
& PAGE_WRITE_ORG
) {
2291 pindex
= (address
- host_start
) >> TARGET_PAGE_BITS
;
2292 if (!(p1
[pindex
].flags
& PAGE_WRITE
)) {
2293 mprotect((void *)g2h(host_start
), qemu_host_page_size
,
2294 (prot
& PAGE_BITS
) | PAGE_WRITE
);
2295 p1
[pindex
].flags
|= PAGE_WRITE
;
2296 /* and since the content will be modified, we must invalidate
2297 the corresponding translated code. */
2298 tb_invalidate_phys_page(address
, pc
, puc
);
2299 #ifdef DEBUG_TB_CHECK
2300 tb_invalidate_check(address
);
2310 static inline void tlb_set_dirty(CPUState
*env
,
2311 unsigned long addr
, target_ulong vaddr
)
2314 #endif /* defined(CONFIG_USER_ONLY) */
2316 #if !defined(CONFIG_USER_ONLY)
2318 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2319 ram_addr_t memory
, ram_addr_t region_offset
);
2320 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2321 ram_addr_t orig_memory
, ram_addr_t region_offset
);
2322 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2325 if (addr > start_addr) \
2328 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2329 if (start_addr2 > 0) \
2333 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2334 end_addr2 = TARGET_PAGE_SIZE - 1; \
2336 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2337 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2342 /* register physical memory. 'size' must be a multiple of the target
2343 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2344 io memory page. The address used when calling the IO function is
2345 the offset from the start of the region, plus region_offset. Both
2346 start_addr and region_offset are rounded down to a page boundary
2347 before calculating this offset. This should not be a problem unless
2348 the low bits of start_addr and region_offset differ. */
2349 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr
,
2351 ram_addr_t phys_offset
,
2352 ram_addr_t region_offset
)
2354 target_phys_addr_t addr
, end_addr
;
2357 ram_addr_t orig_size
= size
;
2361 /* XXX: should not depend on cpu context */
2363 if (env
->kqemu_enabled
) {
2364 kqemu_set_phys_mem(start_addr
, size
, phys_offset
);
2368 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
2370 if (phys_offset
== IO_MEM_UNASSIGNED
) {
2371 region_offset
= start_addr
;
2373 region_offset
&= TARGET_PAGE_MASK
;
2374 size
= (size
+ TARGET_PAGE_SIZE
- 1) & TARGET_PAGE_MASK
;
2375 end_addr
= start_addr
+ (target_phys_addr_t
)size
;
2376 for(addr
= start_addr
; addr
!= end_addr
; addr
+= TARGET_PAGE_SIZE
) {
2377 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2378 if (p
&& p
->phys_offset
!= IO_MEM_UNASSIGNED
) {
2379 ram_addr_t orig_memory
= p
->phys_offset
;
2380 target_phys_addr_t start_addr2
, end_addr2
;
2381 int need_subpage
= 0;
2383 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
, end_addr2
,
2385 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2386 if (!(orig_memory
& IO_MEM_SUBPAGE
)) {
2387 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2388 &p
->phys_offset
, orig_memory
,
2391 subpage
= io_mem_opaque
[(orig_memory
& ~TARGET_PAGE_MASK
)
2394 subpage_register(subpage
, start_addr2
, end_addr2
, phys_offset
,
2396 p
->region_offset
= 0;
2398 p
->phys_offset
= phys_offset
;
2399 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2400 (phys_offset
& IO_MEM_ROMD
))
2401 phys_offset
+= TARGET_PAGE_SIZE
;
2404 p
= phys_page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
2405 p
->phys_offset
= phys_offset
;
2406 p
->region_offset
= region_offset
;
2407 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2408 (phys_offset
& IO_MEM_ROMD
)) {
2409 phys_offset
+= TARGET_PAGE_SIZE
;
2411 target_phys_addr_t start_addr2
, end_addr2
;
2412 int need_subpage
= 0;
2414 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
,
2415 end_addr2
, need_subpage
);
2417 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2418 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2419 &p
->phys_offset
, IO_MEM_UNASSIGNED
,
2420 addr
& TARGET_PAGE_MASK
);
2421 subpage_register(subpage
, start_addr2
, end_addr2
,
2422 phys_offset
, region_offset
);
2423 p
->region_offset
= 0;
2427 region_offset
+= TARGET_PAGE_SIZE
;
2430 /* since each CPU stores ram addresses in its TLB cache, we must
2431 reset the modified entries */
2433 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2438 /* XXX: temporary until new memory mapping API */
2439 ram_addr_t
cpu_get_physical_page_desc(target_phys_addr_t addr
)
2443 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2445 return IO_MEM_UNASSIGNED
;
2446 return p
->phys_offset
;
2449 void qemu_register_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2452 kvm_coalesce_mmio_region(addr
, size
);
2455 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2458 kvm_uncoalesce_mmio_region(addr
, size
);
2462 /* XXX: better than nothing */
2463 static ram_addr_t
kqemu_ram_alloc(ram_addr_t size
)
2466 if ((last_ram_offset
+ size
) > kqemu_phys_ram_size
) {
2467 fprintf(stderr
, "Not enough memory (requested_size = %" PRIu64
", max memory = %" PRIu64
")\n",
2468 (uint64_t)size
, (uint64_t)kqemu_phys_ram_size
);
2471 addr
= last_ram_offset
;
2472 last_ram_offset
= TARGET_PAGE_ALIGN(last_ram_offset
+ size
);
2479 #include <sys/vfs.h>
2481 #define HUGETLBFS_MAGIC 0x958458f6
2483 static long gethugepagesize(const char *path
)
2489 ret
= statfs(path
, &fs
);
2490 } while (ret
!= 0 && errno
== EINTR
);
2497 if (fs
.f_type
!= HUGETLBFS_MAGIC
)
2498 fprintf(stderr
, "Warning: path not on HugeTLBFS: %s\n", path
);
2503 static void *file_ram_alloc(ram_addr_t memory
, const char *path
)
2511 unsigned long hpagesize
;
2512 extern int mem_prealloc
;
2518 hpagesize
= gethugepagesize(path
);
2523 if (memory
< hpagesize
) {
2527 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2528 fprintf(stderr
, "host lacks mmu notifiers, disabling --mem-path\n");
2532 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1) {
2536 fd
= mkstemp(filename
);
2545 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
2548 * ftruncate is not supported by hugetlbfs in older
2549 * hosts, so don't bother checking for errors.
2550 * If anything goes wrong with it under other filesystems,
2553 ftruncate(fd
, memory
);
2556 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2557 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2558 * to sidestep this quirk.
2560 flags
= mem_prealloc
? MAP_POPULATE
|MAP_SHARED
: MAP_PRIVATE
;
2561 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, flags
, fd
, 0);
2563 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
2565 if (area
== MAP_FAILED
) {
2566 perror("alloc_mem_area: can't mmap hugetlbfs pages");
2575 static void *file_ram_alloc(ram_addr_t memory
, const char *path
)
2582 extern const char *mem_path
;
2584 ram_addr_t
qemu_ram_alloc(ram_addr_t size
)
2586 RAMBlock
*new_block
;
2589 if (kqemu_phys_ram_base
) {
2590 return kqemu_ram_alloc(size
);
2594 size
= TARGET_PAGE_ALIGN(size
);
2595 new_block
= qemu_malloc(sizeof(*new_block
));
2597 new_block
->host
= file_ram_alloc(size
, mem_path
);
2598 if (!new_block
->host
) {
2599 new_block
->host
= qemu_vmalloc(size
);
2601 new_block
->offset
= last_ram_offset
;
2602 new_block
->length
= size
;
2604 new_block
->next
= ram_blocks
;
2605 ram_blocks
= new_block
;
2607 phys_ram_dirty
= qemu_realloc(phys_ram_dirty
,
2608 (last_ram_offset
+ size
) >> TARGET_PAGE_BITS
);
2609 memset(phys_ram_dirty
+ (last_ram_offset
>> TARGET_PAGE_BITS
),
2610 0xff, size
>> TARGET_PAGE_BITS
);
2612 last_ram_offset
+= size
;
2615 kvm_setup_guest_memory(new_block
->host
, size
);
2617 return new_block
->offset
;
2620 void qemu_ram_free(ram_addr_t addr
)
2622 /* TODO: implement this. */
2625 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2626 With the exception of the softmmu code in this file, this should
2627 only be used for local memory (e.g. video ram) that the device owns,
2628 and knows it isn't going to access beyond the end of the block.
2630 It should not be used for general purpose DMA.
2631 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2633 void *qemu_get_ram_ptr(ram_addr_t addr
)
2640 if (kqemu_phys_ram_base
) {
2641 return kqemu_phys_ram_base
+ addr
;
2646 prevp
= &ram_blocks
;
2648 while (block
&& (block
->offset
> addr
2649 || block
->offset
+ block
->length
<= addr
)) {
2651 prevp
= &prev
->next
;
2653 block
= block
->next
;
2656 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
2659 /* Move this entry to to start of the list. */
2661 prev
->next
= block
->next
;
2662 block
->next
= *prevp
;
2665 return block
->host
+ (addr
- block
->offset
);
2668 /* Some of the softmmu routines need to translate from a host pointer
2669 (typically a TLB entry) back to a ram offset. */
2670 ram_addr_t
qemu_ram_addr_from_host(void *ptr
)
2675 uint8_t *host
= ptr
;
2678 if (kqemu_phys_ram_base
) {
2679 return host
- kqemu_phys_ram_base
;
2684 prevp
= &ram_blocks
;
2686 while (block
&& (block
->host
> host
2687 || block
->host
+ block
->length
<= host
)) {
2689 prevp
= &prev
->next
;
2691 block
= block
->next
;
2694 fprintf(stderr
, "Bad ram pointer %p\n", ptr
);
2697 return block
->offset
+ (host
- block
->host
);
2700 static uint32_t unassigned_mem_readb(void *opaque
, target_phys_addr_t addr
)
2702 #ifdef DEBUG_UNASSIGNED
2703 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2705 #if defined(TARGET_SPARC)
2706 do_unassigned_access(addr
, 0, 0, 0, 1);
2711 static uint32_t unassigned_mem_readw(void *opaque
, target_phys_addr_t addr
)
2713 #ifdef DEBUG_UNASSIGNED
2714 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2716 #if defined(TARGET_SPARC)
2717 do_unassigned_access(addr
, 0, 0, 0, 2);
2722 static uint32_t unassigned_mem_readl(void *opaque
, target_phys_addr_t addr
)
2724 #ifdef DEBUG_UNASSIGNED
2725 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2727 #if defined(TARGET_SPARC)
2728 do_unassigned_access(addr
, 0, 0, 0, 4);
2733 static void unassigned_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2735 #ifdef DEBUG_UNASSIGNED
2736 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2738 #if defined(TARGET_SPARC)
2739 do_unassigned_access(addr
, 1, 0, 0, 1);
2743 static void unassigned_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2745 #ifdef DEBUG_UNASSIGNED
2746 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2748 #if defined(TARGET_SPARC)
2749 do_unassigned_access(addr
, 1, 0, 0, 2);
2753 static void unassigned_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2755 #ifdef DEBUG_UNASSIGNED
2756 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2758 #if defined(TARGET_SPARC)
2759 do_unassigned_access(addr
, 1, 0, 0, 4);
2763 static CPUReadMemoryFunc
*unassigned_mem_read
[3] = {
2764 unassigned_mem_readb
,
2765 unassigned_mem_readw
,
2766 unassigned_mem_readl
,
2769 static CPUWriteMemoryFunc
*unassigned_mem_write
[3] = {
2770 unassigned_mem_writeb
,
2771 unassigned_mem_writew
,
2772 unassigned_mem_writel
,
2775 static void notdirty_mem_writeb(void *opaque
, target_phys_addr_t ram_addr
,
2779 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2780 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2781 #if !defined(CONFIG_USER_ONLY)
2782 tb_invalidate_phys_page_fast(ram_addr
, 1);
2783 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2786 stb_p(qemu_get_ram_ptr(ram_addr
), val
);
2788 if (cpu_single_env
->kqemu_enabled
&&
2789 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2790 kqemu_modify_page(cpu_single_env
, ram_addr
);
2792 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2793 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2794 /* we remove the notdirty callback only if the code has been
2796 if (dirty_flags
== 0xff)
2797 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2800 static void notdirty_mem_writew(void *opaque
, target_phys_addr_t ram_addr
,
2804 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2805 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2806 #if !defined(CONFIG_USER_ONLY)
2807 tb_invalidate_phys_page_fast(ram_addr
, 2);
2808 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2811 stw_p(qemu_get_ram_ptr(ram_addr
), val
);
2813 if (cpu_single_env
->kqemu_enabled
&&
2814 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2815 kqemu_modify_page(cpu_single_env
, ram_addr
);
2817 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2818 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2819 /* we remove the notdirty callback only if the code has been
2821 if (dirty_flags
== 0xff)
2822 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2825 static void notdirty_mem_writel(void *opaque
, target_phys_addr_t ram_addr
,
2829 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2830 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2831 #if !defined(CONFIG_USER_ONLY)
2832 tb_invalidate_phys_page_fast(ram_addr
, 4);
2833 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2836 stl_p(qemu_get_ram_ptr(ram_addr
), val
);
2838 if (cpu_single_env
->kqemu_enabled
&&
2839 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2840 kqemu_modify_page(cpu_single_env
, ram_addr
);
2842 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2843 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2844 /* we remove the notdirty callback only if the code has been
2846 if (dirty_flags
== 0xff)
2847 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2850 static CPUReadMemoryFunc
*error_mem_read
[3] = {
2851 NULL
, /* never used */
2852 NULL
, /* never used */
2853 NULL
, /* never used */
2856 static CPUWriteMemoryFunc
*notdirty_mem_write
[3] = {
2857 notdirty_mem_writeb
,
2858 notdirty_mem_writew
,
2859 notdirty_mem_writel
,
2862 /* Generate a debug exception if a watchpoint has been hit. */
2863 static void check_watchpoint(int offset
, int len_mask
, int flags
)
2865 CPUState
*env
= cpu_single_env
;
2866 target_ulong pc
, cs_base
;
2867 TranslationBlock
*tb
;
2872 if (env
->watchpoint_hit
) {
2873 /* We re-entered the check after replacing the TB. Now raise
2874 * the debug interrupt so that is will trigger after the
2875 * current instruction. */
2876 cpu_interrupt(env
, CPU_INTERRUPT_DEBUG
);
2879 vaddr
= (env
->mem_io_vaddr
& TARGET_PAGE_MASK
) + offset
;
2880 TAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
2881 if ((vaddr
== (wp
->vaddr
& len_mask
) ||
2882 (vaddr
& wp
->len_mask
) == wp
->vaddr
) && (wp
->flags
& flags
)) {
2883 wp
->flags
|= BP_WATCHPOINT_HIT
;
2884 if (!env
->watchpoint_hit
) {
2885 env
->watchpoint_hit
= wp
;
2886 tb
= tb_find_pc(env
->mem_io_pc
);
2888 cpu_abort(env
, "check_watchpoint: could not find TB for "
2889 "pc=%p", (void *)env
->mem_io_pc
);
2891 cpu_restore_state(tb
, env
, env
->mem_io_pc
, NULL
);
2892 tb_phys_invalidate(tb
, -1);
2893 if (wp
->flags
& BP_STOP_BEFORE_ACCESS
) {
2894 env
->exception_index
= EXCP_DEBUG
;
2896 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &cpu_flags
);
2897 tb_gen_code(env
, pc
, cs_base
, cpu_flags
, 1);
2899 cpu_resume_from_signal(env
, NULL
);
2902 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
2907 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2908 so these check for a hit then pass through to the normal out-of-line
2910 static uint32_t watch_mem_readb(void *opaque
, target_phys_addr_t addr
)
2912 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_READ
);
2913 return ldub_phys(addr
);
2916 static uint32_t watch_mem_readw(void *opaque
, target_phys_addr_t addr
)
2918 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_READ
);
2919 return lduw_phys(addr
);
2922 static uint32_t watch_mem_readl(void *opaque
, target_phys_addr_t addr
)
2924 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_READ
);
2925 return ldl_phys(addr
);
2928 static void watch_mem_writeb(void *opaque
, target_phys_addr_t addr
,
2931 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_WRITE
);
2932 stb_phys(addr
, val
);
2935 static void watch_mem_writew(void *opaque
, target_phys_addr_t addr
,
2938 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_WRITE
);
2939 stw_phys(addr
, val
);
2942 static void watch_mem_writel(void *opaque
, target_phys_addr_t addr
,
2945 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_WRITE
);
2946 stl_phys(addr
, val
);
2949 static CPUReadMemoryFunc
*watch_mem_read
[3] = {
2955 static CPUWriteMemoryFunc
*watch_mem_write
[3] = {
2961 static inline uint32_t subpage_readlen (subpage_t
*mmio
, target_phys_addr_t addr
,
2967 idx
= SUBPAGE_IDX(addr
);
2968 #if defined(DEBUG_SUBPAGE)
2969 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d\n", __func__
,
2970 mmio
, len
, addr
, idx
);
2972 ret
= (**mmio
->mem_read
[idx
][len
])(mmio
->opaque
[idx
][0][len
],
2973 addr
+ mmio
->region_offset
[idx
][0][len
]);
2978 static inline void subpage_writelen (subpage_t
*mmio
, target_phys_addr_t addr
,
2979 uint32_t value
, unsigned int len
)
2983 idx
= SUBPAGE_IDX(addr
);
2984 #if defined(DEBUG_SUBPAGE)
2985 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d value %08x\n", __func__
,
2986 mmio
, len
, addr
, idx
, value
);
2988 (**mmio
->mem_write
[idx
][len
])(mmio
->opaque
[idx
][1][len
],
2989 addr
+ mmio
->region_offset
[idx
][1][len
],
2993 static uint32_t subpage_readb (void *opaque
, target_phys_addr_t addr
)
2995 #if defined(DEBUG_SUBPAGE)
2996 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2999 return subpage_readlen(opaque
, addr
, 0);
3002 static void subpage_writeb (void *opaque
, target_phys_addr_t addr
,
3005 #if defined(DEBUG_SUBPAGE)
3006 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
3008 subpage_writelen(opaque
, addr
, value
, 0);
3011 static uint32_t subpage_readw (void *opaque
, target_phys_addr_t addr
)
3013 #if defined(DEBUG_SUBPAGE)
3014 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
3017 return subpage_readlen(opaque
, addr
, 1);
3020 static void subpage_writew (void *opaque
, target_phys_addr_t addr
,
3023 #if defined(DEBUG_SUBPAGE)
3024 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
3026 subpage_writelen(opaque
, addr
, value
, 1);
3029 static uint32_t subpage_readl (void *opaque
, target_phys_addr_t addr
)
3031 #if defined(DEBUG_SUBPAGE)
3032 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
3035 return subpage_readlen(opaque
, addr
, 2);
3038 static void subpage_writel (void *opaque
,
3039 target_phys_addr_t addr
, uint32_t value
)
3041 #if defined(DEBUG_SUBPAGE)
3042 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
3044 subpage_writelen(opaque
, addr
, value
, 2);
3047 static CPUReadMemoryFunc
*subpage_read
[] = {
3053 static CPUWriteMemoryFunc
*subpage_write
[] = {
3059 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
3060 ram_addr_t memory
, ram_addr_t region_offset
)
3065 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
3067 idx
= SUBPAGE_IDX(start
);
3068 eidx
= SUBPAGE_IDX(end
);
3069 #if defined(DEBUG_SUBPAGE)
3070 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__
,
3071 mmio
, start
, end
, idx
, eidx
, memory
);
3073 memory
>>= IO_MEM_SHIFT
;
3074 for (; idx
<= eidx
; idx
++) {
3075 for (i
= 0; i
< 4; i
++) {
3076 if (io_mem_read
[memory
][i
]) {
3077 mmio
->mem_read
[idx
][i
] = &io_mem_read
[memory
][i
];
3078 mmio
->opaque
[idx
][0][i
] = io_mem_opaque
[memory
];
3079 mmio
->region_offset
[idx
][0][i
] = region_offset
;
3081 if (io_mem_write
[memory
][i
]) {
3082 mmio
->mem_write
[idx
][i
] = &io_mem_write
[memory
][i
];
3083 mmio
->opaque
[idx
][1][i
] = io_mem_opaque
[memory
];
3084 mmio
->region_offset
[idx
][1][i
] = region_offset
;
3092 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
3093 ram_addr_t orig_memory
, ram_addr_t region_offset
)
3098 mmio
= qemu_mallocz(sizeof(subpage_t
));
3101 subpage_memory
= cpu_register_io_memory(subpage_read
, subpage_write
, mmio
);
3102 #if defined(DEBUG_SUBPAGE)
3103 printf("%s: %p base " TARGET_FMT_plx
" len %08x %d\n", __func__
,
3104 mmio
, base
, TARGET_PAGE_SIZE
, subpage_memory
);
3106 *phys
= subpage_memory
| IO_MEM_SUBPAGE
;
3107 subpage_register(mmio
, 0, TARGET_PAGE_SIZE
- 1, orig_memory
,
3113 static int get_free_io_mem_idx(void)
3117 for (i
= 0; i
<IO_MEM_NB_ENTRIES
; i
++)
3118 if (!io_mem_used
[i
]) {
3126 /* mem_read and mem_write are arrays of functions containing the
3127 function to access byte (index 0), word (index 1) and dword (index
3128 2). Functions can be omitted with a NULL function pointer.
3129 If io_index is non zero, the corresponding io zone is
3130 modified. If it is zero, a new io zone is allocated. The return
3131 value can be used with cpu_register_physical_memory(). (-1) is
3132 returned if error. */
3133 static int cpu_register_io_memory_fixed(int io_index
,
3134 CPUReadMemoryFunc
**mem_read
,
3135 CPUWriteMemoryFunc
**mem_write
,
3138 int i
, subwidth
= 0;
3140 if (io_index
<= 0) {
3141 io_index
= get_free_io_mem_idx();
3145 io_index
>>= IO_MEM_SHIFT
;
3146 if (io_index
>= IO_MEM_NB_ENTRIES
)
3150 for(i
= 0;i
< 3; i
++) {
3151 if (!mem_read
[i
] || !mem_write
[i
])
3152 subwidth
= IO_MEM_SUBWIDTH
;
3153 io_mem_read
[io_index
][i
] = mem_read
[i
];
3154 io_mem_write
[io_index
][i
] = mem_write
[i
];
3156 io_mem_opaque
[io_index
] = opaque
;
3157 return (io_index
<< IO_MEM_SHIFT
) | subwidth
;
3160 int cpu_register_io_memory(CPUReadMemoryFunc
**mem_read
,
3161 CPUWriteMemoryFunc
**mem_write
,
3164 return cpu_register_io_memory_fixed(0, mem_read
, mem_write
, opaque
);
3167 void cpu_unregister_io_memory(int io_table_address
)
3170 int io_index
= io_table_address
>> IO_MEM_SHIFT
;
3172 for (i
=0;i
< 3; i
++) {
3173 io_mem_read
[io_index
][i
] = unassigned_mem_read
[i
];
3174 io_mem_write
[io_index
][i
] = unassigned_mem_write
[i
];
3176 io_mem_opaque
[io_index
] = NULL
;
3177 io_mem_used
[io_index
] = 0;
3180 static void io_mem_init(void)
3184 cpu_register_io_memory_fixed(IO_MEM_ROM
, error_mem_read
, unassigned_mem_write
, NULL
);
3185 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED
, unassigned_mem_read
, unassigned_mem_write
, NULL
);
3186 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY
, error_mem_read
, notdirty_mem_write
, NULL
);
3190 io_mem_watch
= cpu_register_io_memory(watch_mem_read
,
3191 watch_mem_write
, NULL
);
3193 if (kqemu_phys_ram_base
) {
3194 /* alloc dirty bits array */
3195 phys_ram_dirty
= qemu_vmalloc(kqemu_phys_ram_size
>> TARGET_PAGE_BITS
);
3196 memset(phys_ram_dirty
, 0xff, kqemu_phys_ram_size
>> TARGET_PAGE_BITS
);
3201 #endif /* !defined(CONFIG_USER_ONLY) */
3203 /* physical memory access (slow version, mainly for debug) */
3204 #if defined(CONFIG_USER_ONLY)
3205 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
3206 int len
, int is_write
)
3213 page
= addr
& TARGET_PAGE_MASK
;
3214 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3217 flags
= page_get_flags(page
);
3218 if (!(flags
& PAGE_VALID
))
3221 if (!(flags
& PAGE_WRITE
))
3223 /* XXX: this code should not depend on lock_user */
3224 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
3225 /* FIXME - should this return an error rather than just fail? */
3228 unlock_user(p
, addr
, l
);
3230 if (!(flags
& PAGE_READ
))
3232 /* XXX: this code should not depend on lock_user */
3233 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
3234 /* FIXME - should this return an error rather than just fail? */
3237 unlock_user(p
, addr
, 0);
3246 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
3247 int len
, int is_write
)
3252 target_phys_addr_t page
;
3257 page
= addr
& TARGET_PAGE_MASK
;
3258 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3261 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3263 pd
= IO_MEM_UNASSIGNED
;
3265 pd
= p
->phys_offset
;
3269 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3270 target_phys_addr_t addr1
= addr
;
3271 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3273 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3274 /* XXX: could force cpu_single_env to NULL to avoid
3276 if (l
>= 4 && ((addr1
& 3) == 0)) {
3277 /* 32 bit write access */
3279 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr1
, val
);
3281 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3282 /* 16 bit write access */
3284 io_mem_write
[io_index
][1](io_mem_opaque
[io_index
], addr1
, val
);
3287 /* 8 bit write access */
3289 io_mem_write
[io_index
][0](io_mem_opaque
[io_index
], addr1
, val
);
3293 unsigned long addr1
;
3294 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3296 ptr
= qemu_get_ram_ptr(addr1
);
3297 memcpy(ptr
, buf
, l
);
3298 if (!cpu_physical_memory_is_dirty(addr1
)) {
3299 /* invalidate code */
3300 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3302 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3303 (0xff & ~CODE_DIRTY_FLAG
);
3305 /* qemu doesn't execute guest code directly, but kvm does
3306 therefore flush instruction caches */
3308 flush_icache_range((unsigned long)ptr
,
3309 ((unsigned long)ptr
)+l
);
3312 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3313 !(pd
& IO_MEM_ROMD
)) {
3314 target_phys_addr_t addr1
= addr
;
3316 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3318 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3319 if (l
>= 4 && ((addr1
& 3) == 0)) {
3320 /* 32 bit read access */
3321 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr1
);
3324 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3325 /* 16 bit read access */
3326 val
= io_mem_read
[io_index
][1](io_mem_opaque
[io_index
], addr1
);
3330 /* 8 bit read access */
3331 val
= io_mem_read
[io_index
][0](io_mem_opaque
[io_index
], addr1
);
3337 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3338 (addr
& ~TARGET_PAGE_MASK
);
3339 memcpy(buf
, ptr
, l
);
3348 /* used for ROM loading : can write in RAM and ROM */
3349 void cpu_physical_memory_write_rom(target_phys_addr_t addr
,
3350 const uint8_t *buf
, int len
)
3354 target_phys_addr_t page
;
3359 page
= addr
& TARGET_PAGE_MASK
;
3360 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3363 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3365 pd
= IO_MEM_UNASSIGNED
;
3367 pd
= p
->phys_offset
;
3370 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
&&
3371 (pd
& ~TARGET_PAGE_MASK
) != IO_MEM_ROM
&&
3372 !(pd
& IO_MEM_ROMD
)) {
3375 unsigned long addr1
;
3376 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3378 ptr
= qemu_get_ram_ptr(addr1
);
3379 memcpy(ptr
, buf
, l
);
3389 target_phys_addr_t addr
;
3390 target_phys_addr_t len
;
3393 static BounceBuffer bounce
;
3395 typedef struct MapClient
{
3397 void (*callback
)(void *opaque
);
3398 LIST_ENTRY(MapClient
) link
;
3401 static LIST_HEAD(map_client_list
, MapClient
) map_client_list
3402 = LIST_HEAD_INITIALIZER(map_client_list
);
3404 void *cpu_register_map_client(void *opaque
, void (*callback
)(void *opaque
))
3406 MapClient
*client
= qemu_malloc(sizeof(*client
));
3408 client
->opaque
= opaque
;
3409 client
->callback
= callback
;
3410 LIST_INSERT_HEAD(&map_client_list
, client
, link
);
3414 void cpu_unregister_map_client(void *_client
)
3416 MapClient
*client
= (MapClient
*)_client
;
3418 LIST_REMOVE(client
, link
);
3422 static void cpu_notify_map_clients(void)
3426 while (!LIST_EMPTY(&map_client_list
)) {
3427 client
= LIST_FIRST(&map_client_list
);
3428 client
->callback(client
->opaque
);
3429 cpu_unregister_map_client(client
);
3433 /* Map a physical memory region into a host virtual address.
3434 * May map a subset of the requested range, given by and returned in *plen.
3435 * May return NULL if resources needed to perform the mapping are exhausted.
3436 * Use only for reads OR writes - not for read-modify-write operations.
3437 * Use cpu_register_map_client() to know when retrying the map operation is
3438 * likely to succeed.
3440 void *cpu_physical_memory_map(target_phys_addr_t addr
,
3441 target_phys_addr_t
*plen
,
3444 target_phys_addr_t len
= *plen
;
3445 target_phys_addr_t done
= 0;
3447 uint8_t *ret
= NULL
;
3449 target_phys_addr_t page
;
3452 unsigned long addr1
;
3455 page
= addr
& TARGET_PAGE_MASK
;
3456 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3459 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3461 pd
= IO_MEM_UNASSIGNED
;
3463 pd
= p
->phys_offset
;
3466 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3467 if (done
|| bounce
.buffer
) {
3470 bounce
.buffer
= qemu_memalign(TARGET_PAGE_SIZE
, TARGET_PAGE_SIZE
);
3474 cpu_physical_memory_rw(addr
, bounce
.buffer
, l
, 0);
3476 ptr
= bounce
.buffer
;
3478 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3479 ptr
= qemu_get_ram_ptr(addr1
);
3483 } else if (ret
+ done
!= ptr
) {
3495 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3496 * Will also mark the memory as dirty if is_write == 1. access_len gives
3497 * the amount of memory that was actually read or written by the caller.
3499 void cpu_physical_memory_unmap(void *buffer
, target_phys_addr_t len
,
3500 int is_write
, target_phys_addr_t access_len
)
3502 unsigned long flush_len
= (unsigned long)access_len
;
3504 if (buffer
!= bounce
.buffer
) {
3506 ram_addr_t addr1
= qemu_ram_addr_from_host(buffer
);
3507 while (access_len
) {
3509 l
= TARGET_PAGE_SIZE
;
3512 if (!cpu_physical_memory_is_dirty(addr1
)) {
3513 /* invalidate code */
3514 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3516 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3517 (0xff & ~CODE_DIRTY_FLAG
);
3522 dma_flush_range((unsigned long)buffer
,
3523 (unsigned long)buffer
+ flush_len
);
3528 cpu_physical_memory_write(bounce
.addr
, bounce
.buffer
, access_len
);
3530 qemu_free(bounce
.buffer
);
3531 bounce
.buffer
= NULL
;
3532 cpu_notify_map_clients();
3535 /* warning: addr must be aligned */
3536 uint32_t ldl_phys(target_phys_addr_t addr
)
3544 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3546 pd
= IO_MEM_UNASSIGNED
;
3548 pd
= p
->phys_offset
;
3551 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3552 !(pd
& IO_MEM_ROMD
)) {
3554 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3556 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3557 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3560 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3561 (addr
& ~TARGET_PAGE_MASK
);
3567 /* warning: addr must be aligned */
3568 uint64_t ldq_phys(target_phys_addr_t addr
)
3576 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3578 pd
= IO_MEM_UNASSIGNED
;
3580 pd
= p
->phys_offset
;
3583 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3584 !(pd
& IO_MEM_ROMD
)) {
3586 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3588 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3589 #ifdef TARGET_WORDS_BIGENDIAN
3590 val
= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
) << 32;
3591 val
|= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4);
3593 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3594 val
|= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4) << 32;
3598 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3599 (addr
& ~TARGET_PAGE_MASK
);
3606 uint32_t ldub_phys(target_phys_addr_t addr
)
3609 cpu_physical_memory_read(addr
, &val
, 1);
3614 uint32_t lduw_phys(target_phys_addr_t addr
)
3617 cpu_physical_memory_read(addr
, (uint8_t *)&val
, 2);
3618 return tswap16(val
);
3621 /* warning: addr must be aligned. The ram page is not masked as dirty
3622 and the code inside is not invalidated. It is useful if the dirty
3623 bits are used to track modified PTEs */
3624 void stl_phys_notdirty(target_phys_addr_t addr
, uint32_t val
)
3631 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3633 pd
= IO_MEM_UNASSIGNED
;
3635 pd
= p
->phys_offset
;
3638 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3639 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3641 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3642 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3644 unsigned long addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3645 ptr
= qemu_get_ram_ptr(addr1
);
3648 if (unlikely(in_migration
)) {
3649 if (!cpu_physical_memory_is_dirty(addr1
)) {
3650 /* invalidate code */
3651 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3653 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3654 (0xff & ~CODE_DIRTY_FLAG
);
3660 void stq_phys_notdirty(target_phys_addr_t addr
, uint64_t val
)
3667 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3669 pd
= IO_MEM_UNASSIGNED
;
3671 pd
= p
->phys_offset
;
3674 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3675 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3677 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3678 #ifdef TARGET_WORDS_BIGENDIAN
3679 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
>> 32);
3680 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
);
3682 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3683 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
>> 32);
3686 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3687 (addr
& ~TARGET_PAGE_MASK
);
3692 /* warning: addr must be aligned */
3693 void stl_phys(target_phys_addr_t addr
, uint32_t val
)
3700 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3702 pd
= IO_MEM_UNASSIGNED
;
3704 pd
= p
->phys_offset
;
3707 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3708 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3710 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3711 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3713 unsigned long addr1
;
3714 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3716 ptr
= qemu_get_ram_ptr(addr1
);
3718 if (!cpu_physical_memory_is_dirty(addr1
)) {
3719 /* invalidate code */
3720 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3722 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3723 (0xff & ~CODE_DIRTY_FLAG
);
3729 void stb_phys(target_phys_addr_t addr
, uint32_t val
)
3732 cpu_physical_memory_write(addr
, &v
, 1);
3736 void stw_phys(target_phys_addr_t addr
, uint32_t val
)
3738 uint16_t v
= tswap16(val
);
3739 cpu_physical_memory_write(addr
, (const uint8_t *)&v
, 2);
3743 void stq_phys(target_phys_addr_t addr
, uint64_t val
)
3746 cpu_physical_memory_write(addr
, (const uint8_t *)&val
, 8);
3751 /* virtual memory access for debug (includes writing to ROM) */
3752 int cpu_memory_rw_debug(CPUState
*env
, target_ulong addr
,
3753 uint8_t *buf
, int len
, int is_write
)
3756 target_phys_addr_t phys_addr
;
3760 page
= addr
& TARGET_PAGE_MASK
;
3761 phys_addr
= cpu_get_phys_page_debug(env
, page
);
3762 /* if no physical page mapped, return an error */
3763 if (phys_addr
== -1)
3765 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3768 phys_addr
+= (addr
& ~TARGET_PAGE_MASK
);
3769 #if !defined(CONFIG_USER_ONLY)
3771 cpu_physical_memory_write_rom(phys_addr
, buf
, l
);
3774 cpu_physical_memory_rw(phys_addr
, buf
, l
, is_write
);
3782 /* in deterministic execution mode, instructions doing device I/Os
3783 must be at the end of the TB */
3784 void cpu_io_recompile(CPUState
*env
, void *retaddr
)
3786 TranslationBlock
*tb
;
3788 target_ulong pc
, cs_base
;
3791 tb
= tb_find_pc((unsigned long)retaddr
);
3793 cpu_abort(env
, "cpu_io_recompile: could not find TB for pc=%p",
3796 n
= env
->icount_decr
.u16
.low
+ tb
->icount
;
3797 cpu_restore_state(tb
, env
, (unsigned long)retaddr
, NULL
);
3798 /* Calculate how many instructions had been executed before the fault
3800 n
= n
- env
->icount_decr
.u16
.low
;
3801 /* Generate a new TB ending on the I/O insn. */
3803 /* On MIPS and SH, delay slot instructions can only be restarted if
3804 they were already the first instruction in the TB. If this is not
3805 the first instruction in a TB then re-execute the preceding
3807 #if defined(TARGET_MIPS)
3808 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0 && n
> 1) {
3809 env
->active_tc
.PC
-= 4;
3810 env
->icount_decr
.u16
.low
++;
3811 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
3813 #elif defined(TARGET_SH4)
3814 if ((env
->flags
& ((DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
))) != 0
3817 env
->icount_decr
.u16
.low
++;
3818 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
);
3821 /* This should never happen. */
3822 if (n
> CF_COUNT_MASK
)
3823 cpu_abort(env
, "TB too big during recompile");
3825 cflags
= n
| CF_LAST_IO
;
3827 cs_base
= tb
->cs_base
;
3829 tb_phys_invalidate(tb
, -1);
3830 /* FIXME: In theory this could raise an exception. In practice
3831 we have already translated the block once so it's probably ok. */
3832 tb_gen_code(env
, pc
, cs_base
, flags
, cflags
);
3833 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3834 the first in the TB) then we end up generating a whole new TB and
3835 repeating the fault, which is horribly inefficient.
3836 Better would be to execute just this insn uncached, or generate a
3838 cpu_resume_from_signal(env
, NULL
);
3841 void dump_exec_info(FILE *f
,
3842 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
3844 int i
, target_code_size
, max_target_code_size
;
3845 int direct_jmp_count
, direct_jmp2_count
, cross_page
;
3846 TranslationBlock
*tb
;
3848 target_code_size
= 0;
3849 max_target_code_size
= 0;
3851 direct_jmp_count
= 0;
3852 direct_jmp2_count
= 0;
3853 for(i
= 0; i
< nb_tbs
; i
++) {
3855 target_code_size
+= tb
->size
;
3856 if (tb
->size
> max_target_code_size
)
3857 max_target_code_size
= tb
->size
;
3858 if (tb
->page_addr
[1] != -1)
3860 if (tb
->tb_next_offset
[0] != 0xffff) {
3862 if (tb
->tb_next_offset
[1] != 0xffff) {
3863 direct_jmp2_count
++;
3867 /* XXX: avoid using doubles ? */
3868 cpu_fprintf(f
, "Translation buffer state:\n");
3869 cpu_fprintf(f
, "gen code size %ld/%ld\n",
3870 code_gen_ptr
- code_gen_buffer
, code_gen_buffer_max_size
);
3871 cpu_fprintf(f
, "TB count %d/%d\n",
3872 nb_tbs
, code_gen_max_blocks
);
3873 cpu_fprintf(f
, "TB avg target size %d max=%d bytes\n",
3874 nb_tbs
? target_code_size
/ nb_tbs
: 0,
3875 max_target_code_size
);
3876 cpu_fprintf(f
, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3877 nb_tbs
? (code_gen_ptr
- code_gen_buffer
) / nb_tbs
: 0,
3878 target_code_size
? (double) (code_gen_ptr
- code_gen_buffer
) / target_code_size
: 0);
3879 cpu_fprintf(f
, "cross page TB count %d (%d%%)\n",
3881 nb_tbs
? (cross_page
* 100) / nb_tbs
: 0);
3882 cpu_fprintf(f
, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3884 nb_tbs
? (direct_jmp_count
* 100) / nb_tbs
: 0,
3886 nb_tbs
? (direct_jmp2_count
* 100) / nb_tbs
: 0);
3887 cpu_fprintf(f
, "\nStatistics:\n");
3888 cpu_fprintf(f
, "TB flush count %d\n", tb_flush_count
);
3889 cpu_fprintf(f
, "TB invalidate count %d\n", tb_phys_invalidate_count
);
3890 cpu_fprintf(f
, "TLB flush count %d\n", tlb_flush_count
);
3891 tcg_dump_info(f
, cpu_fprintf
);
3894 #if !defined(CONFIG_USER_ONLY)
3896 #define MMUSUFFIX _cmmu
3897 #define GETPC() NULL
3898 #define env cpu_single_env
3899 #define SOFTMMU_CODE_ACCESS
3902 #include "softmmu_template.h"
3905 #include "softmmu_template.h"
3908 #include "softmmu_template.h"
3911 #include "softmmu_template.h"