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"
41 #if defined(CONFIG_USER_ONLY)
45 //#define DEBUG_TB_INVALIDATE
48 //#define DEBUG_UNASSIGNED
50 /* make various TB consistency checks */
51 //#define DEBUG_TB_CHECK
52 //#define DEBUG_TLB_CHECK
54 //#define DEBUG_IOPORT
55 //#define DEBUG_SUBPAGE
57 #if !defined(CONFIG_USER_ONLY)
58 /* TB consistency checks only implemented for usermode emulation. */
62 #define SMC_BITMAP_USE_THRESHOLD 10
64 #if defined(TARGET_SPARC64)
65 #define TARGET_PHYS_ADDR_SPACE_BITS 41
66 #elif defined(TARGET_SPARC)
67 #define TARGET_PHYS_ADDR_SPACE_BITS 36
68 #elif defined(TARGET_ALPHA)
69 #define TARGET_PHYS_ADDR_SPACE_BITS 42
70 #define TARGET_VIRT_ADDR_SPACE_BITS 42
71 #elif defined(TARGET_PPC64)
72 #define TARGET_PHYS_ADDR_SPACE_BITS 42
73 #elif defined(TARGET_X86_64)
74 #define TARGET_PHYS_ADDR_SPACE_BITS 42
75 #elif defined(TARGET_I386)
76 #define TARGET_PHYS_ADDR_SPACE_BITS 36
78 #define TARGET_PHYS_ADDR_SPACE_BITS 32
81 static TranslationBlock
*tbs
;
82 int code_gen_max_blocks
;
83 TranslationBlock
*tb_phys_hash
[CODE_GEN_PHYS_HASH_SIZE
];
85 /* any access to the tbs or the page table must use this lock */
86 spinlock_t tb_lock
= SPIN_LOCK_UNLOCKED
;
88 #if defined(__arm__) || defined(__sparc_v9__)
89 /* The prologue must be reachable with a direct jump. ARM and Sparc64
90 have limited branch ranges (possibly also PPC) so place it in a
91 section close to code segment. */
92 #define code_gen_section \
93 __attribute__((__section__(".gen_code"))) \
94 __attribute__((aligned (32)))
96 /* Maximum alignment for Win32 is 16. */
97 #define code_gen_section \
98 __attribute__((aligned (16)))
100 #define code_gen_section \
101 __attribute__((aligned (32)))
104 uint8_t code_gen_prologue
[1024] code_gen_section
;
105 static uint8_t *code_gen_buffer
;
106 static unsigned long code_gen_buffer_size
;
107 /* threshold to flush the translated code buffer */
108 static unsigned long code_gen_buffer_max_size
;
109 uint8_t *code_gen_ptr
;
111 #if !defined(CONFIG_USER_ONLY)
113 uint8_t *phys_ram_dirty
;
114 static int in_migration
;
116 typedef struct RAMBlock
{
120 struct RAMBlock
*next
;
123 static RAMBlock
*ram_blocks
;
124 /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
125 then we can no longer assume contiguous ram offsets, and external uses
126 of this variable will break. */
127 ram_addr_t last_ram_offset
;
131 /* current CPU in the current thread. It is only valid inside
133 CPUState
*cpu_single_env
;
134 /* 0 = Do not count executed instructions.
135 1 = Precise instruction counting.
136 2 = Adaptive rate instruction counting. */
138 /* Current instruction counter. While executing translated code this may
139 include some instructions that have not yet been executed. */
142 typedef struct PageDesc
{
143 /* list of TBs intersecting this ram page */
144 TranslationBlock
*first_tb
;
145 /* in order to optimize self modifying code, we count the number
146 of lookups we do to a given page to use a bitmap */
147 unsigned int code_write_count
;
148 uint8_t *code_bitmap
;
149 #if defined(CONFIG_USER_ONLY)
154 typedef struct PhysPageDesc
{
155 /* offset in host memory of the page + io_index in the low bits */
156 ram_addr_t phys_offset
;
157 ram_addr_t region_offset
;
161 #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
162 /* XXX: this is a temporary hack for alpha target.
163 * In the future, this is to be replaced by a multi-level table
164 * to actually be able to handle the complete 64 bits address space.
166 #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
168 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
171 #define L1_SIZE (1 << L1_BITS)
172 #define L2_SIZE (1 << L2_BITS)
174 unsigned long qemu_real_host_page_size
;
175 unsigned long qemu_host_page_bits
;
176 unsigned long qemu_host_page_size
;
177 unsigned long qemu_host_page_mask
;
179 /* XXX: for system emulation, it could just be an array */
180 static PageDesc
*l1_map
[L1_SIZE
];
181 static PhysPageDesc
**l1_phys_map
;
183 #if !defined(CONFIG_USER_ONLY)
184 static void io_mem_init(void);
186 /* io memory support */
187 CPUWriteMemoryFunc
*io_mem_write
[IO_MEM_NB_ENTRIES
][4];
188 CPUReadMemoryFunc
*io_mem_read
[IO_MEM_NB_ENTRIES
][4];
189 void *io_mem_opaque
[IO_MEM_NB_ENTRIES
];
190 static char io_mem_used
[IO_MEM_NB_ENTRIES
];
191 static int io_mem_watch
;
196 static const char *logfilename
= "qemu.log";
198 static const char *logfilename
= "/tmp/qemu.log";
202 static int log_append
= 0;
205 static int tlb_flush_count
;
206 static int tb_flush_count
;
207 static int tb_phys_invalidate_count
;
209 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
210 typedef struct subpage_t
{
211 target_phys_addr_t base
;
212 CPUReadMemoryFunc
* const *mem_read
[TARGET_PAGE_SIZE
][4];
213 CPUWriteMemoryFunc
* const *mem_write
[TARGET_PAGE_SIZE
][4];
214 void *opaque
[TARGET_PAGE_SIZE
][2][4];
215 ram_addr_t region_offset
[TARGET_PAGE_SIZE
][2][4];
219 static void map_exec(void *addr
, long size
)
222 VirtualProtect(addr
, size
,
223 PAGE_EXECUTE_READWRITE
, &old_protect
);
227 static void map_exec(void *addr
, long size
)
229 unsigned long start
, end
, page_size
;
231 page_size
= getpagesize();
232 start
= (unsigned long)addr
;
233 start
&= ~(page_size
- 1);
235 end
= (unsigned long)addr
+ size
;
236 end
+= page_size
- 1;
237 end
&= ~(page_size
- 1);
239 mprotect((void *)start
, end
- start
,
240 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
244 static void page_init(void)
246 /* NOTE: we can always suppose that qemu_host_page_size >=
250 SYSTEM_INFO system_info
;
252 GetSystemInfo(&system_info
);
253 qemu_real_host_page_size
= system_info
.dwPageSize
;
256 qemu_real_host_page_size
= getpagesize();
258 if (qemu_host_page_size
== 0)
259 qemu_host_page_size
= qemu_real_host_page_size
;
260 if (qemu_host_page_size
< TARGET_PAGE_SIZE
)
261 qemu_host_page_size
= TARGET_PAGE_SIZE
;
262 qemu_host_page_bits
= 0;
263 while ((1 << qemu_host_page_bits
) < qemu_host_page_size
)
264 qemu_host_page_bits
++;
265 qemu_host_page_mask
= ~(qemu_host_page_size
- 1);
266 l1_phys_map
= qemu_vmalloc(L1_SIZE
* sizeof(void *));
267 memset(l1_phys_map
, 0, L1_SIZE
* sizeof(void *));
269 #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
271 long long startaddr
, endaddr
;
276 last_brk
= (unsigned long)sbrk(0);
277 f
= fopen("/proc/self/maps", "r");
280 n
= fscanf (f
, "%llx-%llx %*[^\n]\n", &startaddr
, &endaddr
);
282 startaddr
= MIN(startaddr
,
283 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
284 endaddr
= MIN(endaddr
,
285 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
286 page_set_flags(startaddr
& TARGET_PAGE_MASK
,
287 TARGET_PAGE_ALIGN(endaddr
),
298 static inline PageDesc
**page_l1_map(target_ulong index
)
300 #if TARGET_LONG_BITS > 32
301 /* Host memory outside guest VM. For 32-bit targets we have already
302 excluded high addresses. */
303 if (index
> ((target_ulong
)L2_SIZE
* L1_SIZE
))
306 return &l1_map
[index
>> L2_BITS
];
309 static inline PageDesc
*page_find_alloc(target_ulong index
)
312 lp
= page_l1_map(index
);
318 /* allocate if not found */
319 #if defined(CONFIG_USER_ONLY)
320 size_t len
= sizeof(PageDesc
) * L2_SIZE
;
321 /* Don't use qemu_malloc because it may recurse. */
322 p
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
,
323 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
326 unsigned long addr
= h2g(p
);
327 page_set_flags(addr
& TARGET_PAGE_MASK
,
328 TARGET_PAGE_ALIGN(addr
+ len
),
332 p
= qemu_mallocz(sizeof(PageDesc
) * L2_SIZE
);
336 return p
+ (index
& (L2_SIZE
- 1));
339 static inline PageDesc
*page_find(target_ulong index
)
342 lp
= page_l1_map(index
);
350 return p
+ (index
& (L2_SIZE
- 1));
353 static PhysPageDesc
*phys_page_find_alloc(target_phys_addr_t index
, int alloc
)
358 p
= (void **)l1_phys_map
;
359 #if TARGET_PHYS_ADDR_SPACE_BITS > 32
361 #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
362 #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
364 lp
= p
+ ((index
>> (L1_BITS
+ L2_BITS
)) & (L1_SIZE
- 1));
367 /* allocate if not found */
370 p
= qemu_vmalloc(sizeof(void *) * L1_SIZE
);
371 memset(p
, 0, sizeof(void *) * L1_SIZE
);
375 lp
= p
+ ((index
>> L2_BITS
) & (L1_SIZE
- 1));
379 /* allocate if not found */
382 pd
= qemu_vmalloc(sizeof(PhysPageDesc
) * L2_SIZE
);
384 for (i
= 0; i
< L2_SIZE
; i
++) {
385 pd
[i
].phys_offset
= IO_MEM_UNASSIGNED
;
386 pd
[i
].region_offset
= (index
+ i
) << TARGET_PAGE_BITS
;
389 return ((PhysPageDesc
*)pd
) + (index
& (L2_SIZE
- 1));
392 static inline PhysPageDesc
*phys_page_find(target_phys_addr_t index
)
394 return phys_page_find_alloc(index
, 0);
397 #if !defined(CONFIG_USER_ONLY)
398 static void tlb_protect_code(ram_addr_t ram_addr
);
399 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
401 #define mmap_lock() do { } while(0)
402 #define mmap_unlock() do { } while(0)
405 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
407 #if defined(CONFIG_USER_ONLY)
408 /* Currently it is not recommended to allocate big chunks of data in
409 user mode. It will change when a dedicated libc will be used */
410 #define USE_STATIC_CODE_GEN_BUFFER
413 #ifdef USE_STATIC_CODE_GEN_BUFFER
414 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
];
417 static void code_gen_alloc(unsigned long tb_size
)
419 #ifdef USE_STATIC_CODE_GEN_BUFFER
420 code_gen_buffer
= static_code_gen_buffer
;
421 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
422 map_exec(code_gen_buffer
, code_gen_buffer_size
);
424 code_gen_buffer_size
= tb_size
;
425 if (code_gen_buffer_size
== 0) {
426 #if defined(CONFIG_USER_ONLY)
427 /* in user mode, phys_ram_size is not meaningful */
428 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
430 /* XXX: needs adjustments */
431 code_gen_buffer_size
= (unsigned long)(ram_size
/ 4);
434 if (code_gen_buffer_size
< MIN_CODE_GEN_BUFFER_SIZE
)
435 code_gen_buffer_size
= MIN_CODE_GEN_BUFFER_SIZE
;
436 /* The code gen buffer location may have constraints depending on
437 the host cpu and OS */
438 #if defined(__linux__)
443 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
444 #if defined(__x86_64__)
446 /* Cannot map more than that */
447 if (code_gen_buffer_size
> (800 * 1024 * 1024))
448 code_gen_buffer_size
= (800 * 1024 * 1024);
449 #elif defined(__sparc_v9__)
450 // Map the buffer below 2G, so we can use direct calls and branches
452 start
= (void *) 0x60000000UL
;
453 if (code_gen_buffer_size
> (512 * 1024 * 1024))
454 code_gen_buffer_size
= (512 * 1024 * 1024);
455 #elif defined(__arm__)
456 /* Map the buffer below 32M, so we can use direct calls and branches */
458 start
= (void *) 0x01000000UL
;
459 if (code_gen_buffer_size
> 16 * 1024 * 1024)
460 code_gen_buffer_size
= 16 * 1024 * 1024;
462 code_gen_buffer
= mmap(start
, code_gen_buffer_size
,
463 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
465 if (code_gen_buffer
== MAP_FAILED
) {
466 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
470 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
474 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
475 #if defined(__x86_64__)
476 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
477 * 0x40000000 is free */
479 addr
= (void *)0x40000000;
480 /* Cannot map more than that */
481 if (code_gen_buffer_size
> (800 * 1024 * 1024))
482 code_gen_buffer_size
= (800 * 1024 * 1024);
484 code_gen_buffer
= mmap(addr
, code_gen_buffer_size
,
485 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
487 if (code_gen_buffer
== MAP_FAILED
) {
488 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
493 code_gen_buffer
= qemu_malloc(code_gen_buffer_size
);
494 map_exec(code_gen_buffer
, code_gen_buffer_size
);
496 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
497 map_exec(code_gen_prologue
, sizeof(code_gen_prologue
));
498 code_gen_buffer_max_size
= code_gen_buffer_size
-
499 code_gen_max_block_size();
500 code_gen_max_blocks
= code_gen_buffer_size
/ CODE_GEN_AVG_BLOCK_SIZE
;
501 tbs
= qemu_malloc(code_gen_max_blocks
* sizeof(TranslationBlock
));
504 /* Must be called before using the QEMU cpus. 'tb_size' is the size
505 (in bytes) allocated to the translation buffer. Zero means default
507 void cpu_exec_init_all(unsigned long tb_size
)
510 code_gen_alloc(tb_size
);
511 code_gen_ptr
= code_gen_buffer
;
513 #if !defined(CONFIG_USER_ONLY)
518 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
520 static void cpu_common_pre_save(void *opaque
)
522 CPUState
*env
= opaque
;
524 cpu_synchronize_state(env
);
527 static int cpu_common_pre_load(void *opaque
)
529 CPUState
*env
= opaque
;
531 cpu_synchronize_state(env
);
535 static int cpu_common_post_load(void *opaque
, int version_id
)
537 CPUState
*env
= opaque
;
539 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
540 version_id is increased. */
541 env
->interrupt_request
&= ~0x01;
547 static const VMStateDescription vmstate_cpu_common
= {
548 .name
= "cpu_common",
550 .minimum_version_id
= 1,
551 .minimum_version_id_old
= 1,
552 .pre_save
= cpu_common_pre_save
,
553 .pre_load
= cpu_common_pre_load
,
554 .post_load
= cpu_common_post_load
,
555 .fields
= (VMStateField
[]) {
556 VMSTATE_UINT32(halted
, CPUState
),
557 VMSTATE_UINT32(interrupt_request
, CPUState
),
558 VMSTATE_END_OF_LIST()
563 CPUState
*qemu_get_cpu(int cpu
)
565 CPUState
*env
= first_cpu
;
568 if (env
->cpu_index
== cpu
)
576 void cpu_exec_init(CPUState
*env
)
581 #if defined(CONFIG_USER_ONLY)
584 env
->next_cpu
= NULL
;
587 while (*penv
!= NULL
) {
588 penv
= &(*penv
)->next_cpu
;
591 env
->cpu_index
= cpu_index
;
593 QTAILQ_INIT(&env
->breakpoints
);
594 QTAILQ_INIT(&env
->watchpoints
);
596 #if defined(CONFIG_USER_ONLY)
599 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
600 vmstate_register(cpu_index
, &vmstate_cpu_common
, env
);
601 register_savevm("cpu", cpu_index
, CPU_SAVE_VERSION
,
602 cpu_save
, cpu_load
, env
);
606 static inline void invalidate_page_bitmap(PageDesc
*p
)
608 if (p
->code_bitmap
) {
609 qemu_free(p
->code_bitmap
);
610 p
->code_bitmap
= NULL
;
612 p
->code_write_count
= 0;
615 /* set to NULL all the 'first_tb' fields in all PageDescs */
616 static void page_flush_tb(void)
621 for(i
= 0; i
< L1_SIZE
; i
++) {
624 for(j
= 0; j
< L2_SIZE
; j
++) {
626 invalidate_page_bitmap(p
);
633 /* flush all the translation blocks */
634 /* XXX: tb_flush is currently not thread safe */
635 void tb_flush(CPUState
*env1
)
638 #if defined(DEBUG_FLUSH)
639 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
640 (unsigned long)(code_gen_ptr
- code_gen_buffer
),
642 ((unsigned long)(code_gen_ptr
- code_gen_buffer
)) / nb_tbs
: 0);
644 if ((unsigned long)(code_gen_ptr
- code_gen_buffer
) > code_gen_buffer_size
)
645 cpu_abort(env1
, "Internal error: code buffer overflow\n");
649 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
650 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
653 memset (tb_phys_hash
, 0, CODE_GEN_PHYS_HASH_SIZE
* sizeof (void *));
656 code_gen_ptr
= code_gen_buffer
;
657 /* XXX: flush processor icache at this point if cache flush is
662 #ifdef DEBUG_TB_CHECK
664 static void tb_invalidate_check(target_ulong address
)
666 TranslationBlock
*tb
;
668 address
&= TARGET_PAGE_MASK
;
669 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
670 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
671 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
672 address
>= tb
->pc
+ tb
->size
)) {
673 printf("ERROR invalidate: address=" TARGET_FMT_lx
674 " PC=%08lx size=%04x\n",
675 address
, (long)tb
->pc
, tb
->size
);
681 /* verify that all the pages have correct rights for code */
682 static void tb_page_check(void)
684 TranslationBlock
*tb
;
685 int i
, flags1
, flags2
;
687 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
688 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
689 flags1
= page_get_flags(tb
->pc
);
690 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
691 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
692 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
693 (long)tb
->pc
, tb
->size
, flags1
, flags2
);
701 /* invalidate one TB */
702 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
705 TranslationBlock
*tb1
;
709 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
712 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
716 static inline void tb_page_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
718 TranslationBlock
*tb1
;
724 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
726 *ptb
= tb1
->page_next
[n1
];
729 ptb
= &tb1
->page_next
[n1
];
733 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
735 TranslationBlock
*tb1
, **ptb
;
738 ptb
= &tb
->jmp_next
[n
];
741 /* find tb(n) in circular list */
745 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
746 if (n1
== n
&& tb1
== tb
)
749 ptb
= &tb1
->jmp_first
;
751 ptb
= &tb1
->jmp_next
[n1
];
754 /* now we can suppress tb(n) from the list */
755 *ptb
= tb
->jmp_next
[n
];
757 tb
->jmp_next
[n
] = NULL
;
761 /* reset the jump entry 'n' of a TB so that it is not chained to
763 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
765 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
768 void tb_phys_invalidate(TranslationBlock
*tb
, target_ulong page_addr
)
773 target_phys_addr_t phys_pc
;
774 TranslationBlock
*tb1
, *tb2
;
776 /* remove the TB from the hash list */
777 phys_pc
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
778 h
= tb_phys_hash_func(phys_pc
);
779 tb_remove(&tb_phys_hash
[h
], tb
,
780 offsetof(TranslationBlock
, phys_hash_next
));
782 /* remove the TB from the page list */
783 if (tb
->page_addr
[0] != page_addr
) {
784 p
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
785 tb_page_remove(&p
->first_tb
, tb
);
786 invalidate_page_bitmap(p
);
788 if (tb
->page_addr
[1] != -1 && tb
->page_addr
[1] != page_addr
) {
789 p
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
790 tb_page_remove(&p
->first_tb
, tb
);
791 invalidate_page_bitmap(p
);
794 tb_invalidated_flag
= 1;
796 /* remove the TB from the hash list */
797 h
= tb_jmp_cache_hash_func(tb
->pc
);
798 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
799 if (env
->tb_jmp_cache
[h
] == tb
)
800 env
->tb_jmp_cache
[h
] = NULL
;
803 /* suppress this TB from the two jump lists */
804 tb_jmp_remove(tb
, 0);
805 tb_jmp_remove(tb
, 1);
807 /* suppress any remaining jumps to this TB */
813 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
814 tb2
= tb1
->jmp_next
[n1
];
815 tb_reset_jump(tb1
, n1
);
816 tb1
->jmp_next
[n1
] = NULL
;
819 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
821 tb_phys_invalidate_count
++;
824 static inline void set_bits(uint8_t *tab
, int start
, int len
)
830 mask
= 0xff << (start
& 7);
831 if ((start
& ~7) == (end
& ~7)) {
833 mask
&= ~(0xff << (end
& 7));
838 start
= (start
+ 8) & ~7;
840 while (start
< end1
) {
845 mask
= ~(0xff << (end
& 7));
851 static void build_page_bitmap(PageDesc
*p
)
853 int n
, tb_start
, tb_end
;
854 TranslationBlock
*tb
;
856 p
->code_bitmap
= qemu_mallocz(TARGET_PAGE_SIZE
/ 8);
861 tb
= (TranslationBlock
*)((long)tb
& ~3);
862 /* NOTE: this is subtle as a TB may span two physical pages */
864 /* NOTE: tb_end may be after the end of the page, but
865 it is not a problem */
866 tb_start
= tb
->pc
& ~TARGET_PAGE_MASK
;
867 tb_end
= tb_start
+ tb
->size
;
868 if (tb_end
> TARGET_PAGE_SIZE
)
869 tb_end
= TARGET_PAGE_SIZE
;
872 tb_end
= ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
874 set_bits(p
->code_bitmap
, tb_start
, tb_end
- tb_start
);
875 tb
= tb
->page_next
[n
];
879 TranslationBlock
*tb_gen_code(CPUState
*env
,
880 target_ulong pc
, target_ulong cs_base
,
881 int flags
, int cflags
)
883 TranslationBlock
*tb
;
885 target_ulong phys_pc
, phys_page2
, virt_page2
;
888 phys_pc
= get_phys_addr_code(env
, pc
);
891 /* flush must be done */
893 /* cannot fail at this point */
895 /* Don't forget to invalidate previous TB info. */
896 tb_invalidated_flag
= 1;
898 tc_ptr
= code_gen_ptr
;
900 tb
->cs_base
= cs_base
;
903 cpu_gen_code(env
, tb
, &code_gen_size
);
904 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
906 /* check next page if needed */
907 virt_page2
= (pc
+ tb
->size
- 1) & TARGET_PAGE_MASK
;
909 if ((pc
& TARGET_PAGE_MASK
) != virt_page2
) {
910 phys_page2
= get_phys_addr_code(env
, virt_page2
);
912 tb_link_phys(tb
, phys_pc
, phys_page2
);
916 /* invalidate all TBs which intersect with the target physical page
917 starting in range [start;end[. NOTE: start and end must refer to
918 the same physical page. 'is_cpu_write_access' should be true if called
919 from a real cpu write access: the virtual CPU will exit the current
920 TB if code is modified inside this TB. */
921 void tb_invalidate_phys_page_range(target_phys_addr_t start
, target_phys_addr_t end
,
922 int is_cpu_write_access
)
924 TranslationBlock
*tb
, *tb_next
, *saved_tb
;
925 CPUState
*env
= cpu_single_env
;
926 target_ulong tb_start
, tb_end
;
929 #ifdef TARGET_HAS_PRECISE_SMC
930 int current_tb_not_found
= is_cpu_write_access
;
931 TranslationBlock
*current_tb
= NULL
;
932 int current_tb_modified
= 0;
933 target_ulong current_pc
= 0;
934 target_ulong current_cs_base
= 0;
935 int current_flags
= 0;
936 #endif /* TARGET_HAS_PRECISE_SMC */
938 p
= page_find(start
>> TARGET_PAGE_BITS
);
941 if (!p
->code_bitmap
&&
942 ++p
->code_write_count
>= SMC_BITMAP_USE_THRESHOLD
&&
943 is_cpu_write_access
) {
944 /* build code bitmap */
945 build_page_bitmap(p
);
948 /* we remove all the TBs in the range [start, end[ */
949 /* XXX: see if in some cases it could be faster to invalidate all the code */
953 tb
= (TranslationBlock
*)((long)tb
& ~3);
954 tb_next
= tb
->page_next
[n
];
955 /* NOTE: this is subtle as a TB may span two physical pages */
957 /* NOTE: tb_end may be after the end of the page, but
958 it is not a problem */
959 tb_start
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
960 tb_end
= tb_start
+ tb
->size
;
962 tb_start
= tb
->page_addr
[1];
963 tb_end
= tb_start
+ ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
965 if (!(tb_end
<= start
|| tb_start
>= end
)) {
966 #ifdef TARGET_HAS_PRECISE_SMC
967 if (current_tb_not_found
) {
968 current_tb_not_found
= 0;
970 if (env
->mem_io_pc
) {
971 /* now we have a real cpu fault */
972 current_tb
= tb_find_pc(env
->mem_io_pc
);
975 if (current_tb
== tb
&&
976 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
977 /* If we are modifying the current TB, we must stop
978 its execution. We could be more precise by checking
979 that the modification is after the current PC, but it
980 would require a specialized function to partially
981 restore the CPU state */
983 current_tb_modified
= 1;
984 cpu_restore_state(current_tb
, env
,
985 env
->mem_io_pc
, NULL
);
986 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
989 #endif /* TARGET_HAS_PRECISE_SMC */
990 /* we need to do that to handle the case where a signal
991 occurs while doing tb_phys_invalidate() */
994 saved_tb
= env
->current_tb
;
995 env
->current_tb
= NULL
;
997 tb_phys_invalidate(tb
, -1);
999 env
->current_tb
= saved_tb
;
1000 if (env
->interrupt_request
&& env
->current_tb
)
1001 cpu_interrupt(env
, env
->interrupt_request
);
1006 #if !defined(CONFIG_USER_ONLY)
1007 /* if no code remaining, no need to continue to use slow writes */
1009 invalidate_page_bitmap(p
);
1010 if (is_cpu_write_access
) {
1011 tlb_unprotect_code_phys(env
, start
, env
->mem_io_vaddr
);
1015 #ifdef TARGET_HAS_PRECISE_SMC
1016 if (current_tb_modified
) {
1017 /* we generate a block containing just the instruction
1018 modifying the memory. It will ensure that it cannot modify
1020 env
->current_tb
= NULL
;
1021 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1022 cpu_resume_from_signal(env
, NULL
);
1027 /* len must be <= 8 and start must be a multiple of len */
1028 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start
, int len
)
1034 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1035 cpu_single_env
->mem_io_vaddr
, len
,
1036 cpu_single_env
->eip
,
1037 cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
);
1040 p
= page_find(start
>> TARGET_PAGE_BITS
);
1043 if (p
->code_bitmap
) {
1044 offset
= start
& ~TARGET_PAGE_MASK
;
1045 b
= p
->code_bitmap
[offset
>> 3] >> (offset
& 7);
1046 if (b
& ((1 << len
) - 1))
1050 tb_invalidate_phys_page_range(start
, start
+ len
, 1);
1054 #if !defined(CONFIG_SOFTMMU)
1055 static void tb_invalidate_phys_page(target_phys_addr_t addr
,
1056 unsigned long pc
, void *puc
)
1058 TranslationBlock
*tb
;
1061 #ifdef TARGET_HAS_PRECISE_SMC
1062 TranslationBlock
*current_tb
= NULL
;
1063 CPUState
*env
= cpu_single_env
;
1064 int current_tb_modified
= 0;
1065 target_ulong current_pc
= 0;
1066 target_ulong current_cs_base
= 0;
1067 int current_flags
= 0;
1070 addr
&= TARGET_PAGE_MASK
;
1071 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1075 #ifdef TARGET_HAS_PRECISE_SMC
1076 if (tb
&& pc
!= 0) {
1077 current_tb
= tb_find_pc(pc
);
1080 while (tb
!= NULL
) {
1082 tb
= (TranslationBlock
*)((long)tb
& ~3);
1083 #ifdef TARGET_HAS_PRECISE_SMC
1084 if (current_tb
== tb
&&
1085 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1086 /* If we are modifying the current TB, we must stop
1087 its execution. We could be more precise by checking
1088 that the modification is after the current PC, but it
1089 would require a specialized function to partially
1090 restore the CPU state */
1092 current_tb_modified
= 1;
1093 cpu_restore_state(current_tb
, env
, pc
, puc
);
1094 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1097 #endif /* TARGET_HAS_PRECISE_SMC */
1098 tb_phys_invalidate(tb
, addr
);
1099 tb
= tb
->page_next
[n
];
1102 #ifdef TARGET_HAS_PRECISE_SMC
1103 if (current_tb_modified
) {
1104 /* we generate a block containing just the instruction
1105 modifying the memory. It will ensure that it cannot modify
1107 env
->current_tb
= NULL
;
1108 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1109 cpu_resume_from_signal(env
, puc
);
1115 /* add the tb in the target page and protect it if necessary */
1116 static inline void tb_alloc_page(TranslationBlock
*tb
,
1117 unsigned int n
, target_ulong page_addr
)
1120 TranslationBlock
*last_first_tb
;
1122 tb
->page_addr
[n
] = page_addr
;
1123 p
= page_find_alloc(page_addr
>> TARGET_PAGE_BITS
);
1124 tb
->page_next
[n
] = p
->first_tb
;
1125 last_first_tb
= p
->first_tb
;
1126 p
->first_tb
= (TranslationBlock
*)((long)tb
| n
);
1127 invalidate_page_bitmap(p
);
1129 #if defined(TARGET_HAS_SMC) || 1
1131 #if defined(CONFIG_USER_ONLY)
1132 if (p
->flags
& PAGE_WRITE
) {
1137 /* force the host page as non writable (writes will have a
1138 page fault + mprotect overhead) */
1139 page_addr
&= qemu_host_page_mask
;
1141 for(addr
= page_addr
; addr
< page_addr
+ qemu_host_page_size
;
1142 addr
+= TARGET_PAGE_SIZE
) {
1144 p2
= page_find (addr
>> TARGET_PAGE_BITS
);
1148 p2
->flags
&= ~PAGE_WRITE
;
1149 page_get_flags(addr
);
1151 mprotect(g2h(page_addr
), qemu_host_page_size
,
1152 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
1153 #ifdef DEBUG_TB_INVALIDATE
1154 printf("protecting code page: 0x" TARGET_FMT_lx
"\n",
1159 /* if some code is already present, then the pages are already
1160 protected. So we handle the case where only the first TB is
1161 allocated in a physical page */
1162 if (!last_first_tb
) {
1163 tlb_protect_code(page_addr
);
1167 #endif /* TARGET_HAS_SMC */
1170 /* Allocate a new translation block. Flush the translation buffer if
1171 too many translation blocks or too much generated code. */
1172 TranslationBlock
*tb_alloc(target_ulong pc
)
1174 TranslationBlock
*tb
;
1176 if (nb_tbs
>= code_gen_max_blocks
||
1177 (code_gen_ptr
- code_gen_buffer
) >= code_gen_buffer_max_size
)
1179 tb
= &tbs
[nb_tbs
++];
1185 void tb_free(TranslationBlock
*tb
)
1187 /* In practice this is mostly used for single use temporary TB
1188 Ignore the hard cases and just back up if this TB happens to
1189 be the last one generated. */
1190 if (nb_tbs
> 0 && tb
== &tbs
[nb_tbs
- 1]) {
1191 code_gen_ptr
= tb
->tc_ptr
;
1196 /* add a new TB and link it to the physical page tables. phys_page2 is
1197 (-1) to indicate that only one page contains the TB. */
1198 void tb_link_phys(TranslationBlock
*tb
,
1199 target_ulong phys_pc
, target_ulong phys_page2
)
1202 TranslationBlock
**ptb
;
1204 /* Grab the mmap lock to stop another thread invalidating this TB
1205 before we are done. */
1207 /* add in the physical hash table */
1208 h
= tb_phys_hash_func(phys_pc
);
1209 ptb
= &tb_phys_hash
[h
];
1210 tb
->phys_hash_next
= *ptb
;
1213 /* add in the page list */
1214 tb_alloc_page(tb
, 0, phys_pc
& TARGET_PAGE_MASK
);
1215 if (phys_page2
!= -1)
1216 tb_alloc_page(tb
, 1, phys_page2
);
1218 tb
->page_addr
[1] = -1;
1220 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
1221 tb
->jmp_next
[0] = NULL
;
1222 tb
->jmp_next
[1] = NULL
;
1224 /* init original jump addresses */
1225 if (tb
->tb_next_offset
[0] != 0xffff)
1226 tb_reset_jump(tb
, 0);
1227 if (tb
->tb_next_offset
[1] != 0xffff)
1228 tb_reset_jump(tb
, 1);
1230 #ifdef DEBUG_TB_CHECK
1236 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1237 tb[1].tc_ptr. Return NULL if not found */
1238 TranslationBlock
*tb_find_pc(unsigned long tc_ptr
)
1240 int m_min
, m_max
, m
;
1242 TranslationBlock
*tb
;
1246 if (tc_ptr
< (unsigned long)code_gen_buffer
||
1247 tc_ptr
>= (unsigned long)code_gen_ptr
)
1249 /* binary search (cf Knuth) */
1252 while (m_min
<= m_max
) {
1253 m
= (m_min
+ m_max
) >> 1;
1255 v
= (unsigned long)tb
->tc_ptr
;
1258 else if (tc_ptr
< v
) {
1267 static void tb_reset_jump_recursive(TranslationBlock
*tb
);
1269 static inline void tb_reset_jump_recursive2(TranslationBlock
*tb
, int n
)
1271 TranslationBlock
*tb1
, *tb_next
, **ptb
;
1274 tb1
= tb
->jmp_next
[n
];
1276 /* find head of list */
1279 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1282 tb1
= tb1
->jmp_next
[n1
];
1284 /* we are now sure now that tb jumps to tb1 */
1287 /* remove tb from the jmp_first list */
1288 ptb
= &tb_next
->jmp_first
;
1292 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1293 if (n1
== n
&& tb1
== tb
)
1295 ptb
= &tb1
->jmp_next
[n1
];
1297 *ptb
= tb
->jmp_next
[n
];
1298 tb
->jmp_next
[n
] = NULL
;
1300 /* suppress the jump to next tb in generated code */
1301 tb_reset_jump(tb
, n
);
1303 /* suppress jumps in the tb on which we could have jumped */
1304 tb_reset_jump_recursive(tb_next
);
1308 static void tb_reset_jump_recursive(TranslationBlock
*tb
)
1310 tb_reset_jump_recursive2(tb
, 0);
1311 tb_reset_jump_recursive2(tb
, 1);
1314 #if defined(TARGET_HAS_ICE)
1315 static void breakpoint_invalidate(CPUState
*env
, target_ulong pc
)
1317 target_phys_addr_t addr
;
1319 ram_addr_t ram_addr
;
1322 addr
= cpu_get_phys_page_debug(env
, pc
);
1323 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
1325 pd
= IO_MEM_UNASSIGNED
;
1327 pd
= p
->phys_offset
;
1329 ram_addr
= (pd
& TARGET_PAGE_MASK
) | (pc
& ~TARGET_PAGE_MASK
);
1330 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1, 0);
1334 /* Add a watchpoint. */
1335 int cpu_watchpoint_insert(CPUState
*env
, target_ulong addr
, target_ulong len
,
1336 int flags
, CPUWatchpoint
**watchpoint
)
1338 target_ulong len_mask
= ~(len
- 1);
1341 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1342 if ((len
!= 1 && len
!= 2 && len
!= 4 && len
!= 8) || (addr
& ~len_mask
)) {
1343 fprintf(stderr
, "qemu: tried to set invalid watchpoint at "
1344 TARGET_FMT_lx
", len=" TARGET_FMT_lu
"\n", addr
, len
);
1347 wp
= qemu_malloc(sizeof(*wp
));
1350 wp
->len_mask
= len_mask
;
1353 /* keep all GDB-injected watchpoints in front */
1355 QTAILQ_INSERT_HEAD(&env
->watchpoints
, wp
, entry
);
1357 QTAILQ_INSERT_TAIL(&env
->watchpoints
, wp
, entry
);
1359 tlb_flush_page(env
, addr
);
1366 /* Remove a specific watchpoint. */
1367 int cpu_watchpoint_remove(CPUState
*env
, target_ulong addr
, target_ulong len
,
1370 target_ulong len_mask
= ~(len
- 1);
1373 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1374 if (addr
== wp
->vaddr
&& len_mask
== wp
->len_mask
1375 && flags
== (wp
->flags
& ~BP_WATCHPOINT_HIT
)) {
1376 cpu_watchpoint_remove_by_ref(env
, wp
);
1383 /* Remove a specific watchpoint by reference. */
1384 void cpu_watchpoint_remove_by_ref(CPUState
*env
, CPUWatchpoint
*watchpoint
)
1386 QTAILQ_REMOVE(&env
->watchpoints
, watchpoint
, entry
);
1388 tlb_flush_page(env
, watchpoint
->vaddr
);
1390 qemu_free(watchpoint
);
1393 /* Remove all matching watchpoints. */
1394 void cpu_watchpoint_remove_all(CPUState
*env
, int mask
)
1396 CPUWatchpoint
*wp
, *next
;
1398 QTAILQ_FOREACH_SAFE(wp
, &env
->watchpoints
, entry
, next
) {
1399 if (wp
->flags
& mask
)
1400 cpu_watchpoint_remove_by_ref(env
, wp
);
1404 /* Add a breakpoint. */
1405 int cpu_breakpoint_insert(CPUState
*env
, target_ulong pc
, int flags
,
1406 CPUBreakpoint
**breakpoint
)
1408 #if defined(TARGET_HAS_ICE)
1411 bp
= qemu_malloc(sizeof(*bp
));
1416 /* keep all GDB-injected breakpoints in front */
1418 QTAILQ_INSERT_HEAD(&env
->breakpoints
, bp
, entry
);
1420 QTAILQ_INSERT_TAIL(&env
->breakpoints
, bp
, entry
);
1422 breakpoint_invalidate(env
, pc
);
1432 /* Remove a specific breakpoint. */
1433 int cpu_breakpoint_remove(CPUState
*env
, target_ulong pc
, int flags
)
1435 #if defined(TARGET_HAS_ICE)
1438 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1439 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
1440 cpu_breakpoint_remove_by_ref(env
, bp
);
1450 /* Remove a specific breakpoint by reference. */
1451 void cpu_breakpoint_remove_by_ref(CPUState
*env
, CPUBreakpoint
*breakpoint
)
1453 #if defined(TARGET_HAS_ICE)
1454 QTAILQ_REMOVE(&env
->breakpoints
, breakpoint
, entry
);
1456 breakpoint_invalidate(env
, breakpoint
->pc
);
1458 qemu_free(breakpoint
);
1462 /* Remove all matching breakpoints. */
1463 void cpu_breakpoint_remove_all(CPUState
*env
, int mask
)
1465 #if defined(TARGET_HAS_ICE)
1466 CPUBreakpoint
*bp
, *next
;
1468 QTAILQ_FOREACH_SAFE(bp
, &env
->breakpoints
, entry
, next
) {
1469 if (bp
->flags
& mask
)
1470 cpu_breakpoint_remove_by_ref(env
, bp
);
1475 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1476 CPU loop after each instruction */
1477 void cpu_single_step(CPUState
*env
, int enabled
)
1479 #if defined(TARGET_HAS_ICE)
1480 if (env
->singlestep_enabled
!= enabled
) {
1481 env
->singlestep_enabled
= enabled
;
1483 kvm_update_guest_debug(env
, 0);
1485 /* must flush all the translated code to avoid inconsistencies */
1486 /* XXX: only flush what is necessary */
1493 /* enable or disable low levels log */
1494 void cpu_set_log(int log_flags
)
1496 loglevel
= log_flags
;
1497 if (loglevel
&& !logfile
) {
1498 logfile
= fopen(logfilename
, log_append
? "a" : "w");
1500 perror(logfilename
);
1503 #if !defined(CONFIG_SOFTMMU)
1504 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1506 static char logfile_buf
[4096];
1507 setvbuf(logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
1509 #elif !defined(_WIN32)
1510 /* Win32 doesn't support line-buffering and requires size >= 2 */
1511 setvbuf(logfile
, NULL
, _IOLBF
, 0);
1515 if (!loglevel
&& logfile
) {
1521 void cpu_set_log_filename(const char *filename
)
1523 logfilename
= strdup(filename
);
1528 cpu_set_log(loglevel
);
1531 static void cpu_unlink_tb(CPUState
*env
)
1533 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1534 problem and hope the cpu will stop of its own accord. For userspace
1535 emulation this often isn't actually as bad as it sounds. Often
1536 signals are used primarily to interrupt blocking syscalls. */
1537 TranslationBlock
*tb
;
1538 static spinlock_t interrupt_lock
= SPIN_LOCK_UNLOCKED
;
1540 tb
= env
->current_tb
;
1541 /* if the cpu is currently executing code, we must unlink it and
1542 all the potentially executing TB */
1544 spin_lock(&interrupt_lock
);
1545 env
->current_tb
= NULL
;
1546 tb_reset_jump_recursive(tb
);
1547 spin_unlock(&interrupt_lock
);
1551 /* mask must never be zero, except for A20 change call */
1552 void cpu_interrupt(CPUState
*env
, int mask
)
1556 old_mask
= env
->interrupt_request
;
1557 env
->interrupt_request
|= mask
;
1559 #ifndef CONFIG_USER_ONLY
1561 * If called from iothread context, wake the target cpu in
1564 if (!qemu_cpu_self(env
)) {
1571 env
->icount_decr
.u16
.high
= 0xffff;
1572 #ifndef CONFIG_USER_ONLY
1574 && (mask
& ~old_mask
) != 0) {
1575 cpu_abort(env
, "Raised interrupt while not in I/O function");
1583 void cpu_reset_interrupt(CPUState
*env
, int mask
)
1585 env
->interrupt_request
&= ~mask
;
1588 void cpu_exit(CPUState
*env
)
1590 env
->exit_request
= 1;
1594 const CPULogItem cpu_log_items
[] = {
1595 { CPU_LOG_TB_OUT_ASM
, "out_asm",
1596 "show generated host assembly code for each compiled TB" },
1597 { CPU_LOG_TB_IN_ASM
, "in_asm",
1598 "show target assembly code for each compiled TB" },
1599 { CPU_LOG_TB_OP
, "op",
1600 "show micro ops for each compiled TB" },
1601 { CPU_LOG_TB_OP_OPT
, "op_opt",
1604 "before eflags optimization and "
1606 "after liveness analysis" },
1607 { CPU_LOG_INT
, "int",
1608 "show interrupts/exceptions in short format" },
1609 { CPU_LOG_EXEC
, "exec",
1610 "show trace before each executed TB (lots of logs)" },
1611 { CPU_LOG_TB_CPU
, "cpu",
1612 "show CPU state before block translation" },
1614 { CPU_LOG_PCALL
, "pcall",
1615 "show protected mode far calls/returns/exceptions" },
1616 { CPU_LOG_RESET
, "cpu_reset",
1617 "show CPU state before CPU resets" },
1620 { CPU_LOG_IOPORT
, "ioport",
1621 "show all i/o ports accesses" },
1626 static int cmp1(const char *s1
, int n
, const char *s2
)
1628 if (strlen(s2
) != n
)
1630 return memcmp(s1
, s2
, n
) == 0;
1633 /* takes a comma separated list of log masks. Return 0 if error. */
1634 int cpu_str_to_log_mask(const char *str
)
1636 const CPULogItem
*item
;
1643 p1
= strchr(p
, ',');
1646 if(cmp1(p
,p1
-p
,"all")) {
1647 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1651 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1652 if (cmp1(p
, p1
- p
, item
->name
))
1666 void cpu_abort(CPUState
*env
, const char *fmt
, ...)
1673 fprintf(stderr
, "qemu: fatal: ");
1674 vfprintf(stderr
, fmt
, ap
);
1675 fprintf(stderr
, "\n");
1677 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1679 cpu_dump_state(env
, stderr
, fprintf
, 0);
1681 if (qemu_log_enabled()) {
1682 qemu_log("qemu: fatal: ");
1683 qemu_log_vprintf(fmt
, ap2
);
1686 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1688 log_cpu_state(env
, 0);
1698 CPUState
*cpu_copy(CPUState
*env
)
1700 CPUState
*new_env
= cpu_init(env
->cpu_model_str
);
1701 CPUState
*next_cpu
= new_env
->next_cpu
;
1702 int cpu_index
= new_env
->cpu_index
;
1703 #if defined(TARGET_HAS_ICE)
1708 memcpy(new_env
, env
, sizeof(CPUState
));
1710 /* Preserve chaining and index. */
1711 new_env
->next_cpu
= next_cpu
;
1712 new_env
->cpu_index
= cpu_index
;
1714 /* Clone all break/watchpoints.
1715 Note: Once we support ptrace with hw-debug register access, make sure
1716 BP_CPU break/watchpoints are handled correctly on clone. */
1717 QTAILQ_INIT(&env
->breakpoints
);
1718 QTAILQ_INIT(&env
->watchpoints
);
1719 #if defined(TARGET_HAS_ICE)
1720 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1721 cpu_breakpoint_insert(new_env
, bp
->pc
, bp
->flags
, NULL
);
1723 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1724 cpu_watchpoint_insert(new_env
, wp
->vaddr
, (~wp
->len_mask
) + 1,
1732 #if !defined(CONFIG_USER_ONLY)
1734 static inline void tlb_flush_jmp_cache(CPUState
*env
, target_ulong addr
)
1738 /* Discard jump cache entries for any tb which might potentially
1739 overlap the flushed page. */
1740 i
= tb_jmp_cache_hash_page(addr
- TARGET_PAGE_SIZE
);
1741 memset (&env
->tb_jmp_cache
[i
], 0,
1742 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1744 i
= tb_jmp_cache_hash_page(addr
);
1745 memset (&env
->tb_jmp_cache
[i
], 0,
1746 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1749 static CPUTLBEntry s_cputlb_empty_entry
= {
1756 /* NOTE: if flush_global is true, also flush global entries (not
1758 void tlb_flush(CPUState
*env
, int flush_global
)
1762 #if defined(DEBUG_TLB)
1763 printf("tlb_flush:\n");
1765 /* must reset current TB so that interrupts cannot modify the
1766 links while we are modifying them */
1767 env
->current_tb
= NULL
;
1769 for(i
= 0; i
< CPU_TLB_SIZE
; i
++) {
1771 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1772 env
->tlb_table
[mmu_idx
][i
] = s_cputlb_empty_entry
;
1776 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
1781 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, target_ulong addr
)
1783 if (addr
== (tlb_entry
->addr_read
&
1784 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1785 addr
== (tlb_entry
->addr_write
&
1786 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1787 addr
== (tlb_entry
->addr_code
&
1788 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1789 *tlb_entry
= s_cputlb_empty_entry
;
1793 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
1798 #if defined(DEBUG_TLB)
1799 printf("tlb_flush_page: " TARGET_FMT_lx
"\n", addr
);
1801 /* must reset current TB so that interrupts cannot modify the
1802 links while we are modifying them */
1803 env
->current_tb
= NULL
;
1805 addr
&= TARGET_PAGE_MASK
;
1806 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1807 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
1808 tlb_flush_entry(&env
->tlb_table
[mmu_idx
][i
], addr
);
1810 tlb_flush_jmp_cache(env
, addr
);
1813 /* update the TLBs so that writes to code in the virtual page 'addr'
1815 static void tlb_protect_code(ram_addr_t ram_addr
)
1817 cpu_physical_memory_reset_dirty(ram_addr
,
1818 ram_addr
+ TARGET_PAGE_SIZE
,
1822 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1823 tested for self modifying code */
1824 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
1827 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] |= CODE_DIRTY_FLAG
;
1830 static inline void tlb_reset_dirty_range(CPUTLBEntry
*tlb_entry
,
1831 unsigned long start
, unsigned long length
)
1834 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1835 addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) + tlb_entry
->addend
;
1836 if ((addr
- start
) < length
) {
1837 tlb_entry
->addr_write
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) | TLB_NOTDIRTY
;
1842 /* Note: start and end must be within the same ram block. */
1843 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t end
,
1847 unsigned long length
, start1
;
1851 start
&= TARGET_PAGE_MASK
;
1852 end
= TARGET_PAGE_ALIGN(end
);
1854 length
= end
- start
;
1857 len
= length
>> TARGET_PAGE_BITS
;
1858 mask
= ~dirty_flags
;
1859 p
= phys_ram_dirty
+ (start
>> TARGET_PAGE_BITS
);
1860 for(i
= 0; i
< len
; i
++)
1863 /* we modify the TLB cache so that the dirty bit will be set again
1864 when accessing the range */
1865 start1
= (unsigned long)qemu_get_ram_ptr(start
);
1866 /* Chek that we don't span multiple blocks - this breaks the
1867 address comparisons below. */
1868 if ((unsigned long)qemu_get_ram_ptr(end
- 1) - start1
1869 != (end
- 1) - start
) {
1873 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1875 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1876 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1877 tlb_reset_dirty_range(&env
->tlb_table
[mmu_idx
][i
],
1883 int cpu_physical_memory_set_dirty_tracking(int enable
)
1885 in_migration
= enable
;
1886 if (kvm_enabled()) {
1887 return kvm_set_migration_log(enable
);
1892 int cpu_physical_memory_get_dirty_tracking(void)
1894 return in_migration
;
1897 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
1898 target_phys_addr_t end_addr
)
1903 ret
= kvm_physical_sync_dirty_bitmap(start_addr
, end_addr
);
1907 static inline void tlb_update_dirty(CPUTLBEntry
*tlb_entry
)
1909 ram_addr_t ram_addr
;
1912 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1913 p
= (void *)(unsigned long)((tlb_entry
->addr_write
& TARGET_PAGE_MASK
)
1914 + tlb_entry
->addend
);
1915 ram_addr
= qemu_ram_addr_from_host(p
);
1916 if (!cpu_physical_memory_is_dirty(ram_addr
)) {
1917 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
1922 /* update the TLB according to the current state of the dirty bits */
1923 void cpu_tlb_update_dirty(CPUState
*env
)
1927 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1928 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1929 tlb_update_dirty(&env
->tlb_table
[mmu_idx
][i
]);
1933 static inline void tlb_set_dirty1(CPUTLBEntry
*tlb_entry
, target_ulong vaddr
)
1935 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
))
1936 tlb_entry
->addr_write
= vaddr
;
1939 /* update the TLB corresponding to virtual page vaddr
1940 so that it is no longer dirty */
1941 static inline void tlb_set_dirty(CPUState
*env
, target_ulong vaddr
)
1946 vaddr
&= TARGET_PAGE_MASK
;
1947 i
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1948 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
1949 tlb_set_dirty1(&env
->tlb_table
[mmu_idx
][i
], vaddr
);
1952 /* add a new TLB entry. At most one entry for a given virtual address
1953 is permitted. Return 0 if OK or 2 if the page could not be mapped
1954 (can only happen in non SOFTMMU mode for I/O pages or pages
1955 conflicting with the host address space). */
1956 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
1957 target_phys_addr_t paddr
, int prot
,
1958 int mmu_idx
, int is_softmmu
)
1963 target_ulong address
;
1964 target_ulong code_address
;
1965 target_phys_addr_t addend
;
1969 target_phys_addr_t iotlb
;
1971 p
= phys_page_find(paddr
>> TARGET_PAGE_BITS
);
1973 pd
= IO_MEM_UNASSIGNED
;
1975 pd
= p
->phys_offset
;
1977 #if defined(DEBUG_TLB)
1978 printf("tlb_set_page: vaddr=" TARGET_FMT_lx
" paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1979 vaddr
, (int)paddr
, prot
, mmu_idx
, is_softmmu
, pd
);
1984 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&& !(pd
& IO_MEM_ROMD
)) {
1985 /* IO memory case (romd handled later) */
1986 address
|= TLB_MMIO
;
1988 addend
= (unsigned long)qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
);
1989 if ((pd
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
1991 iotlb
= pd
& TARGET_PAGE_MASK
;
1992 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
)
1993 iotlb
|= IO_MEM_NOTDIRTY
;
1995 iotlb
|= IO_MEM_ROM
;
1997 /* IO handlers are currently passed a physical address.
1998 It would be nice to pass an offset from the base address
1999 of that region. This would avoid having to special case RAM,
2000 and avoid full address decoding in every device.
2001 We can't use the high bits of pd for this because
2002 IO_MEM_ROMD uses these as a ram address. */
2003 iotlb
= (pd
& ~TARGET_PAGE_MASK
);
2005 iotlb
+= p
->region_offset
;
2011 code_address
= address
;
2012 /* Make accesses to pages with watchpoints go via the
2013 watchpoint trap routines. */
2014 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
2015 if (vaddr
== (wp
->vaddr
& TARGET_PAGE_MASK
)) {
2016 iotlb
= io_mem_watch
+ paddr
;
2017 /* TODO: The memory case can be optimized by not trapping
2018 reads of pages with a write breakpoint. */
2019 address
|= TLB_MMIO
;
2023 index
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
2024 env
->iotlb
[mmu_idx
][index
] = iotlb
- vaddr
;
2025 te
= &env
->tlb_table
[mmu_idx
][index
];
2026 te
->addend
= addend
- vaddr
;
2027 if (prot
& PAGE_READ
) {
2028 te
->addr_read
= address
;
2033 if (prot
& PAGE_EXEC
) {
2034 te
->addr_code
= code_address
;
2038 if (prot
& PAGE_WRITE
) {
2039 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_ROM
||
2040 (pd
& IO_MEM_ROMD
)) {
2041 /* Write access calls the I/O callback. */
2042 te
->addr_write
= address
| TLB_MMIO
;
2043 } else if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
&&
2044 !cpu_physical_memory_is_dirty(pd
)) {
2045 te
->addr_write
= address
| TLB_NOTDIRTY
;
2047 te
->addr_write
= address
;
2050 te
->addr_write
= -1;
2057 void tlb_flush(CPUState
*env
, int flush_global
)
2061 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
2065 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
2066 target_phys_addr_t paddr
, int prot
,
2067 int mmu_idx
, int is_softmmu
)
2073 * Walks guest process memory "regions" one by one
2074 * and calls callback function 'fn' for each region.
2076 int walk_memory_regions(void *priv
,
2077 int (*fn
)(void *, unsigned long, unsigned long, unsigned long))
2079 unsigned long start
, end
;
2081 int i
, j
, prot
, prot1
;
2087 for (i
= 0; i
<= L1_SIZE
; i
++) {
2088 p
= (i
< L1_SIZE
) ? l1_map
[i
] : NULL
;
2089 for (j
= 0; j
< L2_SIZE
; j
++) {
2090 prot1
= (p
== NULL
) ? 0 : p
[j
].flags
;
2092 * "region" is one continuous chunk of memory
2093 * that has same protection flags set.
2095 if (prot1
!= prot
) {
2096 end
= (i
<< (32 - L1_BITS
)) | (j
<< TARGET_PAGE_BITS
);
2098 rc
= (*fn
)(priv
, start
, end
, prot
);
2099 /* callback can stop iteration by returning != 0 */
2116 static int dump_region(void *priv
, unsigned long start
,
2117 unsigned long end
, unsigned long prot
)
2119 FILE *f
= (FILE *)priv
;
2121 (void) fprintf(f
, "%08lx-%08lx %08lx %c%c%c\n",
2122 start
, end
, end
- start
,
2123 ((prot
& PAGE_READ
) ? 'r' : '-'),
2124 ((prot
& PAGE_WRITE
) ? 'w' : '-'),
2125 ((prot
& PAGE_EXEC
) ? 'x' : '-'));
2130 /* dump memory mappings */
2131 void page_dump(FILE *f
)
2133 (void) fprintf(f
, "%-8s %-8s %-8s %s\n",
2134 "start", "end", "size", "prot");
2135 walk_memory_regions(f
, dump_region
);
2138 int page_get_flags(target_ulong address
)
2142 p
= page_find(address
>> TARGET_PAGE_BITS
);
2148 /* modify the flags of a page and invalidate the code if
2149 necessary. The flag PAGE_WRITE_ORG is positioned automatically
2150 depending on PAGE_WRITE */
2151 void page_set_flags(target_ulong start
, target_ulong end
, int flags
)
2156 /* mmap_lock should already be held. */
2157 start
= start
& TARGET_PAGE_MASK
;
2158 end
= TARGET_PAGE_ALIGN(end
);
2159 if (flags
& PAGE_WRITE
)
2160 flags
|= PAGE_WRITE_ORG
;
2161 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2162 p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
);
2163 /* We may be called for host regions that are outside guest
2167 /* if the write protection is set, then we invalidate the code
2169 if (!(p
->flags
& PAGE_WRITE
) &&
2170 (flags
& PAGE_WRITE
) &&
2172 tb_invalidate_phys_page(addr
, 0, NULL
);
2178 int page_check_range(target_ulong start
, target_ulong len
, int flags
)
2184 if (start
+ len
< start
)
2185 /* we've wrapped around */
2188 end
= TARGET_PAGE_ALIGN(start
+len
); /* must do before we loose bits in the next step */
2189 start
= start
& TARGET_PAGE_MASK
;
2191 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2192 p
= page_find(addr
>> TARGET_PAGE_BITS
);
2195 if( !(p
->flags
& PAGE_VALID
) )
2198 if ((flags
& PAGE_READ
) && !(p
->flags
& PAGE_READ
))
2200 if (flags
& PAGE_WRITE
) {
2201 if (!(p
->flags
& PAGE_WRITE_ORG
))
2203 /* unprotect the page if it was put read-only because it
2204 contains translated code */
2205 if (!(p
->flags
& PAGE_WRITE
)) {
2206 if (!page_unprotect(addr
, 0, NULL
))
2215 /* called from signal handler: invalidate the code and unprotect the
2216 page. Return TRUE if the fault was successfully handled. */
2217 int page_unprotect(target_ulong address
, unsigned long pc
, void *puc
)
2219 unsigned int page_index
, prot
, pindex
;
2221 target_ulong host_start
, host_end
, addr
;
2223 /* Technically this isn't safe inside a signal handler. However we
2224 know this only ever happens in a synchronous SEGV handler, so in
2225 practice it seems to be ok. */
2228 host_start
= address
& qemu_host_page_mask
;
2229 page_index
= host_start
>> TARGET_PAGE_BITS
;
2230 p1
= page_find(page_index
);
2235 host_end
= host_start
+ qemu_host_page_size
;
2238 for(addr
= host_start
;addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
2242 /* if the page was really writable, then we change its
2243 protection back to writable */
2244 if (prot
& PAGE_WRITE_ORG
) {
2245 pindex
= (address
- host_start
) >> TARGET_PAGE_BITS
;
2246 if (!(p1
[pindex
].flags
& PAGE_WRITE
)) {
2247 mprotect((void *)g2h(host_start
), qemu_host_page_size
,
2248 (prot
& PAGE_BITS
) | PAGE_WRITE
);
2249 p1
[pindex
].flags
|= PAGE_WRITE
;
2250 /* and since the content will be modified, we must invalidate
2251 the corresponding translated code. */
2252 tb_invalidate_phys_page(address
, pc
, puc
);
2253 #ifdef DEBUG_TB_CHECK
2254 tb_invalidate_check(address
);
2264 static inline void tlb_set_dirty(CPUState
*env
,
2265 unsigned long addr
, target_ulong vaddr
)
2268 #endif /* defined(CONFIG_USER_ONLY) */
2270 #if !defined(CONFIG_USER_ONLY)
2272 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2273 ram_addr_t memory
, ram_addr_t region_offset
);
2274 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2275 ram_addr_t orig_memory
, ram_addr_t region_offset
);
2276 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2279 if (addr > start_addr) \
2282 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2283 if (start_addr2 > 0) \
2287 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2288 end_addr2 = TARGET_PAGE_SIZE - 1; \
2290 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2291 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2296 /* register physical memory.
2297 For RAM, 'size' must be a multiple of the target page size.
2298 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2299 io memory page. The address used when calling the IO function is
2300 the offset from the start of the region, plus region_offset. Both
2301 start_addr and region_offset are rounded down to a page boundary
2302 before calculating this offset. This should not be a problem unless
2303 the low bits of start_addr and region_offset differ. */
2304 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr
,
2306 ram_addr_t phys_offset
,
2307 ram_addr_t region_offset
)
2309 target_phys_addr_t addr
, end_addr
;
2312 ram_addr_t orig_size
= size
;
2316 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
2318 if (phys_offset
== IO_MEM_UNASSIGNED
) {
2319 region_offset
= start_addr
;
2321 region_offset
&= TARGET_PAGE_MASK
;
2322 size
= (size
+ TARGET_PAGE_SIZE
- 1) & TARGET_PAGE_MASK
;
2323 end_addr
= start_addr
+ (target_phys_addr_t
)size
;
2324 for(addr
= start_addr
; addr
!= end_addr
; addr
+= TARGET_PAGE_SIZE
) {
2325 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2326 if (p
&& p
->phys_offset
!= IO_MEM_UNASSIGNED
) {
2327 ram_addr_t orig_memory
= p
->phys_offset
;
2328 target_phys_addr_t start_addr2
, end_addr2
;
2329 int need_subpage
= 0;
2331 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
, end_addr2
,
2333 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2334 if (!(orig_memory
& IO_MEM_SUBPAGE
)) {
2335 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2336 &p
->phys_offset
, orig_memory
,
2339 subpage
= io_mem_opaque
[(orig_memory
& ~TARGET_PAGE_MASK
)
2342 subpage_register(subpage
, start_addr2
, end_addr2
, phys_offset
,
2344 p
->region_offset
= 0;
2346 p
->phys_offset
= phys_offset
;
2347 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2348 (phys_offset
& IO_MEM_ROMD
))
2349 phys_offset
+= TARGET_PAGE_SIZE
;
2352 p
= phys_page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
2353 p
->phys_offset
= phys_offset
;
2354 p
->region_offset
= region_offset
;
2355 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2356 (phys_offset
& IO_MEM_ROMD
)) {
2357 phys_offset
+= TARGET_PAGE_SIZE
;
2359 target_phys_addr_t start_addr2
, end_addr2
;
2360 int need_subpage
= 0;
2362 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
,
2363 end_addr2
, need_subpage
);
2365 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2366 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2367 &p
->phys_offset
, IO_MEM_UNASSIGNED
,
2368 addr
& TARGET_PAGE_MASK
);
2369 subpage_register(subpage
, start_addr2
, end_addr2
,
2370 phys_offset
, region_offset
);
2371 p
->region_offset
= 0;
2375 region_offset
+= TARGET_PAGE_SIZE
;
2378 /* since each CPU stores ram addresses in its TLB cache, we must
2379 reset the modified entries */
2381 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2386 /* XXX: temporary until new memory mapping API */
2387 ram_addr_t
cpu_get_physical_page_desc(target_phys_addr_t addr
)
2391 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2393 return IO_MEM_UNASSIGNED
;
2394 return p
->phys_offset
;
2397 void qemu_register_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2400 kvm_coalesce_mmio_region(addr
, size
);
2403 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2406 kvm_uncoalesce_mmio_region(addr
, size
);
2409 void qemu_flush_coalesced_mmio_buffer(void)
2412 kvm_flush_coalesced_mmio_buffer();
2415 ram_addr_t
qemu_ram_alloc(ram_addr_t size
)
2417 RAMBlock
*new_block
;
2419 size
= TARGET_PAGE_ALIGN(size
);
2420 new_block
= qemu_malloc(sizeof(*new_block
));
2422 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
2423 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2424 new_block
->host
= mmap((void*)0x1000000, size
, PROT_EXEC
|PROT_READ
|PROT_WRITE
,
2425 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
2427 new_block
->host
= qemu_vmalloc(size
);
2429 #ifdef MADV_MERGEABLE
2430 madvise(new_block
->host
, size
, MADV_MERGEABLE
);
2432 new_block
->offset
= last_ram_offset
;
2433 new_block
->length
= size
;
2435 new_block
->next
= ram_blocks
;
2436 ram_blocks
= new_block
;
2438 phys_ram_dirty
= qemu_realloc(phys_ram_dirty
,
2439 (last_ram_offset
+ size
) >> TARGET_PAGE_BITS
);
2440 memset(phys_ram_dirty
+ (last_ram_offset
>> TARGET_PAGE_BITS
),
2441 0xff, size
>> TARGET_PAGE_BITS
);
2443 last_ram_offset
+= size
;
2446 kvm_setup_guest_memory(new_block
->host
, size
);
2448 return new_block
->offset
;
2451 void qemu_ram_free(ram_addr_t addr
)
2453 /* TODO: implement this. */
2456 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2457 With the exception of the softmmu code in this file, this should
2458 only be used for local memory (e.g. video ram) that the device owns,
2459 and knows it isn't going to access beyond the end of the block.
2461 It should not be used for general purpose DMA.
2462 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2464 void *qemu_get_ram_ptr(ram_addr_t addr
)
2471 prevp
= &ram_blocks
;
2473 while (block
&& (block
->offset
> addr
2474 || block
->offset
+ block
->length
<= addr
)) {
2476 prevp
= &prev
->next
;
2478 block
= block
->next
;
2481 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
2484 /* Move this entry to to start of the list. */
2486 prev
->next
= block
->next
;
2487 block
->next
= *prevp
;
2490 return block
->host
+ (addr
- block
->offset
);
2493 /* Some of the softmmu routines need to translate from a host pointer
2494 (typically a TLB entry) back to a ram offset. */
2495 ram_addr_t
qemu_ram_addr_from_host(void *ptr
)
2500 uint8_t *host
= ptr
;
2503 prevp
= &ram_blocks
;
2505 while (block
&& (block
->host
> host
2506 || block
->host
+ block
->length
<= host
)) {
2508 prevp
= &prev
->next
;
2510 block
= block
->next
;
2513 fprintf(stderr
, "Bad ram pointer %p\n", ptr
);
2516 return block
->offset
+ (host
- block
->host
);
2519 static uint32_t unassigned_mem_readb(void *opaque
, target_phys_addr_t addr
)
2521 #ifdef DEBUG_UNASSIGNED
2522 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2524 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2525 do_unassigned_access(addr
, 0, 0, 0, 1);
2530 static uint32_t unassigned_mem_readw(void *opaque
, target_phys_addr_t addr
)
2532 #ifdef DEBUG_UNASSIGNED
2533 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2535 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2536 do_unassigned_access(addr
, 0, 0, 0, 2);
2541 static uint32_t unassigned_mem_readl(void *opaque
, target_phys_addr_t addr
)
2543 #ifdef DEBUG_UNASSIGNED
2544 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2546 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2547 do_unassigned_access(addr
, 0, 0, 0, 4);
2552 static void unassigned_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2554 #ifdef DEBUG_UNASSIGNED
2555 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2557 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2558 do_unassigned_access(addr
, 1, 0, 0, 1);
2562 static void unassigned_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2564 #ifdef DEBUG_UNASSIGNED
2565 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2567 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2568 do_unassigned_access(addr
, 1, 0, 0, 2);
2572 static void unassigned_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2574 #ifdef DEBUG_UNASSIGNED
2575 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2577 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
2578 do_unassigned_access(addr
, 1, 0, 0, 4);
2582 static CPUReadMemoryFunc
* const unassigned_mem_read
[3] = {
2583 unassigned_mem_readb
,
2584 unassigned_mem_readw
,
2585 unassigned_mem_readl
,
2588 static CPUWriteMemoryFunc
* const unassigned_mem_write
[3] = {
2589 unassigned_mem_writeb
,
2590 unassigned_mem_writew
,
2591 unassigned_mem_writel
,
2594 static void notdirty_mem_writeb(void *opaque
, target_phys_addr_t ram_addr
,
2598 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2599 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2600 #if !defined(CONFIG_USER_ONLY)
2601 tb_invalidate_phys_page_fast(ram_addr
, 1);
2602 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2605 stb_p(qemu_get_ram_ptr(ram_addr
), val
);
2606 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2607 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2608 /* we remove the notdirty callback only if the code has been
2610 if (dirty_flags
== 0xff)
2611 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2614 static void notdirty_mem_writew(void *opaque
, target_phys_addr_t ram_addr
,
2618 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2619 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2620 #if !defined(CONFIG_USER_ONLY)
2621 tb_invalidate_phys_page_fast(ram_addr
, 2);
2622 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2625 stw_p(qemu_get_ram_ptr(ram_addr
), val
);
2626 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2627 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2628 /* we remove the notdirty callback only if the code has been
2630 if (dirty_flags
== 0xff)
2631 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2634 static void notdirty_mem_writel(void *opaque
, target_phys_addr_t ram_addr
,
2638 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2639 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2640 #if !defined(CONFIG_USER_ONLY)
2641 tb_invalidate_phys_page_fast(ram_addr
, 4);
2642 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2645 stl_p(qemu_get_ram_ptr(ram_addr
), val
);
2646 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2647 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2648 /* we remove the notdirty callback only if the code has been
2650 if (dirty_flags
== 0xff)
2651 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2654 static CPUReadMemoryFunc
* const error_mem_read
[3] = {
2655 NULL
, /* never used */
2656 NULL
, /* never used */
2657 NULL
, /* never used */
2660 static CPUWriteMemoryFunc
* const notdirty_mem_write
[3] = {
2661 notdirty_mem_writeb
,
2662 notdirty_mem_writew
,
2663 notdirty_mem_writel
,
2666 /* Generate a debug exception if a watchpoint has been hit. */
2667 static void check_watchpoint(int offset
, int len_mask
, int flags
)
2669 CPUState
*env
= cpu_single_env
;
2670 target_ulong pc
, cs_base
;
2671 TranslationBlock
*tb
;
2676 if (env
->watchpoint_hit
) {
2677 /* We re-entered the check after replacing the TB. Now raise
2678 * the debug interrupt so that is will trigger after the
2679 * current instruction. */
2680 cpu_interrupt(env
, CPU_INTERRUPT_DEBUG
);
2683 vaddr
= (env
->mem_io_vaddr
& TARGET_PAGE_MASK
) + offset
;
2684 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
2685 if ((vaddr
== (wp
->vaddr
& len_mask
) ||
2686 (vaddr
& wp
->len_mask
) == wp
->vaddr
) && (wp
->flags
& flags
)) {
2687 wp
->flags
|= BP_WATCHPOINT_HIT
;
2688 if (!env
->watchpoint_hit
) {
2689 env
->watchpoint_hit
= wp
;
2690 tb
= tb_find_pc(env
->mem_io_pc
);
2692 cpu_abort(env
, "check_watchpoint: could not find TB for "
2693 "pc=%p", (void *)env
->mem_io_pc
);
2695 cpu_restore_state(tb
, env
, env
->mem_io_pc
, NULL
);
2696 tb_phys_invalidate(tb
, -1);
2697 if (wp
->flags
& BP_STOP_BEFORE_ACCESS
) {
2698 env
->exception_index
= EXCP_DEBUG
;
2700 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &cpu_flags
);
2701 tb_gen_code(env
, pc
, cs_base
, cpu_flags
, 1);
2703 cpu_resume_from_signal(env
, NULL
);
2706 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
2711 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2712 so these check for a hit then pass through to the normal out-of-line
2714 static uint32_t watch_mem_readb(void *opaque
, target_phys_addr_t addr
)
2716 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_READ
);
2717 return ldub_phys(addr
);
2720 static uint32_t watch_mem_readw(void *opaque
, target_phys_addr_t addr
)
2722 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_READ
);
2723 return lduw_phys(addr
);
2726 static uint32_t watch_mem_readl(void *opaque
, target_phys_addr_t addr
)
2728 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_READ
);
2729 return ldl_phys(addr
);
2732 static void watch_mem_writeb(void *opaque
, target_phys_addr_t addr
,
2735 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_WRITE
);
2736 stb_phys(addr
, val
);
2739 static void watch_mem_writew(void *opaque
, target_phys_addr_t addr
,
2742 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_WRITE
);
2743 stw_phys(addr
, val
);
2746 static void watch_mem_writel(void *opaque
, target_phys_addr_t addr
,
2749 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_WRITE
);
2750 stl_phys(addr
, val
);
2753 static CPUReadMemoryFunc
* const watch_mem_read
[3] = {
2759 static CPUWriteMemoryFunc
* const watch_mem_write
[3] = {
2765 static inline uint32_t subpage_readlen (subpage_t
*mmio
, target_phys_addr_t addr
,
2771 idx
= SUBPAGE_IDX(addr
);
2772 #if defined(DEBUG_SUBPAGE)
2773 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d\n", __func__
,
2774 mmio
, len
, addr
, idx
);
2776 ret
= (**mmio
->mem_read
[idx
][len
])(mmio
->opaque
[idx
][0][len
],
2777 addr
+ mmio
->region_offset
[idx
][0][len
]);
2782 static inline void subpage_writelen (subpage_t
*mmio
, target_phys_addr_t addr
,
2783 uint32_t value
, unsigned int len
)
2787 idx
= SUBPAGE_IDX(addr
);
2788 #if defined(DEBUG_SUBPAGE)
2789 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d value %08x\n", __func__
,
2790 mmio
, len
, addr
, idx
, value
);
2792 (**mmio
->mem_write
[idx
][len
])(mmio
->opaque
[idx
][1][len
],
2793 addr
+ mmio
->region_offset
[idx
][1][len
],
2797 static uint32_t subpage_readb (void *opaque
, target_phys_addr_t addr
)
2799 #if defined(DEBUG_SUBPAGE)
2800 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2803 return subpage_readlen(opaque
, addr
, 0);
2806 static void subpage_writeb (void *opaque
, target_phys_addr_t addr
,
2809 #if defined(DEBUG_SUBPAGE)
2810 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2812 subpage_writelen(opaque
, addr
, value
, 0);
2815 static uint32_t subpage_readw (void *opaque
, target_phys_addr_t addr
)
2817 #if defined(DEBUG_SUBPAGE)
2818 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2821 return subpage_readlen(opaque
, addr
, 1);
2824 static void subpage_writew (void *opaque
, target_phys_addr_t addr
,
2827 #if defined(DEBUG_SUBPAGE)
2828 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2830 subpage_writelen(opaque
, addr
, value
, 1);
2833 static uint32_t subpage_readl (void *opaque
, target_phys_addr_t addr
)
2835 #if defined(DEBUG_SUBPAGE)
2836 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2839 return subpage_readlen(opaque
, addr
, 2);
2842 static void subpage_writel (void *opaque
,
2843 target_phys_addr_t addr
, uint32_t value
)
2845 #if defined(DEBUG_SUBPAGE)
2846 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2848 subpage_writelen(opaque
, addr
, value
, 2);
2851 static CPUReadMemoryFunc
* const subpage_read
[] = {
2857 static CPUWriteMemoryFunc
* const subpage_write
[] = {
2863 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2864 ram_addr_t memory
, ram_addr_t region_offset
)
2869 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
2871 idx
= SUBPAGE_IDX(start
);
2872 eidx
= SUBPAGE_IDX(end
);
2873 #if defined(DEBUG_SUBPAGE)
2874 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__
,
2875 mmio
, start
, end
, idx
, eidx
, memory
);
2877 memory
>>= IO_MEM_SHIFT
;
2878 for (; idx
<= eidx
; idx
++) {
2879 for (i
= 0; i
< 4; i
++) {
2880 if (io_mem_read
[memory
][i
]) {
2881 mmio
->mem_read
[idx
][i
] = &io_mem_read
[memory
][i
];
2882 mmio
->opaque
[idx
][0][i
] = io_mem_opaque
[memory
];
2883 mmio
->region_offset
[idx
][0][i
] = region_offset
;
2885 if (io_mem_write
[memory
][i
]) {
2886 mmio
->mem_write
[idx
][i
] = &io_mem_write
[memory
][i
];
2887 mmio
->opaque
[idx
][1][i
] = io_mem_opaque
[memory
];
2888 mmio
->region_offset
[idx
][1][i
] = region_offset
;
2896 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2897 ram_addr_t orig_memory
, ram_addr_t region_offset
)
2902 mmio
= qemu_mallocz(sizeof(subpage_t
));
2905 subpage_memory
= cpu_register_io_memory(subpage_read
, subpage_write
, mmio
);
2906 #if defined(DEBUG_SUBPAGE)
2907 printf("%s: %p base " TARGET_FMT_plx
" len %08x %d\n", __func__
,
2908 mmio
, base
, TARGET_PAGE_SIZE
, subpage_memory
);
2910 *phys
= subpage_memory
| IO_MEM_SUBPAGE
;
2911 subpage_register(mmio
, 0, TARGET_PAGE_SIZE
- 1, orig_memory
,
2917 static int get_free_io_mem_idx(void)
2921 for (i
= 0; i
<IO_MEM_NB_ENTRIES
; i
++)
2922 if (!io_mem_used
[i
]) {
2926 fprintf(stderr
, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES
);
2930 /* mem_read and mem_write are arrays of functions containing the
2931 function to access byte (index 0), word (index 1) and dword (index
2932 2). Functions can be omitted with a NULL function pointer.
2933 If io_index is non zero, the corresponding io zone is
2934 modified. If it is zero, a new io zone is allocated. The return
2935 value can be used with cpu_register_physical_memory(). (-1) is
2936 returned if error. */
2937 static int cpu_register_io_memory_fixed(int io_index
,
2938 CPUReadMemoryFunc
* const *mem_read
,
2939 CPUWriteMemoryFunc
* const *mem_write
,
2942 int i
, subwidth
= 0;
2944 if (io_index
<= 0) {
2945 io_index
= get_free_io_mem_idx();
2949 io_index
>>= IO_MEM_SHIFT
;
2950 if (io_index
>= IO_MEM_NB_ENTRIES
)
2954 for(i
= 0;i
< 3; i
++) {
2955 if (!mem_read
[i
] || !mem_write
[i
])
2956 subwidth
= IO_MEM_SUBWIDTH
;
2957 io_mem_read
[io_index
][i
] = mem_read
[i
];
2958 io_mem_write
[io_index
][i
] = mem_write
[i
];
2960 io_mem_opaque
[io_index
] = opaque
;
2961 return (io_index
<< IO_MEM_SHIFT
) | subwidth
;
2964 int cpu_register_io_memory(CPUReadMemoryFunc
* const *mem_read
,
2965 CPUWriteMemoryFunc
* const *mem_write
,
2968 return cpu_register_io_memory_fixed(0, mem_read
, mem_write
, opaque
);
2971 void cpu_unregister_io_memory(int io_table_address
)
2974 int io_index
= io_table_address
>> IO_MEM_SHIFT
;
2976 for (i
=0;i
< 3; i
++) {
2977 io_mem_read
[io_index
][i
] = unassigned_mem_read
[i
];
2978 io_mem_write
[io_index
][i
] = unassigned_mem_write
[i
];
2980 io_mem_opaque
[io_index
] = NULL
;
2981 io_mem_used
[io_index
] = 0;
2984 static void io_mem_init(void)
2988 cpu_register_io_memory_fixed(IO_MEM_ROM
, error_mem_read
, unassigned_mem_write
, NULL
);
2989 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED
, unassigned_mem_read
, unassigned_mem_write
, NULL
);
2990 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY
, error_mem_read
, notdirty_mem_write
, NULL
);
2994 io_mem_watch
= cpu_register_io_memory(watch_mem_read
,
2995 watch_mem_write
, NULL
);
2998 #endif /* !defined(CONFIG_USER_ONLY) */
3000 /* physical memory access (slow version, mainly for debug) */
3001 #if defined(CONFIG_USER_ONLY)
3002 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
3003 int len
, int is_write
)
3010 page
= addr
& TARGET_PAGE_MASK
;
3011 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3014 flags
= page_get_flags(page
);
3015 if (!(flags
& PAGE_VALID
))
3018 if (!(flags
& PAGE_WRITE
))
3020 /* XXX: this code should not depend on lock_user */
3021 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
3022 /* FIXME - should this return an error rather than just fail? */
3025 unlock_user(p
, addr
, l
);
3027 if (!(flags
& PAGE_READ
))
3029 /* XXX: this code should not depend on lock_user */
3030 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
3031 /* FIXME - should this return an error rather than just fail? */
3034 unlock_user(p
, addr
, 0);
3043 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
3044 int len
, int is_write
)
3049 target_phys_addr_t page
;
3054 page
= addr
& TARGET_PAGE_MASK
;
3055 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3058 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3060 pd
= IO_MEM_UNASSIGNED
;
3062 pd
= p
->phys_offset
;
3066 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3067 target_phys_addr_t addr1
= addr
;
3068 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3070 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3071 /* XXX: could force cpu_single_env to NULL to avoid
3073 if (l
>= 4 && ((addr1
& 3) == 0)) {
3074 /* 32 bit write access */
3076 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr1
, val
);
3078 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3079 /* 16 bit write access */
3081 io_mem_write
[io_index
][1](io_mem_opaque
[io_index
], addr1
, val
);
3084 /* 8 bit write access */
3086 io_mem_write
[io_index
][0](io_mem_opaque
[io_index
], addr1
, val
);
3090 unsigned long addr1
;
3091 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3093 ptr
= qemu_get_ram_ptr(addr1
);
3094 memcpy(ptr
, buf
, l
);
3095 if (!cpu_physical_memory_is_dirty(addr1
)) {
3096 /* invalidate code */
3097 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3099 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3100 (0xff & ~CODE_DIRTY_FLAG
);
3104 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3105 !(pd
& IO_MEM_ROMD
)) {
3106 target_phys_addr_t addr1
= addr
;
3108 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3110 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3111 if (l
>= 4 && ((addr1
& 3) == 0)) {
3112 /* 32 bit read access */
3113 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr1
);
3116 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3117 /* 16 bit read access */
3118 val
= io_mem_read
[io_index
][1](io_mem_opaque
[io_index
], addr1
);
3122 /* 8 bit read access */
3123 val
= io_mem_read
[io_index
][0](io_mem_opaque
[io_index
], addr1
);
3129 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3130 (addr
& ~TARGET_PAGE_MASK
);
3131 memcpy(buf
, ptr
, l
);
3140 /* used for ROM loading : can write in RAM and ROM */
3141 void cpu_physical_memory_write_rom(target_phys_addr_t addr
,
3142 const uint8_t *buf
, int len
)
3146 target_phys_addr_t page
;
3151 page
= addr
& TARGET_PAGE_MASK
;
3152 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3155 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3157 pd
= IO_MEM_UNASSIGNED
;
3159 pd
= p
->phys_offset
;
3162 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
&&
3163 (pd
& ~TARGET_PAGE_MASK
) != IO_MEM_ROM
&&
3164 !(pd
& IO_MEM_ROMD
)) {
3167 unsigned long addr1
;
3168 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3170 ptr
= qemu_get_ram_ptr(addr1
);
3171 memcpy(ptr
, buf
, l
);
3181 target_phys_addr_t addr
;
3182 target_phys_addr_t len
;
3185 static BounceBuffer bounce
;
3187 typedef struct MapClient
{
3189 void (*callback
)(void *opaque
);
3190 QLIST_ENTRY(MapClient
) link
;
3193 static QLIST_HEAD(map_client_list
, MapClient
) map_client_list
3194 = QLIST_HEAD_INITIALIZER(map_client_list
);
3196 void *cpu_register_map_client(void *opaque
, void (*callback
)(void *opaque
))
3198 MapClient
*client
= qemu_malloc(sizeof(*client
));
3200 client
->opaque
= opaque
;
3201 client
->callback
= callback
;
3202 QLIST_INSERT_HEAD(&map_client_list
, client
, link
);
3206 void cpu_unregister_map_client(void *_client
)
3208 MapClient
*client
= (MapClient
*)_client
;
3210 QLIST_REMOVE(client
, link
);
3214 static void cpu_notify_map_clients(void)
3218 while (!QLIST_EMPTY(&map_client_list
)) {
3219 client
= QLIST_FIRST(&map_client_list
);
3220 client
->callback(client
->opaque
);
3221 cpu_unregister_map_client(client
);
3225 /* Map a physical memory region into a host virtual address.
3226 * May map a subset of the requested range, given by and returned in *plen.
3227 * May return NULL if resources needed to perform the mapping are exhausted.
3228 * Use only for reads OR writes - not for read-modify-write operations.
3229 * Use cpu_register_map_client() to know when retrying the map operation is
3230 * likely to succeed.
3232 void *cpu_physical_memory_map(target_phys_addr_t addr
,
3233 target_phys_addr_t
*plen
,
3236 target_phys_addr_t len
= *plen
;
3237 target_phys_addr_t done
= 0;
3239 uint8_t *ret
= NULL
;
3241 target_phys_addr_t page
;
3244 unsigned long addr1
;
3247 page
= addr
& TARGET_PAGE_MASK
;
3248 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3251 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3253 pd
= IO_MEM_UNASSIGNED
;
3255 pd
= p
->phys_offset
;
3258 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3259 if (done
|| bounce
.buffer
) {
3262 bounce
.buffer
= qemu_memalign(TARGET_PAGE_SIZE
, TARGET_PAGE_SIZE
);
3266 cpu_physical_memory_rw(addr
, bounce
.buffer
, l
, 0);
3268 ptr
= bounce
.buffer
;
3270 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3271 ptr
= qemu_get_ram_ptr(addr1
);
3275 } else if (ret
+ done
!= ptr
) {
3287 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3288 * Will also mark the memory as dirty if is_write == 1. access_len gives
3289 * the amount of memory that was actually read or written by the caller.
3291 void cpu_physical_memory_unmap(void *buffer
, target_phys_addr_t len
,
3292 int is_write
, target_phys_addr_t access_len
)
3294 if (buffer
!= bounce
.buffer
) {
3296 ram_addr_t addr1
= qemu_ram_addr_from_host(buffer
);
3297 while (access_len
) {
3299 l
= TARGET_PAGE_SIZE
;
3302 if (!cpu_physical_memory_is_dirty(addr1
)) {
3303 /* invalidate code */
3304 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3306 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3307 (0xff & ~CODE_DIRTY_FLAG
);
3316 cpu_physical_memory_write(bounce
.addr
, bounce
.buffer
, access_len
);
3318 qemu_vfree(bounce
.buffer
);
3319 bounce
.buffer
= NULL
;
3320 cpu_notify_map_clients();
3323 /* warning: addr must be aligned */
3324 uint32_t ldl_phys(target_phys_addr_t addr
)
3332 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3334 pd
= IO_MEM_UNASSIGNED
;
3336 pd
= p
->phys_offset
;
3339 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3340 !(pd
& IO_MEM_ROMD
)) {
3342 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3344 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3345 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3348 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3349 (addr
& ~TARGET_PAGE_MASK
);
3355 /* warning: addr must be aligned */
3356 uint64_t ldq_phys(target_phys_addr_t addr
)
3364 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3366 pd
= IO_MEM_UNASSIGNED
;
3368 pd
= p
->phys_offset
;
3371 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3372 !(pd
& IO_MEM_ROMD
)) {
3374 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3376 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3377 #ifdef TARGET_WORDS_BIGENDIAN
3378 val
= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
) << 32;
3379 val
|= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4);
3381 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3382 val
|= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4) << 32;
3386 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3387 (addr
& ~TARGET_PAGE_MASK
);
3394 uint32_t ldub_phys(target_phys_addr_t addr
)
3397 cpu_physical_memory_read(addr
, &val
, 1);
3402 uint32_t lduw_phys(target_phys_addr_t addr
)
3405 cpu_physical_memory_read(addr
, (uint8_t *)&val
, 2);
3406 return tswap16(val
);
3409 /* warning: addr must be aligned. The ram page is not masked as dirty
3410 and the code inside is not invalidated. It is useful if the dirty
3411 bits are used to track modified PTEs */
3412 void stl_phys_notdirty(target_phys_addr_t addr
, uint32_t val
)
3419 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3421 pd
= IO_MEM_UNASSIGNED
;
3423 pd
= p
->phys_offset
;
3426 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3427 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3429 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3430 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3432 unsigned long addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3433 ptr
= qemu_get_ram_ptr(addr1
);
3436 if (unlikely(in_migration
)) {
3437 if (!cpu_physical_memory_is_dirty(addr1
)) {
3438 /* invalidate code */
3439 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3441 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3442 (0xff & ~CODE_DIRTY_FLAG
);
3448 void stq_phys_notdirty(target_phys_addr_t addr
, uint64_t val
)
3455 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3457 pd
= IO_MEM_UNASSIGNED
;
3459 pd
= p
->phys_offset
;
3462 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3463 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3465 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3466 #ifdef TARGET_WORDS_BIGENDIAN
3467 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
>> 32);
3468 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
);
3470 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3471 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
>> 32);
3474 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3475 (addr
& ~TARGET_PAGE_MASK
);
3480 /* warning: addr must be aligned */
3481 void stl_phys(target_phys_addr_t addr
, uint32_t val
)
3488 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3490 pd
= IO_MEM_UNASSIGNED
;
3492 pd
= p
->phys_offset
;
3495 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3496 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3498 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3499 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3501 unsigned long addr1
;
3502 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3504 ptr
= qemu_get_ram_ptr(addr1
);
3506 if (!cpu_physical_memory_is_dirty(addr1
)) {
3507 /* invalidate code */
3508 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3510 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3511 (0xff & ~CODE_DIRTY_FLAG
);
3517 void stb_phys(target_phys_addr_t addr
, uint32_t val
)
3520 cpu_physical_memory_write(addr
, &v
, 1);
3524 void stw_phys(target_phys_addr_t addr
, uint32_t val
)
3526 uint16_t v
= tswap16(val
);
3527 cpu_physical_memory_write(addr
, (const uint8_t *)&v
, 2);
3531 void stq_phys(target_phys_addr_t addr
, uint64_t val
)
3534 cpu_physical_memory_write(addr
, (const uint8_t *)&val
, 8);
3539 /* virtual memory access for debug (includes writing to ROM) */
3540 int cpu_memory_rw_debug(CPUState
*env
, target_ulong addr
,
3541 uint8_t *buf
, int len
, int is_write
)
3544 target_phys_addr_t phys_addr
;
3548 page
= addr
& TARGET_PAGE_MASK
;
3549 phys_addr
= cpu_get_phys_page_debug(env
, page
);
3550 /* if no physical page mapped, return an error */
3551 if (phys_addr
== -1)
3553 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3556 phys_addr
+= (addr
& ~TARGET_PAGE_MASK
);
3557 #if !defined(CONFIG_USER_ONLY)
3559 cpu_physical_memory_write_rom(phys_addr
, buf
, l
);
3562 cpu_physical_memory_rw(phys_addr
, buf
, l
, is_write
);
3570 /* in deterministic execution mode, instructions doing device I/Os
3571 must be at the end of the TB */
3572 void cpu_io_recompile(CPUState
*env
, void *retaddr
)
3574 TranslationBlock
*tb
;
3576 target_ulong pc
, cs_base
;
3579 tb
= tb_find_pc((unsigned long)retaddr
);
3581 cpu_abort(env
, "cpu_io_recompile: could not find TB for pc=%p",
3584 n
= env
->icount_decr
.u16
.low
+ tb
->icount
;
3585 cpu_restore_state(tb
, env
, (unsigned long)retaddr
, NULL
);
3586 /* Calculate how many instructions had been executed before the fault
3588 n
= n
- env
->icount_decr
.u16
.low
;
3589 /* Generate a new TB ending on the I/O insn. */
3591 /* On MIPS and SH, delay slot instructions can only be restarted if
3592 they were already the first instruction in the TB. If this is not
3593 the first instruction in a TB then re-execute the preceding
3595 #if defined(TARGET_MIPS)
3596 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0 && n
> 1) {
3597 env
->active_tc
.PC
-= 4;
3598 env
->icount_decr
.u16
.low
++;
3599 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
3601 #elif defined(TARGET_SH4)
3602 if ((env
->flags
& ((DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
))) != 0
3605 env
->icount_decr
.u16
.low
++;
3606 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
);
3609 /* This should never happen. */
3610 if (n
> CF_COUNT_MASK
)
3611 cpu_abort(env
, "TB too big during recompile");
3613 cflags
= n
| CF_LAST_IO
;
3615 cs_base
= tb
->cs_base
;
3617 tb_phys_invalidate(tb
, -1);
3618 /* FIXME: In theory this could raise an exception. In practice
3619 we have already translated the block once so it's probably ok. */
3620 tb_gen_code(env
, pc
, cs_base
, flags
, cflags
);
3621 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3622 the first in the TB) then we end up generating a whole new TB and
3623 repeating the fault, which is horribly inefficient.
3624 Better would be to execute just this insn uncached, or generate a
3626 cpu_resume_from_signal(env
, NULL
);
3629 void dump_exec_info(FILE *f
,
3630 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
3632 int i
, target_code_size
, max_target_code_size
;
3633 int direct_jmp_count
, direct_jmp2_count
, cross_page
;
3634 TranslationBlock
*tb
;
3636 target_code_size
= 0;
3637 max_target_code_size
= 0;
3639 direct_jmp_count
= 0;
3640 direct_jmp2_count
= 0;
3641 for(i
= 0; i
< nb_tbs
; i
++) {
3643 target_code_size
+= tb
->size
;
3644 if (tb
->size
> max_target_code_size
)
3645 max_target_code_size
= tb
->size
;
3646 if (tb
->page_addr
[1] != -1)
3648 if (tb
->tb_next_offset
[0] != 0xffff) {
3650 if (tb
->tb_next_offset
[1] != 0xffff) {
3651 direct_jmp2_count
++;
3655 /* XXX: avoid using doubles ? */
3656 cpu_fprintf(f
, "Translation buffer state:\n");
3657 cpu_fprintf(f
, "gen code size %ld/%ld\n",
3658 code_gen_ptr
- code_gen_buffer
, code_gen_buffer_max_size
);
3659 cpu_fprintf(f
, "TB count %d/%d\n",
3660 nb_tbs
, code_gen_max_blocks
);
3661 cpu_fprintf(f
, "TB avg target size %d max=%d bytes\n",
3662 nb_tbs
? target_code_size
/ nb_tbs
: 0,
3663 max_target_code_size
);
3664 cpu_fprintf(f
, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3665 nb_tbs
? (code_gen_ptr
- code_gen_buffer
) / nb_tbs
: 0,
3666 target_code_size
? (double) (code_gen_ptr
- code_gen_buffer
) / target_code_size
: 0);
3667 cpu_fprintf(f
, "cross page TB count %d (%d%%)\n",
3669 nb_tbs
? (cross_page
* 100) / nb_tbs
: 0);
3670 cpu_fprintf(f
, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3672 nb_tbs
? (direct_jmp_count
* 100) / nb_tbs
: 0,
3674 nb_tbs
? (direct_jmp2_count
* 100) / nb_tbs
: 0);
3675 cpu_fprintf(f
, "\nStatistics:\n");
3676 cpu_fprintf(f
, "TB flush count %d\n", tb_flush_count
);
3677 cpu_fprintf(f
, "TB invalidate count %d\n", tb_phys_invalidate_count
);
3678 cpu_fprintf(f
, "TLB flush count %d\n", tlb_flush_count
);
3679 tcg_dump_info(f
, cpu_fprintf
);
3682 #if !defined(CONFIG_USER_ONLY)
3684 #define MMUSUFFIX _cmmu
3685 #define GETPC() NULL
3686 #define env cpu_single_env
3687 #define SOFTMMU_CODE_ACCESS
3690 #include "softmmu_template.h"
3693 #include "softmmu_template.h"
3696 #include "softmmu_template.h"
3699 #include "softmmu_template.h"