2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define WIN32_LEAN_AND_MEAN
25 #include <sys/types.h>
38 #include "qemu-common.h"
40 #if !defined(TARGET_IA64)
46 #if defined(CONFIG_USER_ONLY)
50 //#define DEBUG_TB_INVALIDATE
53 //#define DEBUG_UNASSIGNED
55 /* make various TB consistency checks */
56 //#define DEBUG_TB_CHECK
57 //#define DEBUG_TLB_CHECK
59 //#define DEBUG_IOPORT
60 //#define DEBUG_SUBPAGE
62 #if !defined(CONFIG_USER_ONLY)
63 /* TB consistency checks only implemented for usermode emulation. */
67 #define SMC_BITMAP_USE_THRESHOLD 10
69 #define MMAP_AREA_START 0x00000000
70 #define MMAP_AREA_END 0xa8000000
72 #if defined(TARGET_SPARC64)
73 #define TARGET_PHYS_ADDR_SPACE_BITS 41
74 #elif defined(TARGET_SPARC)
75 #define TARGET_PHYS_ADDR_SPACE_BITS 36
76 #elif defined(TARGET_ALPHA)
77 #define TARGET_PHYS_ADDR_SPACE_BITS 42
78 #define TARGET_VIRT_ADDR_SPACE_BITS 42
79 #elif defined(TARGET_PPC64)
80 #define TARGET_PHYS_ADDR_SPACE_BITS 42
81 #elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
82 #define TARGET_PHYS_ADDR_SPACE_BITS 42
83 #elif defined(TARGET_I386) && !defined(USE_KQEMU)
84 #define TARGET_PHYS_ADDR_SPACE_BITS 36
86 /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
87 #define TARGET_PHYS_ADDR_SPACE_BITS 32
90 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)))
105 #define code_gen_section \
106 __attribute__((aligned (32)))
109 uint8_t code_gen_prologue
[1024] code_gen_section
;
110 uint8_t *code_gen_buffer
;
111 unsigned long code_gen_buffer_size
;
112 /* threshold to flush the translated code buffer */
113 unsigned long code_gen_buffer_max_size
;
114 uint8_t *code_gen_ptr
;
116 #if !defined(CONFIG_USER_ONLY)
117 ram_addr_t phys_ram_size
;
119 uint8_t *phys_ram_base
;
120 uint8_t *phys_ram_dirty
;
122 static int in_migration
;
123 static ram_addr_t phys_ram_alloc_offset
= 0;
127 /* current CPU in the current thread. It is only valid inside
129 CPUState
*cpu_single_env
;
130 /* 0 = Do not count executed instructions.
131 1 = Precise instruction counting.
132 2 = Adaptive rate instruction counting. */
134 /* Current instruction counter. While executing translated code this may
135 include some instructions that have not yet been executed. */
138 typedef struct PageDesc
{
139 /* list of TBs intersecting this ram page */
140 TranslationBlock
*first_tb
;
141 /* in order to optimize self modifying code, we count the number
142 of lookups we do to a given page to use a bitmap */
143 unsigned int code_write_count
;
144 uint8_t *code_bitmap
;
145 #if defined(CONFIG_USER_ONLY)
150 typedef struct PhysPageDesc
{
151 /* offset in host memory of the page + io_index in the low bits */
152 ram_addr_t phys_offset
;
156 #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
157 /* XXX: this is a temporary hack for alpha target.
158 * In the future, this is to be replaced by a multi-level table
159 * to actually be able to handle the complete 64 bits address space.
161 #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
163 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
166 #define L1_SIZE (1 << L1_BITS)
167 #define L2_SIZE (1 << L2_BITS)
169 unsigned long qemu_real_host_page_size
;
170 unsigned long qemu_host_page_bits
;
171 unsigned long qemu_host_page_size
;
172 unsigned long qemu_host_page_mask
;
174 /* XXX: for system emulation, it could just be an array */
175 static PageDesc
*l1_map
[L1_SIZE
];
176 PhysPageDesc
**l1_phys_map
;
178 #if !defined(CONFIG_USER_ONLY)
179 static void io_mem_init(void);
181 /* io memory support */
182 CPUWriteMemoryFunc
*io_mem_write
[IO_MEM_NB_ENTRIES
][4];
183 CPUReadMemoryFunc
*io_mem_read
[IO_MEM_NB_ENTRIES
][4];
184 void *io_mem_opaque
[IO_MEM_NB_ENTRIES
];
185 static int io_mem_nb
;
186 char io_mem_used
[IO_MEM_NB_ENTRIES
];
187 static int io_mem_watch
;
191 char *logfilename
= "/tmp/qemu.log";
194 static int log_append
= 0;
197 static int tlb_flush_count
;
198 static int tb_flush_count
;
199 static int tb_phys_invalidate_count
;
201 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
202 typedef struct subpage_t
{
203 target_phys_addr_t base
;
204 CPUReadMemoryFunc
**mem_read
[TARGET_PAGE_SIZE
][4];
205 CPUWriteMemoryFunc
**mem_write
[TARGET_PAGE_SIZE
][4];
206 void *opaque
[TARGET_PAGE_SIZE
][2][4];
210 static void map_exec(void *addr
, long size
)
213 VirtualProtect(addr
, size
,
214 PAGE_EXECUTE_READWRITE
, &old_protect
);
218 static void map_exec(void *addr
, long size
)
220 unsigned long start
, end
, page_size
;
222 page_size
= getpagesize();
223 start
= (unsigned long)addr
;
224 start
&= ~(page_size
- 1);
226 end
= (unsigned long)addr
+ size
;
227 end
+= page_size
- 1;
228 end
&= ~(page_size
- 1);
230 mprotect((void *)start
, end
- start
,
231 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
235 static void page_init(void)
237 /* NOTE: we can always suppose that qemu_host_page_size >=
241 SYSTEM_INFO system_info
;
244 GetSystemInfo(&system_info
);
245 qemu_real_host_page_size
= system_info
.dwPageSize
;
248 qemu_real_host_page_size
= getpagesize();
250 if (qemu_host_page_size
== 0)
251 qemu_host_page_size
= qemu_real_host_page_size
;
252 if (qemu_host_page_size
< TARGET_PAGE_SIZE
)
253 qemu_host_page_size
= TARGET_PAGE_SIZE
;
254 qemu_host_page_bits
= 0;
255 while ((1 << qemu_host_page_bits
) < qemu_host_page_size
)
256 qemu_host_page_bits
++;
257 qemu_host_page_mask
= ~(qemu_host_page_size
- 1);
258 l1_phys_map
= qemu_vmalloc(L1_SIZE
* sizeof(void *));
259 memset(l1_phys_map
, 0, L1_SIZE
* sizeof(void *));
261 #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
263 long long startaddr
, endaddr
;
268 last_brk
= (unsigned long)sbrk(0);
269 f
= fopen("/proc/self/maps", "r");
272 n
= fscanf (f
, "%llx-%llx %*[^\n]\n", &startaddr
, &endaddr
);
274 startaddr
= MIN(startaddr
,
275 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
276 endaddr
= MIN(endaddr
,
277 (1ULL << TARGET_PHYS_ADDR_SPACE_BITS
) - 1);
278 page_set_flags(startaddr
& TARGET_PAGE_MASK
,
279 TARGET_PAGE_ALIGN(endaddr
),
290 static inline PageDesc
*page_find_alloc(target_ulong index
)
294 #if TARGET_LONG_BITS > 32
295 /* Host memory outside guest VM. For 32-bit targets we have already
296 excluded high addresses. */
297 if (index
> ((target_ulong
)L2_SIZE
* L1_SIZE
* TARGET_PAGE_SIZE
))
300 lp
= &l1_map
[index
>> L2_BITS
];
303 /* allocate if not found */
304 #if defined(CONFIG_USER_ONLY)
306 size_t len
= sizeof(PageDesc
) * L2_SIZE
;
307 /* Don't use qemu_malloc because it may recurse. */
308 p
= mmap(0, len
, PROT_READ
| PROT_WRITE
,
309 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
312 if (addr
== (target_ulong
)addr
) {
313 page_set_flags(addr
& TARGET_PAGE_MASK
,
314 TARGET_PAGE_ALIGN(addr
+ len
),
318 p
= qemu_mallocz(sizeof(PageDesc
) * L2_SIZE
);
322 return p
+ (index
& (L2_SIZE
- 1));
325 static inline PageDesc
*page_find(target_ulong index
)
329 p
= l1_map
[index
>> L2_BITS
];
332 return p
+ (index
& (L2_SIZE
- 1));
335 static PhysPageDesc
*phys_page_find_alloc(target_phys_addr_t index
, int alloc
)
340 p
= (void **)l1_phys_map
;
341 #if TARGET_PHYS_ADDR_SPACE_BITS > 32
343 #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
344 #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
346 lp
= p
+ ((index
>> (L1_BITS
+ L2_BITS
)) & (L1_SIZE
- 1));
349 /* allocate if not found */
352 p
= qemu_vmalloc(sizeof(void *) * L1_SIZE
);
353 memset(p
, 0, sizeof(void *) * L1_SIZE
);
357 lp
= p
+ ((index
>> L2_BITS
) & (L1_SIZE
- 1));
361 /* allocate if not found */
364 pd
= qemu_vmalloc(sizeof(PhysPageDesc
) * L2_SIZE
);
366 for (i
= 0; i
< L2_SIZE
; i
++)
367 pd
[i
].phys_offset
= IO_MEM_UNASSIGNED
;
369 return ((PhysPageDesc
*)pd
) + (index
& (L2_SIZE
- 1));
372 static inline PhysPageDesc
*phys_page_find(target_phys_addr_t index
)
374 return phys_page_find_alloc(index
, 0);
377 #if !defined(CONFIG_USER_ONLY)
378 static void tlb_protect_code(ram_addr_t ram_addr
);
379 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
381 #define mmap_lock() do { } while(0)
382 #define mmap_unlock() do { } while(0)
385 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
387 #if defined(CONFIG_USER_ONLY)
388 /* Currently it is not recommanded to allocate big chunks of data in
389 user mode. It will change when a dedicated libc will be used */
390 #define USE_STATIC_CODE_GEN_BUFFER
393 #ifdef USE_STATIC_CODE_GEN_BUFFER
394 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
];
397 void code_gen_alloc(unsigned long tb_size
)
399 #ifdef USE_STATIC_CODE_GEN_BUFFER
400 code_gen_buffer
= static_code_gen_buffer
;
401 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
402 map_exec(code_gen_buffer
, code_gen_buffer_size
);
404 code_gen_buffer_size
= tb_size
;
405 if (code_gen_buffer_size
== 0) {
406 #if defined(CONFIG_USER_ONLY)
407 /* in user mode, phys_ram_size is not meaningful */
408 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
410 /* XXX: needs ajustments */
411 code_gen_buffer_size
= (int)(phys_ram_size
/ 4);
414 if (code_gen_buffer_size
< MIN_CODE_GEN_BUFFER_SIZE
)
415 code_gen_buffer_size
= MIN_CODE_GEN_BUFFER_SIZE
;
416 /* The code gen buffer location may have constraints depending on
417 the host cpu and OS */
418 #if defined(__linux__)
423 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
424 #if defined(__x86_64__)
426 /* Cannot map more than that */
427 if (code_gen_buffer_size
> (800 * 1024 * 1024))
428 code_gen_buffer_size
= (800 * 1024 * 1024);
429 #elif defined(__sparc_v9__)
430 // Map the buffer below 2G, so we can use direct calls and branches
432 start
= (void *) 0x60000000UL
;
433 if (code_gen_buffer_size
> (512 * 1024 * 1024))
434 code_gen_buffer_size
= (512 * 1024 * 1024);
436 code_gen_buffer
= mmap(start
, code_gen_buffer_size
,
437 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
439 if (code_gen_buffer
== MAP_FAILED
) {
440 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
445 code_gen_buffer
= qemu_malloc(code_gen_buffer_size
);
446 if (!code_gen_buffer
) {
447 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
450 map_exec(code_gen_buffer
, code_gen_buffer_size
);
452 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
453 map_exec(code_gen_prologue
, sizeof(code_gen_prologue
));
454 code_gen_buffer_max_size
= code_gen_buffer_size
-
455 code_gen_max_block_size();
456 code_gen_max_blocks
= code_gen_buffer_size
/ CODE_GEN_AVG_BLOCK_SIZE
;
457 tbs
= qemu_malloc(code_gen_max_blocks
* sizeof(TranslationBlock
));
460 /* Must be called before using the QEMU cpus. 'tb_size' is the size
461 (in bytes) allocated to the translation buffer. Zero means default
463 void cpu_exec_init_all(unsigned long tb_size
)
466 code_gen_alloc(tb_size
);
467 code_gen_ptr
= code_gen_buffer
;
469 #if !defined(CONFIG_USER_ONLY)
474 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
476 #define CPU_COMMON_SAVE_VERSION 1
478 static void cpu_common_save(QEMUFile
*f
, void *opaque
)
480 CPUState
*env
= opaque
;
482 qemu_put_be32s(f
, &env
->halted
);
483 qemu_put_be32s(f
, &env
->interrupt_request
);
486 static int cpu_common_load(QEMUFile
*f
, void *opaque
, int version_id
)
488 CPUState
*env
= opaque
;
490 if (version_id
!= CPU_COMMON_SAVE_VERSION
)
493 qemu_get_be32s(f
, &env
->halted
);
494 qemu_get_be32s(f
, &env
->interrupt_request
);
501 void cpu_exec_init(CPUState
*env
)
506 env
->next_cpu
= NULL
;
509 while (*penv
!= NULL
) {
510 penv
= (CPUState
**)&(*penv
)->next_cpu
;
513 env
->cpu_index
= cpu_index
;
514 env
->nb_watchpoints
= 0;
516 env
->thread_id
= GetCurrentProcessId();
518 env
->thread_id
= getpid();
521 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
522 register_savevm("cpu_common", cpu_index
, CPU_COMMON_SAVE_VERSION
,
523 cpu_common_save
, cpu_common_load
, env
);
524 register_savevm("cpu", cpu_index
, CPU_SAVE_VERSION
,
525 cpu_save
, cpu_load
, env
);
529 static inline void invalidate_page_bitmap(PageDesc
*p
)
531 if (p
->code_bitmap
) {
532 qemu_free(p
->code_bitmap
);
533 p
->code_bitmap
= NULL
;
535 p
->code_write_count
= 0;
538 /* set to NULL all the 'first_tb' fields in all PageDescs */
539 static void page_flush_tb(void)
544 for(i
= 0; i
< L1_SIZE
; i
++) {
547 for(j
= 0; j
< L2_SIZE
; j
++) {
549 invalidate_page_bitmap(p
);
556 /* flush all the translation blocks */
557 /* XXX: tb_flush is currently not thread safe */
558 void tb_flush(CPUState
*env1
)
561 #if defined(DEBUG_FLUSH)
562 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
563 (unsigned long)(code_gen_ptr
- code_gen_buffer
),
565 ((unsigned long)(code_gen_ptr
- code_gen_buffer
)) / nb_tbs
: 0);
567 if ((unsigned long)(code_gen_ptr
- code_gen_buffer
) > code_gen_buffer_size
)
568 cpu_abort(env1
, "Internal error: code buffer overflow\n");
572 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
573 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
576 memset (tb_phys_hash
, 0, CODE_GEN_PHYS_HASH_SIZE
* sizeof (void *));
579 code_gen_ptr
= code_gen_buffer
;
580 /* XXX: flush processor icache at this point if cache flush is
585 #ifdef DEBUG_TB_CHECK
587 static void tb_invalidate_check(target_ulong address
)
589 TranslationBlock
*tb
;
591 address
&= TARGET_PAGE_MASK
;
592 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
593 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
594 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
595 address
>= tb
->pc
+ tb
->size
)) {
596 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
597 address
, (long)tb
->pc
, tb
->size
);
603 /* verify that all the pages have correct rights for code */
604 static void tb_page_check(void)
606 TranslationBlock
*tb
;
607 int i
, flags1
, flags2
;
609 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
610 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
611 flags1
= page_get_flags(tb
->pc
);
612 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
613 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
614 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
615 (long)tb
->pc
, tb
->size
, flags1
, flags2
);
621 void tb_jmp_check(TranslationBlock
*tb
)
623 TranslationBlock
*tb1
;
626 /* suppress any remaining jumps to this TB */
630 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
633 tb1
= tb1
->jmp_next
[n1
];
635 /* check end of list */
637 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb
);
643 /* invalidate one TB */
644 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
647 TranslationBlock
*tb1
;
651 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
654 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
658 static inline void tb_page_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
660 TranslationBlock
*tb1
;
666 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
668 *ptb
= tb1
->page_next
[n1
];
671 ptb
= &tb1
->page_next
[n1
];
675 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
677 TranslationBlock
*tb1
, **ptb
;
680 ptb
= &tb
->jmp_next
[n
];
683 /* find tb(n) in circular list */
687 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
688 if (n1
== n
&& tb1
== tb
)
691 ptb
= &tb1
->jmp_first
;
693 ptb
= &tb1
->jmp_next
[n1
];
696 /* now we can suppress tb(n) from the list */
697 *ptb
= tb
->jmp_next
[n
];
699 tb
->jmp_next
[n
] = NULL
;
703 /* reset the jump entry 'n' of a TB so that it is not chained to
705 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
707 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
710 void tb_phys_invalidate(TranslationBlock
*tb
, target_ulong page_addr
)
715 target_phys_addr_t phys_pc
;
716 TranslationBlock
*tb1
, *tb2
;
718 /* remove the TB from the hash list */
719 phys_pc
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
720 h
= tb_phys_hash_func(phys_pc
);
721 tb_remove(&tb_phys_hash
[h
], tb
,
722 offsetof(TranslationBlock
, phys_hash_next
));
724 /* remove the TB from the page list */
725 if (tb
->page_addr
[0] != page_addr
) {
726 p
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
727 tb_page_remove(&p
->first_tb
, tb
);
728 invalidate_page_bitmap(p
);
730 if (tb
->page_addr
[1] != -1 && tb
->page_addr
[1] != page_addr
) {
731 p
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
732 tb_page_remove(&p
->first_tb
, tb
);
733 invalidate_page_bitmap(p
);
736 tb_invalidated_flag
= 1;
738 /* remove the TB from the hash list */
739 h
= tb_jmp_cache_hash_func(tb
->pc
);
740 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
741 if (env
->tb_jmp_cache
[h
] == tb
)
742 env
->tb_jmp_cache
[h
] = NULL
;
745 /* suppress this TB from the two jump lists */
746 tb_jmp_remove(tb
, 0);
747 tb_jmp_remove(tb
, 1);
749 /* suppress any remaining jumps to this TB */
755 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
756 tb2
= tb1
->jmp_next
[n1
];
757 tb_reset_jump(tb1
, n1
);
758 tb1
->jmp_next
[n1
] = NULL
;
761 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
763 tb_phys_invalidate_count
++;
766 static inline void set_bits(uint8_t *tab
, int start
, int len
)
772 mask
= 0xff << (start
& 7);
773 if ((start
& ~7) == (end
& ~7)) {
775 mask
&= ~(0xff << (end
& 7));
780 start
= (start
+ 8) & ~7;
782 while (start
< end1
) {
787 mask
= ~(0xff << (end
& 7));
793 static void build_page_bitmap(PageDesc
*p
)
795 int n
, tb_start
, tb_end
;
796 TranslationBlock
*tb
;
798 p
->code_bitmap
= qemu_mallocz(TARGET_PAGE_SIZE
/ 8);
805 tb
= (TranslationBlock
*)((long)tb
& ~3);
806 /* NOTE: this is subtle as a TB may span two physical pages */
808 /* NOTE: tb_end may be after the end of the page, but
809 it is not a problem */
810 tb_start
= tb
->pc
& ~TARGET_PAGE_MASK
;
811 tb_end
= tb_start
+ tb
->size
;
812 if (tb_end
> TARGET_PAGE_SIZE
)
813 tb_end
= TARGET_PAGE_SIZE
;
816 tb_end
= ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
818 set_bits(p
->code_bitmap
, tb_start
, tb_end
- tb_start
);
819 tb
= tb
->page_next
[n
];
823 TranslationBlock
*tb_gen_code(CPUState
*env
,
824 target_ulong pc
, target_ulong cs_base
,
825 int flags
, int cflags
)
827 TranslationBlock
*tb
;
829 target_ulong phys_pc
, phys_page2
, virt_page2
;
832 phys_pc
= get_phys_addr_code(env
, pc
);
835 /* flush must be done */
837 /* cannot fail at this point */
839 /* Don't forget to invalidate previous TB info. */
840 tb_invalidated_flag
= 1;
842 tc_ptr
= code_gen_ptr
;
844 tb
->cs_base
= cs_base
;
847 cpu_gen_code(env
, tb
, &code_gen_size
);
848 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
850 /* check next page if needed */
851 virt_page2
= (pc
+ tb
->size
- 1) & TARGET_PAGE_MASK
;
853 if ((pc
& TARGET_PAGE_MASK
) != virt_page2
) {
854 phys_page2
= get_phys_addr_code(env
, virt_page2
);
856 tb_link_phys(tb
, phys_pc
, phys_page2
);
860 /* invalidate all TBs which intersect with the target physical page
861 starting in range [start;end[. NOTE: start and end must refer to
862 the same physical page. 'is_cpu_write_access' should be true if called
863 from a real cpu write access: the virtual CPU will exit the current
864 TB if code is modified inside this TB. */
865 void tb_invalidate_phys_page_range(target_phys_addr_t start
, target_phys_addr_t end
,
866 int is_cpu_write_access
)
868 int n
, current_tb_modified
, current_tb_not_found
, current_flags
;
869 CPUState
*env
= cpu_single_env
;
871 TranslationBlock
*tb
, *tb_next
, *current_tb
, *saved_tb
;
872 target_ulong tb_start
, tb_end
;
873 target_ulong current_pc
, current_cs_base
;
875 p
= page_find(start
>> TARGET_PAGE_BITS
);
878 if (!p
->code_bitmap
&&
879 ++p
->code_write_count
>= SMC_BITMAP_USE_THRESHOLD
&&
880 is_cpu_write_access
) {
881 /* build code bitmap */
882 build_page_bitmap(p
);
885 /* we remove all the TBs in the range [start, end[ */
886 /* XXX: see if in some cases it could be faster to invalidate all the code */
887 current_tb_not_found
= is_cpu_write_access
;
888 current_tb_modified
= 0;
889 current_tb
= NULL
; /* avoid warning */
890 current_pc
= 0; /* avoid warning */
891 current_cs_base
= 0; /* avoid warning */
892 current_flags
= 0; /* avoid warning */
896 tb
= (TranslationBlock
*)((long)tb
& ~3);
897 tb_next
= tb
->page_next
[n
];
898 /* NOTE: this is subtle as a TB may span two physical pages */
900 /* NOTE: tb_end may be after the end of the page, but
901 it is not a problem */
902 tb_start
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
903 tb_end
= tb_start
+ tb
->size
;
905 tb_start
= tb
->page_addr
[1];
906 tb_end
= tb_start
+ ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
908 if (!(tb_end
<= start
|| tb_start
>= end
)) {
909 #ifdef TARGET_HAS_PRECISE_SMC
910 if (current_tb_not_found
) {
911 current_tb_not_found
= 0;
913 if (env
->mem_io_pc
) {
914 /* now we have a real cpu fault */
915 current_tb
= tb_find_pc(env
->mem_io_pc
);
918 if (current_tb
== tb
&&
919 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
920 /* If we are modifying the current TB, we must stop
921 its execution. We could be more precise by checking
922 that the modification is after the current PC, but it
923 would require a specialized function to partially
924 restore the CPU state */
926 current_tb_modified
= 1;
927 cpu_restore_state(current_tb
, env
,
928 env
->mem_io_pc
, NULL
);
929 #if defined(TARGET_I386)
930 current_flags
= env
->hflags
;
931 current_flags
|= (env
->eflags
& (IOPL_MASK
| TF_MASK
| VM_MASK
));
932 current_cs_base
= (target_ulong
)env
->segs
[R_CS
].base
;
933 current_pc
= current_cs_base
+ env
->eip
;
935 #error unsupported CPU
938 #endif /* TARGET_HAS_PRECISE_SMC */
939 /* we need to do that to handle the case where a signal
940 occurs while doing tb_phys_invalidate() */
943 saved_tb
= env
->current_tb
;
944 env
->current_tb
= NULL
;
946 tb_phys_invalidate(tb
, -1);
948 env
->current_tb
= saved_tb
;
949 if (env
->interrupt_request
&& env
->current_tb
)
950 cpu_interrupt(env
, env
->interrupt_request
);
955 #if !defined(CONFIG_USER_ONLY)
956 /* if no code remaining, no need to continue to use slow writes */
958 invalidate_page_bitmap(p
);
959 if (is_cpu_write_access
) {
960 tlb_unprotect_code_phys(env
, start
, env
->mem_io_vaddr
);
964 #ifdef TARGET_HAS_PRECISE_SMC
965 if (current_tb_modified
) {
966 /* we generate a block containing just the instruction
967 modifying the memory. It will ensure that it cannot modify
969 env
->current_tb
= NULL
;
970 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
971 cpu_resume_from_signal(env
, NULL
);
976 /* len must be <= 8 and start must be a multiple of len */
977 static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start
, int len
)
984 fprintf(logfile
, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
985 cpu_single_env
->mem_io_vaddr
, len
,
987 cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
);
991 p
= page_find(start
>> TARGET_PAGE_BITS
);
994 if (p
->code_bitmap
) {
995 offset
= start
& ~TARGET_PAGE_MASK
;
996 b
= p
->code_bitmap
[offset
>> 3] >> (offset
& 7);
997 if (b
& ((1 << len
) - 1))
1001 tb_invalidate_phys_page_range(start
, start
+ len
, 1);
1005 #if !defined(CONFIG_SOFTMMU)
1006 static void tb_invalidate_phys_page(target_phys_addr_t addr
,
1007 unsigned long pc
, void *puc
)
1009 int n
, current_flags
, current_tb_modified
;
1010 target_ulong current_pc
, current_cs_base
;
1012 TranslationBlock
*tb
, *current_tb
;
1013 #ifdef TARGET_HAS_PRECISE_SMC
1014 CPUState
*env
= cpu_single_env
;
1017 addr
&= TARGET_PAGE_MASK
;
1018 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1022 current_tb_modified
= 0;
1024 current_pc
= 0; /* avoid warning */
1025 current_cs_base
= 0; /* avoid warning */
1026 current_flags
= 0; /* avoid warning */
1027 #ifdef TARGET_HAS_PRECISE_SMC
1028 if (tb
&& pc
!= 0) {
1029 current_tb
= tb_find_pc(pc
);
1032 while (tb
!= NULL
) {
1034 tb
= (TranslationBlock
*)((long)tb
& ~3);
1035 #ifdef TARGET_HAS_PRECISE_SMC
1036 if (current_tb
== tb
&&
1037 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1038 /* If we are modifying the current TB, we must stop
1039 its execution. We could be more precise by checking
1040 that the modification is after the current PC, but it
1041 would require a specialized function to partially
1042 restore the CPU state */
1044 current_tb_modified
= 1;
1045 cpu_restore_state(current_tb
, env
, pc
, puc
);
1046 #if defined(TARGET_I386)
1047 current_flags
= env
->hflags
;
1048 current_flags
|= (env
->eflags
& (IOPL_MASK
| TF_MASK
| VM_MASK
));
1049 current_cs_base
= (target_ulong
)env
->segs
[R_CS
].base
;
1050 current_pc
= current_cs_base
+ env
->eip
;
1052 #error unsupported CPU
1055 #endif /* TARGET_HAS_PRECISE_SMC */
1056 tb_phys_invalidate(tb
, addr
);
1057 tb
= tb
->page_next
[n
];
1060 #ifdef TARGET_HAS_PRECISE_SMC
1061 if (current_tb_modified
) {
1062 /* we generate a block containing just the instruction
1063 modifying the memory. It will ensure that it cannot modify
1065 env
->current_tb
= NULL
;
1066 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1067 cpu_resume_from_signal(env
, puc
);
1073 /* add the tb in the target page and protect it if necessary */
1074 static inline void tb_alloc_page(TranslationBlock
*tb
,
1075 unsigned int n
, target_ulong page_addr
)
1078 TranslationBlock
*last_first_tb
;
1080 tb
->page_addr
[n
] = page_addr
;
1081 p
= page_find_alloc(page_addr
>> TARGET_PAGE_BITS
);
1082 tb
->page_next
[n
] = p
->first_tb
;
1083 last_first_tb
= p
->first_tb
;
1084 p
->first_tb
= (TranslationBlock
*)((long)tb
| n
);
1085 invalidate_page_bitmap(p
);
1087 #if defined(TARGET_HAS_SMC) || 1
1089 #if defined(CONFIG_USER_ONLY)
1090 if (p
->flags
& PAGE_WRITE
) {
1095 /* force the host page as non writable (writes will have a
1096 page fault + mprotect overhead) */
1097 page_addr
&= qemu_host_page_mask
;
1099 for(addr
= page_addr
; addr
< page_addr
+ qemu_host_page_size
;
1100 addr
+= TARGET_PAGE_SIZE
) {
1102 p2
= page_find (addr
>> TARGET_PAGE_BITS
);
1106 p2
->flags
&= ~PAGE_WRITE
;
1107 page_get_flags(addr
);
1109 mprotect(g2h(page_addr
), qemu_host_page_size
,
1110 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
1111 #ifdef DEBUG_TB_INVALIDATE
1112 printf("protecting code page: 0x" TARGET_FMT_lx
"\n",
1117 /* if some code is already present, then the pages are already
1118 protected. So we handle the case where only the first TB is
1119 allocated in a physical page */
1120 if (!last_first_tb
) {
1121 tlb_protect_code(page_addr
);
1125 #endif /* TARGET_HAS_SMC */
1128 /* Allocate a new translation block. Flush the translation buffer if
1129 too many translation blocks or too much generated code. */
1130 TranslationBlock
*tb_alloc(target_ulong pc
)
1132 TranslationBlock
*tb
;
1134 if (nb_tbs
>= code_gen_max_blocks
||
1135 (code_gen_ptr
- code_gen_buffer
) >= code_gen_buffer_max_size
)
1137 tb
= &tbs
[nb_tbs
++];
1143 void tb_free(TranslationBlock
*tb
)
1145 /* In practice this is mostly used for single use temporary TB
1146 Ignore the hard cases and just back up if this TB happens to
1147 be the last one generated. */
1148 if (nb_tbs
> 0 && tb
== &tbs
[nb_tbs
- 1]) {
1149 code_gen_ptr
= tb
->tc_ptr
;
1154 /* add a new TB and link it to the physical page tables. phys_page2 is
1155 (-1) to indicate that only one page contains the TB. */
1156 void tb_link_phys(TranslationBlock
*tb
,
1157 target_ulong phys_pc
, target_ulong phys_page2
)
1160 TranslationBlock
**ptb
;
1162 /* Grab the mmap lock to stop another thread invalidating this TB
1163 before we are done. */
1165 /* add in the physical hash table */
1166 h
= tb_phys_hash_func(phys_pc
);
1167 ptb
= &tb_phys_hash
[h
];
1168 tb
->phys_hash_next
= *ptb
;
1171 /* add in the page list */
1172 tb_alloc_page(tb
, 0, phys_pc
& TARGET_PAGE_MASK
);
1173 if (phys_page2
!= -1)
1174 tb_alloc_page(tb
, 1, phys_page2
);
1176 tb
->page_addr
[1] = -1;
1178 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
1179 tb
->jmp_next
[0] = NULL
;
1180 tb
->jmp_next
[1] = NULL
;
1182 /* init original jump addresses */
1183 if (tb
->tb_next_offset
[0] != 0xffff)
1184 tb_reset_jump(tb
, 0);
1185 if (tb
->tb_next_offset
[1] != 0xffff)
1186 tb_reset_jump(tb
, 1);
1188 #ifdef DEBUG_TB_CHECK
1194 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1195 tb[1].tc_ptr. Return NULL if not found */
1196 TranslationBlock
*tb_find_pc(unsigned long tc_ptr
)
1198 int m_min
, m_max
, m
;
1200 TranslationBlock
*tb
;
1204 if (tc_ptr
< (unsigned long)code_gen_buffer
||
1205 tc_ptr
>= (unsigned long)code_gen_ptr
)
1207 /* binary search (cf Knuth) */
1210 while (m_min
<= m_max
) {
1211 m
= (m_min
+ m_max
) >> 1;
1213 v
= (unsigned long)tb
->tc_ptr
;
1216 else if (tc_ptr
< v
) {
1225 static void tb_reset_jump_recursive(TranslationBlock
*tb
);
1227 static inline void tb_reset_jump_recursive2(TranslationBlock
*tb
, int n
)
1229 TranslationBlock
*tb1
, *tb_next
, **ptb
;
1232 tb1
= tb
->jmp_next
[n
];
1234 /* find head of list */
1237 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1240 tb1
= tb1
->jmp_next
[n1
];
1242 /* we are now sure now that tb jumps to tb1 */
1245 /* remove tb from the jmp_first list */
1246 ptb
= &tb_next
->jmp_first
;
1250 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1251 if (n1
== n
&& tb1
== tb
)
1253 ptb
= &tb1
->jmp_next
[n1
];
1255 *ptb
= tb
->jmp_next
[n
];
1256 tb
->jmp_next
[n
] = NULL
;
1258 /* suppress the jump to next tb in generated code */
1259 tb_reset_jump(tb
, n
);
1261 /* suppress jumps in the tb on which we could have jumped */
1262 tb_reset_jump_recursive(tb_next
);
1266 static void tb_reset_jump_recursive(TranslationBlock
*tb
)
1268 tb_reset_jump_recursive2(tb
, 0);
1269 tb_reset_jump_recursive2(tb
, 1);
1272 #if defined(TARGET_HAS_ICE)
1273 static void breakpoint_invalidate(CPUState
*env
, target_ulong pc
)
1275 target_phys_addr_t addr
;
1277 ram_addr_t ram_addr
;
1280 addr
= cpu_get_phys_page_debug(env
, pc
);
1281 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
1283 pd
= IO_MEM_UNASSIGNED
;
1285 pd
= p
->phys_offset
;
1287 ram_addr
= (pd
& TARGET_PAGE_MASK
) | (pc
& ~TARGET_PAGE_MASK
);
1288 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1, 0);
1292 /* Add a watchpoint. */
1293 int cpu_watchpoint_insert(CPUState
*env
, target_ulong addr
, int type
)
1297 for (i
= 0; i
< env
->nb_watchpoints
; i
++) {
1298 if (addr
== env
->watchpoint
[i
].vaddr
)
1301 if (env
->nb_watchpoints
>= MAX_WATCHPOINTS
)
1304 i
= env
->nb_watchpoints
++;
1305 env
->watchpoint
[i
].vaddr
= addr
;
1306 env
->watchpoint
[i
].type
= type
;
1307 tlb_flush_page(env
, addr
);
1308 /* FIXME: This flush is needed because of the hack to make memory ops
1309 terminate the TB. It can be removed once the proper IO trap and
1310 re-execute bits are in. */
1315 /* Remove a watchpoint. */
1316 int cpu_watchpoint_remove(CPUState
*env
, target_ulong addr
)
1320 for (i
= 0; i
< env
->nb_watchpoints
; i
++) {
1321 if (addr
== env
->watchpoint
[i
].vaddr
) {
1322 env
->nb_watchpoints
--;
1323 env
->watchpoint
[i
] = env
->watchpoint
[env
->nb_watchpoints
];
1324 tlb_flush_page(env
, addr
);
1331 /* Remove all watchpoints. */
1332 void cpu_watchpoint_remove_all(CPUState
*env
) {
1335 for (i
= 0; i
< env
->nb_watchpoints
; i
++) {
1336 tlb_flush_page(env
, env
->watchpoint
[i
].vaddr
);
1338 env
->nb_watchpoints
= 0;
1341 /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1342 breakpoint is reached */
1343 int cpu_breakpoint_insert(CPUState
*env
, target_ulong pc
)
1345 #if defined(TARGET_HAS_ICE)
1348 for(i
= 0; i
< env
->nb_breakpoints
; i
++) {
1349 if (env
->breakpoints
[i
] == pc
)
1353 if (env
->nb_breakpoints
>= MAX_BREAKPOINTS
)
1355 env
->breakpoints
[env
->nb_breakpoints
++] = pc
;
1358 kvm_update_debugger(env
);
1360 breakpoint_invalidate(env
, pc
);
1367 /* remove all breakpoints */
1368 void cpu_breakpoint_remove_all(CPUState
*env
) {
1369 #if defined(TARGET_HAS_ICE)
1371 for(i
= 0; i
< env
->nb_breakpoints
; i
++) {
1372 breakpoint_invalidate(env
, env
->breakpoints
[i
]);
1374 env
->nb_breakpoints
= 0;
1378 /* remove a breakpoint */
1379 int cpu_breakpoint_remove(CPUState
*env
, target_ulong pc
)
1381 #if defined(TARGET_HAS_ICE)
1383 for(i
= 0; i
< env
->nb_breakpoints
; i
++) {
1384 if (env
->breakpoints
[i
] == pc
)
1389 env
->nb_breakpoints
--;
1390 if (i
< env
->nb_breakpoints
)
1391 env
->breakpoints
[i
] = env
->breakpoints
[env
->nb_breakpoints
];
1394 kvm_update_debugger(env
);
1396 breakpoint_invalidate(env
, pc
);
1403 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1404 CPU loop after each instruction */
1405 void cpu_single_step(CPUState
*env
, int enabled
)
1407 #if defined(TARGET_HAS_ICE)
1408 if (env
->singlestep_enabled
!= enabled
) {
1409 env
->singlestep_enabled
= enabled
;
1410 /* must flush all the translated code to avoid inconsistancies */
1411 /* XXX: only flush what is necessary */
1415 kvm_update_debugger(env
);
1419 /* enable or disable low levels log */
1420 void cpu_set_log(int log_flags
)
1422 loglevel
= log_flags
;
1423 if (loglevel
&& !logfile
) {
1424 logfile
= fopen(logfilename
, log_append
? "a" : "w");
1426 perror(logfilename
);
1429 #if !defined(CONFIG_SOFTMMU)
1430 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1432 static uint8_t logfile_buf
[4096];
1433 setvbuf(logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
1436 setvbuf(logfile
, NULL
, _IOLBF
, 0);
1440 if (!loglevel
&& logfile
) {
1446 void cpu_set_log_filename(const char *filename
)
1448 logfilename
= strdup(filename
);
1453 cpu_set_log(loglevel
);
1456 /* mask must never be zero, except for A20 change call */
1457 void cpu_interrupt(CPUState
*env
, int mask
)
1459 #if !defined(USE_NPTL)
1460 TranslationBlock
*tb
;
1461 static spinlock_t interrupt_lock
= SPIN_LOCK_UNLOCKED
;
1465 old_mask
= env
->interrupt_request
;
1466 /* FIXME: This is probably not threadsafe. A different thread could
1467 be in the middle of a read-modify-write operation. */
1468 env
->interrupt_request
|= mask
;
1469 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
1470 kvm_update_interrupt_request(env
);
1471 #if defined(USE_NPTL)
1472 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1473 problem and hope the cpu will stop of its own accord. For userspace
1474 emulation this often isn't actually as bad as it sounds. Often
1475 signals are used primarily to interrupt blocking syscalls. */
1478 env
->icount_decr
.u16
.high
= 0xffff;
1479 #ifndef CONFIG_USER_ONLY
1480 /* CPU_INTERRUPT_EXIT isn't a real interrupt. It just means
1481 an async event happened and we need to process it. */
1483 && (mask
& ~(old_mask
| CPU_INTERRUPT_EXIT
)) != 0) {
1484 cpu_abort(env
, "Raised interrupt while not in I/O function");
1488 tb
= env
->current_tb
;
1489 /* if the cpu is currently executing code, we must unlink it and
1490 all the potentially executing TB */
1491 if (tb
&& !testandset(&interrupt_lock
)) {
1492 env
->current_tb
= NULL
;
1493 tb_reset_jump_recursive(tb
);
1494 resetlock(&interrupt_lock
);
1500 void cpu_reset_interrupt(CPUState
*env
, int mask
)
1502 env
->interrupt_request
&= ~mask
;
1505 CPULogItem cpu_log_items
[] = {
1506 { CPU_LOG_TB_OUT_ASM
, "out_asm",
1507 "show generated host assembly code for each compiled TB" },
1508 { CPU_LOG_TB_IN_ASM
, "in_asm",
1509 "show target assembly code for each compiled TB" },
1510 { CPU_LOG_TB_OP
, "op",
1511 "show micro ops for each compiled TB" },
1512 { CPU_LOG_TB_OP_OPT
, "op_opt",
1515 "before eflags optimization and "
1517 "after liveness analysis" },
1518 { CPU_LOG_INT
, "int",
1519 "show interrupts/exceptions in short format" },
1520 { CPU_LOG_EXEC
, "exec",
1521 "show trace before each executed TB (lots of logs)" },
1522 { CPU_LOG_TB_CPU
, "cpu",
1523 "show CPU state before block translation" },
1525 { CPU_LOG_PCALL
, "pcall",
1526 "show protected mode far calls/returns/exceptions" },
1529 { CPU_LOG_IOPORT
, "ioport",
1530 "show all i/o ports accesses" },
1535 static int cmp1(const char *s1
, int n
, const char *s2
)
1537 if (strlen(s2
) != n
)
1539 return memcmp(s1
, s2
, n
) == 0;
1542 /* takes a comma separated list of log masks. Return 0 if error. */
1543 int cpu_str_to_log_mask(const char *str
)
1552 p1
= strchr(p
, ',');
1555 if(cmp1(p
,p1
-p
,"all")) {
1556 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1560 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1561 if (cmp1(p
, p1
- p
, item
->name
))
1575 void cpu_abort(CPUState
*env
, const char *fmt
, ...)
1582 fprintf(stderr
, "qemu: fatal: ");
1583 vfprintf(stderr
, fmt
, ap
);
1584 fprintf(stderr
, "\n");
1586 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1588 cpu_dump_state(env
, stderr
, fprintf
, 0);
1591 fprintf(logfile
, "qemu: fatal: ");
1592 vfprintf(logfile
, fmt
, ap2
);
1593 fprintf(logfile
, "\n");
1595 cpu_dump_state(env
, logfile
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1597 cpu_dump_state(env
, logfile
, fprintf
, 0);
1607 CPUState
*cpu_copy(CPUState
*env
)
1609 CPUState
*new_env
= cpu_init(env
->cpu_model_str
);
1610 /* preserve chaining and index */
1611 CPUState
*next_cpu
= new_env
->next_cpu
;
1612 int cpu_index
= new_env
->cpu_index
;
1613 memcpy(new_env
, env
, sizeof(CPUState
));
1614 new_env
->next_cpu
= next_cpu
;
1615 new_env
->cpu_index
= cpu_index
;
1619 #if !defined(CONFIG_USER_ONLY)
1621 static inline void tlb_flush_jmp_cache(CPUState
*env
, target_ulong addr
)
1625 /* Discard jump cache entries for any tb which might potentially
1626 overlap the flushed page. */
1627 i
= tb_jmp_cache_hash_page(addr
- TARGET_PAGE_SIZE
);
1628 memset (&env
->tb_jmp_cache
[i
], 0,
1629 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1631 i
= tb_jmp_cache_hash_page(addr
);
1632 memset (&env
->tb_jmp_cache
[i
], 0,
1633 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1636 /* NOTE: if flush_global is true, also flush global entries (not
1638 void tlb_flush(CPUState
*env
, int flush_global
)
1642 #if defined(DEBUG_TLB)
1643 printf("tlb_flush:\n");
1645 /* must reset current TB so that interrupts cannot modify the
1646 links while we are modifying them */
1647 env
->current_tb
= NULL
;
1649 for(i
= 0; i
< CPU_TLB_SIZE
; i
++) {
1650 env
->tlb_table
[0][i
].addr_read
= -1;
1651 env
->tlb_table
[0][i
].addr_write
= -1;
1652 env
->tlb_table
[0][i
].addr_code
= -1;
1653 env
->tlb_table
[1][i
].addr_read
= -1;
1654 env
->tlb_table
[1][i
].addr_write
= -1;
1655 env
->tlb_table
[1][i
].addr_code
= -1;
1656 #if (NB_MMU_MODES >= 3)
1657 env
->tlb_table
[2][i
].addr_read
= -1;
1658 env
->tlb_table
[2][i
].addr_write
= -1;
1659 env
->tlb_table
[2][i
].addr_code
= -1;
1660 #if (NB_MMU_MODES == 4)
1661 env
->tlb_table
[3][i
].addr_read
= -1;
1662 env
->tlb_table
[3][i
].addr_write
= -1;
1663 env
->tlb_table
[3][i
].addr_code
= -1;
1668 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
1671 if (env
->kqemu_enabled
) {
1672 kqemu_flush(env
, flush_global
);
1678 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, target_ulong addr
)
1680 if (addr
== (tlb_entry
->addr_read
&
1681 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1682 addr
== (tlb_entry
->addr_write
&
1683 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1684 addr
== (tlb_entry
->addr_code
&
1685 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1686 tlb_entry
->addr_read
= -1;
1687 tlb_entry
->addr_write
= -1;
1688 tlb_entry
->addr_code
= -1;
1692 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
1696 #if defined(DEBUG_TLB)
1697 printf("tlb_flush_page: " TARGET_FMT_lx
"\n", addr
);
1699 /* must reset current TB so that interrupts cannot modify the
1700 links while we are modifying them */
1701 env
->current_tb
= NULL
;
1703 addr
&= TARGET_PAGE_MASK
;
1704 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1705 tlb_flush_entry(&env
->tlb_table
[0][i
], addr
);
1706 tlb_flush_entry(&env
->tlb_table
[1][i
], addr
);
1707 #if (NB_MMU_MODES >= 3)
1708 tlb_flush_entry(&env
->tlb_table
[2][i
], addr
);
1709 #if (NB_MMU_MODES == 4)
1710 tlb_flush_entry(&env
->tlb_table
[3][i
], addr
);
1714 tlb_flush_jmp_cache(env
, addr
);
1717 if (env
->kqemu_enabled
) {
1718 kqemu_flush_page(env
, addr
);
1723 /* update the TLBs so that writes to code in the virtual page 'addr'
1725 static void tlb_protect_code(ram_addr_t ram_addr
)
1727 cpu_physical_memory_reset_dirty(ram_addr
,
1728 ram_addr
+ TARGET_PAGE_SIZE
,
1732 /* update the TLB so that writes in physical page 'phys_addr' are no longer
1733 tested for self modifying code */
1734 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
1737 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] |= CODE_DIRTY_FLAG
;
1740 static inline void tlb_reset_dirty_range(CPUTLBEntry
*tlb_entry
,
1741 unsigned long start
, unsigned long length
)
1744 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1745 addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) + tlb_entry
->addend
;
1746 if ((addr
- start
) < length
) {
1747 tlb_entry
->addr_write
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) | TLB_NOTDIRTY
;
1752 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t end
,
1756 unsigned long length
, start1
;
1760 start
&= TARGET_PAGE_MASK
;
1761 end
= TARGET_PAGE_ALIGN(end
);
1763 length
= end
- start
;
1766 len
= length
>> TARGET_PAGE_BITS
;
1768 /* XXX: should not depend on cpu context */
1770 if (env
->kqemu_enabled
) {
1773 for(i
= 0; i
< len
; i
++) {
1774 kqemu_set_notdirty(env
, addr
);
1775 addr
+= TARGET_PAGE_SIZE
;
1779 mask
= ~dirty_flags
;
1780 p
= phys_ram_dirty
+ (start
>> TARGET_PAGE_BITS
);
1781 for(i
= 0; i
< len
; i
++)
1784 /* we modify the TLB cache so that the dirty bit will be set again
1785 when accessing the range */
1786 start1
= start
+ (unsigned long)phys_ram_base
;
1787 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1788 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1789 tlb_reset_dirty_range(&env
->tlb_table
[0][i
], start1
, length
);
1790 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1791 tlb_reset_dirty_range(&env
->tlb_table
[1][i
], start1
, length
);
1792 #if (NB_MMU_MODES >= 3)
1793 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1794 tlb_reset_dirty_range(&env
->tlb_table
[2][i
], start1
, length
);
1795 #if (NB_MMU_MODES == 4)
1796 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1797 tlb_reset_dirty_range(&env
->tlb_table
[3][i
], start1
, length
);
1803 int cpu_physical_memory_set_dirty_tracking(int enable
)
1808 r
= kvm_physical_memory_set_dirty_tracking(enable
);
1809 in_migration
= enable
;
1813 int cpu_physical_memory_get_dirty_tracking(void)
1815 return in_migration
;
1818 static inline void tlb_update_dirty(CPUTLBEntry
*tlb_entry
)
1820 ram_addr_t ram_addr
;
1822 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
1823 ram_addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) +
1824 tlb_entry
->addend
- (unsigned long)phys_ram_base
;
1825 if (!cpu_physical_memory_is_dirty(ram_addr
)) {
1826 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
1831 /* update the TLB according to the current state of the dirty bits */
1832 void cpu_tlb_update_dirty(CPUState
*env
)
1835 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1836 tlb_update_dirty(&env
->tlb_table
[0][i
]);
1837 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1838 tlb_update_dirty(&env
->tlb_table
[1][i
]);
1839 #if (NB_MMU_MODES >= 3)
1840 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1841 tlb_update_dirty(&env
->tlb_table
[2][i
]);
1842 #if (NB_MMU_MODES == 4)
1843 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
1844 tlb_update_dirty(&env
->tlb_table
[3][i
]);
1849 static inline void tlb_set_dirty1(CPUTLBEntry
*tlb_entry
, target_ulong vaddr
)
1851 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
))
1852 tlb_entry
->addr_write
= vaddr
;
1855 /* update the TLB corresponding to virtual page vaddr
1856 so that it is no longer dirty */
1857 static inline void tlb_set_dirty(CPUState
*env
, target_ulong vaddr
)
1861 vaddr
&= TARGET_PAGE_MASK
;
1862 i
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1863 tlb_set_dirty1(&env
->tlb_table
[0][i
], vaddr
);
1864 tlb_set_dirty1(&env
->tlb_table
[1][i
], vaddr
);
1865 #if (NB_MMU_MODES >= 3)
1866 tlb_set_dirty1(&env
->tlb_table
[2][i
], vaddr
);
1867 #if (NB_MMU_MODES == 4)
1868 tlb_set_dirty1(&env
->tlb_table
[3][i
], vaddr
);
1873 /* add a new TLB entry. At most one entry for a given virtual address
1874 is permitted. Return 0 if OK or 2 if the page could not be mapped
1875 (can only happen in non SOFTMMU mode for I/O pages or pages
1876 conflicting with the host address space). */
1877 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
1878 target_phys_addr_t paddr
, int prot
,
1879 int mmu_idx
, int is_softmmu
)
1884 target_ulong address
;
1885 target_ulong code_address
;
1886 target_phys_addr_t addend
;
1890 target_phys_addr_t iotlb
;
1892 p
= phys_page_find(paddr
>> TARGET_PAGE_BITS
);
1894 pd
= IO_MEM_UNASSIGNED
;
1896 pd
= p
->phys_offset
;
1898 #if defined(DEBUG_TLB)
1899 printf("tlb_set_page: vaddr=" TARGET_FMT_lx
" paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
1900 vaddr
, (int)paddr
, prot
, mmu_idx
, is_softmmu
, pd
);
1905 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&& !(pd
& IO_MEM_ROMD
)) {
1906 /* IO memory case (romd handled later) */
1907 address
|= TLB_MMIO
;
1909 addend
= (unsigned long)phys_ram_base
+ (pd
& TARGET_PAGE_MASK
);
1910 if ((pd
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
1912 iotlb
= pd
& TARGET_PAGE_MASK
;
1913 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
)
1914 iotlb
|= IO_MEM_NOTDIRTY
;
1916 iotlb
|= IO_MEM_ROM
;
1918 /* IO handlers are currently passed a phsical address.
1919 It would be nice to pass an offset from the base address
1920 of that region. This would avoid having to special case RAM,
1921 and avoid full address decoding in every device.
1922 We can't use the high bits of pd for this because
1923 IO_MEM_ROMD uses these as a ram address. */
1924 iotlb
= (pd
& ~TARGET_PAGE_MASK
) + paddr
;
1927 code_address
= address
;
1928 /* Make accesses to pages with watchpoints go via the
1929 watchpoint trap routines. */
1930 for (i
= 0; i
< env
->nb_watchpoints
; i
++) {
1931 if (vaddr
== (env
->watchpoint
[i
].vaddr
& TARGET_PAGE_MASK
)) {
1932 iotlb
= io_mem_watch
+ paddr
;
1933 /* TODO: The memory case can be optimized by not trapping
1934 reads of pages with a write breakpoint. */
1935 address
|= TLB_MMIO
;
1939 index
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1940 env
->iotlb
[mmu_idx
][index
] = iotlb
- vaddr
;
1941 te
= &env
->tlb_table
[mmu_idx
][index
];
1942 te
->addend
= addend
- vaddr
;
1943 if (prot
& PAGE_READ
) {
1944 te
->addr_read
= address
;
1949 if (prot
& PAGE_EXEC
) {
1950 te
->addr_code
= code_address
;
1954 if (prot
& PAGE_WRITE
) {
1955 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_ROM
||
1956 (pd
& IO_MEM_ROMD
)) {
1957 /* Write access calls the I/O callback. */
1958 te
->addr_write
= address
| TLB_MMIO
;
1959 } else if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
&&
1960 !cpu_physical_memory_is_dirty(pd
)) {
1961 te
->addr_write
= address
| TLB_NOTDIRTY
;
1963 te
->addr_write
= address
;
1966 te
->addr_write
= -1;
1973 void tlb_flush(CPUState
*env
, int flush_global
)
1977 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
1981 int tlb_set_page_exec(CPUState
*env
, target_ulong vaddr
,
1982 target_phys_addr_t paddr
, int prot
,
1983 int mmu_idx
, int is_softmmu
)
1988 /* dump memory mappings */
1989 void page_dump(FILE *f
)
1991 unsigned long start
, end
;
1992 int i
, j
, prot
, prot1
;
1995 fprintf(f
, "%-8s %-8s %-8s %s\n",
1996 "start", "end", "size", "prot");
2000 for(i
= 0; i
<= L1_SIZE
; i
++) {
2005 for(j
= 0;j
< L2_SIZE
; j
++) {
2010 if (prot1
!= prot
) {
2011 end
= (i
<< (32 - L1_BITS
)) | (j
<< TARGET_PAGE_BITS
);
2013 fprintf(f
, "%08lx-%08lx %08lx %c%c%c\n",
2014 start
, end
, end
- start
,
2015 prot
& PAGE_READ
? 'r' : '-',
2016 prot
& PAGE_WRITE
? 'w' : '-',
2017 prot
& PAGE_EXEC
? 'x' : '-');
2031 int page_get_flags(target_ulong address
)
2035 p
= page_find(address
>> TARGET_PAGE_BITS
);
2041 /* modify the flags of a page and invalidate the code if
2042 necessary. The flag PAGE_WRITE_ORG is positionned automatically
2043 depending on PAGE_WRITE */
2044 void page_set_flags(target_ulong start
, target_ulong end
, int flags
)
2049 /* mmap_lock should already be held. */
2050 start
= start
& TARGET_PAGE_MASK
;
2051 end
= TARGET_PAGE_ALIGN(end
);
2052 if (flags
& PAGE_WRITE
)
2053 flags
|= PAGE_WRITE_ORG
;
2054 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2055 p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
);
2056 /* We may be called for host regions that are outside guest
2060 /* if the write protection is set, then we invalidate the code
2062 if (!(p
->flags
& PAGE_WRITE
) &&
2063 (flags
& PAGE_WRITE
) &&
2065 tb_invalidate_phys_page(addr
, 0, NULL
);
2071 int page_check_range(target_ulong start
, target_ulong len
, int flags
)
2077 end
= TARGET_PAGE_ALIGN(start
+len
); /* must do before we loose bits in the next step */
2078 start
= start
& TARGET_PAGE_MASK
;
2081 /* we've wrapped around */
2083 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
2084 p
= page_find(addr
>> TARGET_PAGE_BITS
);
2087 if( !(p
->flags
& PAGE_VALID
) )
2090 if ((flags
& PAGE_READ
) && !(p
->flags
& PAGE_READ
))
2092 if (flags
& PAGE_WRITE
) {
2093 if (!(p
->flags
& PAGE_WRITE_ORG
))
2095 /* unprotect the page if it was put read-only because it
2096 contains translated code */
2097 if (!(p
->flags
& PAGE_WRITE
)) {
2098 if (!page_unprotect(addr
, 0, NULL
))
2107 /* called from signal handler: invalidate the code and unprotect the
2108 page. Return TRUE if the fault was succesfully handled. */
2109 int page_unprotect(target_ulong address
, unsigned long pc
, void *puc
)
2111 unsigned int page_index
, prot
, pindex
;
2113 target_ulong host_start
, host_end
, addr
;
2115 /* Technically this isn't safe inside a signal handler. However we
2116 know this only ever happens in a synchronous SEGV handler, so in
2117 practice it seems to be ok. */
2120 host_start
= address
& qemu_host_page_mask
;
2121 page_index
= host_start
>> TARGET_PAGE_BITS
;
2122 p1
= page_find(page_index
);
2127 host_end
= host_start
+ qemu_host_page_size
;
2130 for(addr
= host_start
;addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
2134 /* if the page was really writable, then we change its
2135 protection back to writable */
2136 if (prot
& PAGE_WRITE_ORG
) {
2137 pindex
= (address
- host_start
) >> TARGET_PAGE_BITS
;
2138 if (!(p1
[pindex
].flags
& PAGE_WRITE
)) {
2139 mprotect((void *)g2h(host_start
), qemu_host_page_size
,
2140 (prot
& PAGE_BITS
) | PAGE_WRITE
);
2141 p1
[pindex
].flags
|= PAGE_WRITE
;
2142 /* and since the content will be modified, we must invalidate
2143 the corresponding translated code. */
2144 tb_invalidate_phys_page(address
, pc
, puc
);
2145 #ifdef DEBUG_TB_CHECK
2146 tb_invalidate_check(address
);
2156 static inline void tlb_set_dirty(CPUState
*env
,
2157 unsigned long addr
, target_ulong vaddr
)
2160 #endif /* defined(CONFIG_USER_ONLY) */
2162 #if !defined(CONFIG_USER_ONLY)
2163 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2165 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2166 ram_addr_t orig_memory
);
2167 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2170 if (addr > start_addr) \
2173 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2174 if (start_addr2 > 0) \
2178 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2179 end_addr2 = TARGET_PAGE_SIZE - 1; \
2181 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2182 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2187 /* register physical memory. 'size' must be a multiple of the target
2188 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2190 void cpu_register_physical_memory(target_phys_addr_t start_addr
,
2192 ram_addr_t phys_offset
)
2194 target_phys_addr_t addr
, end_addr
;
2197 ram_addr_t orig_size
= size
;
2201 /* XXX: should not depend on cpu context */
2203 if (env
->kqemu_enabled
) {
2204 kqemu_set_phys_mem(start_addr
, size
, phys_offset
);
2207 size
= (size
+ TARGET_PAGE_SIZE
- 1) & TARGET_PAGE_MASK
;
2208 end_addr
= start_addr
+ (target_phys_addr_t
)size
;
2209 for(addr
= start_addr
; addr
!= end_addr
; addr
+= TARGET_PAGE_SIZE
) {
2210 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2211 if (p
&& p
->phys_offset
!= IO_MEM_UNASSIGNED
) {
2212 ram_addr_t orig_memory
= p
->phys_offset
;
2213 target_phys_addr_t start_addr2
, end_addr2
;
2214 int need_subpage
= 0;
2216 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
, end_addr2
,
2218 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2219 if (!(orig_memory
& IO_MEM_SUBPAGE
)) {
2220 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2221 &p
->phys_offset
, orig_memory
);
2223 subpage
= io_mem_opaque
[(orig_memory
& ~TARGET_PAGE_MASK
)
2226 subpage_register(subpage
, start_addr2
, end_addr2
, phys_offset
);
2228 p
->phys_offset
= phys_offset
;
2229 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2230 (phys_offset
& IO_MEM_ROMD
))
2231 phys_offset
+= TARGET_PAGE_SIZE
;
2234 p
= phys_page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
2235 p
->phys_offset
= phys_offset
;
2236 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2237 (phys_offset
& IO_MEM_ROMD
))
2238 phys_offset
+= TARGET_PAGE_SIZE
;
2240 target_phys_addr_t start_addr2
, end_addr2
;
2241 int need_subpage
= 0;
2243 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
,
2244 end_addr2
, need_subpage
);
2246 if (need_subpage
|| phys_offset
& IO_MEM_SUBWIDTH
) {
2247 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2248 &p
->phys_offset
, IO_MEM_UNASSIGNED
);
2249 subpage_register(subpage
, start_addr2
, end_addr2
,
2256 /* since each CPU stores ram addresses in its TLB cache, we must
2257 reset the modified entries */
2259 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2264 /* XXX: temporary until new memory mapping API */
2265 ram_addr_t
cpu_get_physical_page_desc(target_phys_addr_t addr
)
2269 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2271 return IO_MEM_UNASSIGNED
;
2272 return p
->phys_offset
;
2275 /* XXX: better than nothing */
2276 ram_addr_t
qemu_ram_alloc(ram_addr_t size
)
2279 if ((phys_ram_alloc_offset
+ size
) > phys_ram_size
) {
2280 fprintf(stderr
, "Not enough memory (requested_size = %" PRIu64
", max memory = %" PRIu64
"\n",
2281 (uint64_t)size
, (uint64_t)phys_ram_size
);
2284 addr
= phys_ram_alloc_offset
;
2285 phys_ram_alloc_offset
= TARGET_PAGE_ALIGN(phys_ram_alloc_offset
+ size
);
2289 void qemu_ram_free(ram_addr_t addr
)
2293 static uint32_t unassigned_mem_readb(void *opaque
, target_phys_addr_t addr
)
2295 #ifdef DEBUG_UNASSIGNED
2296 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
2299 do_unassigned_access(addr
, 0, 0, 0);
2301 do_unassigned_access(addr
, 0, 0, 0);
2306 static void unassigned_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
2308 #ifdef DEBUG_UNASSIGNED
2309 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
2312 do_unassigned_access(addr
, 1, 0, 0);
2314 do_unassigned_access(addr
, 1, 0, 0);
2318 static CPUReadMemoryFunc
*unassigned_mem_read
[3] = {
2319 unassigned_mem_readb
,
2320 unassigned_mem_readb
,
2321 unassigned_mem_readb
,
2324 static CPUWriteMemoryFunc
*unassigned_mem_write
[3] = {
2325 unassigned_mem_writeb
,
2326 unassigned_mem_writeb
,
2327 unassigned_mem_writeb
,
2330 static void notdirty_mem_writeb(void *opaque
, target_phys_addr_t ram_addr
,
2334 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2335 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2336 #if !defined(CONFIG_USER_ONLY)
2337 tb_invalidate_phys_page_fast(ram_addr
, 1);
2338 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2341 stb_p(phys_ram_base
+ ram_addr
, val
);
2343 if (cpu_single_env
->kqemu_enabled
&&
2344 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2345 kqemu_modify_page(cpu_single_env
, ram_addr
);
2347 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2348 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2349 /* we remove the notdirty callback only if the code has been
2351 if (dirty_flags
== 0xff)
2352 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2355 static void notdirty_mem_writew(void *opaque
, target_phys_addr_t ram_addr
,
2359 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2360 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2361 #if !defined(CONFIG_USER_ONLY)
2362 tb_invalidate_phys_page_fast(ram_addr
, 2);
2363 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2366 stw_p(phys_ram_base
+ ram_addr
, val
);
2368 if (cpu_single_env
->kqemu_enabled
&&
2369 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2370 kqemu_modify_page(cpu_single_env
, ram_addr
);
2372 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2373 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2374 /* we remove the notdirty callback only if the code has been
2376 if (dirty_flags
== 0xff)
2377 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2380 static void notdirty_mem_writel(void *opaque
, target_phys_addr_t ram_addr
,
2384 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2385 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
2386 #if !defined(CONFIG_USER_ONLY)
2387 tb_invalidate_phys_page_fast(ram_addr
, 4);
2388 dirty_flags
= phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
];
2391 stl_p(phys_ram_base
+ ram_addr
, val
);
2393 if (cpu_single_env
->kqemu_enabled
&&
2394 (dirty_flags
& KQEMU_MODIFY_PAGE_MASK
) != KQEMU_MODIFY_PAGE_MASK
)
2395 kqemu_modify_page(cpu_single_env
, ram_addr
);
2397 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
2398 phys_ram_dirty
[ram_addr
>> TARGET_PAGE_BITS
] = dirty_flags
;
2399 /* we remove the notdirty callback only if the code has been
2401 if (dirty_flags
== 0xff)
2402 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
2405 static CPUReadMemoryFunc
*error_mem_read
[3] = {
2406 NULL
, /* never used */
2407 NULL
, /* never used */
2408 NULL
, /* never used */
2411 static CPUWriteMemoryFunc
*notdirty_mem_write
[3] = {
2412 notdirty_mem_writeb
,
2413 notdirty_mem_writew
,
2414 notdirty_mem_writel
,
2417 /* Generate a debug exception if a watchpoint has been hit. */
2418 static void check_watchpoint(int offset
, int flags
)
2420 CPUState
*env
= cpu_single_env
;
2424 vaddr
= (env
->mem_io_vaddr
& TARGET_PAGE_MASK
) + offset
;
2425 for (i
= 0; i
< env
->nb_watchpoints
; i
++) {
2426 if (vaddr
== env
->watchpoint
[i
].vaddr
2427 && (env
->watchpoint
[i
].type
& flags
)) {
2428 env
->watchpoint_hit
= i
+ 1;
2429 cpu_interrupt(env
, CPU_INTERRUPT_DEBUG
);
2435 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
2436 so these check for a hit then pass through to the normal out-of-line
2438 static uint32_t watch_mem_readb(void *opaque
, target_phys_addr_t addr
)
2440 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_READ
);
2441 return ldub_phys(addr
);
2444 static uint32_t watch_mem_readw(void *opaque
, target_phys_addr_t addr
)
2446 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_READ
);
2447 return lduw_phys(addr
);
2450 static uint32_t watch_mem_readl(void *opaque
, target_phys_addr_t addr
)
2452 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_READ
);
2453 return ldl_phys(addr
);
2456 static void watch_mem_writeb(void *opaque
, target_phys_addr_t addr
,
2459 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_WRITE
);
2460 stb_phys(addr
, val
);
2463 static void watch_mem_writew(void *opaque
, target_phys_addr_t addr
,
2466 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_WRITE
);
2467 stw_phys(addr
, val
);
2470 static void watch_mem_writel(void *opaque
, target_phys_addr_t addr
,
2473 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, PAGE_WRITE
);
2474 stl_phys(addr
, val
);
2477 static CPUReadMemoryFunc
*watch_mem_read
[3] = {
2483 static CPUWriteMemoryFunc
*watch_mem_write
[3] = {
2489 static inline uint32_t subpage_readlen (subpage_t
*mmio
, target_phys_addr_t addr
,
2495 idx
= SUBPAGE_IDX(addr
- mmio
->base
);
2496 #if defined(DEBUG_SUBPAGE)
2497 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d\n", __func__
,
2498 mmio
, len
, addr
, idx
);
2500 ret
= (**mmio
->mem_read
[idx
][len
])(mmio
->opaque
[idx
][0][len
], addr
);
2505 static inline void subpage_writelen (subpage_t
*mmio
, target_phys_addr_t addr
,
2506 uint32_t value
, unsigned int len
)
2510 idx
= SUBPAGE_IDX(addr
- mmio
->base
);
2511 #if defined(DEBUG_SUBPAGE)
2512 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d value %08x\n", __func__
,
2513 mmio
, len
, addr
, idx
, value
);
2515 (**mmio
->mem_write
[idx
][len
])(mmio
->opaque
[idx
][1][len
], addr
, value
);
2518 static uint32_t subpage_readb (void *opaque
, target_phys_addr_t addr
)
2520 #if defined(DEBUG_SUBPAGE)
2521 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2524 return subpage_readlen(opaque
, addr
, 0);
2527 static void subpage_writeb (void *opaque
, target_phys_addr_t addr
,
2530 #if defined(DEBUG_SUBPAGE)
2531 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2533 subpage_writelen(opaque
, addr
, value
, 0);
2536 static uint32_t subpage_readw (void *opaque
, target_phys_addr_t addr
)
2538 #if defined(DEBUG_SUBPAGE)
2539 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2542 return subpage_readlen(opaque
, addr
, 1);
2545 static void subpage_writew (void *opaque
, target_phys_addr_t addr
,
2548 #if defined(DEBUG_SUBPAGE)
2549 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2551 subpage_writelen(opaque
, addr
, value
, 1);
2554 static uint32_t subpage_readl (void *opaque
, target_phys_addr_t addr
)
2556 #if defined(DEBUG_SUBPAGE)
2557 printf("%s: addr " TARGET_FMT_plx
"\n", __func__
, addr
);
2560 return subpage_readlen(opaque
, addr
, 2);
2563 static void subpage_writel (void *opaque
,
2564 target_phys_addr_t addr
, uint32_t value
)
2566 #if defined(DEBUG_SUBPAGE)
2567 printf("%s: addr " TARGET_FMT_plx
" val %08x\n", __func__
, addr
, value
);
2569 subpage_writelen(opaque
, addr
, value
, 2);
2572 static CPUReadMemoryFunc
*subpage_read
[] = {
2578 static CPUWriteMemoryFunc
*subpage_write
[] = {
2584 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2590 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
2592 idx
= SUBPAGE_IDX(start
);
2593 eidx
= SUBPAGE_IDX(end
);
2594 #if defined(DEBUG_SUBPAGE)
2595 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__
,
2596 mmio
, start
, end
, idx
, eidx
, memory
);
2598 memory
>>= IO_MEM_SHIFT
;
2599 for (; idx
<= eidx
; idx
++) {
2600 for (i
= 0; i
< 4; i
++) {
2601 if (io_mem_read
[memory
][i
]) {
2602 mmio
->mem_read
[idx
][i
] = &io_mem_read
[memory
][i
];
2603 mmio
->opaque
[idx
][0][i
] = io_mem_opaque
[memory
];
2605 if (io_mem_write
[memory
][i
]) {
2606 mmio
->mem_write
[idx
][i
] = &io_mem_write
[memory
][i
];
2607 mmio
->opaque
[idx
][1][i
] = io_mem_opaque
[memory
];
2615 static void *subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2616 ram_addr_t orig_memory
)
2621 mmio
= qemu_mallocz(sizeof(subpage_t
));
2624 subpage_memory
= cpu_register_io_memory(0, subpage_read
, subpage_write
, mmio
);
2625 #if defined(DEBUG_SUBPAGE)
2626 printf("%s: %p base " TARGET_FMT_plx
" len %08x %d\n", __func__
,
2627 mmio
, base
, TARGET_PAGE_SIZE
, subpage_memory
);
2629 *phys
= subpage_memory
| IO_MEM_SUBPAGE
;
2630 subpage_register(mmio
, 0, TARGET_PAGE_SIZE
- 1, orig_memory
);
2636 static int get_free_io_mem_idx(void)
2640 for (i
= 0; i
<IO_MEM_NB_ENTRIES
; i
++)
2641 if (!io_mem_used
[i
]) {
2649 static void io_mem_init(void)
2653 cpu_register_io_memory(IO_MEM_ROM
>> IO_MEM_SHIFT
, error_mem_read
, unassigned_mem_write
, NULL
);
2654 cpu_register_io_memory(IO_MEM_UNASSIGNED
>> IO_MEM_SHIFT
, unassigned_mem_read
, unassigned_mem_write
, NULL
);
2655 cpu_register_io_memory(IO_MEM_NOTDIRTY
>> IO_MEM_SHIFT
, error_mem_read
, notdirty_mem_write
, NULL
);
2659 io_mem_watch
= cpu_register_io_memory(0, watch_mem_read
,
2660 watch_mem_write
, NULL
);
2661 /* alloc dirty bits array */
2662 phys_ram_dirty
= qemu_vmalloc(phys_ram_size
>> TARGET_PAGE_BITS
);
2663 memset(phys_ram_dirty
, 0xff, phys_ram_size
>> TARGET_PAGE_BITS
);
2666 /* mem_read and mem_write are arrays of functions containing the
2667 function to access byte (index 0), word (index 1) and dword (index
2668 2). Functions can be omitted with a NULL function pointer. The
2669 registered functions may be modified dynamically later.
2670 If io_index is non zero, the corresponding io zone is
2671 modified. If it is zero, a new io zone is allocated. The return
2672 value can be used with cpu_register_physical_memory(). (-1) is
2673 returned if error. */
2674 int cpu_register_io_memory(int io_index
,
2675 CPUReadMemoryFunc
**mem_read
,
2676 CPUWriteMemoryFunc
**mem_write
,
2679 int i
, subwidth
= 0;
2681 if (io_index
<= 0) {
2682 io_index
= get_free_io_mem_idx();
2686 if (io_index
>= IO_MEM_NB_ENTRIES
)
2690 for(i
= 0;i
< 3; i
++) {
2691 if (!mem_read
[i
] || !mem_write
[i
])
2692 subwidth
= IO_MEM_SUBWIDTH
;
2693 io_mem_read
[io_index
][i
] = mem_read
[i
];
2694 io_mem_write
[io_index
][i
] = mem_write
[i
];
2696 io_mem_opaque
[io_index
] = opaque
;
2697 return (io_index
<< IO_MEM_SHIFT
) | subwidth
;
2700 void cpu_unregister_io_memory(int io_table_address
)
2703 int io_index
= io_table_address
>> IO_MEM_SHIFT
;
2705 for (i
=0;i
< 3; i
++) {
2706 io_mem_read
[io_index
][i
] = unassigned_mem_read
[i
];
2707 io_mem_write
[io_index
][i
] = unassigned_mem_write
[i
];
2709 io_mem_opaque
[io_index
] = NULL
;
2710 io_mem_used
[io_index
] = 0;
2713 CPUWriteMemoryFunc
**cpu_get_io_memory_write(int io_index
)
2715 return io_mem_write
[io_index
>> IO_MEM_SHIFT
];
2718 CPUReadMemoryFunc
**cpu_get_io_memory_read(int io_index
)
2720 return io_mem_read
[io_index
>> IO_MEM_SHIFT
];
2723 #endif /* !defined(CONFIG_USER_ONLY) */
2725 /* physical memory access (slow version, mainly for debug) */
2726 #if defined(CONFIG_USER_ONLY)
2727 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
2728 int len
, int is_write
)
2735 page
= addr
& TARGET_PAGE_MASK
;
2736 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
2739 flags
= page_get_flags(page
);
2740 if (!(flags
& PAGE_VALID
))
2743 if (!(flags
& PAGE_WRITE
))
2745 /* XXX: this code should not depend on lock_user */
2746 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
2747 /* FIXME - should this return an error rather than just fail? */
2750 unlock_user(p
, addr
, l
);
2752 if (!(flags
& PAGE_READ
))
2754 /* XXX: this code should not depend on lock_user */
2755 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
2756 /* FIXME - should this return an error rather than just fail? */
2759 unlock_user(p
, addr
, 0);
2768 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
2769 int len
, int is_write
)
2774 target_phys_addr_t page
;
2779 page
= addr
& TARGET_PAGE_MASK
;
2780 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
2783 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
2785 pd
= IO_MEM_UNASSIGNED
;
2787 pd
= p
->phys_offset
;
2791 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
2792 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
2793 /* XXX: could force cpu_single_env to NULL to avoid
2795 if (l
>= 4 && ((addr
& 3) == 0)) {
2796 /* 32 bit write access */
2798 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
2800 } else if (l
>= 2 && ((addr
& 1) == 0)) {
2801 /* 16 bit write access */
2803 io_mem_write
[io_index
][1](io_mem_opaque
[io_index
], addr
, val
);
2806 /* 8 bit write access */
2808 io_mem_write
[io_index
][0](io_mem_opaque
[io_index
], addr
, val
);
2812 unsigned long addr1
;
2813 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
2815 ptr
= phys_ram_base
+ addr1
;
2816 memcpy(ptr
, buf
, l
);
2817 if (!cpu_physical_memory_is_dirty(addr1
)) {
2818 /* invalidate code */
2819 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
2821 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
2822 (0xff & ~CODE_DIRTY_FLAG
);
2824 /* qemu doesn't execute guest code directly, but kvm does
2825 therefore fluch instruction caches */
2827 flush_icache_range((unsigned long)ptr
,
2828 ((unsigned long)ptr
)+l
);
2831 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
2832 !(pd
& IO_MEM_ROMD
)) {
2834 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
2835 if (l
>= 4 && ((addr
& 3) == 0)) {
2836 /* 32 bit read access */
2837 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
2840 } else if (l
>= 2 && ((addr
& 1) == 0)) {
2841 /* 16 bit read access */
2842 val
= io_mem_read
[io_index
][1](io_mem_opaque
[io_index
], addr
);
2846 /* 8 bit read access */
2847 val
= io_mem_read
[io_index
][0](io_mem_opaque
[io_index
], addr
);
2853 ptr
= phys_ram_base
+ (pd
& TARGET_PAGE_MASK
) +
2854 (addr
& ~TARGET_PAGE_MASK
);
2855 memcpy(buf
, ptr
, l
);
2864 /* used for ROM loading : can write in RAM and ROM */
2865 void cpu_physical_memory_write_rom(target_phys_addr_t addr
,
2866 const uint8_t *buf
, int len
)
2870 target_phys_addr_t page
;
2875 page
= addr
& TARGET_PAGE_MASK
;
2876 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
2879 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
2881 pd
= IO_MEM_UNASSIGNED
;
2883 pd
= p
->phys_offset
;
2886 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
&&
2887 (pd
& ~TARGET_PAGE_MASK
) != IO_MEM_ROM
&&
2888 !(pd
& IO_MEM_ROMD
)) {
2891 unsigned long addr1
;
2892 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
2894 ptr
= phys_ram_base
+ addr1
;
2895 memcpy(ptr
, buf
, l
);
2904 /* warning: addr must be aligned */
2905 uint32_t ldl_phys(target_phys_addr_t addr
)
2913 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2915 pd
= IO_MEM_UNASSIGNED
;
2917 pd
= p
->phys_offset
;
2920 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
2921 !(pd
& IO_MEM_ROMD
)) {
2923 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
2924 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
2927 ptr
= phys_ram_base
+ (pd
& TARGET_PAGE_MASK
) +
2928 (addr
& ~TARGET_PAGE_MASK
);
2934 /* warning: addr must be aligned */
2935 uint64_t ldq_phys(target_phys_addr_t addr
)
2943 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2945 pd
= IO_MEM_UNASSIGNED
;
2947 pd
= p
->phys_offset
;
2950 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
2951 !(pd
& IO_MEM_ROMD
)) {
2953 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
2954 #ifdef TARGET_WORDS_BIGENDIAN
2955 val
= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
) << 32;
2956 val
|= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4);
2958 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
2959 val
|= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4) << 32;
2963 ptr
= phys_ram_base
+ (pd
& TARGET_PAGE_MASK
) +
2964 (addr
& ~TARGET_PAGE_MASK
);
2971 uint32_t ldub_phys(target_phys_addr_t addr
)
2974 cpu_physical_memory_read(addr
, &val
, 1);
2979 uint32_t lduw_phys(target_phys_addr_t addr
)
2982 cpu_physical_memory_read(addr
, (uint8_t *)&val
, 2);
2983 return tswap16(val
);
2987 #define likely(x) __builtin_expect(!!(x), 1)
2988 #define unlikely(x) __builtin_expect(!!(x), 0)
2991 #define unlikely(x) x
2994 /* warning: addr must be aligned. The ram page is not masked as dirty
2995 and the code inside is not invalidated. It is useful if the dirty
2996 bits are used to track modified PTEs */
2997 void stl_phys_notdirty(target_phys_addr_t addr
, uint32_t val
)
3004 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3006 pd
= IO_MEM_UNASSIGNED
;
3008 pd
= p
->phys_offset
;
3011 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3012 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3013 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3015 unsigned long addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3016 ptr
= phys_ram_base
+ addr1
;
3019 if (unlikely(in_migration
)) {
3020 if (!cpu_physical_memory_is_dirty(addr1
)) {
3021 /* invalidate code */
3022 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3024 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3025 (0xff & ~CODE_DIRTY_FLAG
);
3031 void stq_phys_notdirty(target_phys_addr_t addr
, uint64_t val
)
3038 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3040 pd
= IO_MEM_UNASSIGNED
;
3042 pd
= p
->phys_offset
;
3045 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3046 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3047 #ifdef TARGET_WORDS_BIGENDIAN
3048 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
>> 32);
3049 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
);
3051 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3052 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
>> 32);
3055 ptr
= phys_ram_base
+ (pd
& TARGET_PAGE_MASK
) +
3056 (addr
& ~TARGET_PAGE_MASK
);
3061 /* warning: addr must be aligned */
3062 void stl_phys(target_phys_addr_t addr
, uint32_t val
)
3069 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3071 pd
= IO_MEM_UNASSIGNED
;
3073 pd
= p
->phys_offset
;
3076 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3077 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3078 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
3080 unsigned long addr1
;
3081 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3083 ptr
= phys_ram_base
+ addr1
;
3085 if (!cpu_physical_memory_is_dirty(addr1
)) {
3086 /* invalidate code */
3087 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
3089 phys_ram_dirty
[addr1
>> TARGET_PAGE_BITS
] |=
3090 (0xff & ~CODE_DIRTY_FLAG
);
3096 void stb_phys(target_phys_addr_t addr
, uint32_t val
)
3099 cpu_physical_memory_write(addr
, &v
, 1);
3103 void stw_phys(target_phys_addr_t addr
, uint32_t val
)
3105 uint16_t v
= tswap16(val
);
3106 cpu_physical_memory_write(addr
, (const uint8_t *)&v
, 2);
3110 void stq_phys(target_phys_addr_t addr
, uint64_t val
)
3113 cpu_physical_memory_write(addr
, (const uint8_t *)&val
, 8);
3118 /* virtual memory access for debug */
3119 int cpu_memory_rw_debug(CPUState
*env
, target_ulong addr
,
3120 uint8_t *buf
, int len
, int is_write
)
3123 target_phys_addr_t phys_addr
;
3127 page
= addr
& TARGET_PAGE_MASK
;
3128 phys_addr
= cpu_get_phys_page_debug(env
, page
);
3129 /* if no physical page mapped, return an error */
3130 if (phys_addr
== -1)
3132 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3135 cpu_physical_memory_rw(phys_addr
+ (addr
& ~TARGET_PAGE_MASK
),
3144 /* in deterministic execution mode, instructions doing device I/Os
3145 must be at the end of the TB */
3146 void cpu_io_recompile(CPUState
*env
, void *retaddr
)
3148 TranslationBlock
*tb
;
3150 target_ulong pc
, cs_base
;
3153 tb
= tb_find_pc((unsigned long)retaddr
);
3155 cpu_abort(env
, "cpu_io_recompile: could not find TB for pc=%p",
3158 n
= env
->icount_decr
.u16
.low
+ tb
->icount
;
3159 cpu_restore_state(tb
, env
, (unsigned long)retaddr
, NULL
);
3160 /* Calculate how many instructions had been executed before the fault
3162 n
= n
- env
->icount_decr
.u16
.low
;
3163 /* Generate a new TB ending on the I/O insn. */
3165 /* On MIPS and SH, delay slot instructions can only be restarted if
3166 they were already the first instruction in the TB. If this is not
3167 the first instruction in a TB then re-execute the preceding
3169 #if defined(TARGET_MIPS)
3170 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0 && n
> 1) {
3171 env
->active_tc
.PC
-= 4;
3172 env
->icount_decr
.u16
.low
++;
3173 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
3175 #elif defined(TARGET_SH4)
3176 if ((env
->flags
& ((DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
))) != 0
3179 env
->icount_decr
.u16
.low
++;
3180 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
);
3183 /* This should never happen. */
3184 if (n
> CF_COUNT_MASK
)
3185 cpu_abort(env
, "TB too big during recompile");
3187 cflags
= n
| CF_LAST_IO
;
3189 cs_base
= tb
->cs_base
;
3191 tb_phys_invalidate(tb
, -1);
3192 /* FIXME: In theory this could raise an exception. In practice
3193 we have already translated the block once so it's probably ok. */
3194 tb_gen_code(env
, pc
, cs_base
, flags
, cflags
);
3195 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3196 the first in the TB) then we end up generating a whole new TB and
3197 repeating the fault, which is horribly inefficient.
3198 Better would be to execute just this insn uncached, or generate a
3200 cpu_resume_from_signal(env
, NULL
);
3203 void dump_exec_info(FILE *f
,
3204 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
3206 int i
, target_code_size
, max_target_code_size
;
3207 int direct_jmp_count
, direct_jmp2_count
, cross_page
;
3208 TranslationBlock
*tb
;
3210 target_code_size
= 0;
3211 max_target_code_size
= 0;
3213 direct_jmp_count
= 0;
3214 direct_jmp2_count
= 0;
3215 for(i
= 0; i
< nb_tbs
; i
++) {
3217 target_code_size
+= tb
->size
;
3218 if (tb
->size
> max_target_code_size
)
3219 max_target_code_size
= tb
->size
;
3220 if (tb
->page_addr
[1] != -1)
3222 if (tb
->tb_next_offset
[0] != 0xffff) {
3224 if (tb
->tb_next_offset
[1] != 0xffff) {
3225 direct_jmp2_count
++;
3229 /* XXX: avoid using doubles ? */
3230 cpu_fprintf(f
, "Translation buffer state:\n");
3231 cpu_fprintf(f
, "gen code size %ld/%ld\n",
3232 code_gen_ptr
- code_gen_buffer
, code_gen_buffer_max_size
);
3233 cpu_fprintf(f
, "TB count %d/%d\n",
3234 nb_tbs
, code_gen_max_blocks
);
3235 cpu_fprintf(f
, "TB avg target size %d max=%d bytes\n",
3236 nb_tbs
? target_code_size
/ nb_tbs
: 0,
3237 max_target_code_size
);
3238 cpu_fprintf(f
, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
3239 nb_tbs
? (code_gen_ptr
- code_gen_buffer
) / nb_tbs
: 0,
3240 target_code_size
? (double) (code_gen_ptr
- code_gen_buffer
) / target_code_size
: 0);
3241 cpu_fprintf(f
, "cross page TB count %d (%d%%)\n",
3243 nb_tbs
? (cross_page
* 100) / nb_tbs
: 0);
3244 cpu_fprintf(f
, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
3246 nb_tbs
? (direct_jmp_count
* 100) / nb_tbs
: 0,
3248 nb_tbs
? (direct_jmp2_count
* 100) / nb_tbs
: 0);
3249 cpu_fprintf(f
, "\nStatistics:\n");
3250 cpu_fprintf(f
, "TB flush count %d\n", tb_flush_count
);
3251 cpu_fprintf(f
, "TB invalidate count %d\n", tb_phys_invalidate_count
);
3252 cpu_fprintf(f
, "TLB flush count %d\n", tlb_flush_count
);
3253 tcg_dump_info(f
, cpu_fprintf
);
3256 #if !defined(CONFIG_USER_ONLY)
3258 #define MMUSUFFIX _cmmu
3259 #define GETPC() NULL
3260 #define env cpu_single_env
3261 #define SOFTMMU_CODE_ACCESS
3264 #include "softmmu_template.h"
3267 #include "softmmu_template.h"
3270 #include "softmmu_template.h"
3273 #include "softmmu_template.h"