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>
27 #include "qemu-common.h"
30 #include "cache-utils.h"
32 #if !defined(TARGET_IA64)
41 #include "qemu-timer.h"
42 #if defined(CONFIG_USER_ONLY)
45 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
46 #include <sys/param.h>
47 #if __FreeBSD_version >= 700104
48 #define HAVE_KINFO_GETVMMAP
49 #define sigqueue sigqueue_freebsd /* avoid redefinition */
52 #include <machine/profile.h>
62 //#define DEBUG_TB_INVALIDATE
65 //#define DEBUG_UNASSIGNED
67 /* make various TB consistency checks */
68 //#define DEBUG_TB_CHECK
69 //#define DEBUG_TLB_CHECK
71 //#define DEBUG_IOPORT
72 //#define DEBUG_SUBPAGE
74 #if !defined(CONFIG_USER_ONLY)
75 /* TB consistency checks only implemented for usermode emulation. */
79 #define SMC_BITMAP_USE_THRESHOLD 10
81 static TranslationBlock
*tbs
;
82 static 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 static uint8_t *code_gen_ptr
;
111 #if !defined(CONFIG_USER_ONLY)
113 static int in_migration
;
115 RAMList ram_list
= { .blocks
= QLIST_HEAD_INITIALIZER(ram_list
) };
119 /* current CPU in the current thread. It is only valid inside
121 CPUState
*cpu_single_env
;
122 /* 0 = Do not count executed instructions.
123 1 = Precise instruction counting.
124 2 = Adaptive rate instruction counting. */
126 /* Current instruction counter. While executing translated code this may
127 include some instructions that have not yet been executed. */
130 typedef struct PageDesc
{
131 /* list of TBs intersecting this ram page */
132 TranslationBlock
*first_tb
;
133 /* in order to optimize self modifying code, we count the number
134 of lookups we do to a given page to use a bitmap */
135 unsigned int code_write_count
;
136 uint8_t *code_bitmap
;
137 #if defined(CONFIG_USER_ONLY)
142 /* In system mode we want L1_MAP to be based on ram offsets,
143 while in user mode we want it to be based on virtual addresses. */
144 #if !defined(CONFIG_USER_ONLY)
145 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
146 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
148 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
151 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
154 /* Size of the L2 (and L3, etc) page tables. */
156 #define L2_SIZE (1 << L2_BITS)
158 /* The bits remaining after N lower levels of page tables. */
159 #define P_L1_BITS_REM \
160 ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
161 #define V_L1_BITS_REM \
162 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
164 /* Size of the L1 page table. Avoid silly small sizes. */
165 #if P_L1_BITS_REM < 4
166 #define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
168 #define P_L1_BITS P_L1_BITS_REM
171 #if V_L1_BITS_REM < 4
172 #define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
174 #define V_L1_BITS V_L1_BITS_REM
177 #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
178 #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
180 #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
181 #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
183 unsigned long qemu_real_host_page_size
;
184 unsigned long qemu_host_page_bits
;
185 unsigned long qemu_host_page_size
;
186 unsigned long qemu_host_page_mask
;
188 /* This is a multi-level map on the virtual address space.
189 The bottom level has pointers to PageDesc. */
190 static void *l1_map
[V_L1_SIZE
];
192 #if !defined(CONFIG_USER_ONLY)
193 typedef struct PhysPageDesc
{
194 /* offset in host memory of the page + io_index in the low bits */
195 ram_addr_t phys_offset
;
196 ram_addr_t region_offset
;
199 /* This is a multi-level map on the physical address space.
200 The bottom level has pointers to PhysPageDesc. */
201 static void *l1_phys_map
[P_L1_SIZE
];
203 static void io_mem_init(void);
205 /* io memory support */
206 CPUWriteMemoryFunc
*io_mem_write
[IO_MEM_NB_ENTRIES
][4];
207 CPUReadMemoryFunc
*io_mem_read
[IO_MEM_NB_ENTRIES
][4];
208 void *io_mem_opaque
[IO_MEM_NB_ENTRIES
];
209 static char io_mem_used
[IO_MEM_NB_ENTRIES
];
210 static int io_mem_watch
;
215 static const char *logfilename
= "qemu.log";
217 static const char *logfilename
= "/tmp/qemu.log";
221 static int log_append
= 0;
224 #if !defined(CONFIG_USER_ONLY)
225 static int tlb_flush_count
;
227 static int tb_flush_count
;
228 static int tb_phys_invalidate_count
;
231 static void map_exec(void *addr
, long size
)
234 VirtualProtect(addr
, size
,
235 PAGE_EXECUTE_READWRITE
, &old_protect
);
239 static void map_exec(void *addr
, long size
)
241 unsigned long start
, end
, page_size
;
243 page_size
= getpagesize();
244 start
= (unsigned long)addr
;
245 start
&= ~(page_size
- 1);
247 end
= (unsigned long)addr
+ size
;
248 end
+= page_size
- 1;
249 end
&= ~(page_size
- 1);
251 mprotect((void *)start
, end
- start
,
252 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
256 static void page_init(void)
258 /* NOTE: we can always suppose that qemu_host_page_size >=
262 SYSTEM_INFO system_info
;
264 GetSystemInfo(&system_info
);
265 qemu_real_host_page_size
= system_info
.dwPageSize
;
268 qemu_real_host_page_size
= getpagesize();
270 if (qemu_host_page_size
== 0)
271 qemu_host_page_size
= qemu_real_host_page_size
;
272 if (qemu_host_page_size
< TARGET_PAGE_SIZE
)
273 qemu_host_page_size
= TARGET_PAGE_SIZE
;
274 qemu_host_page_bits
= 0;
275 while ((1 << qemu_host_page_bits
) < qemu_host_page_size
)
276 qemu_host_page_bits
++;
277 qemu_host_page_mask
= ~(qemu_host_page_size
- 1);
279 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
281 #ifdef HAVE_KINFO_GETVMMAP
282 struct kinfo_vmentry
*freep
;
285 freep
= kinfo_getvmmap(getpid(), &cnt
);
288 for (i
= 0; i
< cnt
; i
++) {
289 unsigned long startaddr
, endaddr
;
291 startaddr
= freep
[i
].kve_start
;
292 endaddr
= freep
[i
].kve_end
;
293 if (h2g_valid(startaddr
)) {
294 startaddr
= h2g(startaddr
) & TARGET_PAGE_MASK
;
296 if (h2g_valid(endaddr
)) {
297 endaddr
= h2g(endaddr
);
298 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
300 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
302 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
313 last_brk
= (unsigned long)sbrk(0);
315 f
= fopen("/compat/linux/proc/self/maps", "r");
320 unsigned long startaddr
, endaddr
;
323 n
= fscanf (f
, "%lx-%lx %*[^\n]\n", &startaddr
, &endaddr
);
325 if (n
== 2 && h2g_valid(startaddr
)) {
326 startaddr
= h2g(startaddr
) & TARGET_PAGE_MASK
;
328 if (h2g_valid(endaddr
)) {
329 endaddr
= h2g(endaddr
);
333 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
345 static PageDesc
*page_find_alloc(tb_page_addr_t index
, int alloc
)
351 #if defined(CONFIG_USER_ONLY)
352 /* We can't use qemu_malloc because it may recurse into a locked mutex. */
353 # define ALLOC(P, SIZE) \
355 P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
356 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
359 # define ALLOC(P, SIZE) \
360 do { P = qemu_mallocz(SIZE); } while (0)
363 /* Level 1. Always allocated. */
364 lp
= l1_map
+ ((index
>> V_L1_SHIFT
) & (V_L1_SIZE
- 1));
367 for (i
= V_L1_SHIFT
/ L2_BITS
- 1; i
> 0; i
--) {
374 ALLOC(p
, sizeof(void *) * L2_SIZE
);
378 lp
= p
+ ((index
>> (i
* L2_BITS
)) & (L2_SIZE
- 1));
386 ALLOC(pd
, sizeof(PageDesc
) * L2_SIZE
);
392 return pd
+ (index
& (L2_SIZE
- 1));
395 static inline PageDesc
*page_find(tb_page_addr_t index
)
397 return page_find_alloc(index
, 0);
400 #if !defined(CONFIG_USER_ONLY)
401 static PhysPageDesc
*phys_page_find_alloc(target_phys_addr_t index
, int alloc
)
407 /* Level 1. Always allocated. */
408 lp
= l1_phys_map
+ ((index
>> P_L1_SHIFT
) & (P_L1_SIZE
- 1));
411 for (i
= P_L1_SHIFT
/ L2_BITS
- 1; i
> 0; i
--) {
417 *lp
= p
= qemu_mallocz(sizeof(void *) * L2_SIZE
);
419 lp
= p
+ ((index
>> (i
* L2_BITS
)) & (L2_SIZE
- 1));
430 *lp
= pd
= qemu_malloc(sizeof(PhysPageDesc
) * L2_SIZE
);
432 for (i
= 0; i
< L2_SIZE
; i
++) {
433 pd
[i
].phys_offset
= IO_MEM_UNASSIGNED
;
434 pd
[i
].region_offset
= (index
+ i
) << TARGET_PAGE_BITS
;
438 return pd
+ (index
& (L2_SIZE
- 1));
441 static inline PhysPageDesc
*phys_page_find(target_phys_addr_t index
)
443 return phys_page_find_alloc(index
, 0);
446 static void tlb_protect_code(ram_addr_t ram_addr
);
447 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
449 #define mmap_lock() do { } while(0)
450 #define mmap_unlock() do { } while(0)
453 #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
455 #if defined(CONFIG_USER_ONLY)
456 /* Currently it is not recommended to allocate big chunks of data in
457 user mode. It will change when a dedicated libc will be used */
458 #define USE_STATIC_CODE_GEN_BUFFER
461 #ifdef USE_STATIC_CODE_GEN_BUFFER
462 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
]
463 __attribute__((aligned (CODE_GEN_ALIGN
)));
466 static void code_gen_alloc(unsigned long tb_size
)
471 #ifdef USE_STATIC_CODE_GEN_BUFFER
472 code_gen_buffer
= static_code_gen_buffer
;
473 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
474 map_exec(code_gen_buffer
, code_gen_buffer_size
);
476 code_gen_buffer_size
= tb_size
;
477 if (code_gen_buffer_size
== 0) {
478 #if defined(CONFIG_USER_ONLY)
479 /* in user mode, phys_ram_size is not meaningful */
480 code_gen_buffer_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
482 /* XXX: needs adjustments */
483 code_gen_buffer_size
= (unsigned long)(ram_size
/ 4);
486 if (code_gen_buffer_size
< MIN_CODE_GEN_BUFFER_SIZE
)
487 code_gen_buffer_size
= MIN_CODE_GEN_BUFFER_SIZE
;
488 /* The code gen buffer location may have constraints depending on
489 the host cpu and OS */
490 #if defined(__linux__)
495 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
496 #if defined(__x86_64__)
498 /* Cannot map more than that */
499 if (code_gen_buffer_size
> (800 * 1024 * 1024))
500 code_gen_buffer_size
= (800 * 1024 * 1024);
501 #elif defined(__sparc_v9__)
502 // Map the buffer below 2G, so we can use direct calls and branches
504 start
= (void *) 0x60000000UL
;
505 if (code_gen_buffer_size
> (512 * 1024 * 1024))
506 code_gen_buffer_size
= (512 * 1024 * 1024);
507 #elif defined(__arm__)
508 /* Map the buffer below 32M, so we can use direct calls and branches */
510 start
= (void *) 0x01000000UL
;
511 if (code_gen_buffer_size
> 16 * 1024 * 1024)
512 code_gen_buffer_size
= 16 * 1024 * 1024;
513 #elif defined(__s390x__)
514 /* Map the buffer so that we can use direct calls and branches. */
515 /* We have a +- 4GB range on the branches; leave some slop. */
516 if (code_gen_buffer_size
> (3ul * 1024 * 1024 * 1024)) {
517 code_gen_buffer_size
= 3ul * 1024 * 1024 * 1024;
519 start
= (void *)0x90000000UL
;
521 code_gen_buffer
= mmap(start
, code_gen_buffer_size
,
522 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
524 if (code_gen_buffer
== MAP_FAILED
) {
525 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
529 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
530 || defined(__DragonFly__) || defined(__OpenBSD__)
534 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
535 #if defined(__x86_64__)
536 /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
537 * 0x40000000 is free */
539 addr
= (void *)0x40000000;
540 /* Cannot map more than that */
541 if (code_gen_buffer_size
> (800 * 1024 * 1024))
542 code_gen_buffer_size
= (800 * 1024 * 1024);
543 #elif defined(__sparc_v9__)
544 // Map the buffer below 2G, so we can use direct calls and branches
546 addr
= (void *) 0x60000000UL
;
547 if (code_gen_buffer_size
> (512 * 1024 * 1024)) {
548 code_gen_buffer_size
= (512 * 1024 * 1024);
551 code_gen_buffer
= mmap(addr
, code_gen_buffer_size
,
552 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
554 if (code_gen_buffer
== MAP_FAILED
) {
555 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
560 code_gen_buffer
= qemu_malloc(code_gen_buffer_size
);
561 map_exec(code_gen_buffer
, code_gen_buffer_size
);
563 #endif /* !USE_STATIC_CODE_GEN_BUFFER */
564 map_exec(code_gen_prologue
, sizeof(code_gen_prologue
));
565 code_gen_buffer_max_size
= code_gen_buffer_size
-
566 (TCG_MAX_OP_SIZE
* OPC_MAX_SIZE
);
567 code_gen_max_blocks
= code_gen_buffer_size
/ CODE_GEN_AVG_BLOCK_SIZE
;
568 tbs
= qemu_malloc(code_gen_max_blocks
* sizeof(TranslationBlock
));
571 /* Must be called before using the QEMU cpus. 'tb_size' is the size
572 (in bytes) allocated to the translation buffer. Zero means default
574 void cpu_exec_init_all(unsigned long tb_size
)
577 code_gen_alloc(tb_size
);
578 code_gen_ptr
= code_gen_buffer
;
580 #if !defined(CONFIG_USER_ONLY)
583 #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
584 /* There's no guest base to take into account, so go ahead and
585 initialize the prologue now. */
586 tcg_prologue_init(&tcg_ctx
);
590 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
592 static int cpu_common_post_load(void *opaque
, int version_id
)
594 CPUState
*env
= opaque
;
596 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
597 version_id is increased. */
598 env
->interrupt_request
&= ~0x01;
604 static const VMStateDescription vmstate_cpu_common
= {
605 .name
= "cpu_common",
607 .minimum_version_id
= 1,
608 .minimum_version_id_old
= 1,
609 .post_load
= cpu_common_post_load
,
610 .fields
= (VMStateField
[]) {
611 VMSTATE_UINT32(halted
, CPUState
),
612 VMSTATE_UINT32(interrupt_request
, CPUState
),
613 VMSTATE_END_OF_LIST()
618 CPUState
*qemu_get_cpu(int cpu
)
620 CPUState
*env
= first_cpu
;
623 if (env
->cpu_index
== cpu
)
631 void cpu_exec_init(CPUState
*env
)
636 #if defined(CONFIG_USER_ONLY)
639 env
->next_cpu
= NULL
;
642 while (*penv
!= NULL
) {
643 penv
= &(*penv
)->next_cpu
;
646 env
->cpu_index
= cpu_index
;
648 QTAILQ_INIT(&env
->breakpoints
);
649 QTAILQ_INIT(&env
->watchpoints
);
651 env
->thread_id
= GetCurrentProcessId();
653 env
->thread_id
= getpid();
656 #if defined(CONFIG_USER_ONLY)
659 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
660 vmstate_register(NULL
, cpu_index
, &vmstate_cpu_common
, env
);
661 register_savevm(NULL
, "cpu", cpu_index
, CPU_SAVE_VERSION
,
662 cpu_save
, cpu_load
, env
);
666 static inline void invalidate_page_bitmap(PageDesc
*p
)
668 if (p
->code_bitmap
) {
669 qemu_free(p
->code_bitmap
);
670 p
->code_bitmap
= NULL
;
672 p
->code_write_count
= 0;
675 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
677 static void page_flush_tb_1 (int level
, void **lp
)
686 for (i
= 0; i
< L2_SIZE
; ++i
) {
687 pd
[i
].first_tb
= NULL
;
688 invalidate_page_bitmap(pd
+ i
);
692 for (i
= 0; i
< L2_SIZE
; ++i
) {
693 page_flush_tb_1 (level
- 1, pp
+ i
);
698 static void page_flush_tb(void)
701 for (i
= 0; i
< V_L1_SIZE
; i
++) {
702 page_flush_tb_1(V_L1_SHIFT
/ L2_BITS
- 1, l1_map
+ i
);
706 /* flush all the translation blocks */
707 /* XXX: tb_flush is currently not thread safe */
708 void tb_flush(CPUState
*env1
)
711 #if defined(DEBUG_FLUSH)
712 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
713 (unsigned long)(code_gen_ptr
- code_gen_buffer
),
715 ((unsigned long)(code_gen_ptr
- code_gen_buffer
)) / nb_tbs
: 0);
717 if ((unsigned long)(code_gen_ptr
- code_gen_buffer
) > code_gen_buffer_size
)
718 cpu_abort(env1
, "Internal error: code buffer overflow\n");
722 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
723 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
726 memset (tb_phys_hash
, 0, CODE_GEN_PHYS_HASH_SIZE
* sizeof (void *));
729 code_gen_ptr
= code_gen_buffer
;
730 /* XXX: flush processor icache at this point if cache flush is
735 #ifdef DEBUG_TB_CHECK
737 static void tb_invalidate_check(target_ulong address
)
739 TranslationBlock
*tb
;
741 address
&= TARGET_PAGE_MASK
;
742 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
743 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
744 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
745 address
>= tb
->pc
+ tb
->size
)) {
746 printf("ERROR invalidate: address=" TARGET_FMT_lx
747 " PC=%08lx size=%04x\n",
748 address
, (long)tb
->pc
, tb
->size
);
754 /* verify that all the pages have correct rights for code */
755 static void tb_page_check(void)
757 TranslationBlock
*tb
;
758 int i
, flags1
, flags2
;
760 for(i
= 0;i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
761 for(tb
= tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
762 flags1
= page_get_flags(tb
->pc
);
763 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
764 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
765 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
766 (long)tb
->pc
, tb
->size
, flags1
, flags2
);
774 /* invalidate one TB */
775 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
778 TranslationBlock
*tb1
;
782 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
785 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
789 static inline void tb_page_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
791 TranslationBlock
*tb1
;
797 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
799 *ptb
= tb1
->page_next
[n1
];
802 ptb
= &tb1
->page_next
[n1
];
806 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
808 TranslationBlock
*tb1
, **ptb
;
811 ptb
= &tb
->jmp_next
[n
];
814 /* find tb(n) in circular list */
818 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
819 if (n1
== n
&& tb1
== tb
)
822 ptb
= &tb1
->jmp_first
;
824 ptb
= &tb1
->jmp_next
[n1
];
827 /* now we can suppress tb(n) from the list */
828 *ptb
= tb
->jmp_next
[n
];
830 tb
->jmp_next
[n
] = NULL
;
834 /* reset the jump entry 'n' of a TB so that it is not chained to
836 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
838 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
841 void tb_phys_invalidate(TranslationBlock
*tb
, tb_page_addr_t page_addr
)
846 tb_page_addr_t phys_pc
;
847 TranslationBlock
*tb1
, *tb2
;
849 /* remove the TB from the hash list */
850 phys_pc
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
851 h
= tb_phys_hash_func(phys_pc
);
852 tb_remove(&tb_phys_hash
[h
], tb
,
853 offsetof(TranslationBlock
, phys_hash_next
));
855 /* remove the TB from the page list */
856 if (tb
->page_addr
[0] != page_addr
) {
857 p
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
858 tb_page_remove(&p
->first_tb
, tb
);
859 invalidate_page_bitmap(p
);
861 if (tb
->page_addr
[1] != -1 && tb
->page_addr
[1] != page_addr
) {
862 p
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
863 tb_page_remove(&p
->first_tb
, tb
);
864 invalidate_page_bitmap(p
);
867 tb_invalidated_flag
= 1;
869 /* remove the TB from the hash list */
870 h
= tb_jmp_cache_hash_func(tb
->pc
);
871 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
872 if (env
->tb_jmp_cache
[h
] == tb
)
873 env
->tb_jmp_cache
[h
] = NULL
;
876 /* suppress this TB from the two jump lists */
877 tb_jmp_remove(tb
, 0);
878 tb_jmp_remove(tb
, 1);
880 /* suppress any remaining jumps to this TB */
886 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
887 tb2
= tb1
->jmp_next
[n1
];
888 tb_reset_jump(tb1
, n1
);
889 tb1
->jmp_next
[n1
] = NULL
;
892 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
894 tb_phys_invalidate_count
++;
897 static inline void set_bits(uint8_t *tab
, int start
, int len
)
903 mask
= 0xff << (start
& 7);
904 if ((start
& ~7) == (end
& ~7)) {
906 mask
&= ~(0xff << (end
& 7));
911 start
= (start
+ 8) & ~7;
913 while (start
< end1
) {
918 mask
= ~(0xff << (end
& 7));
924 static void build_page_bitmap(PageDesc
*p
)
926 int n
, tb_start
, tb_end
;
927 TranslationBlock
*tb
;
929 p
->code_bitmap
= qemu_mallocz(TARGET_PAGE_SIZE
/ 8);
934 tb
= (TranslationBlock
*)((long)tb
& ~3);
935 /* NOTE: this is subtle as a TB may span two physical pages */
937 /* NOTE: tb_end may be after the end of the page, but
938 it is not a problem */
939 tb_start
= tb
->pc
& ~TARGET_PAGE_MASK
;
940 tb_end
= tb_start
+ tb
->size
;
941 if (tb_end
> TARGET_PAGE_SIZE
)
942 tb_end
= TARGET_PAGE_SIZE
;
945 tb_end
= ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
947 set_bits(p
->code_bitmap
, tb_start
, tb_end
- tb_start
);
948 tb
= tb
->page_next
[n
];
952 TranslationBlock
*tb_gen_code(CPUState
*env
,
953 target_ulong pc
, target_ulong cs_base
,
954 int flags
, int cflags
)
956 TranslationBlock
*tb
;
958 tb_page_addr_t phys_pc
, phys_page2
;
959 target_ulong virt_page2
;
962 phys_pc
= get_page_addr_code(env
, pc
);
965 /* flush must be done */
967 /* cannot fail at this point */
969 /* Don't forget to invalidate previous TB info. */
970 tb_invalidated_flag
= 1;
972 tc_ptr
= code_gen_ptr
;
974 tb
->cs_base
= cs_base
;
977 cpu_gen_code(env
, tb
, &code_gen_size
);
978 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
980 /* check next page if needed */
981 virt_page2
= (pc
+ tb
->size
- 1) & TARGET_PAGE_MASK
;
983 if ((pc
& TARGET_PAGE_MASK
) != virt_page2
) {
984 phys_page2
= get_page_addr_code(env
, virt_page2
);
986 tb_link_page(tb
, phys_pc
, phys_page2
);
990 /* invalidate all TBs which intersect with the target physical page
991 starting in range [start;end[. NOTE: start and end must refer to
992 the same physical page. 'is_cpu_write_access' should be true if called
993 from a real cpu write access: the virtual CPU will exit the current
994 TB if code is modified inside this TB. */
995 void tb_invalidate_phys_page_range(tb_page_addr_t start
, tb_page_addr_t end
,
996 int is_cpu_write_access
)
998 TranslationBlock
*tb
, *tb_next
, *saved_tb
;
999 CPUState
*env
= cpu_single_env
;
1000 tb_page_addr_t tb_start
, tb_end
;
1003 #ifdef TARGET_HAS_PRECISE_SMC
1004 int current_tb_not_found
= is_cpu_write_access
;
1005 TranslationBlock
*current_tb
= NULL
;
1006 int current_tb_modified
= 0;
1007 target_ulong current_pc
= 0;
1008 target_ulong current_cs_base
= 0;
1009 int current_flags
= 0;
1010 #endif /* TARGET_HAS_PRECISE_SMC */
1012 p
= page_find(start
>> TARGET_PAGE_BITS
);
1015 if (!p
->code_bitmap
&&
1016 ++p
->code_write_count
>= SMC_BITMAP_USE_THRESHOLD
&&
1017 is_cpu_write_access
) {
1018 /* build code bitmap */
1019 build_page_bitmap(p
);
1022 /* we remove all the TBs in the range [start, end[ */
1023 /* XXX: see if in some cases it could be faster to invalidate all the code */
1025 while (tb
!= NULL
) {
1027 tb
= (TranslationBlock
*)((long)tb
& ~3);
1028 tb_next
= tb
->page_next
[n
];
1029 /* NOTE: this is subtle as a TB may span two physical pages */
1031 /* NOTE: tb_end may be after the end of the page, but
1032 it is not a problem */
1033 tb_start
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
1034 tb_end
= tb_start
+ tb
->size
;
1036 tb_start
= tb
->page_addr
[1];
1037 tb_end
= tb_start
+ ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
1039 if (!(tb_end
<= start
|| tb_start
>= end
)) {
1040 #ifdef TARGET_HAS_PRECISE_SMC
1041 if (current_tb_not_found
) {
1042 current_tb_not_found
= 0;
1044 if (env
->mem_io_pc
) {
1045 /* now we have a real cpu fault */
1046 current_tb
= tb_find_pc(env
->mem_io_pc
);
1049 if (current_tb
== tb
&&
1050 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1051 /* If we are modifying the current TB, we must stop
1052 its execution. We could be more precise by checking
1053 that the modification is after the current PC, but it
1054 would require a specialized function to partially
1055 restore the CPU state */
1057 current_tb_modified
= 1;
1058 cpu_restore_state(current_tb
, env
,
1059 env
->mem_io_pc
, NULL
);
1060 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1063 #endif /* TARGET_HAS_PRECISE_SMC */
1064 /* we need to do that to handle the case where a signal
1065 occurs while doing tb_phys_invalidate() */
1068 saved_tb
= env
->current_tb
;
1069 env
->current_tb
= NULL
;
1071 tb_phys_invalidate(tb
, -1);
1073 env
->current_tb
= saved_tb
;
1074 if (env
->interrupt_request
&& env
->current_tb
)
1075 cpu_interrupt(env
, env
->interrupt_request
);
1080 #if !defined(CONFIG_USER_ONLY)
1081 /* if no code remaining, no need to continue to use slow writes */
1083 invalidate_page_bitmap(p
);
1084 if (is_cpu_write_access
) {
1085 tlb_unprotect_code_phys(env
, start
, env
->mem_io_vaddr
);
1089 #ifdef TARGET_HAS_PRECISE_SMC
1090 if (current_tb_modified
) {
1091 /* we generate a block containing just the instruction
1092 modifying the memory. It will ensure that it cannot modify
1094 env
->current_tb
= NULL
;
1095 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1096 cpu_resume_from_signal(env
, NULL
);
1101 /* len must be <= 8 and start must be a multiple of len */
1102 static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start
, int len
)
1108 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1109 cpu_single_env
->mem_io_vaddr
, len
,
1110 cpu_single_env
->eip
,
1111 cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
);
1114 p
= page_find(start
>> TARGET_PAGE_BITS
);
1117 if (p
->code_bitmap
) {
1118 offset
= start
& ~TARGET_PAGE_MASK
;
1119 b
= p
->code_bitmap
[offset
>> 3] >> (offset
& 7);
1120 if (b
& ((1 << len
) - 1))
1124 tb_invalidate_phys_page_range(start
, start
+ len
, 1);
1128 #if !defined(CONFIG_SOFTMMU)
1129 static void tb_invalidate_phys_page(tb_page_addr_t addr
,
1130 unsigned long pc
, void *puc
)
1132 TranslationBlock
*tb
;
1135 #ifdef TARGET_HAS_PRECISE_SMC
1136 TranslationBlock
*current_tb
= NULL
;
1137 CPUState
*env
= cpu_single_env
;
1138 int current_tb_modified
= 0;
1139 target_ulong current_pc
= 0;
1140 target_ulong current_cs_base
= 0;
1141 int current_flags
= 0;
1144 addr
&= TARGET_PAGE_MASK
;
1145 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1149 #ifdef TARGET_HAS_PRECISE_SMC
1150 if (tb
&& pc
!= 0) {
1151 current_tb
= tb_find_pc(pc
);
1154 while (tb
!= NULL
) {
1156 tb
= (TranslationBlock
*)((long)tb
& ~3);
1157 #ifdef TARGET_HAS_PRECISE_SMC
1158 if (current_tb
== tb
&&
1159 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1160 /* If we are modifying the current TB, we must stop
1161 its execution. We could be more precise by checking
1162 that the modification is after the current PC, but it
1163 would require a specialized function to partially
1164 restore the CPU state */
1166 current_tb_modified
= 1;
1167 cpu_restore_state(current_tb
, env
, pc
, puc
);
1168 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1171 #endif /* TARGET_HAS_PRECISE_SMC */
1172 tb_phys_invalidate(tb
, addr
);
1173 tb
= tb
->page_next
[n
];
1176 #ifdef TARGET_HAS_PRECISE_SMC
1177 if (current_tb_modified
) {
1178 /* we generate a block containing just the instruction
1179 modifying the memory. It will ensure that it cannot modify
1181 env
->current_tb
= NULL
;
1182 tb_gen_code(env
, current_pc
, current_cs_base
, current_flags
, 1);
1183 cpu_resume_from_signal(env
, puc
);
1189 /* add the tb in the target page and protect it if necessary */
1190 static inline void tb_alloc_page(TranslationBlock
*tb
,
1191 unsigned int n
, tb_page_addr_t page_addr
)
1194 TranslationBlock
*last_first_tb
;
1196 tb
->page_addr
[n
] = page_addr
;
1197 p
= page_find_alloc(page_addr
>> TARGET_PAGE_BITS
, 1);
1198 tb
->page_next
[n
] = p
->first_tb
;
1199 last_first_tb
= p
->first_tb
;
1200 p
->first_tb
= (TranslationBlock
*)((long)tb
| n
);
1201 invalidate_page_bitmap(p
);
1203 #if defined(TARGET_HAS_SMC) || 1
1205 #if defined(CONFIG_USER_ONLY)
1206 if (p
->flags
& PAGE_WRITE
) {
1211 /* force the host page as non writable (writes will have a
1212 page fault + mprotect overhead) */
1213 page_addr
&= qemu_host_page_mask
;
1215 for(addr
= page_addr
; addr
< page_addr
+ qemu_host_page_size
;
1216 addr
+= TARGET_PAGE_SIZE
) {
1218 p2
= page_find (addr
>> TARGET_PAGE_BITS
);
1222 p2
->flags
&= ~PAGE_WRITE
;
1224 mprotect(g2h(page_addr
), qemu_host_page_size
,
1225 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
1226 #ifdef DEBUG_TB_INVALIDATE
1227 printf("protecting code page: 0x" TARGET_FMT_lx
"\n",
1232 /* if some code is already present, then the pages are already
1233 protected. So we handle the case where only the first TB is
1234 allocated in a physical page */
1235 if (!last_first_tb
) {
1236 tlb_protect_code(page_addr
);
1240 #endif /* TARGET_HAS_SMC */
1243 /* Allocate a new translation block. Flush the translation buffer if
1244 too many translation blocks or too much generated code. */
1245 TranslationBlock
*tb_alloc(target_ulong pc
)
1247 TranslationBlock
*tb
;
1249 if (nb_tbs
>= code_gen_max_blocks
||
1250 (code_gen_ptr
- code_gen_buffer
) >= code_gen_buffer_max_size
)
1252 tb
= &tbs
[nb_tbs
++];
1258 void tb_free(TranslationBlock
*tb
)
1260 /* In practice this is mostly used for single use temporary TB
1261 Ignore the hard cases and just back up if this TB happens to
1262 be the last one generated. */
1263 if (nb_tbs
> 0 && tb
== &tbs
[nb_tbs
- 1]) {
1264 code_gen_ptr
= tb
->tc_ptr
;
1269 /* add a new TB and link it to the physical page tables. phys_page2 is
1270 (-1) to indicate that only one page contains the TB. */
1271 void tb_link_page(TranslationBlock
*tb
,
1272 tb_page_addr_t phys_pc
, tb_page_addr_t phys_page2
)
1275 TranslationBlock
**ptb
;
1277 /* Grab the mmap lock to stop another thread invalidating this TB
1278 before we are done. */
1280 /* add in the physical hash table */
1281 h
= tb_phys_hash_func(phys_pc
);
1282 ptb
= &tb_phys_hash
[h
];
1283 tb
->phys_hash_next
= *ptb
;
1286 /* add in the page list */
1287 tb_alloc_page(tb
, 0, phys_pc
& TARGET_PAGE_MASK
);
1288 if (phys_page2
!= -1)
1289 tb_alloc_page(tb
, 1, phys_page2
);
1291 tb
->page_addr
[1] = -1;
1293 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
1294 tb
->jmp_next
[0] = NULL
;
1295 tb
->jmp_next
[1] = NULL
;
1297 /* init original jump addresses */
1298 if (tb
->tb_next_offset
[0] != 0xffff)
1299 tb_reset_jump(tb
, 0);
1300 if (tb
->tb_next_offset
[1] != 0xffff)
1301 tb_reset_jump(tb
, 1);
1303 #ifdef DEBUG_TB_CHECK
1309 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1310 tb[1].tc_ptr. Return NULL if not found */
1311 TranslationBlock
*tb_find_pc(unsigned long tc_ptr
)
1313 int m_min
, m_max
, m
;
1315 TranslationBlock
*tb
;
1319 if (tc_ptr
< (unsigned long)code_gen_buffer
||
1320 tc_ptr
>= (unsigned long)code_gen_ptr
)
1322 /* binary search (cf Knuth) */
1325 while (m_min
<= m_max
) {
1326 m
= (m_min
+ m_max
) >> 1;
1328 v
= (unsigned long)tb
->tc_ptr
;
1331 else if (tc_ptr
< v
) {
1340 static void tb_reset_jump_recursive(TranslationBlock
*tb
);
1342 static inline void tb_reset_jump_recursive2(TranslationBlock
*tb
, int n
)
1344 TranslationBlock
*tb1
, *tb_next
, **ptb
;
1347 tb1
= tb
->jmp_next
[n
];
1349 /* find head of list */
1352 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1355 tb1
= tb1
->jmp_next
[n1
];
1357 /* we are now sure now that tb jumps to tb1 */
1360 /* remove tb from the jmp_first list */
1361 ptb
= &tb_next
->jmp_first
;
1365 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
1366 if (n1
== n
&& tb1
== tb
)
1368 ptb
= &tb1
->jmp_next
[n1
];
1370 *ptb
= tb
->jmp_next
[n
];
1371 tb
->jmp_next
[n
] = NULL
;
1373 /* suppress the jump to next tb in generated code */
1374 tb_reset_jump(tb
, n
);
1376 /* suppress jumps in the tb on which we could have jumped */
1377 tb_reset_jump_recursive(tb_next
);
1381 static void tb_reset_jump_recursive(TranslationBlock
*tb
)
1383 tb_reset_jump_recursive2(tb
, 0);
1384 tb_reset_jump_recursive2(tb
, 1);
1387 #if defined(TARGET_HAS_ICE)
1388 #if defined(CONFIG_USER_ONLY)
1389 static void breakpoint_invalidate(CPUState
*env
, target_ulong pc
)
1391 tb_invalidate_phys_page_range(pc
, pc
+ 1, 0);
1394 static void breakpoint_invalidate(CPUState
*env
, target_ulong pc
)
1396 target_phys_addr_t addr
;
1398 ram_addr_t ram_addr
;
1401 addr
= cpu_get_phys_page_debug(env
, pc
);
1402 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
1404 pd
= IO_MEM_UNASSIGNED
;
1406 pd
= p
->phys_offset
;
1408 ram_addr
= (pd
& TARGET_PAGE_MASK
) | (pc
& ~TARGET_PAGE_MASK
);
1409 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1, 0);
1412 #endif /* TARGET_HAS_ICE */
1414 #if defined(CONFIG_USER_ONLY)
1415 void cpu_watchpoint_remove_all(CPUState
*env
, int mask
)
1420 int cpu_watchpoint_insert(CPUState
*env
, target_ulong addr
, target_ulong len
,
1421 int flags
, CPUWatchpoint
**watchpoint
)
1426 /* Add a watchpoint. */
1427 int cpu_watchpoint_insert(CPUState
*env
, target_ulong addr
, target_ulong len
,
1428 int flags
, CPUWatchpoint
**watchpoint
)
1430 target_ulong len_mask
= ~(len
- 1);
1433 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
1434 if ((len
!= 1 && len
!= 2 && len
!= 4 && len
!= 8) || (addr
& ~len_mask
)) {
1435 fprintf(stderr
, "qemu: tried to set invalid watchpoint at "
1436 TARGET_FMT_lx
", len=" TARGET_FMT_lu
"\n", addr
, len
);
1439 wp
= qemu_malloc(sizeof(*wp
));
1442 wp
->len_mask
= len_mask
;
1445 /* keep all GDB-injected watchpoints in front */
1447 QTAILQ_INSERT_HEAD(&env
->watchpoints
, wp
, entry
);
1449 QTAILQ_INSERT_TAIL(&env
->watchpoints
, wp
, entry
);
1451 tlb_flush_page(env
, addr
);
1458 /* Remove a specific watchpoint. */
1459 int cpu_watchpoint_remove(CPUState
*env
, target_ulong addr
, target_ulong len
,
1462 target_ulong len_mask
= ~(len
- 1);
1465 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1466 if (addr
== wp
->vaddr
&& len_mask
== wp
->len_mask
1467 && flags
== (wp
->flags
& ~BP_WATCHPOINT_HIT
)) {
1468 cpu_watchpoint_remove_by_ref(env
, wp
);
1475 /* Remove a specific watchpoint by reference. */
1476 void cpu_watchpoint_remove_by_ref(CPUState
*env
, CPUWatchpoint
*watchpoint
)
1478 QTAILQ_REMOVE(&env
->watchpoints
, watchpoint
, entry
);
1480 tlb_flush_page(env
, watchpoint
->vaddr
);
1482 qemu_free(watchpoint
);
1485 /* Remove all matching watchpoints. */
1486 void cpu_watchpoint_remove_all(CPUState
*env
, int mask
)
1488 CPUWatchpoint
*wp
, *next
;
1490 QTAILQ_FOREACH_SAFE(wp
, &env
->watchpoints
, entry
, next
) {
1491 if (wp
->flags
& mask
)
1492 cpu_watchpoint_remove_by_ref(env
, wp
);
1497 /* Add a breakpoint. */
1498 int cpu_breakpoint_insert(CPUState
*env
, target_ulong pc
, int flags
,
1499 CPUBreakpoint
**breakpoint
)
1501 #if defined(TARGET_HAS_ICE)
1504 bp
= qemu_malloc(sizeof(*bp
));
1509 /* keep all GDB-injected breakpoints in front */
1511 QTAILQ_INSERT_HEAD(&env
->breakpoints
, bp
, entry
);
1513 QTAILQ_INSERT_TAIL(&env
->breakpoints
, bp
, entry
);
1515 breakpoint_invalidate(env
, pc
);
1525 /* Remove a specific breakpoint. */
1526 int cpu_breakpoint_remove(CPUState
*env
, target_ulong pc
, int flags
)
1528 #if defined(TARGET_HAS_ICE)
1531 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1532 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
1533 cpu_breakpoint_remove_by_ref(env
, bp
);
1543 /* Remove a specific breakpoint by reference. */
1544 void cpu_breakpoint_remove_by_ref(CPUState
*env
, CPUBreakpoint
*breakpoint
)
1546 #if defined(TARGET_HAS_ICE)
1547 QTAILQ_REMOVE(&env
->breakpoints
, breakpoint
, entry
);
1549 breakpoint_invalidate(env
, breakpoint
->pc
);
1551 qemu_free(breakpoint
);
1555 /* Remove all matching breakpoints. */
1556 void cpu_breakpoint_remove_all(CPUState
*env
, int mask
)
1558 #if defined(TARGET_HAS_ICE)
1559 CPUBreakpoint
*bp
, *next
;
1561 QTAILQ_FOREACH_SAFE(bp
, &env
->breakpoints
, entry
, next
) {
1562 if (bp
->flags
& mask
)
1563 cpu_breakpoint_remove_by_ref(env
, bp
);
1568 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1569 CPU loop after each instruction */
1570 void cpu_single_step(CPUState
*env
, int enabled
)
1572 #if defined(TARGET_HAS_ICE)
1573 if (env
->singlestep_enabled
!= enabled
) {
1574 env
->singlestep_enabled
= enabled
;
1576 kvm_update_guest_debug(env
, 0);
1578 /* must flush all the translated code to avoid inconsistencies */
1579 /* XXX: only flush what is necessary */
1586 /* enable or disable low levels log */
1587 void cpu_set_log(int log_flags
)
1589 loglevel
= log_flags
;
1590 if (loglevel
&& !logfile
) {
1591 logfile
= fopen(logfilename
, log_append
? "a" : "w");
1593 perror(logfilename
);
1596 #if !defined(CONFIG_SOFTMMU)
1597 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1599 static char logfile_buf
[4096];
1600 setvbuf(logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
1602 #elif !defined(_WIN32)
1603 /* Win32 doesn't support line-buffering and requires size >= 2 */
1604 setvbuf(logfile
, NULL
, _IOLBF
, 0);
1608 if (!loglevel
&& logfile
) {
1614 void cpu_set_log_filename(const char *filename
)
1616 logfilename
= strdup(filename
);
1621 cpu_set_log(loglevel
);
1624 static void cpu_unlink_tb(CPUState
*env
)
1626 /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
1627 problem and hope the cpu will stop of its own accord. For userspace
1628 emulation this often isn't actually as bad as it sounds. Often
1629 signals are used primarily to interrupt blocking syscalls. */
1630 TranslationBlock
*tb
;
1631 static spinlock_t interrupt_lock
= SPIN_LOCK_UNLOCKED
;
1633 spin_lock(&interrupt_lock
);
1634 tb
= env
->current_tb
;
1635 /* if the cpu is currently executing code, we must unlink it and
1636 all the potentially executing TB */
1638 env
->current_tb
= NULL
;
1639 tb_reset_jump_recursive(tb
);
1641 spin_unlock(&interrupt_lock
);
1644 /* mask must never be zero, except for A20 change call */
1645 void cpu_interrupt(CPUState
*env
, int mask
)
1649 old_mask
= env
->interrupt_request
;
1650 env
->interrupt_request
|= mask
;
1651 if (kvm_enabled() && !kvm_irqchip_in_kernel())
1652 kvm_update_interrupt_request(env
);
1654 #ifndef CONFIG_USER_ONLY
1656 * If called from iothread context, wake the target cpu in
1659 if (!qemu_cpu_self(env
)) {
1666 env
->icount_decr
.u16
.high
= 0xffff;
1667 #ifndef CONFIG_USER_ONLY
1669 && (mask
& ~old_mask
) != 0) {
1670 cpu_abort(env
, "Raised interrupt while not in I/O function");
1678 void cpu_reset_interrupt(CPUState
*env
, int mask
)
1680 env
->interrupt_request
&= ~mask
;
1683 void cpu_exit(CPUState
*env
)
1685 env
->exit_request
= 1;
1689 const CPULogItem cpu_log_items
[] = {
1690 { CPU_LOG_TB_OUT_ASM
, "out_asm",
1691 "show generated host assembly code for each compiled TB" },
1692 { CPU_LOG_TB_IN_ASM
, "in_asm",
1693 "show target assembly code for each compiled TB" },
1694 { CPU_LOG_TB_OP
, "op",
1695 "show micro ops for each compiled TB" },
1696 { CPU_LOG_TB_OP_OPT
, "op_opt",
1699 "before eflags optimization and "
1701 "after liveness analysis" },
1702 { CPU_LOG_INT
, "int",
1703 "show interrupts/exceptions in short format" },
1704 { CPU_LOG_EXEC
, "exec",
1705 "show trace before each executed TB (lots of logs)" },
1706 { CPU_LOG_TB_CPU
, "cpu",
1707 "show CPU state before block translation" },
1709 { CPU_LOG_PCALL
, "pcall",
1710 "show protected mode far calls/returns/exceptions" },
1711 { CPU_LOG_RESET
, "cpu_reset",
1712 "show CPU state before CPU resets" },
1715 { CPU_LOG_IOPORT
, "ioport",
1716 "show all i/o ports accesses" },
1721 #ifndef CONFIG_USER_ONLY
1722 static QLIST_HEAD(memory_client_list
, CPUPhysMemoryClient
) memory_client_list
1723 = QLIST_HEAD_INITIALIZER(memory_client_list
);
1725 static void cpu_notify_set_memory(target_phys_addr_t start_addr
,
1727 ram_addr_t phys_offset
)
1729 CPUPhysMemoryClient
*client
;
1730 QLIST_FOREACH(client
, &memory_client_list
, list
) {
1731 client
->set_memory(client
, start_addr
, size
, phys_offset
);
1735 static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start
,
1736 target_phys_addr_t end
)
1738 CPUPhysMemoryClient
*client
;
1739 QLIST_FOREACH(client
, &memory_client_list
, list
) {
1740 int r
= client
->sync_dirty_bitmap(client
, start
, end
);
1747 static int cpu_notify_migration_log(int enable
)
1749 CPUPhysMemoryClient
*client
;
1750 QLIST_FOREACH(client
, &memory_client_list
, list
) {
1751 int r
= client
->migration_log(client
, enable
);
1758 static void phys_page_for_each_1(CPUPhysMemoryClient
*client
,
1759 int level
, void **lp
)
1767 PhysPageDesc
*pd
= *lp
;
1768 for (i
= 0; i
< L2_SIZE
; ++i
) {
1769 if (pd
[i
].phys_offset
!= IO_MEM_UNASSIGNED
) {
1770 client
->set_memory(client
, pd
[i
].region_offset
,
1771 TARGET_PAGE_SIZE
, pd
[i
].phys_offset
);
1776 for (i
= 0; i
< L2_SIZE
; ++i
) {
1777 phys_page_for_each_1(client
, level
- 1, pp
+ i
);
1782 static void phys_page_for_each(CPUPhysMemoryClient
*client
)
1785 for (i
= 0; i
< P_L1_SIZE
; ++i
) {
1786 phys_page_for_each_1(client
, P_L1_SHIFT
/ L2_BITS
- 1,
1791 void cpu_register_phys_memory_client(CPUPhysMemoryClient
*client
)
1793 QLIST_INSERT_HEAD(&memory_client_list
, client
, list
);
1794 phys_page_for_each(client
);
1797 void cpu_unregister_phys_memory_client(CPUPhysMemoryClient
*client
)
1799 QLIST_REMOVE(client
, list
);
1803 static int cmp1(const char *s1
, int n
, const char *s2
)
1805 if (strlen(s2
) != n
)
1807 return memcmp(s1
, s2
, n
) == 0;
1810 /* takes a comma separated list of log masks. Return 0 if error. */
1811 int cpu_str_to_log_mask(const char *str
)
1813 const CPULogItem
*item
;
1820 p1
= strchr(p
, ',');
1823 if(cmp1(p
,p1
-p
,"all")) {
1824 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1828 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1829 if (cmp1(p
, p1
- p
, item
->name
))
1843 void cpu_abort(CPUState
*env
, const char *fmt
, ...)
1850 fprintf(stderr
, "qemu: fatal: ");
1851 vfprintf(stderr
, fmt
, ap
);
1852 fprintf(stderr
, "\n");
1854 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1856 cpu_dump_state(env
, stderr
, fprintf
, 0);
1858 if (qemu_log_enabled()) {
1859 qemu_log("qemu: fatal: ");
1860 qemu_log_vprintf(fmt
, ap2
);
1863 log_cpu_state(env
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
1865 log_cpu_state(env
, 0);
1872 #if defined(CONFIG_USER_ONLY)
1874 struct sigaction act
;
1875 sigfillset(&act
.sa_mask
);
1876 act
.sa_handler
= SIG_DFL
;
1877 sigaction(SIGABRT
, &act
, NULL
);
1883 CPUState
*cpu_copy(CPUState
*env
)
1885 CPUState
*new_env
= cpu_init(env
->cpu_model_str
);
1886 CPUState
*next_cpu
= new_env
->next_cpu
;
1887 int cpu_index
= new_env
->cpu_index
;
1888 #if defined(TARGET_HAS_ICE)
1893 memcpy(new_env
, env
, sizeof(CPUState
));
1895 /* Preserve chaining and index. */
1896 new_env
->next_cpu
= next_cpu
;
1897 new_env
->cpu_index
= cpu_index
;
1899 /* Clone all break/watchpoints.
1900 Note: Once we support ptrace with hw-debug register access, make sure
1901 BP_CPU break/watchpoints are handled correctly on clone. */
1902 QTAILQ_INIT(&env
->breakpoints
);
1903 QTAILQ_INIT(&env
->watchpoints
);
1904 #if defined(TARGET_HAS_ICE)
1905 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1906 cpu_breakpoint_insert(new_env
, bp
->pc
, bp
->flags
, NULL
);
1908 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
1909 cpu_watchpoint_insert(new_env
, wp
->vaddr
, (~wp
->len_mask
) + 1,
1917 #if !defined(CONFIG_USER_ONLY)
1919 static inline void tlb_flush_jmp_cache(CPUState
*env
, target_ulong addr
)
1923 /* Discard jump cache entries for any tb which might potentially
1924 overlap the flushed page. */
1925 i
= tb_jmp_cache_hash_page(addr
- TARGET_PAGE_SIZE
);
1926 memset (&env
->tb_jmp_cache
[i
], 0,
1927 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1929 i
= tb_jmp_cache_hash_page(addr
);
1930 memset (&env
->tb_jmp_cache
[i
], 0,
1931 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1934 static CPUTLBEntry s_cputlb_empty_entry
= {
1941 /* NOTE: if flush_global is true, also flush global entries (not
1943 void tlb_flush(CPUState
*env
, int flush_global
)
1947 #if defined(DEBUG_TLB)
1948 printf("tlb_flush:\n");
1950 /* must reset current TB so that interrupts cannot modify the
1951 links while we are modifying them */
1952 env
->current_tb
= NULL
;
1954 for(i
= 0; i
< CPU_TLB_SIZE
; i
++) {
1956 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
1957 env
->tlb_table
[mmu_idx
][i
] = s_cputlb_empty_entry
;
1961 memset (env
->tb_jmp_cache
, 0, TB_JMP_CACHE_SIZE
* sizeof (void *));
1963 env
->tlb_flush_addr
= -1;
1964 env
->tlb_flush_mask
= 0;
1968 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, target_ulong addr
)
1970 if (addr
== (tlb_entry
->addr_read
&
1971 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1972 addr
== (tlb_entry
->addr_write
&
1973 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)) ||
1974 addr
== (tlb_entry
->addr_code
&
1975 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1976 *tlb_entry
= s_cputlb_empty_entry
;
1980 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
1985 #if defined(DEBUG_TLB)
1986 printf("tlb_flush_page: " TARGET_FMT_lx
"\n", addr
);
1988 /* Check if we need to flush due to large pages. */
1989 if ((addr
& env
->tlb_flush_mask
) == env
->tlb_flush_addr
) {
1990 #if defined(DEBUG_TLB)
1991 printf("tlb_flush_page: forced full flush ("
1992 TARGET_FMT_lx
"/" TARGET_FMT_lx
")\n",
1993 env
->tlb_flush_addr
, env
->tlb_flush_mask
);
1998 /* must reset current TB so that interrupts cannot modify the
1999 links while we are modifying them */
2000 env
->current_tb
= NULL
;
2002 addr
&= TARGET_PAGE_MASK
;
2003 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
2004 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
2005 tlb_flush_entry(&env
->tlb_table
[mmu_idx
][i
], addr
);
2007 tlb_flush_jmp_cache(env
, addr
);
2010 /* update the TLBs so that writes to code in the virtual page 'addr'
2012 static void tlb_protect_code(ram_addr_t ram_addr
)
2014 cpu_physical_memory_reset_dirty(ram_addr
,
2015 ram_addr
+ TARGET_PAGE_SIZE
,
2019 /* update the TLB so that writes in physical page 'phys_addr' are no longer
2020 tested for self modifying code */
2021 static void tlb_unprotect_code_phys(CPUState
*env
, ram_addr_t ram_addr
,
2024 cpu_physical_memory_set_dirty_flags(ram_addr
, CODE_DIRTY_FLAG
);
2027 static inline void tlb_reset_dirty_range(CPUTLBEntry
*tlb_entry
,
2028 unsigned long start
, unsigned long length
)
2031 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
2032 addr
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) + tlb_entry
->addend
;
2033 if ((addr
- start
) < length
) {
2034 tlb_entry
->addr_write
= (tlb_entry
->addr_write
& TARGET_PAGE_MASK
) | TLB_NOTDIRTY
;
2039 /* Note: start and end must be within the same ram block. */
2040 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t end
,
2044 unsigned long length
, start1
;
2047 start
&= TARGET_PAGE_MASK
;
2048 end
= TARGET_PAGE_ALIGN(end
);
2050 length
= end
- start
;
2053 cpu_physical_memory_mask_dirty_range(start
, length
, dirty_flags
);
2055 /* we modify the TLB cache so that the dirty bit will be set again
2056 when accessing the range */
2057 start1
= (unsigned long)qemu_safe_ram_ptr(start
);
2058 /* Chek that we don't span multiple blocks - this breaks the
2059 address comparisons below. */
2060 if ((unsigned long)qemu_safe_ram_ptr(end
- 1) - start1
2061 != (end
- 1) - start
) {
2065 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2067 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
2068 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
2069 tlb_reset_dirty_range(&env
->tlb_table
[mmu_idx
][i
],
2075 int cpu_physical_memory_set_dirty_tracking(int enable
)
2078 in_migration
= enable
;
2079 ret
= cpu_notify_migration_log(!!enable
);
2083 int cpu_physical_memory_get_dirty_tracking(void)
2085 return in_migration
;
2088 int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
,
2089 target_phys_addr_t end_addr
)
2093 ret
= cpu_notify_sync_dirty_bitmap(start_addr
, end_addr
);
2097 static inline void tlb_update_dirty(CPUTLBEntry
*tlb_entry
)
2099 ram_addr_t ram_addr
;
2102 if ((tlb_entry
->addr_write
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
) {
2103 p
= (void *)(unsigned long)((tlb_entry
->addr_write
& TARGET_PAGE_MASK
)
2104 + tlb_entry
->addend
);
2105 ram_addr
= qemu_ram_addr_from_host_nofail(p
);
2106 if (!cpu_physical_memory_is_dirty(ram_addr
)) {
2107 tlb_entry
->addr_write
|= TLB_NOTDIRTY
;
2112 /* update the TLB according to the current state of the dirty bits */
2113 void cpu_tlb_update_dirty(CPUState
*env
)
2117 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++) {
2118 for(i
= 0; i
< CPU_TLB_SIZE
; i
++)
2119 tlb_update_dirty(&env
->tlb_table
[mmu_idx
][i
]);
2123 static inline void tlb_set_dirty1(CPUTLBEntry
*tlb_entry
, target_ulong vaddr
)
2125 if (tlb_entry
->addr_write
== (vaddr
| TLB_NOTDIRTY
))
2126 tlb_entry
->addr_write
= vaddr
;
2129 /* update the TLB corresponding to virtual page vaddr
2130 so that it is no longer dirty */
2131 static inline void tlb_set_dirty(CPUState
*env
, target_ulong vaddr
)
2136 vaddr
&= TARGET_PAGE_MASK
;
2137 i
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
2138 for (mmu_idx
= 0; mmu_idx
< NB_MMU_MODES
; mmu_idx
++)
2139 tlb_set_dirty1(&env
->tlb_table
[mmu_idx
][i
], vaddr
);
2142 /* Our TLB does not support large pages, so remember the area covered by
2143 large pages and trigger a full TLB flush if these are invalidated. */
2144 static void tlb_add_large_page(CPUState
*env
, target_ulong vaddr
,
2147 target_ulong mask
= ~(size
- 1);
2149 if (env
->tlb_flush_addr
== (target_ulong
)-1) {
2150 env
->tlb_flush_addr
= vaddr
& mask
;
2151 env
->tlb_flush_mask
= mask
;
2154 /* Extend the existing region to include the new page.
2155 This is a compromise between unnecessary flushes and the cost
2156 of maintaining a full variable size TLB. */
2157 mask
&= env
->tlb_flush_mask
;
2158 while (((env
->tlb_flush_addr
^ vaddr
) & mask
) != 0) {
2161 env
->tlb_flush_addr
&= mask
;
2162 env
->tlb_flush_mask
= mask
;
2165 /* Add a new TLB entry. At most one entry for a given virtual address
2166 is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
2167 supplied size is only used by tlb_flush_page. */
2168 void tlb_set_page(CPUState
*env
, target_ulong vaddr
,
2169 target_phys_addr_t paddr
, int prot
,
2170 int mmu_idx
, target_ulong size
)
2175 target_ulong address
;
2176 target_ulong code_address
;
2177 unsigned long addend
;
2180 target_phys_addr_t iotlb
;
2182 assert(size
>= TARGET_PAGE_SIZE
);
2183 if (size
!= TARGET_PAGE_SIZE
) {
2184 tlb_add_large_page(env
, vaddr
, size
);
2186 p
= phys_page_find(paddr
>> TARGET_PAGE_BITS
);
2188 pd
= IO_MEM_UNASSIGNED
;
2190 pd
= p
->phys_offset
;
2192 #if defined(DEBUG_TLB)
2193 printf("tlb_set_page: vaddr=" TARGET_FMT_lx
" paddr=0x" TARGET_FMT_plx
2194 " prot=%x idx=%d pd=0x%08lx\n",
2195 vaddr
, paddr
, prot
, mmu_idx
, pd
);
2199 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&& !(pd
& IO_MEM_ROMD
)) {
2200 /* IO memory case (romd handled later) */
2201 address
|= TLB_MMIO
;
2203 addend
= (unsigned long)qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
);
2204 if ((pd
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
2206 iotlb
= pd
& TARGET_PAGE_MASK
;
2207 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
)
2208 iotlb
|= IO_MEM_NOTDIRTY
;
2210 iotlb
|= IO_MEM_ROM
;
2212 /* IO handlers are currently passed a physical address.
2213 It would be nice to pass an offset from the base address
2214 of that region. This would avoid having to special case RAM,
2215 and avoid full address decoding in every device.
2216 We can't use the high bits of pd for this because
2217 IO_MEM_ROMD uses these as a ram address. */
2218 iotlb
= (pd
& ~TARGET_PAGE_MASK
);
2220 iotlb
+= p
->region_offset
;
2226 code_address
= address
;
2227 /* Make accesses to pages with watchpoints go via the
2228 watchpoint trap routines. */
2229 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
2230 if (vaddr
== (wp
->vaddr
& TARGET_PAGE_MASK
)) {
2231 /* Avoid trapping reads of pages with a write breakpoint. */
2232 if ((prot
& PAGE_WRITE
) || (wp
->flags
& BP_MEM_READ
)) {
2233 iotlb
= io_mem_watch
+ paddr
;
2234 address
|= TLB_MMIO
;
2240 index
= (vaddr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
2241 env
->iotlb
[mmu_idx
][index
] = iotlb
- vaddr
;
2242 te
= &env
->tlb_table
[mmu_idx
][index
];
2243 te
->addend
= addend
- vaddr
;
2244 if (prot
& PAGE_READ
) {
2245 te
->addr_read
= address
;
2250 if (prot
& PAGE_EXEC
) {
2251 te
->addr_code
= code_address
;
2255 if (prot
& PAGE_WRITE
) {
2256 if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_ROM
||
2257 (pd
& IO_MEM_ROMD
)) {
2258 /* Write access calls the I/O callback. */
2259 te
->addr_write
= address
| TLB_MMIO
;
2260 } else if ((pd
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
&&
2261 !cpu_physical_memory_is_dirty(pd
)) {
2262 te
->addr_write
= address
| TLB_NOTDIRTY
;
2264 te
->addr_write
= address
;
2267 te
->addr_write
= -1;
2273 void tlb_flush(CPUState
*env
, int flush_global
)
2277 void tlb_flush_page(CPUState
*env
, target_ulong addr
)
2282 * Walks guest process memory "regions" one by one
2283 * and calls callback function 'fn' for each region.
2286 struct walk_memory_regions_data
2288 walk_memory_regions_fn fn
;
2290 unsigned long start
;
2294 static int walk_memory_regions_end(struct walk_memory_regions_data
*data
,
2295 abi_ulong end
, int new_prot
)
2297 if (data
->start
!= -1ul) {
2298 int rc
= data
->fn(data
->priv
, data
->start
, end
, data
->prot
);
2304 data
->start
= (new_prot
? end
: -1ul);
2305 data
->prot
= new_prot
;
2310 static int walk_memory_regions_1(struct walk_memory_regions_data
*data
,
2311 abi_ulong base
, int level
, void **lp
)
2317 return walk_memory_regions_end(data
, base
, 0);
2322 for (i
= 0; i
< L2_SIZE
; ++i
) {
2323 int prot
= pd
[i
].flags
;
2325 pa
= base
| (i
<< TARGET_PAGE_BITS
);
2326 if (prot
!= data
->prot
) {
2327 rc
= walk_memory_regions_end(data
, pa
, prot
);
2335 for (i
= 0; i
< L2_SIZE
; ++i
) {
2336 pa
= base
| ((abi_ulong
)i
<<
2337 (TARGET_PAGE_BITS
+ L2_BITS
* level
));
2338 rc
= walk_memory_regions_1(data
, pa
, level
- 1, pp
+ i
);
2348 int walk_memory_regions(void *priv
, walk_memory_regions_fn fn
)
2350 struct walk_memory_regions_data data
;
2358 for (i
= 0; i
< V_L1_SIZE
; i
++) {
2359 int rc
= walk_memory_regions_1(&data
, (abi_ulong
)i
<< V_L1_SHIFT
,
2360 V_L1_SHIFT
/ L2_BITS
- 1, l1_map
+ i
);
2366 return walk_memory_regions_end(&data
, 0, 0);
2369 static int dump_region(void *priv
, abi_ulong start
,
2370 abi_ulong end
, unsigned long prot
)
2372 FILE *f
= (FILE *)priv
;
2374 (void) fprintf(f
, TARGET_ABI_FMT_lx
"-"TARGET_ABI_FMT_lx
2375 " "TARGET_ABI_FMT_lx
" %c%c%c\n",
2376 start
, end
, end
- start
,
2377 ((prot
& PAGE_READ
) ? 'r' : '-'),
2378 ((prot
& PAGE_WRITE
) ? 'w' : '-'),
2379 ((prot
& PAGE_EXEC
) ? 'x' : '-'));
2384 /* dump memory mappings */
2385 void page_dump(FILE *f
)
2387 (void) fprintf(f
, "%-8s %-8s %-8s %s\n",
2388 "start", "end", "size", "prot");
2389 walk_memory_regions(f
, dump_region
);
2392 int page_get_flags(target_ulong address
)
2396 p
= page_find(address
>> TARGET_PAGE_BITS
);
2402 /* Modify the flags of a page and invalidate the code if necessary.
2403 The flag PAGE_WRITE_ORG is positioned automatically depending
2404 on PAGE_WRITE. The mmap_lock should already be held. */
2405 void page_set_flags(target_ulong start
, target_ulong end
, int flags
)
2407 target_ulong addr
, len
;
2409 /* This function should never be called with addresses outside the
2410 guest address space. If this assert fires, it probably indicates
2411 a missing call to h2g_valid. */
2412 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2413 assert(end
< ((abi_ulong
)1 << L1_MAP_ADDR_SPACE_BITS
));
2415 assert(start
< end
);
2417 start
= start
& TARGET_PAGE_MASK
;
2418 end
= TARGET_PAGE_ALIGN(end
);
2420 if (flags
& PAGE_WRITE
) {
2421 flags
|= PAGE_WRITE_ORG
;
2424 for (addr
= start
, len
= end
- start
;
2426 len
-= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
2427 PageDesc
*p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
2429 /* If the write protection bit is set, then we invalidate
2431 if (!(p
->flags
& PAGE_WRITE
) &&
2432 (flags
& PAGE_WRITE
) &&
2434 tb_invalidate_phys_page(addr
, 0, NULL
);
2440 int page_check_range(target_ulong start
, target_ulong len
, int flags
)
2446 /* This function should never be called with addresses outside the
2447 guest address space. If this assert fires, it probably indicates
2448 a missing call to h2g_valid. */
2449 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2450 assert(start
< ((abi_ulong
)1 << L1_MAP_ADDR_SPACE_BITS
));
2456 if (start
+ len
- 1 < start
) {
2457 /* We've wrapped around. */
2461 end
= TARGET_PAGE_ALIGN(start
+len
); /* must do before we loose bits in the next step */
2462 start
= start
& TARGET_PAGE_MASK
;
2464 for (addr
= start
, len
= end
- start
;
2466 len
-= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
2467 p
= page_find(addr
>> TARGET_PAGE_BITS
);
2470 if( !(p
->flags
& PAGE_VALID
) )
2473 if ((flags
& PAGE_READ
) && !(p
->flags
& PAGE_READ
))
2475 if (flags
& PAGE_WRITE
) {
2476 if (!(p
->flags
& PAGE_WRITE_ORG
))
2478 /* unprotect the page if it was put read-only because it
2479 contains translated code */
2480 if (!(p
->flags
& PAGE_WRITE
)) {
2481 if (!page_unprotect(addr
, 0, NULL
))
2490 /* called from signal handler: invalidate the code and unprotect the
2491 page. Return TRUE if the fault was successfully handled. */
2492 int page_unprotect(target_ulong address
, unsigned long pc
, void *puc
)
2496 target_ulong host_start
, host_end
, addr
;
2498 /* Technically this isn't safe inside a signal handler. However we
2499 know this only ever happens in a synchronous SEGV handler, so in
2500 practice it seems to be ok. */
2503 p
= page_find(address
>> TARGET_PAGE_BITS
);
2509 /* if the page was really writable, then we change its
2510 protection back to writable */
2511 if ((p
->flags
& PAGE_WRITE_ORG
) && !(p
->flags
& PAGE_WRITE
)) {
2512 host_start
= address
& qemu_host_page_mask
;
2513 host_end
= host_start
+ qemu_host_page_size
;
2516 for (addr
= host_start
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
2517 p
= page_find(addr
>> TARGET_PAGE_BITS
);
2518 p
->flags
|= PAGE_WRITE
;
2521 /* and since the content will be modified, we must invalidate
2522 the corresponding translated code. */
2523 tb_invalidate_phys_page(addr
, pc
, puc
);
2524 #ifdef DEBUG_TB_CHECK
2525 tb_invalidate_check(addr
);
2528 mprotect((void *)g2h(host_start
), qemu_host_page_size
,
2538 static inline void tlb_set_dirty(CPUState
*env
,
2539 unsigned long addr
, target_ulong vaddr
)
2542 #endif /* defined(CONFIG_USER_ONLY) */
2544 #if !defined(CONFIG_USER_ONLY)
2546 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
2547 typedef struct subpage_t
{
2548 target_phys_addr_t base
;
2549 ram_addr_t sub_io_index
[TARGET_PAGE_SIZE
];
2550 ram_addr_t region_offset
[TARGET_PAGE_SIZE
];
2553 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2554 ram_addr_t memory
, ram_addr_t region_offset
);
2555 static subpage_t
*subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
2556 ram_addr_t orig_memory
,
2557 ram_addr_t region_offset
);
2558 #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2561 if (addr > start_addr) \
2564 start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
2565 if (start_addr2 > 0) \
2569 if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
2570 end_addr2 = TARGET_PAGE_SIZE - 1; \
2572 end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
2573 if (end_addr2 < TARGET_PAGE_SIZE - 1) \
2578 /* register physical memory.
2579 For RAM, 'size' must be a multiple of the target page size.
2580 If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2581 io memory page. The address used when calling the IO function is
2582 the offset from the start of the region, plus region_offset. Both
2583 start_addr and region_offset are rounded down to a page boundary
2584 before calculating this offset. This should not be a problem unless
2585 the low bits of start_addr and region_offset differ. */
2586 void cpu_register_physical_memory_offset(target_phys_addr_t start_addr
,
2588 ram_addr_t phys_offset
,
2589 ram_addr_t region_offset
)
2591 target_phys_addr_t addr
, end_addr
;
2594 ram_addr_t orig_size
= size
;
2597 cpu_notify_set_memory(start_addr
, size
, phys_offset
);
2599 if (phys_offset
== IO_MEM_UNASSIGNED
) {
2600 region_offset
= start_addr
;
2602 region_offset
&= TARGET_PAGE_MASK
;
2603 size
= (size
+ TARGET_PAGE_SIZE
- 1) & TARGET_PAGE_MASK
;
2604 end_addr
= start_addr
+ (target_phys_addr_t
)size
;
2605 for(addr
= start_addr
; addr
!= end_addr
; addr
+= TARGET_PAGE_SIZE
) {
2606 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2607 if (p
&& p
->phys_offset
!= IO_MEM_UNASSIGNED
) {
2608 ram_addr_t orig_memory
= p
->phys_offset
;
2609 target_phys_addr_t start_addr2
, end_addr2
;
2610 int need_subpage
= 0;
2612 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
, end_addr2
,
2615 if (!(orig_memory
& IO_MEM_SUBPAGE
)) {
2616 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2617 &p
->phys_offset
, orig_memory
,
2620 subpage
= io_mem_opaque
[(orig_memory
& ~TARGET_PAGE_MASK
)
2623 subpage_register(subpage
, start_addr2
, end_addr2
, phys_offset
,
2625 p
->region_offset
= 0;
2627 p
->phys_offset
= phys_offset
;
2628 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2629 (phys_offset
& IO_MEM_ROMD
))
2630 phys_offset
+= TARGET_PAGE_SIZE
;
2633 p
= phys_page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
2634 p
->phys_offset
= phys_offset
;
2635 p
->region_offset
= region_offset
;
2636 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
||
2637 (phys_offset
& IO_MEM_ROMD
)) {
2638 phys_offset
+= TARGET_PAGE_SIZE
;
2640 target_phys_addr_t start_addr2
, end_addr2
;
2641 int need_subpage
= 0;
2643 CHECK_SUBPAGE(addr
, start_addr
, start_addr2
, end_addr
,
2644 end_addr2
, need_subpage
);
2647 subpage
= subpage_init((addr
& TARGET_PAGE_MASK
),
2648 &p
->phys_offset
, IO_MEM_UNASSIGNED
,
2649 addr
& TARGET_PAGE_MASK
);
2650 subpage_register(subpage
, start_addr2
, end_addr2
,
2651 phys_offset
, region_offset
);
2652 p
->region_offset
= 0;
2656 region_offset
+= TARGET_PAGE_SIZE
;
2659 /* since each CPU stores ram addresses in its TLB cache, we must
2660 reset the modified entries */
2662 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2667 /* XXX: temporary until new memory mapping API */
2668 ram_addr_t
cpu_get_physical_page_desc(target_phys_addr_t addr
)
2672 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
2674 return IO_MEM_UNASSIGNED
;
2675 return p
->phys_offset
;
2678 void qemu_register_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2681 kvm_coalesce_mmio_region(addr
, size
);
2684 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr
, ram_addr_t size
)
2687 kvm_uncoalesce_mmio_region(addr
, size
);
2690 void qemu_flush_coalesced_mmio_buffer(void)
2693 kvm_flush_coalesced_mmio_buffer();
2696 #if defined(__linux__) && !defined(TARGET_S390X)
2698 #include <sys/vfs.h>
2700 #define HUGETLBFS_MAGIC 0x958458f6
2702 static long gethugepagesize(const char *path
)
2708 ret
= statfs(path
, &fs
);
2709 } while (ret
!= 0 && errno
== EINTR
);
2716 if (fs
.f_type
!= HUGETLBFS_MAGIC
)
2717 fprintf(stderr
, "Warning: path not on HugeTLBFS: %s\n", path
);
2722 static void *file_ram_alloc(RAMBlock
*block
,
2732 unsigned long hpagesize
;
2734 hpagesize
= gethugepagesize(path
);
2739 if (memory
< hpagesize
) {
2743 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2744 fprintf(stderr
, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
2748 if (asprintf(&filename
, "%s/qemu_back_mem.XXXXXX", path
) == -1) {
2752 fd
= mkstemp(filename
);
2754 perror("unable to create backing store for hugepages");
2761 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
2764 * ftruncate is not supported by hugetlbfs in older
2765 * hosts, so don't bother bailing out on errors.
2766 * If anything goes wrong with it under other filesystems,
2769 if (ftruncate(fd
, memory
))
2770 perror("ftruncate");
2773 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
2774 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
2775 * to sidestep this quirk.
2777 flags
= mem_prealloc
? MAP_POPULATE
| MAP_SHARED
: MAP_PRIVATE
;
2778 area
= mmap(0, memory
, PROT_READ
| PROT_WRITE
, flags
, fd
, 0);
2780 area
= mmap(0, memory
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
2782 if (area
== MAP_FAILED
) {
2783 perror("file_ram_alloc: can't mmap RAM pages");
2792 static ram_addr_t
find_ram_offset(ram_addr_t size
)
2794 RAMBlock
*block
, *next_block
;
2795 ram_addr_t offset
= 0, mingap
= ULONG_MAX
;
2797 if (QLIST_EMPTY(&ram_list
.blocks
))
2800 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2801 ram_addr_t end
, next
= ULONG_MAX
;
2803 end
= block
->offset
+ block
->length
;
2805 QLIST_FOREACH(next_block
, &ram_list
.blocks
, next
) {
2806 if (next_block
->offset
>= end
) {
2807 next
= MIN(next
, next_block
->offset
);
2810 if (next
- end
>= size
&& next
- end
< mingap
) {
2812 mingap
= next
- end
;
2818 static ram_addr_t
last_ram_offset(void)
2821 ram_addr_t last
= 0;
2823 QLIST_FOREACH(block
, &ram_list
.blocks
, next
)
2824 last
= MAX(last
, block
->offset
+ block
->length
);
2829 ram_addr_t
qemu_ram_alloc_from_ptr(DeviceState
*dev
, const char *name
,
2830 ram_addr_t size
, void *host
)
2832 RAMBlock
*new_block
, *block
;
2834 size
= TARGET_PAGE_ALIGN(size
);
2835 new_block
= qemu_mallocz(sizeof(*new_block
));
2837 if (dev
&& dev
->parent_bus
&& dev
->parent_bus
->info
->get_dev_path
) {
2838 char *id
= dev
->parent_bus
->info
->get_dev_path(dev
);
2840 snprintf(new_block
->idstr
, sizeof(new_block
->idstr
), "%s/", id
);
2844 pstrcat(new_block
->idstr
, sizeof(new_block
->idstr
), name
);
2846 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2847 if (!strcmp(block
->idstr
, new_block
->idstr
)) {
2848 fprintf(stderr
, "RAMBlock \"%s\" already registered, abort!\n",
2855 new_block
->host
= host
;
2858 #if defined (__linux__) && !defined(TARGET_S390X)
2859 new_block
->host
= file_ram_alloc(new_block
, size
, mem_path
);
2860 if (!new_block
->host
) {
2861 new_block
->host
= qemu_vmalloc(size
);
2862 qemu_madvise(new_block
->host
, size
, QEMU_MADV_MERGEABLE
);
2865 fprintf(stderr
, "-mem-path option unsupported\n");
2869 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
2870 /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
2871 new_block
->host
= mmap((void*)0x1000000, size
,
2872 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
2873 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
2875 new_block
->host
= qemu_vmalloc(size
);
2877 qemu_madvise(new_block
->host
, size
, QEMU_MADV_MERGEABLE
);
2881 new_block
->offset
= find_ram_offset(size
);
2882 new_block
->length
= size
;
2884 QLIST_INSERT_HEAD(&ram_list
.blocks
, new_block
, next
);
2886 ram_list
.phys_dirty
= qemu_realloc(ram_list
.phys_dirty
,
2887 last_ram_offset() >> TARGET_PAGE_BITS
);
2888 memset(ram_list
.phys_dirty
+ (new_block
->offset
>> TARGET_PAGE_BITS
),
2889 0xff, size
>> TARGET_PAGE_BITS
);
2892 kvm_setup_guest_memory(new_block
->host
, size
);
2894 return new_block
->offset
;
2897 void qemu_ram_unmap(ram_addr_t addr
)
2901 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2902 if (addr
== block
->offset
) {
2903 QLIST_REMOVE(block
, next
);
2910 ram_addr_t
qemu_ram_alloc(DeviceState
*dev
, const char *name
, ram_addr_t size
)
2912 return qemu_ram_alloc_from_ptr(dev
, name
, size
, NULL
);
2915 void qemu_ram_free(ram_addr_t addr
)
2919 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2920 if (addr
== block
->offset
) {
2921 QLIST_REMOVE(block
, next
);
2923 #if defined (__linux__) && !defined(TARGET_S390X)
2925 munmap(block
->host
, block
->length
);
2928 qemu_vfree(block
->host
);
2932 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
2933 munmap(block
->host
, block
->length
);
2935 qemu_vfree(block
->host
);
2945 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2946 With the exception of the softmmu code in this file, this should
2947 only be used for local memory (e.g. video ram) that the device owns,
2948 and knows it isn't going to access beyond the end of the block.
2950 It should not be used for general purpose DMA.
2951 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
2953 void *qemu_get_ram_ptr(ram_addr_t addr
)
2957 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2958 if (addr
- block
->offset
< block
->length
) {
2959 QLIST_REMOVE(block
, next
);
2960 QLIST_INSERT_HEAD(&ram_list
.blocks
, block
, next
);
2961 return block
->host
+ (addr
- block
->offset
);
2965 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
2971 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2972 * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
2974 void *qemu_safe_ram_ptr(ram_addr_t addr
)
2978 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2979 if (addr
- block
->offset
< block
->length
) {
2980 return block
->host
+ (addr
- block
->offset
);
2984 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
2990 int qemu_ram_addr_from_host(void *ptr
, ram_addr_t
*ram_addr
)
2993 uint8_t *host
= ptr
;
2995 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
2996 if (host
- block
->host
< block
->length
) {
2997 *ram_addr
= block
->offset
+ (host
- block
->host
);
3004 /* Some of the softmmu routines need to translate from a host pointer
3005 (typically a TLB entry) back to a ram offset. */
3006 ram_addr_t
qemu_ram_addr_from_host_nofail(void *ptr
)
3008 ram_addr_t ram_addr
;
3010 if (qemu_ram_addr_from_host(ptr
, &ram_addr
)) {
3011 fprintf(stderr
, "Bad ram pointer %p\n", ptr
);
3017 static uint32_t unassigned_mem_readb(void *opaque
, target_phys_addr_t addr
)
3019 #ifdef DEBUG_UNASSIGNED
3020 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
3022 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3023 do_unassigned_access(addr
, 0, 0, 0, 1);
3028 static uint32_t unassigned_mem_readw(void *opaque
, target_phys_addr_t addr
)
3030 #ifdef DEBUG_UNASSIGNED
3031 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
3033 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3034 do_unassigned_access(addr
, 0, 0, 0, 2);
3039 static uint32_t unassigned_mem_readl(void *opaque
, target_phys_addr_t addr
)
3041 #ifdef DEBUG_UNASSIGNED
3042 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
3044 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3045 do_unassigned_access(addr
, 0, 0, 0, 4);
3050 static void unassigned_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
3052 #ifdef DEBUG_UNASSIGNED
3053 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
3055 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3056 do_unassigned_access(addr
, 1, 0, 0, 1);
3060 static void unassigned_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
3062 #ifdef DEBUG_UNASSIGNED
3063 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
3065 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3066 do_unassigned_access(addr
, 1, 0, 0, 2);
3070 static void unassigned_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
3072 #ifdef DEBUG_UNASSIGNED
3073 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%x\n", addr
, val
);
3075 #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
3076 do_unassigned_access(addr
, 1, 0, 0, 4);
3080 static CPUReadMemoryFunc
* const unassigned_mem_read
[3] = {
3081 unassigned_mem_readb
,
3082 unassigned_mem_readw
,
3083 unassigned_mem_readl
,
3086 static CPUWriteMemoryFunc
* const unassigned_mem_write
[3] = {
3087 unassigned_mem_writeb
,
3088 unassigned_mem_writew
,
3089 unassigned_mem_writel
,
3092 static void notdirty_mem_writeb(void *opaque
, target_phys_addr_t ram_addr
,
3096 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3097 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
3098 #if !defined(CONFIG_USER_ONLY)
3099 tb_invalidate_phys_page_fast(ram_addr
, 1);
3100 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3103 stb_p(qemu_get_ram_ptr(ram_addr
), val
);
3104 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
3105 cpu_physical_memory_set_dirty_flags(ram_addr
, dirty_flags
);
3106 /* we remove the notdirty callback only if the code has been
3108 if (dirty_flags
== 0xff)
3109 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
3112 static void notdirty_mem_writew(void *opaque
, target_phys_addr_t ram_addr
,
3116 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3117 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
3118 #if !defined(CONFIG_USER_ONLY)
3119 tb_invalidate_phys_page_fast(ram_addr
, 2);
3120 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3123 stw_p(qemu_get_ram_ptr(ram_addr
), val
);
3124 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
3125 cpu_physical_memory_set_dirty_flags(ram_addr
, dirty_flags
);
3126 /* we remove the notdirty callback only if the code has been
3128 if (dirty_flags
== 0xff)
3129 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
3132 static void notdirty_mem_writel(void *opaque
, target_phys_addr_t ram_addr
,
3136 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3137 if (!(dirty_flags
& CODE_DIRTY_FLAG
)) {
3138 #if !defined(CONFIG_USER_ONLY)
3139 tb_invalidate_phys_page_fast(ram_addr
, 4);
3140 dirty_flags
= cpu_physical_memory_get_dirty_flags(ram_addr
);
3143 stl_p(qemu_get_ram_ptr(ram_addr
), val
);
3144 dirty_flags
|= (0xff & ~CODE_DIRTY_FLAG
);
3145 cpu_physical_memory_set_dirty_flags(ram_addr
, dirty_flags
);
3146 /* we remove the notdirty callback only if the code has been
3148 if (dirty_flags
== 0xff)
3149 tlb_set_dirty(cpu_single_env
, cpu_single_env
->mem_io_vaddr
);
3152 static CPUReadMemoryFunc
* const error_mem_read
[3] = {
3153 NULL
, /* never used */
3154 NULL
, /* never used */
3155 NULL
, /* never used */
3158 static CPUWriteMemoryFunc
* const notdirty_mem_write
[3] = {
3159 notdirty_mem_writeb
,
3160 notdirty_mem_writew
,
3161 notdirty_mem_writel
,
3164 /* Generate a debug exception if a watchpoint has been hit. */
3165 static void check_watchpoint(int offset
, int len_mask
, int flags
)
3167 CPUState
*env
= cpu_single_env
;
3168 target_ulong pc
, cs_base
;
3169 TranslationBlock
*tb
;
3174 if (env
->watchpoint_hit
) {
3175 /* We re-entered the check after replacing the TB. Now raise
3176 * the debug interrupt so that is will trigger after the
3177 * current instruction. */
3178 cpu_interrupt(env
, CPU_INTERRUPT_DEBUG
);
3181 vaddr
= (env
->mem_io_vaddr
& TARGET_PAGE_MASK
) + offset
;
3182 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
3183 if ((vaddr
== (wp
->vaddr
& len_mask
) ||
3184 (vaddr
& wp
->len_mask
) == wp
->vaddr
) && (wp
->flags
& flags
)) {
3185 wp
->flags
|= BP_WATCHPOINT_HIT
;
3186 if (!env
->watchpoint_hit
) {
3187 env
->watchpoint_hit
= wp
;
3188 tb
= tb_find_pc(env
->mem_io_pc
);
3190 cpu_abort(env
, "check_watchpoint: could not find TB for "
3191 "pc=%p", (void *)env
->mem_io_pc
);
3193 cpu_restore_state(tb
, env
, env
->mem_io_pc
, NULL
);
3194 tb_phys_invalidate(tb
, -1);
3195 if (wp
->flags
& BP_STOP_BEFORE_ACCESS
) {
3196 env
->exception_index
= EXCP_DEBUG
;
3198 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &cpu_flags
);
3199 tb_gen_code(env
, pc
, cs_base
, cpu_flags
, 1);
3201 cpu_resume_from_signal(env
, NULL
);
3204 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
3209 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
3210 so these check for a hit then pass through to the normal out-of-line
3212 static uint32_t watch_mem_readb(void *opaque
, target_phys_addr_t addr
)
3214 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_READ
);
3215 return ldub_phys(addr
);
3218 static uint32_t watch_mem_readw(void *opaque
, target_phys_addr_t addr
)
3220 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_READ
);
3221 return lduw_phys(addr
);
3224 static uint32_t watch_mem_readl(void *opaque
, target_phys_addr_t addr
)
3226 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_READ
);
3227 return ldl_phys(addr
);
3230 static void watch_mem_writeb(void *opaque
, target_phys_addr_t addr
,
3233 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x0, BP_MEM_WRITE
);
3234 stb_phys(addr
, val
);
3237 static void watch_mem_writew(void *opaque
, target_phys_addr_t addr
,
3240 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x1, BP_MEM_WRITE
);
3241 stw_phys(addr
, val
);
3244 static void watch_mem_writel(void *opaque
, target_phys_addr_t addr
,
3247 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, ~0x3, BP_MEM_WRITE
);
3248 stl_phys(addr
, val
);
3251 static CPUReadMemoryFunc
* const watch_mem_read
[3] = {
3257 static CPUWriteMemoryFunc
* const watch_mem_write
[3] = {
3263 static inline uint32_t subpage_readlen (subpage_t
*mmio
,
3264 target_phys_addr_t addr
,
3267 unsigned int idx
= SUBPAGE_IDX(addr
);
3268 #if defined(DEBUG_SUBPAGE)
3269 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d\n", __func__
,
3270 mmio
, len
, addr
, idx
);
3273 addr
+= mmio
->region_offset
[idx
];
3274 idx
= mmio
->sub_io_index
[idx
];
3275 return io_mem_read
[idx
][len
](io_mem_opaque
[idx
], addr
);
3278 static inline void subpage_writelen (subpage_t
*mmio
, target_phys_addr_t addr
,
3279 uint32_t value
, unsigned int len
)
3281 unsigned int idx
= SUBPAGE_IDX(addr
);
3282 #if defined(DEBUG_SUBPAGE)
3283 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
" idx %d value %08x\n",
3284 __func__
, mmio
, len
, addr
, idx
, value
);
3287 addr
+= mmio
->region_offset
[idx
];
3288 idx
= mmio
->sub_io_index
[idx
];
3289 io_mem_write
[idx
][len
](io_mem_opaque
[idx
], addr
, value
);
3292 static uint32_t subpage_readb (void *opaque
, target_phys_addr_t addr
)
3294 return subpage_readlen(opaque
, addr
, 0);
3297 static void subpage_writeb (void *opaque
, target_phys_addr_t addr
,
3300 subpage_writelen(opaque
, addr
, value
, 0);
3303 static uint32_t subpage_readw (void *opaque
, target_phys_addr_t addr
)
3305 return subpage_readlen(opaque
, addr
, 1);
3308 static void subpage_writew (void *opaque
, target_phys_addr_t addr
,
3311 subpage_writelen(opaque
, addr
, value
, 1);
3314 static uint32_t subpage_readl (void *opaque
, target_phys_addr_t addr
)
3316 return subpage_readlen(opaque
, addr
, 2);
3319 static void subpage_writel (void *opaque
, target_phys_addr_t addr
,
3322 subpage_writelen(opaque
, addr
, value
, 2);
3325 static CPUReadMemoryFunc
* const subpage_read
[] = {
3331 static CPUWriteMemoryFunc
* const subpage_write
[] = {
3337 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
3338 ram_addr_t memory
, ram_addr_t region_offset
)
3342 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
3344 idx
= SUBPAGE_IDX(start
);
3345 eidx
= SUBPAGE_IDX(end
);
3346 #if defined(DEBUG_SUBPAGE)
3347 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__
,
3348 mmio
, start
, end
, idx
, eidx
, memory
);
3350 if ((memory
& ~TARGET_PAGE_MASK
) == IO_MEM_RAM
)
3351 memory
= IO_MEM_UNASSIGNED
;
3352 memory
= (memory
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3353 for (; idx
<= eidx
; idx
++) {
3354 mmio
->sub_io_index
[idx
] = memory
;
3355 mmio
->region_offset
[idx
] = region_offset
;
3361 static subpage_t
*subpage_init (target_phys_addr_t base
, ram_addr_t
*phys
,
3362 ram_addr_t orig_memory
,
3363 ram_addr_t region_offset
)
3368 mmio
= qemu_mallocz(sizeof(subpage_t
));
3371 subpage_memory
= cpu_register_io_memory(subpage_read
, subpage_write
, mmio
,
3372 DEVICE_NATIVE_ENDIAN
);
3373 #if defined(DEBUG_SUBPAGE)
3374 printf("%s: %p base " TARGET_FMT_plx
" len %08x %d\n", __func__
,
3375 mmio
, base
, TARGET_PAGE_SIZE
, subpage_memory
);
3377 *phys
= subpage_memory
| IO_MEM_SUBPAGE
;
3378 subpage_register(mmio
, 0, TARGET_PAGE_SIZE
-1, orig_memory
, region_offset
);
3383 static int get_free_io_mem_idx(void)
3387 for (i
= 0; i
<IO_MEM_NB_ENTRIES
; i
++)
3388 if (!io_mem_used
[i
]) {
3392 fprintf(stderr
, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES
);
3397 * Usually, devices operate in little endian mode. There are devices out
3398 * there that operate in big endian too. Each device gets byte swapped
3399 * mmio if plugged onto a CPU that does the other endianness.
3409 typedef struct SwapEndianContainer
{
3410 CPUReadMemoryFunc
*read
[3];
3411 CPUWriteMemoryFunc
*write
[3];
3413 } SwapEndianContainer
;
3415 static uint32_t swapendian_mem_readb (void *opaque
, target_phys_addr_t addr
)
3418 SwapEndianContainer
*c
= opaque
;
3419 val
= c
->read
[0](c
->opaque
, addr
);
3423 static uint32_t swapendian_mem_readw(void *opaque
, target_phys_addr_t addr
)
3426 SwapEndianContainer
*c
= opaque
;
3427 val
= bswap16(c
->read
[1](c
->opaque
, addr
));
3431 static uint32_t swapendian_mem_readl(void *opaque
, target_phys_addr_t addr
)
3434 SwapEndianContainer
*c
= opaque
;
3435 val
= bswap32(c
->read
[2](c
->opaque
, addr
));
3439 static CPUReadMemoryFunc
* const swapendian_readfn
[3]={
3440 swapendian_mem_readb
,
3441 swapendian_mem_readw
,
3442 swapendian_mem_readl
3445 static void swapendian_mem_writeb(void *opaque
, target_phys_addr_t addr
,
3448 SwapEndianContainer
*c
= opaque
;
3449 c
->write
[0](c
->opaque
, addr
, val
);
3452 static void swapendian_mem_writew(void *opaque
, target_phys_addr_t addr
,
3455 SwapEndianContainer
*c
= opaque
;
3456 c
->write
[1](c
->opaque
, addr
, bswap16(val
));
3459 static void swapendian_mem_writel(void *opaque
, target_phys_addr_t addr
,
3462 SwapEndianContainer
*c
= opaque
;
3463 c
->write
[2](c
->opaque
, addr
, bswap32(val
));
3466 static CPUWriteMemoryFunc
* const swapendian_writefn
[3]={
3467 swapendian_mem_writeb
,
3468 swapendian_mem_writew
,
3469 swapendian_mem_writel
3472 static void swapendian_init(int io_index
)
3474 SwapEndianContainer
*c
= qemu_malloc(sizeof(SwapEndianContainer
));
3477 /* Swap mmio for big endian targets */
3478 c
->opaque
= io_mem_opaque
[io_index
];
3479 for (i
= 0; i
< 3; i
++) {
3480 c
->read
[i
] = io_mem_read
[io_index
][i
];
3481 c
->write
[i
] = io_mem_write
[io_index
][i
];
3483 io_mem_read
[io_index
][i
] = swapendian_readfn
[i
];
3484 io_mem_write
[io_index
][i
] = swapendian_writefn
[i
];
3486 io_mem_opaque
[io_index
] = c
;
3489 static void swapendian_del(int io_index
)
3491 if (io_mem_read
[io_index
][0] == swapendian_readfn
[0]) {
3492 qemu_free(io_mem_opaque
[io_index
]);
3496 /* mem_read and mem_write are arrays of functions containing the
3497 function to access byte (index 0), word (index 1) and dword (index
3498 2). Functions can be omitted with a NULL function pointer.
3499 If io_index is non zero, the corresponding io zone is
3500 modified. If it is zero, a new io zone is allocated. The return
3501 value can be used with cpu_register_physical_memory(). (-1) is
3502 returned if error. */
3503 static int cpu_register_io_memory_fixed(int io_index
,
3504 CPUReadMemoryFunc
* const *mem_read
,
3505 CPUWriteMemoryFunc
* const *mem_write
,
3506 void *opaque
, enum device_endian endian
)
3510 if (io_index
<= 0) {
3511 io_index
= get_free_io_mem_idx();
3515 io_index
>>= IO_MEM_SHIFT
;
3516 if (io_index
>= IO_MEM_NB_ENTRIES
)
3520 for (i
= 0; i
< 3; ++i
) {
3521 io_mem_read
[io_index
][i
]
3522 = (mem_read
[i
] ? mem_read
[i
] : unassigned_mem_read
[i
]);
3524 for (i
= 0; i
< 3; ++i
) {
3525 io_mem_write
[io_index
][i
]
3526 = (mem_write
[i
] ? mem_write
[i
] : unassigned_mem_write
[i
]);
3528 io_mem_opaque
[io_index
] = opaque
;
3531 case DEVICE_BIG_ENDIAN
:
3532 #ifndef TARGET_WORDS_BIGENDIAN
3533 swapendian_init(io_index
);
3536 case DEVICE_LITTLE_ENDIAN
:
3537 #ifdef TARGET_WORDS_BIGENDIAN
3538 swapendian_init(io_index
);
3541 case DEVICE_NATIVE_ENDIAN
:
3546 return (io_index
<< IO_MEM_SHIFT
);
3549 int cpu_register_io_memory(CPUReadMemoryFunc
* const *mem_read
,
3550 CPUWriteMemoryFunc
* const *mem_write
,
3551 void *opaque
, enum device_endian endian
)
3553 return cpu_register_io_memory_fixed(0, mem_read
, mem_write
, opaque
, endian
);
3556 void cpu_unregister_io_memory(int io_table_address
)
3559 int io_index
= io_table_address
>> IO_MEM_SHIFT
;
3561 swapendian_del(io_index
);
3563 for (i
=0;i
< 3; i
++) {
3564 io_mem_read
[io_index
][i
] = unassigned_mem_read
[i
];
3565 io_mem_write
[io_index
][i
] = unassigned_mem_write
[i
];
3567 io_mem_opaque
[io_index
] = NULL
;
3568 io_mem_used
[io_index
] = 0;
3571 static void io_mem_init(void)
3575 cpu_register_io_memory_fixed(IO_MEM_ROM
, error_mem_read
,
3576 unassigned_mem_write
, NULL
,
3577 DEVICE_NATIVE_ENDIAN
);
3578 cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED
, unassigned_mem_read
,
3579 unassigned_mem_write
, NULL
,
3580 DEVICE_NATIVE_ENDIAN
);
3581 cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY
, error_mem_read
,
3582 notdirty_mem_write
, NULL
,
3583 DEVICE_NATIVE_ENDIAN
);
3587 io_mem_watch
= cpu_register_io_memory(watch_mem_read
,
3588 watch_mem_write
, NULL
,
3589 DEVICE_NATIVE_ENDIAN
);
3592 #endif /* !defined(CONFIG_USER_ONLY) */
3594 /* physical memory access (slow version, mainly for debug) */
3595 #if defined(CONFIG_USER_ONLY)
3596 int cpu_memory_rw_debug(CPUState
*env
, target_ulong addr
,
3597 uint8_t *buf
, int len
, int is_write
)
3604 page
= addr
& TARGET_PAGE_MASK
;
3605 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3608 flags
= page_get_flags(page
);
3609 if (!(flags
& PAGE_VALID
))
3612 if (!(flags
& PAGE_WRITE
))
3614 /* XXX: this code should not depend on lock_user */
3615 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
3618 unlock_user(p
, addr
, l
);
3620 if (!(flags
& PAGE_READ
))
3622 /* XXX: this code should not depend on lock_user */
3623 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
3626 unlock_user(p
, addr
, 0);
3636 void cpu_physical_memory_rw(target_phys_addr_t addr
, uint8_t *buf
,
3637 int len
, int is_write
)
3642 target_phys_addr_t page
;
3647 page
= addr
& TARGET_PAGE_MASK
;
3648 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3651 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3653 pd
= IO_MEM_UNASSIGNED
;
3655 pd
= p
->phys_offset
;
3659 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3660 target_phys_addr_t addr1
= addr
;
3661 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3663 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3664 /* XXX: could force cpu_single_env to NULL to avoid
3666 if (l
>= 4 && ((addr1
& 3) == 0)) {
3667 /* 32 bit write access */
3669 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr1
, val
);
3671 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3672 /* 16 bit write access */
3674 io_mem_write
[io_index
][1](io_mem_opaque
[io_index
], addr1
, val
);
3677 /* 8 bit write access */
3679 io_mem_write
[io_index
][0](io_mem_opaque
[io_index
], addr1
, val
);
3683 unsigned long addr1
;
3684 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3686 ptr
= qemu_get_ram_ptr(addr1
);
3687 memcpy(ptr
, buf
, l
);
3688 if (!cpu_physical_memory_is_dirty(addr1
)) {
3689 /* invalidate code */
3690 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3692 cpu_physical_memory_set_dirty_flags(
3693 addr1
, (0xff & ~CODE_DIRTY_FLAG
));
3695 /* qemu doesn't execute guest code directly, but kvm does
3696 therefore flush instruction caches */
3698 flush_icache_range((unsigned long)ptr
,
3699 ((unsigned long)ptr
)+l
);
3702 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3703 !(pd
& IO_MEM_ROMD
)) {
3704 target_phys_addr_t addr1
= addr
;
3706 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3708 addr1
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3709 if (l
>= 4 && ((addr1
& 3) == 0)) {
3710 /* 32 bit read access */
3711 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr1
);
3714 } else if (l
>= 2 && ((addr1
& 1) == 0)) {
3715 /* 16 bit read access */
3716 val
= io_mem_read
[io_index
][1](io_mem_opaque
[io_index
], addr1
);
3720 /* 8 bit read access */
3721 val
= io_mem_read
[io_index
][0](io_mem_opaque
[io_index
], addr1
);
3727 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3728 (addr
& ~TARGET_PAGE_MASK
);
3729 memcpy(buf
, ptr
, l
);
3738 /* used for ROM loading : can write in RAM and ROM */
3739 void cpu_physical_memory_write_rom(target_phys_addr_t addr
,
3740 const uint8_t *buf
, int len
)
3744 target_phys_addr_t page
;
3749 page
= addr
& TARGET_PAGE_MASK
;
3750 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3753 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3755 pd
= IO_MEM_UNASSIGNED
;
3757 pd
= p
->phys_offset
;
3760 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
&&
3761 (pd
& ~TARGET_PAGE_MASK
) != IO_MEM_ROM
&&
3762 !(pd
& IO_MEM_ROMD
)) {
3765 unsigned long addr1
;
3766 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3768 ptr
= qemu_get_ram_ptr(addr1
);
3769 memcpy(ptr
, buf
, l
);
3779 target_phys_addr_t addr
;
3780 target_phys_addr_t len
;
3783 static BounceBuffer bounce
;
3785 typedef struct MapClient
{
3787 void (*callback
)(void *opaque
);
3788 QLIST_ENTRY(MapClient
) link
;
3791 static QLIST_HEAD(map_client_list
, MapClient
) map_client_list
3792 = QLIST_HEAD_INITIALIZER(map_client_list
);
3794 void *cpu_register_map_client(void *opaque
, void (*callback
)(void *opaque
))
3796 MapClient
*client
= qemu_malloc(sizeof(*client
));
3798 client
->opaque
= opaque
;
3799 client
->callback
= callback
;
3800 QLIST_INSERT_HEAD(&map_client_list
, client
, link
);
3804 void cpu_unregister_map_client(void *_client
)
3806 MapClient
*client
= (MapClient
*)_client
;
3808 QLIST_REMOVE(client
, link
);
3812 static void cpu_notify_map_clients(void)
3816 while (!QLIST_EMPTY(&map_client_list
)) {
3817 client
= QLIST_FIRST(&map_client_list
);
3818 client
->callback(client
->opaque
);
3819 cpu_unregister_map_client(client
);
3823 /* Map a physical memory region into a host virtual address.
3824 * May map a subset of the requested range, given by and returned in *plen.
3825 * May return NULL if resources needed to perform the mapping are exhausted.
3826 * Use only for reads OR writes - not for read-modify-write operations.
3827 * Use cpu_register_map_client() to know when retrying the map operation is
3828 * likely to succeed.
3830 void *cpu_physical_memory_map(target_phys_addr_t addr
,
3831 target_phys_addr_t
*plen
,
3834 target_phys_addr_t len
= *plen
;
3835 target_phys_addr_t done
= 0;
3837 uint8_t *ret
= NULL
;
3839 target_phys_addr_t page
;
3842 unsigned long addr1
;
3845 page
= addr
& TARGET_PAGE_MASK
;
3846 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3849 p
= phys_page_find(page
>> TARGET_PAGE_BITS
);
3851 pd
= IO_MEM_UNASSIGNED
;
3853 pd
= p
->phys_offset
;
3856 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
3857 if (done
|| bounce
.buffer
) {
3860 bounce
.buffer
= qemu_memalign(TARGET_PAGE_SIZE
, TARGET_PAGE_SIZE
);
3864 cpu_physical_memory_rw(addr
, bounce
.buffer
, l
, 0);
3866 ptr
= bounce
.buffer
;
3868 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
3869 ptr
= qemu_get_ram_ptr(addr1
);
3873 } else if (ret
+ done
!= ptr
) {
3885 /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
3886 * Will also mark the memory as dirty if is_write == 1. access_len gives
3887 * the amount of memory that was actually read or written by the caller.
3889 void cpu_physical_memory_unmap(void *buffer
, target_phys_addr_t len
,
3890 int is_write
, target_phys_addr_t access_len
)
3892 unsigned long flush_len
= (unsigned long)access_len
;
3894 if (buffer
!= bounce
.buffer
) {
3896 ram_addr_t addr1
= qemu_ram_addr_from_host_nofail(buffer
);
3897 while (access_len
) {
3899 l
= TARGET_PAGE_SIZE
;
3902 if (!cpu_physical_memory_is_dirty(addr1
)) {
3903 /* invalidate code */
3904 tb_invalidate_phys_page_range(addr1
, addr1
+ l
, 0);
3906 cpu_physical_memory_set_dirty_flags(
3907 addr1
, (0xff & ~CODE_DIRTY_FLAG
));
3912 dma_flush_range((unsigned long)buffer
,
3913 (unsigned long)buffer
+ flush_len
);
3918 cpu_physical_memory_write(bounce
.addr
, bounce
.buffer
, access_len
);
3920 qemu_vfree(bounce
.buffer
);
3921 bounce
.buffer
= NULL
;
3922 cpu_notify_map_clients();
3925 /* warning: addr must be aligned */
3926 uint32_t ldl_phys(target_phys_addr_t addr
)
3934 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3936 pd
= IO_MEM_UNASSIGNED
;
3938 pd
= p
->phys_offset
;
3941 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3942 !(pd
& IO_MEM_ROMD
)) {
3944 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3946 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3947 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3950 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3951 (addr
& ~TARGET_PAGE_MASK
);
3957 /* warning: addr must be aligned */
3958 uint64_t ldq_phys(target_phys_addr_t addr
)
3966 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
3968 pd
= IO_MEM_UNASSIGNED
;
3970 pd
= p
->phys_offset
;
3973 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
3974 !(pd
& IO_MEM_ROMD
)) {
3976 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
3978 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
3979 #ifdef TARGET_WORDS_BIGENDIAN
3980 val
= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
) << 32;
3981 val
|= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4);
3983 val
= io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
);
3984 val
|= (uint64_t)io_mem_read
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4) << 32;
3988 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
3989 (addr
& ~TARGET_PAGE_MASK
);
3996 uint32_t ldub_phys(target_phys_addr_t addr
)
3999 cpu_physical_memory_read(addr
, &val
, 1);
4003 /* warning: addr must be aligned */
4004 uint32_t lduw_phys(target_phys_addr_t addr
)
4012 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
4014 pd
= IO_MEM_UNASSIGNED
;
4016 pd
= p
->phys_offset
;
4019 if ((pd
& ~TARGET_PAGE_MASK
) > IO_MEM_ROM
&&
4020 !(pd
& IO_MEM_ROMD
)) {
4022 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
4024 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
4025 val
= io_mem_read
[io_index
][1](io_mem_opaque
[io_index
], addr
);
4028 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
4029 (addr
& ~TARGET_PAGE_MASK
);
4035 /* warning: addr must be aligned. The ram page is not masked as dirty
4036 and the code inside is not invalidated. It is useful if the dirty
4037 bits are used to track modified PTEs */
4038 void stl_phys_notdirty(target_phys_addr_t addr
, uint32_t val
)
4045 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
4047 pd
= IO_MEM_UNASSIGNED
;
4049 pd
= p
->phys_offset
;
4052 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
4053 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
4055 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
4056 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
4058 unsigned long addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
4059 ptr
= qemu_get_ram_ptr(addr1
);
4062 if (unlikely(in_migration
)) {
4063 if (!cpu_physical_memory_is_dirty(addr1
)) {
4064 /* invalidate code */
4065 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
4067 cpu_physical_memory_set_dirty_flags(
4068 addr1
, (0xff & ~CODE_DIRTY_FLAG
));
4074 void stq_phys_notdirty(target_phys_addr_t addr
, uint64_t val
)
4081 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
4083 pd
= IO_MEM_UNASSIGNED
;
4085 pd
= p
->phys_offset
;
4088 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
4089 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
4091 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
4092 #ifdef TARGET_WORDS_BIGENDIAN
4093 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
>> 32);
4094 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
);
4096 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
4097 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
+ 4, val
>> 32);
4100 ptr
= qemu_get_ram_ptr(pd
& TARGET_PAGE_MASK
) +
4101 (addr
& ~TARGET_PAGE_MASK
);
4106 /* warning: addr must be aligned */
4107 void stl_phys(target_phys_addr_t addr
, uint32_t val
)
4114 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
4116 pd
= IO_MEM_UNASSIGNED
;
4118 pd
= p
->phys_offset
;
4121 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
4122 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
4124 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
4125 io_mem_write
[io_index
][2](io_mem_opaque
[io_index
], addr
, val
);
4127 unsigned long addr1
;
4128 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
4130 ptr
= qemu_get_ram_ptr(addr1
);
4132 if (!cpu_physical_memory_is_dirty(addr1
)) {
4133 /* invalidate code */
4134 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
4136 cpu_physical_memory_set_dirty_flags(addr1
,
4137 (0xff & ~CODE_DIRTY_FLAG
));
4143 void stb_phys(target_phys_addr_t addr
, uint32_t val
)
4146 cpu_physical_memory_write(addr
, &v
, 1);
4149 /* warning: addr must be aligned */
4150 void stw_phys(target_phys_addr_t addr
, uint32_t val
)
4157 p
= phys_page_find(addr
>> TARGET_PAGE_BITS
);
4159 pd
= IO_MEM_UNASSIGNED
;
4161 pd
= p
->phys_offset
;
4164 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
) {
4165 io_index
= (pd
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
4167 addr
= (addr
& ~TARGET_PAGE_MASK
) + p
->region_offset
;
4168 io_mem_write
[io_index
][1](io_mem_opaque
[io_index
], addr
, val
);
4170 unsigned long addr1
;
4171 addr1
= (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
4173 ptr
= qemu_get_ram_ptr(addr1
);
4175 if (!cpu_physical_memory_is_dirty(addr1
)) {
4176 /* invalidate code */
4177 tb_invalidate_phys_page_range(addr1
, addr1
+ 2, 0);
4179 cpu_physical_memory_set_dirty_flags(addr1
,
4180 (0xff & ~CODE_DIRTY_FLAG
));
4186 void stq_phys(target_phys_addr_t addr
, uint64_t val
)
4189 cpu_physical_memory_write(addr
, (const uint8_t *)&val
, 8);
4192 /* virtual memory access for debug (includes writing to ROM) */
4193 int cpu_memory_rw_debug(CPUState
*env
, target_ulong addr
,
4194 uint8_t *buf
, int len
, int is_write
)
4197 target_phys_addr_t phys_addr
;
4201 page
= addr
& TARGET_PAGE_MASK
;
4202 phys_addr
= cpu_get_phys_page_debug(env
, page
);
4203 /* if no physical page mapped, return an error */
4204 if (phys_addr
== -1)
4206 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
4209 phys_addr
+= (addr
& ~TARGET_PAGE_MASK
);
4211 cpu_physical_memory_write_rom(phys_addr
, buf
, l
);
4213 cpu_physical_memory_rw(phys_addr
, buf
, l
, is_write
);
4222 /* in deterministic execution mode, instructions doing device I/Os
4223 must be at the end of the TB */
4224 void cpu_io_recompile(CPUState
*env
, void *retaddr
)
4226 TranslationBlock
*tb
;
4228 target_ulong pc
, cs_base
;
4231 tb
= tb_find_pc((unsigned long)retaddr
);
4233 cpu_abort(env
, "cpu_io_recompile: could not find TB for pc=%p",
4236 n
= env
->icount_decr
.u16
.low
+ tb
->icount
;
4237 cpu_restore_state(tb
, env
, (unsigned long)retaddr
, NULL
);
4238 /* Calculate how many instructions had been executed before the fault
4240 n
= n
- env
->icount_decr
.u16
.low
;
4241 /* Generate a new TB ending on the I/O insn. */
4243 /* On MIPS and SH, delay slot instructions can only be restarted if
4244 they were already the first instruction in the TB. If this is not
4245 the first instruction in a TB then re-execute the preceding
4247 #if defined(TARGET_MIPS)
4248 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0 && n
> 1) {
4249 env
->active_tc
.PC
-= 4;
4250 env
->icount_decr
.u16
.low
++;
4251 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
4253 #elif defined(TARGET_SH4)
4254 if ((env
->flags
& ((DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
))) != 0
4257 env
->icount_decr
.u16
.low
++;
4258 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
);
4261 /* This should never happen. */
4262 if (n
> CF_COUNT_MASK
)
4263 cpu_abort(env
, "TB too big during recompile");
4265 cflags
= n
| CF_LAST_IO
;
4267 cs_base
= tb
->cs_base
;
4269 tb_phys_invalidate(tb
, -1);
4270 /* FIXME: In theory this could raise an exception. In practice
4271 we have already translated the block once so it's probably ok. */
4272 tb_gen_code(env
, pc
, cs_base
, flags
, cflags
);
4273 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
4274 the first in the TB) then we end up generating a whole new TB and
4275 repeating the fault, which is horribly inefficient.
4276 Better would be to execute just this insn uncached, or generate a
4278 cpu_resume_from_signal(env
, NULL
);
4281 #if !defined(CONFIG_USER_ONLY)
4283 void dump_exec_info(FILE *f
, fprintf_function cpu_fprintf
)
4285 int i
, target_code_size
, max_target_code_size
;
4286 int direct_jmp_count
, direct_jmp2_count
, cross_page
;
4287 TranslationBlock
*tb
;
4289 target_code_size
= 0;
4290 max_target_code_size
= 0;
4292 direct_jmp_count
= 0;
4293 direct_jmp2_count
= 0;
4294 for(i
= 0; i
< nb_tbs
; i
++) {
4296 target_code_size
+= tb
->size
;
4297 if (tb
->size
> max_target_code_size
)
4298 max_target_code_size
= tb
->size
;
4299 if (tb
->page_addr
[1] != -1)
4301 if (tb
->tb_next_offset
[0] != 0xffff) {
4303 if (tb
->tb_next_offset
[1] != 0xffff) {
4304 direct_jmp2_count
++;
4308 /* XXX: avoid using doubles ? */
4309 cpu_fprintf(f
, "Translation buffer state:\n");
4310 cpu_fprintf(f
, "gen code size %td/%ld\n",
4311 code_gen_ptr
- code_gen_buffer
, code_gen_buffer_max_size
);
4312 cpu_fprintf(f
, "TB count %d/%d\n",
4313 nb_tbs
, code_gen_max_blocks
);
4314 cpu_fprintf(f
, "TB avg target size %d max=%d bytes\n",
4315 nb_tbs
? target_code_size
/ nb_tbs
: 0,
4316 max_target_code_size
);
4317 cpu_fprintf(f
, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
4318 nb_tbs
? (code_gen_ptr
- code_gen_buffer
) / nb_tbs
: 0,
4319 target_code_size
? (double) (code_gen_ptr
- code_gen_buffer
) / target_code_size
: 0);
4320 cpu_fprintf(f
, "cross page TB count %d (%d%%)\n",
4322 nb_tbs
? (cross_page
* 100) / nb_tbs
: 0);
4323 cpu_fprintf(f
, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
4325 nb_tbs
? (direct_jmp_count
* 100) / nb_tbs
: 0,
4327 nb_tbs
? (direct_jmp2_count
* 100) / nb_tbs
: 0);
4328 cpu_fprintf(f
, "\nStatistics:\n");
4329 cpu_fprintf(f
, "TB flush count %d\n", tb_flush_count
);
4330 cpu_fprintf(f
, "TB invalidate count %d\n", tb_phys_invalidate_count
);
4331 cpu_fprintf(f
, "TLB flush count %d\n", tlb_flush_count
);
4332 #ifdef CONFIG_PROFILER
4333 tcg_dump_info(f
, cpu_fprintf
);
4337 #define MMUSUFFIX _cmmu
4338 #define GETPC() NULL
4339 #define env cpu_single_env
4340 #define SOFTMMU_CODE_ACCESS
4343 #include "softmmu_template.h"
4346 #include "softmmu_template.h"
4349 #include "softmmu_template.h"
4352 #include "softmmu_template.h"