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/>.
22 #include <sys/types.h>
33 #include "qemu-common.h"
34 #define NO_CPU_IO_DEFS
37 #include "disas/disas.h"
39 #if defined(CONFIG_USER_ONLY)
41 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
42 #include <sys/param.h>
43 #if __FreeBSD_version >= 700104
44 #define HAVE_KINFO_GETVMMAP
45 #define sigqueue sigqueue_freebsd /* avoid redefinition */
48 #include <machine/profile.h>
57 #include "exec/address-spaces.h"
60 #include "exec/cputlb.h"
61 #include "translate-all.h"
62 #include "qemu/timer.h"
64 //#define DEBUG_TB_INVALIDATE
66 /* make various TB consistency checks */
67 //#define DEBUG_TB_CHECK
69 #if !defined(CONFIG_USER_ONLY)
70 /* TB consistency checks only implemented for usermode emulation. */
74 #define SMC_BITMAP_USE_THRESHOLD 10
76 typedef struct PageDesc
{
77 /* list of TBs intersecting this ram page */
78 TranslationBlock
*first_tb
;
79 /* in order to optimize self modifying code, we count the number
80 of lookups we do to a given page to use a bitmap */
81 unsigned int code_write_count
;
83 #if defined(CONFIG_USER_ONLY)
88 /* In system mode we want L1_MAP to be based on ram offsets,
89 while in user mode we want it to be based on virtual addresses. */
90 #if !defined(CONFIG_USER_ONLY)
91 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
92 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
94 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
97 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
100 /* Size of the L2 (and L3, etc) page tables. */
102 #define V_L2_SIZE (1 << V_L2_BITS)
104 /* The bits remaining after N lower levels of page tables. */
105 #define V_L1_BITS_REM \
106 ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS)
108 #if V_L1_BITS_REM < 4
109 #define V_L1_BITS (V_L1_BITS_REM + V_L2_BITS)
111 #define V_L1_BITS V_L1_BITS_REM
114 #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
116 #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
118 uintptr_t qemu_real_host_page_size
;
119 uintptr_t qemu_host_page_size
;
120 uintptr_t qemu_host_page_mask
;
122 /* This is a multi-level map on the virtual address space.
123 The bottom level has pointers to PageDesc. */
124 static void *l1_map
[V_L1_SIZE
];
126 /* code generation context */
129 static void tb_link_page(TranslationBlock
*tb
, tb_page_addr_t phys_pc
,
130 tb_page_addr_t phys_page2
);
131 static TranslationBlock
*tb_find_pc(uintptr_t tc_ptr
);
133 void cpu_gen_init(void)
135 tcg_context_init(&tcg_ctx
);
138 /* return non zero if the very first instruction is invalid so that
139 the virtual CPU can trigger an exception.
141 '*gen_code_size_ptr' contains the size of the generated code (host
144 int cpu_gen_code(CPUArchState
*env
, TranslationBlock
*tb
, int *gen_code_size_ptr
)
146 TCGContext
*s
= &tcg_ctx
;
147 tcg_insn_unit
*gen_code_buf
;
149 #ifdef CONFIG_PROFILER
153 #ifdef CONFIG_PROFILER
154 s
->tb_count1
++; /* includes aborted translations because of
156 ti
= profile_getclock();
160 gen_intermediate_code(env
, tb
);
162 trace_translate_block(tb
, tb
->pc
, tb
->tc_ptr
);
164 /* generate machine code */
165 gen_code_buf
= tb
->tc_ptr
;
166 tb
->tb_next_offset
[0] = 0xffff;
167 tb
->tb_next_offset
[1] = 0xffff;
168 s
->tb_next_offset
= tb
->tb_next_offset
;
169 #ifdef USE_DIRECT_JUMP
170 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
173 s
->tb_jmp_offset
= NULL
;
174 s
->tb_next
= tb
->tb_next
;
177 #ifdef CONFIG_PROFILER
179 s
->interm_time
+= profile_getclock() - ti
;
180 s
->code_time
-= profile_getclock();
182 gen_code_size
= tcg_gen_code(s
, gen_code_buf
);
183 *gen_code_size_ptr
= gen_code_size
;
184 #ifdef CONFIG_PROFILER
185 s
->code_time
+= profile_getclock();
186 s
->code_in_len
+= tb
->size
;
187 s
->code_out_len
+= gen_code_size
;
191 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
192 qemu_log("OUT: [size=%d]\n", gen_code_size
);
193 log_disas(tb
->tc_ptr
, gen_code_size
);
201 /* The cpu state corresponding to 'searched_pc' is restored.
203 static int cpu_restore_state_from_tb(CPUState
*cpu
, TranslationBlock
*tb
,
204 uintptr_t searched_pc
)
206 CPUArchState
*env
= cpu
->env_ptr
;
207 TCGContext
*s
= &tcg_ctx
;
210 #ifdef CONFIG_PROFILER
214 #ifdef CONFIG_PROFILER
215 ti
= profile_getclock();
219 gen_intermediate_code_pc(env
, tb
);
221 if (tb
->cflags
& CF_USE_ICOUNT
) {
222 /* Reset the cycle counter to the start of the block. */
223 cpu
->icount_decr
.u16
.low
+= tb
->icount
;
224 /* Clear the IO flag. */
228 /* find opc index corresponding to search_pc */
229 tc_ptr
= (uintptr_t)tb
->tc_ptr
;
230 if (searched_pc
< tc_ptr
)
233 s
->tb_next_offset
= tb
->tb_next_offset
;
234 #ifdef USE_DIRECT_JUMP
235 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
238 s
->tb_jmp_offset
= NULL
;
239 s
->tb_next
= tb
->tb_next
;
241 j
= tcg_gen_code_search_pc(s
, (tcg_insn_unit
*)tc_ptr
,
242 searched_pc
- tc_ptr
);
245 /* now find start of instruction before */
246 while (s
->gen_opc_instr_start
[j
] == 0) {
249 cpu
->icount_decr
.u16
.low
-= s
->gen_opc_icount
[j
];
251 restore_state_to_opc(env
, tb
, j
);
253 #ifdef CONFIG_PROFILER
254 s
->restore_time
+= profile_getclock() - ti
;
260 bool cpu_restore_state(CPUState
*cpu
, uintptr_t retaddr
)
262 TranslationBlock
*tb
;
264 tb
= tb_find_pc(retaddr
);
266 cpu_restore_state_from_tb(cpu
, tb
, retaddr
);
267 if (tb
->cflags
& CF_NOCACHE
) {
268 /* one-shot translation, invalidate it immediately */
269 cpu
->current_tb
= NULL
;
270 tb_phys_invalidate(tb
, -1);
279 static __attribute__((unused
)) void map_exec(void *addr
, long size
)
282 VirtualProtect(addr
, size
,
283 PAGE_EXECUTE_READWRITE
, &old_protect
);
286 static __attribute__((unused
)) void map_exec(void *addr
, long size
)
288 unsigned long start
, end
, page_size
;
290 page_size
= getpagesize();
291 start
= (unsigned long)addr
;
292 start
&= ~(page_size
- 1);
294 end
= (unsigned long)addr
+ size
;
295 end
+= page_size
- 1;
296 end
&= ~(page_size
- 1);
298 mprotect((void *)start
, end
- start
,
299 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
303 void page_size_init(void)
305 /* NOTE: we can always suppose that qemu_host_page_size >=
307 qemu_real_host_page_size
= getpagesize();
308 if (qemu_host_page_size
== 0) {
309 qemu_host_page_size
= qemu_real_host_page_size
;
311 if (qemu_host_page_size
< TARGET_PAGE_SIZE
) {
312 qemu_host_page_size
= TARGET_PAGE_SIZE
;
314 qemu_host_page_mask
= ~(qemu_host_page_size
- 1);
317 static void page_init(void)
320 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
322 #ifdef HAVE_KINFO_GETVMMAP
323 struct kinfo_vmentry
*freep
;
326 freep
= kinfo_getvmmap(getpid(), &cnt
);
329 for (i
= 0; i
< cnt
; i
++) {
330 unsigned long startaddr
, endaddr
;
332 startaddr
= freep
[i
].kve_start
;
333 endaddr
= freep
[i
].kve_end
;
334 if (h2g_valid(startaddr
)) {
335 startaddr
= h2g(startaddr
) & TARGET_PAGE_MASK
;
337 if (h2g_valid(endaddr
)) {
338 endaddr
= h2g(endaddr
);
339 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
341 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
343 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
354 last_brk
= (unsigned long)sbrk(0);
356 f
= fopen("/compat/linux/proc/self/maps", "r");
361 unsigned long startaddr
, endaddr
;
364 n
= fscanf(f
, "%lx-%lx %*[^\n]\n", &startaddr
, &endaddr
);
366 if (n
== 2 && h2g_valid(startaddr
)) {
367 startaddr
= h2g(startaddr
) & TARGET_PAGE_MASK
;
369 if (h2g_valid(endaddr
)) {
370 endaddr
= h2g(endaddr
);
374 page_set_flags(startaddr
, endaddr
, PAGE_RESERVED
);
386 static PageDesc
*page_find_alloc(tb_page_addr_t index
, int alloc
)
392 /* Level 1. Always allocated. */
393 lp
= l1_map
+ ((index
>> V_L1_SHIFT
) & (V_L1_SIZE
- 1));
396 for (i
= V_L1_SHIFT
/ V_L2_BITS
- 1; i
> 0; i
--) {
403 p
= g_new0(void *, V_L2_SIZE
);
407 lp
= p
+ ((index
>> (i
* V_L2_BITS
)) & (V_L2_SIZE
- 1));
415 pd
= g_new0(PageDesc
, V_L2_SIZE
);
419 return pd
+ (index
& (V_L2_SIZE
- 1));
422 static inline PageDesc
*page_find(tb_page_addr_t index
)
424 return page_find_alloc(index
, 0);
427 #if !defined(CONFIG_USER_ONLY)
428 #define mmap_lock() do { } while (0)
429 #define mmap_unlock() do { } while (0)
432 #if defined(CONFIG_USER_ONLY)
433 /* Currently it is not recommended to allocate big chunks of data in
434 user mode. It will change when a dedicated libc will be used. */
435 /* ??? 64-bit hosts ought to have no problem mmaping data outside the
436 region in which the guest needs to run. Revisit this. */
437 #define USE_STATIC_CODE_GEN_BUFFER
440 /* ??? Should configure for this, not list operating systems here. */
441 #if (defined(__linux__) \
442 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
443 || defined(__DragonFly__) || defined(__OpenBSD__) \
444 || defined(__NetBSD__))
448 /* Minimum size of the code gen buffer. This number is randomly chosen,
449 but not so small that we can't have a fair number of TB's live. */
450 #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024)
452 /* Maximum size of the code gen buffer we'd like to use. Unless otherwise
453 indicated, this is constrained by the range of direct branches on the
454 host cpu, as used by the TCG implementation of goto_tb. */
455 #if defined(__x86_64__)
456 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
457 #elif defined(__sparc__)
458 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
459 #elif defined(__aarch64__)
460 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
461 #elif defined(__arm__)
462 # define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024)
463 #elif defined(__s390x__)
464 /* We have a +- 4GB range on the branches; leave some slop. */
465 # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
466 #elif defined(__mips__)
467 /* We have a 256MB branch region, but leave room to make sure the
468 main executable is also within that region. */
469 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
471 # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
474 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024)
476 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
477 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
478 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
480 static inline size_t size_code_gen_buffer(size_t tb_size
)
482 /* Size the buffer. */
484 #ifdef USE_STATIC_CODE_GEN_BUFFER
485 tb_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
487 /* ??? Needs adjustments. */
488 /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
489 static buffer, we could size this on RESERVED_VA, on the text
490 segment size of the executable, or continue to use the default. */
491 tb_size
= (unsigned long)(ram_size
/ 4);
494 if (tb_size
< MIN_CODE_GEN_BUFFER_SIZE
) {
495 tb_size
= MIN_CODE_GEN_BUFFER_SIZE
;
497 if (tb_size
> MAX_CODE_GEN_BUFFER_SIZE
) {
498 tb_size
= MAX_CODE_GEN_BUFFER_SIZE
;
500 tcg_ctx
.code_gen_buffer_size
= tb_size
;
505 /* In order to use J and JAL within the code_gen_buffer, we require
506 that the buffer not cross a 256MB boundary. */
507 static inline bool cross_256mb(void *addr
, size_t size
)
509 return ((uintptr_t)addr
^ ((uintptr_t)addr
+ size
)) & 0xf0000000;
512 /* We weren't able to allocate a buffer without crossing that boundary,
513 so make do with the larger portion of the buffer that doesn't cross.
514 Returns the new base of the buffer, and adjusts code_gen_buffer_size. */
515 static inline void *split_cross_256mb(void *buf1
, size_t size1
)
517 void *buf2
= (void *)(((uintptr_t)buf1
+ size1
) & 0xf0000000);
518 size_t size2
= buf1
+ size1
- buf2
;
526 tcg_ctx
.code_gen_buffer_size
= size1
;
531 #ifdef USE_STATIC_CODE_GEN_BUFFER
532 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
]
533 __attribute__((aligned(CODE_GEN_ALIGN
)));
535 static inline void *alloc_code_gen_buffer(void)
537 void *buf
= static_code_gen_buffer
;
539 if (cross_256mb(buf
, tcg_ctx
.code_gen_buffer_size
)) {
540 buf
= split_cross_256mb(buf
, tcg_ctx
.code_gen_buffer_size
);
543 map_exec(buf
, tcg_ctx
.code_gen_buffer_size
);
546 #elif defined(USE_MMAP)
547 static inline void *alloc_code_gen_buffer(void)
549 int flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
553 /* Constrain the position of the buffer based on the host cpu.
554 Note that these addresses are chosen in concert with the
555 addresses assigned in the relevant linker script file. */
556 # if defined(__PIE__) || defined(__PIC__)
557 /* Don't bother setting a preferred location if we're building
558 a position-independent executable. We're more likely to get
559 an address near the main executable if we let the kernel
560 choose the address. */
561 # elif defined(__x86_64__) && defined(MAP_32BIT)
562 /* Force the memory down into low memory with the executable.
563 Leave the choice of exact location with the kernel. */
565 /* Cannot expect to map more than 800MB in low memory. */
566 if (tcg_ctx
.code_gen_buffer_size
> 800u * 1024 * 1024) {
567 tcg_ctx
.code_gen_buffer_size
= 800u * 1024 * 1024;
569 # elif defined(__sparc__)
570 start
= 0x40000000ul
;
571 # elif defined(__s390x__)
572 start
= 0x90000000ul
;
573 # elif defined(__mips__)
574 /* ??? We ought to more explicitly manage layout for softmmu too. */
575 # ifdef CONFIG_USER_ONLY
576 start
= 0x68000000ul
;
577 # elif _MIPS_SIM == _ABI64
578 start
= 0x128000000ul
;
580 start
= 0x08000000ul
;
584 buf
= mmap((void *)start
, tcg_ctx
.code_gen_buffer_size
,
585 PROT_WRITE
| PROT_READ
| PROT_EXEC
, flags
, -1, 0);
586 if (buf
== MAP_FAILED
) {
591 if (cross_256mb(buf
, tcg_ctx
.code_gen_buffer_size
)) {
592 /* Try again, with the original still mapped, to avoid re-acquiring
593 that 256mb crossing. This time don't specify an address. */
594 size_t size2
, size1
= tcg_ctx
.code_gen_buffer_size
;
595 void *buf2
= mmap(NULL
, size1
, PROT_WRITE
| PROT_READ
| PROT_EXEC
,
597 if (buf2
!= MAP_FAILED
) {
598 if (!cross_256mb(buf2
, size1
)) {
599 /* Success! Use the new buffer. */
603 /* Failure. Work with what we had. */
607 /* Split the original buffer. Free the smaller half. */
608 buf2
= split_cross_256mb(buf
, size1
);
609 size2
= tcg_ctx
.code_gen_buffer_size
;
610 munmap(buf
+ (buf
== buf2
? size2
: 0), size1
- size2
);
618 static inline void *alloc_code_gen_buffer(void)
620 void *buf
= g_try_malloc(tcg_ctx
.code_gen_buffer_size
);
627 if (cross_256mb(buf
, tcg_ctx
.code_gen_buffer_size
)) {
628 void *buf2
= g_malloc(tcg_ctx
.code_gen_buffer_size
);
629 if (buf2
!= NULL
&& !cross_256mb(buf2
, size1
)) {
630 /* Success! Use the new buffer. */
634 /* Failure. Work with what we had. Since this is malloc
635 and not mmap, we can't free the other half. */
637 buf
= split_cross_256mb(buf
, tcg_ctx
.code_gen_buffer_size
);
642 map_exec(buf
, tcg_ctx
.code_gen_buffer_size
);
645 #endif /* USE_STATIC_CODE_GEN_BUFFER, USE_MMAP */
647 static inline void code_gen_alloc(size_t tb_size
)
649 tcg_ctx
.code_gen_buffer_size
= size_code_gen_buffer(tb_size
);
650 tcg_ctx
.code_gen_buffer
= alloc_code_gen_buffer();
651 if (tcg_ctx
.code_gen_buffer
== NULL
) {
652 fprintf(stderr
, "Could not allocate dynamic translator buffer\n");
656 qemu_madvise(tcg_ctx
.code_gen_buffer
, tcg_ctx
.code_gen_buffer_size
,
659 /* Steal room for the prologue at the end of the buffer. This ensures
660 (via the MAX_CODE_GEN_BUFFER_SIZE limits above) that direct branches
661 from TB's to the prologue are going to be in range. It also means
662 that we don't need to mark (additional) portions of the data segment
664 tcg_ctx
.code_gen_prologue
= tcg_ctx
.code_gen_buffer
+
665 tcg_ctx
.code_gen_buffer_size
- 1024;
666 tcg_ctx
.code_gen_buffer_size
-= 1024;
668 tcg_ctx
.code_gen_buffer_max_size
= tcg_ctx
.code_gen_buffer_size
-
669 (TCG_MAX_OP_SIZE
* OPC_BUF_SIZE
);
670 tcg_ctx
.code_gen_max_blocks
= tcg_ctx
.code_gen_buffer_size
/
671 CODE_GEN_AVG_BLOCK_SIZE
;
673 g_malloc(tcg_ctx
.code_gen_max_blocks
* sizeof(TranslationBlock
));
676 /* Must be called before using the QEMU cpus. 'tb_size' is the size
677 (in bytes) allocated to the translation buffer. Zero means default
679 void tcg_exec_init(unsigned long tb_size
)
682 code_gen_alloc(tb_size
);
683 tcg_ctx
.code_gen_ptr
= tcg_ctx
.code_gen_buffer
;
684 tcg_register_jit(tcg_ctx
.code_gen_buffer
, tcg_ctx
.code_gen_buffer_size
);
686 #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
687 /* There's no guest base to take into account, so go ahead and
688 initialize the prologue now. */
689 tcg_prologue_init(&tcg_ctx
);
693 bool tcg_enabled(void)
695 return tcg_ctx
.code_gen_buffer
!= NULL
;
698 /* Allocate a new translation block. Flush the translation buffer if
699 too many translation blocks or too much generated code. */
700 static TranslationBlock
*tb_alloc(target_ulong pc
)
702 TranslationBlock
*tb
;
704 if (tcg_ctx
.tb_ctx
.nb_tbs
>= tcg_ctx
.code_gen_max_blocks
||
705 (tcg_ctx
.code_gen_ptr
- tcg_ctx
.code_gen_buffer
) >=
706 tcg_ctx
.code_gen_buffer_max_size
) {
709 tb
= &tcg_ctx
.tb_ctx
.tbs
[tcg_ctx
.tb_ctx
.nb_tbs
++];
715 void tb_free(TranslationBlock
*tb
)
717 /* In practice this is mostly used for single use temporary TB
718 Ignore the hard cases and just back up if this TB happens to
719 be the last one generated. */
720 if (tcg_ctx
.tb_ctx
.nb_tbs
> 0 &&
721 tb
== &tcg_ctx
.tb_ctx
.tbs
[tcg_ctx
.tb_ctx
.nb_tbs
- 1]) {
722 tcg_ctx
.code_gen_ptr
= tb
->tc_ptr
;
723 tcg_ctx
.tb_ctx
.nb_tbs
--;
727 static inline void invalidate_page_bitmap(PageDesc
*p
)
729 if (p
->code_bitmap
) {
730 g_free(p
->code_bitmap
);
731 p
->code_bitmap
= NULL
;
733 p
->code_write_count
= 0;
736 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
737 static void page_flush_tb_1(int level
, void **lp
)
747 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
748 pd
[i
].first_tb
= NULL
;
749 invalidate_page_bitmap(pd
+ i
);
754 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
755 page_flush_tb_1(level
- 1, pp
+ i
);
760 static void page_flush_tb(void)
764 for (i
= 0; i
< V_L1_SIZE
; i
++) {
765 page_flush_tb_1(V_L1_SHIFT
/ V_L2_BITS
- 1, l1_map
+ i
);
769 /* flush all the translation blocks */
770 /* XXX: tb_flush is currently not thread safe */
771 void tb_flush(CPUArchState
*env1
)
773 CPUState
*cpu
= ENV_GET_CPU(env1
);
775 #if defined(DEBUG_FLUSH)
776 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
777 (unsigned long)(tcg_ctx
.code_gen_ptr
- tcg_ctx
.code_gen_buffer
),
778 tcg_ctx
.tb_ctx
.nb_tbs
, tcg_ctx
.tb_ctx
.nb_tbs
> 0 ?
779 ((unsigned long)(tcg_ctx
.code_gen_ptr
- tcg_ctx
.code_gen_buffer
)) /
780 tcg_ctx
.tb_ctx
.nb_tbs
: 0);
782 if ((unsigned long)(tcg_ctx
.code_gen_ptr
- tcg_ctx
.code_gen_buffer
)
783 > tcg_ctx
.code_gen_buffer_size
) {
784 cpu_abort(cpu
, "Internal error: code buffer overflow\n");
786 tcg_ctx
.tb_ctx
.nb_tbs
= 0;
789 memset(cpu
->tb_jmp_cache
, 0, sizeof(cpu
->tb_jmp_cache
));
792 memset(tcg_ctx
.tb_ctx
.tb_phys_hash
, 0, sizeof(tcg_ctx
.tb_ctx
.tb_phys_hash
));
795 tcg_ctx
.code_gen_ptr
= tcg_ctx
.code_gen_buffer
;
796 /* XXX: flush processor icache at this point if cache flush is
798 tcg_ctx
.tb_ctx
.tb_flush_count
++;
801 #ifdef DEBUG_TB_CHECK
803 static void tb_invalidate_check(target_ulong address
)
805 TranslationBlock
*tb
;
808 address
&= TARGET_PAGE_MASK
;
809 for (i
= 0; i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
810 for (tb
= tb_ctx
.tb_phys_hash
[i
]; tb
!= NULL
; tb
= tb
->phys_hash_next
) {
811 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
812 address
>= tb
->pc
+ tb
->size
)) {
813 printf("ERROR invalidate: address=" TARGET_FMT_lx
814 " PC=%08lx size=%04x\n",
815 address
, (long)tb
->pc
, tb
->size
);
821 /* verify that all the pages have correct rights for code */
822 static void tb_page_check(void)
824 TranslationBlock
*tb
;
825 int i
, flags1
, flags2
;
827 for (i
= 0; i
< CODE_GEN_PHYS_HASH_SIZE
; i
++) {
828 for (tb
= tcg_ctx
.tb_ctx
.tb_phys_hash
[i
]; tb
!= NULL
;
829 tb
= tb
->phys_hash_next
) {
830 flags1
= page_get_flags(tb
->pc
);
831 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
832 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
833 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
834 (long)tb
->pc
, tb
->size
, flags1
, flags2
);
842 static inline void tb_hash_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
844 TranslationBlock
*tb1
;
849 *ptb
= tb1
->phys_hash_next
;
852 ptb
= &tb1
->phys_hash_next
;
856 static inline void tb_page_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
)
858 TranslationBlock
*tb1
;
863 n1
= (uintptr_t)tb1
& 3;
864 tb1
= (TranslationBlock
*)((uintptr_t)tb1
& ~3);
866 *ptb
= tb1
->page_next
[n1
];
869 ptb
= &tb1
->page_next
[n1
];
873 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
875 TranslationBlock
*tb1
, **ptb
;
878 ptb
= &tb
->jmp_next
[n
];
881 /* find tb(n) in circular list */
884 n1
= (uintptr_t)tb1
& 3;
885 tb1
= (TranslationBlock
*)((uintptr_t)tb1
& ~3);
886 if (n1
== n
&& tb1
== tb
) {
890 ptb
= &tb1
->jmp_first
;
892 ptb
= &tb1
->jmp_next
[n1
];
895 /* now we can suppress tb(n) from the list */
896 *ptb
= tb
->jmp_next
[n
];
898 tb
->jmp_next
[n
] = NULL
;
902 /* reset the jump entry 'n' of a TB so that it is not chained to
904 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
906 tb_set_jmp_target(tb
, n
, (uintptr_t)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
909 /* invalidate one TB */
910 void tb_phys_invalidate(TranslationBlock
*tb
, tb_page_addr_t page_addr
)
915 tb_page_addr_t phys_pc
;
916 TranslationBlock
*tb1
, *tb2
;
918 /* remove the TB from the hash list */
919 phys_pc
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
920 h
= tb_phys_hash_func(phys_pc
);
921 tb_hash_remove(&tcg_ctx
.tb_ctx
.tb_phys_hash
[h
], tb
);
923 /* remove the TB from the page list */
924 if (tb
->page_addr
[0] != page_addr
) {
925 p
= page_find(tb
->page_addr
[0] >> TARGET_PAGE_BITS
);
926 tb_page_remove(&p
->first_tb
, tb
);
927 invalidate_page_bitmap(p
);
929 if (tb
->page_addr
[1] != -1 && tb
->page_addr
[1] != page_addr
) {
930 p
= page_find(tb
->page_addr
[1] >> TARGET_PAGE_BITS
);
931 tb_page_remove(&p
->first_tb
, tb
);
932 invalidate_page_bitmap(p
);
935 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 1;
937 /* remove the TB from the hash list */
938 h
= tb_jmp_cache_hash_func(tb
->pc
);
940 if (cpu
->tb_jmp_cache
[h
] == tb
) {
941 cpu
->tb_jmp_cache
[h
] = NULL
;
945 /* suppress this TB from the two jump lists */
946 tb_jmp_remove(tb
, 0);
947 tb_jmp_remove(tb
, 1);
949 /* suppress any remaining jumps to this TB */
952 n1
= (uintptr_t)tb1
& 3;
956 tb1
= (TranslationBlock
*)((uintptr_t)tb1
& ~3);
957 tb2
= tb1
->jmp_next
[n1
];
958 tb_reset_jump(tb1
, n1
);
959 tb1
->jmp_next
[n1
] = NULL
;
962 tb
->jmp_first
= (TranslationBlock
*)((uintptr_t)tb
| 2); /* fail safe */
964 tcg_ctx
.tb_ctx
.tb_phys_invalidate_count
++;
967 static inline void set_bits(uint8_t *tab
, int start
, int len
)
973 mask
= 0xff << (start
& 7);
974 if ((start
& ~7) == (end
& ~7)) {
976 mask
&= ~(0xff << (end
& 7));
981 start
= (start
+ 8) & ~7;
983 while (start
< end1
) {
988 mask
= ~(0xff << (end
& 7));
994 static void build_page_bitmap(PageDesc
*p
)
996 int n
, tb_start
, tb_end
;
997 TranslationBlock
*tb
;
999 p
->code_bitmap
= g_malloc0(TARGET_PAGE_SIZE
/ 8);
1002 while (tb
!= NULL
) {
1003 n
= (uintptr_t)tb
& 3;
1004 tb
= (TranslationBlock
*)((uintptr_t)tb
& ~3);
1005 /* NOTE: this is subtle as a TB may span two physical pages */
1007 /* NOTE: tb_end may be after the end of the page, but
1008 it is not a problem */
1009 tb_start
= tb
->pc
& ~TARGET_PAGE_MASK
;
1010 tb_end
= tb_start
+ tb
->size
;
1011 if (tb_end
> TARGET_PAGE_SIZE
) {
1012 tb_end
= TARGET_PAGE_SIZE
;
1016 tb_end
= ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
1018 set_bits(p
->code_bitmap
, tb_start
, tb_end
- tb_start
);
1019 tb
= tb
->page_next
[n
];
1023 TranslationBlock
*tb_gen_code(CPUState
*cpu
,
1024 target_ulong pc
, target_ulong cs_base
,
1025 int flags
, int cflags
)
1027 CPUArchState
*env
= cpu
->env_ptr
;
1028 TranslationBlock
*tb
;
1029 tb_page_addr_t phys_pc
, phys_page2
;
1030 target_ulong virt_page2
;
1033 phys_pc
= get_page_addr_code(env
, pc
);
1035 cflags
|= CF_USE_ICOUNT
;
1039 /* flush must be done */
1041 /* cannot fail at this point */
1043 /* Don't forget to invalidate previous TB info. */
1044 tcg_ctx
.tb_ctx
.tb_invalidated_flag
= 1;
1046 tb
->tc_ptr
= tcg_ctx
.code_gen_ptr
;
1047 tb
->cs_base
= cs_base
;
1049 tb
->cflags
= cflags
;
1050 cpu_gen_code(env
, tb
, &code_gen_size
);
1051 tcg_ctx
.code_gen_ptr
= (void *)(((uintptr_t)tcg_ctx
.code_gen_ptr
+
1052 code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
1054 /* check next page if needed */
1055 virt_page2
= (pc
+ tb
->size
- 1) & TARGET_PAGE_MASK
;
1057 if ((pc
& TARGET_PAGE_MASK
) != virt_page2
) {
1058 phys_page2
= get_page_addr_code(env
, virt_page2
);
1060 tb_link_page(tb
, phys_pc
, phys_page2
);
1065 * Invalidate all TBs which intersect with the target physical address range
1066 * [start;end[. NOTE: start and end may refer to *different* physical pages.
1067 * 'is_cpu_write_access' should be true if called from a real cpu write
1068 * access: the virtual CPU will exit the current TB if code is modified inside
1071 void tb_invalidate_phys_range(tb_page_addr_t start
, tb_page_addr_t end
,
1072 int is_cpu_write_access
)
1074 while (start
< end
) {
1075 tb_invalidate_phys_page_range(start
, end
, is_cpu_write_access
);
1076 start
&= TARGET_PAGE_MASK
;
1077 start
+= TARGET_PAGE_SIZE
;
1082 * Invalidate all TBs which intersect with the target physical address range
1083 * [start;end[. NOTE: start and end must refer to the *same* physical page.
1084 * 'is_cpu_write_access' should be true if called from a real cpu write
1085 * access: the virtual CPU will exit the current TB if code is modified inside
1088 void tb_invalidate_phys_page_range(tb_page_addr_t start
, tb_page_addr_t end
,
1089 int is_cpu_write_access
)
1091 TranslationBlock
*tb
, *tb_next
, *saved_tb
;
1092 CPUState
*cpu
= current_cpu
;
1093 #if defined(TARGET_HAS_PRECISE_SMC)
1094 CPUArchState
*env
= NULL
;
1096 tb_page_addr_t tb_start
, tb_end
;
1099 #ifdef TARGET_HAS_PRECISE_SMC
1100 int current_tb_not_found
= is_cpu_write_access
;
1101 TranslationBlock
*current_tb
= NULL
;
1102 int current_tb_modified
= 0;
1103 target_ulong current_pc
= 0;
1104 target_ulong current_cs_base
= 0;
1105 int current_flags
= 0;
1106 #endif /* TARGET_HAS_PRECISE_SMC */
1108 p
= page_find(start
>> TARGET_PAGE_BITS
);
1112 if (!p
->code_bitmap
&&
1113 ++p
->code_write_count
>= SMC_BITMAP_USE_THRESHOLD
&&
1114 is_cpu_write_access
) {
1115 /* build code bitmap */
1116 build_page_bitmap(p
);
1118 #if defined(TARGET_HAS_PRECISE_SMC)
1124 /* we remove all the TBs in the range [start, end[ */
1125 /* XXX: see if in some cases it could be faster to invalidate all
1128 while (tb
!= NULL
) {
1129 n
= (uintptr_t)tb
& 3;
1130 tb
= (TranslationBlock
*)((uintptr_t)tb
& ~3);
1131 tb_next
= tb
->page_next
[n
];
1132 /* NOTE: this is subtle as a TB may span two physical pages */
1134 /* NOTE: tb_end may be after the end of the page, but
1135 it is not a problem */
1136 tb_start
= tb
->page_addr
[0] + (tb
->pc
& ~TARGET_PAGE_MASK
);
1137 tb_end
= tb_start
+ tb
->size
;
1139 tb_start
= tb
->page_addr
[1];
1140 tb_end
= tb_start
+ ((tb
->pc
+ tb
->size
) & ~TARGET_PAGE_MASK
);
1142 if (!(tb_end
<= start
|| tb_start
>= end
)) {
1143 #ifdef TARGET_HAS_PRECISE_SMC
1144 if (current_tb_not_found
) {
1145 current_tb_not_found
= 0;
1147 if (cpu
->mem_io_pc
) {
1148 /* now we have a real cpu fault */
1149 current_tb
= tb_find_pc(cpu
->mem_io_pc
);
1152 if (current_tb
== tb
&&
1153 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1154 /* If we are modifying the current TB, we must stop
1155 its execution. We could be more precise by checking
1156 that the modification is after the current PC, but it
1157 would require a specialized function to partially
1158 restore the CPU state */
1160 current_tb_modified
= 1;
1161 cpu_restore_state_from_tb(cpu
, current_tb
, cpu
->mem_io_pc
);
1162 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1165 #endif /* TARGET_HAS_PRECISE_SMC */
1166 /* we need to do that to handle the case where a signal
1167 occurs while doing tb_phys_invalidate() */
1170 saved_tb
= cpu
->current_tb
;
1171 cpu
->current_tb
= NULL
;
1173 tb_phys_invalidate(tb
, -1);
1175 cpu
->current_tb
= saved_tb
;
1176 if (cpu
->interrupt_request
&& cpu
->current_tb
) {
1177 cpu_interrupt(cpu
, cpu
->interrupt_request
);
1183 #if !defined(CONFIG_USER_ONLY)
1184 /* if no code remaining, no need to continue to use slow writes */
1186 invalidate_page_bitmap(p
);
1187 if (is_cpu_write_access
) {
1188 tlb_unprotect_code_phys(cpu
, start
, cpu
->mem_io_vaddr
);
1192 #ifdef TARGET_HAS_PRECISE_SMC
1193 if (current_tb_modified
) {
1194 /* we generate a block containing just the instruction
1195 modifying the memory. It will ensure that it cannot modify
1197 cpu
->current_tb
= NULL
;
1198 tb_gen_code(cpu
, current_pc
, current_cs_base
, current_flags
, 1);
1199 cpu_resume_from_signal(cpu
, NULL
);
1204 /* len must be <= 8 and start must be a multiple of len */
1205 void tb_invalidate_phys_page_fast(tb_page_addr_t start
, int len
)
1212 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1213 cpu_single_env
->mem_io_vaddr
, len
,
1214 cpu_single_env
->eip
,
1215 cpu_single_env
->eip
+
1216 (intptr_t)cpu_single_env
->segs
[R_CS
].base
);
1219 p
= page_find(start
>> TARGET_PAGE_BITS
);
1223 if (p
->code_bitmap
) {
1224 offset
= start
& ~TARGET_PAGE_MASK
;
1225 b
= p
->code_bitmap
[offset
>> 3] >> (offset
& 7);
1226 if (b
& ((1 << len
) - 1)) {
1231 tb_invalidate_phys_page_range(start
, start
+ len
, 1);
1235 #if !defined(CONFIG_SOFTMMU)
1236 static void tb_invalidate_phys_page(tb_page_addr_t addr
,
1237 uintptr_t pc
, void *puc
,
1240 TranslationBlock
*tb
;
1243 #ifdef TARGET_HAS_PRECISE_SMC
1244 TranslationBlock
*current_tb
= NULL
;
1245 CPUState
*cpu
= current_cpu
;
1246 CPUArchState
*env
= NULL
;
1247 int current_tb_modified
= 0;
1248 target_ulong current_pc
= 0;
1249 target_ulong current_cs_base
= 0;
1250 int current_flags
= 0;
1253 addr
&= TARGET_PAGE_MASK
;
1254 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1259 #ifdef TARGET_HAS_PRECISE_SMC
1260 if (tb
&& pc
!= 0) {
1261 current_tb
= tb_find_pc(pc
);
1267 while (tb
!= NULL
) {
1268 n
= (uintptr_t)tb
& 3;
1269 tb
= (TranslationBlock
*)((uintptr_t)tb
& ~3);
1270 #ifdef TARGET_HAS_PRECISE_SMC
1271 if (current_tb
== tb
&&
1272 (current_tb
->cflags
& CF_COUNT_MASK
) != 1) {
1273 /* If we are modifying the current TB, we must stop
1274 its execution. We could be more precise by checking
1275 that the modification is after the current PC, but it
1276 would require a specialized function to partially
1277 restore the CPU state */
1279 current_tb_modified
= 1;
1280 cpu_restore_state_from_tb(cpu
, current_tb
, pc
);
1281 cpu_get_tb_cpu_state(env
, ¤t_pc
, ¤t_cs_base
,
1284 #endif /* TARGET_HAS_PRECISE_SMC */
1285 tb_phys_invalidate(tb
, addr
);
1286 tb
= tb
->page_next
[n
];
1289 #ifdef TARGET_HAS_PRECISE_SMC
1290 if (current_tb_modified
) {
1291 /* we generate a block containing just the instruction
1292 modifying the memory. It will ensure that it cannot modify
1294 cpu
->current_tb
= NULL
;
1295 tb_gen_code(cpu
, current_pc
, current_cs_base
, current_flags
, 1);
1299 cpu_resume_from_signal(cpu
, puc
);
1305 /* add the tb in the target page and protect it if necessary */
1306 static inline void tb_alloc_page(TranslationBlock
*tb
,
1307 unsigned int n
, tb_page_addr_t page_addr
)
1310 #ifndef CONFIG_USER_ONLY
1311 bool page_already_protected
;
1314 tb
->page_addr
[n
] = page_addr
;
1315 p
= page_find_alloc(page_addr
>> TARGET_PAGE_BITS
, 1);
1316 tb
->page_next
[n
] = p
->first_tb
;
1317 #ifndef CONFIG_USER_ONLY
1318 page_already_protected
= p
->first_tb
!= NULL
;
1320 p
->first_tb
= (TranslationBlock
*)((uintptr_t)tb
| n
);
1321 invalidate_page_bitmap(p
);
1323 #if defined(CONFIG_USER_ONLY)
1324 if (p
->flags
& PAGE_WRITE
) {
1329 /* force the host page as non writable (writes will have a
1330 page fault + mprotect overhead) */
1331 page_addr
&= qemu_host_page_mask
;
1333 for (addr
= page_addr
; addr
< page_addr
+ qemu_host_page_size
;
1334 addr
+= TARGET_PAGE_SIZE
) {
1336 p2
= page_find(addr
>> TARGET_PAGE_BITS
);
1341 p2
->flags
&= ~PAGE_WRITE
;
1343 mprotect(g2h(page_addr
), qemu_host_page_size
,
1344 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
1345 #ifdef DEBUG_TB_INVALIDATE
1346 printf("protecting code page: 0x" TARGET_FMT_lx
"\n",
1351 /* if some code is already present, then the pages are already
1352 protected. So we handle the case where only the first TB is
1353 allocated in a physical page */
1354 if (!page_already_protected
) {
1355 tlb_protect_code(page_addr
);
1360 /* add a new TB and link it to the physical page tables. phys_page2 is
1361 (-1) to indicate that only one page contains the TB. */
1362 static void tb_link_page(TranslationBlock
*tb
, tb_page_addr_t phys_pc
,
1363 tb_page_addr_t phys_page2
)
1366 TranslationBlock
**ptb
;
1368 /* Grab the mmap lock to stop another thread invalidating this TB
1369 before we are done. */
1371 /* add in the physical hash table */
1372 h
= tb_phys_hash_func(phys_pc
);
1373 ptb
= &tcg_ctx
.tb_ctx
.tb_phys_hash
[h
];
1374 tb
->phys_hash_next
= *ptb
;
1377 /* add in the page list */
1378 tb_alloc_page(tb
, 0, phys_pc
& TARGET_PAGE_MASK
);
1379 if (phys_page2
!= -1) {
1380 tb_alloc_page(tb
, 1, phys_page2
);
1382 tb
->page_addr
[1] = -1;
1385 tb
->jmp_first
= (TranslationBlock
*)((uintptr_t)tb
| 2);
1386 tb
->jmp_next
[0] = NULL
;
1387 tb
->jmp_next
[1] = NULL
;
1389 /* init original jump addresses */
1390 if (tb
->tb_next_offset
[0] != 0xffff) {
1391 tb_reset_jump(tb
, 0);
1393 if (tb
->tb_next_offset
[1] != 0xffff) {
1394 tb_reset_jump(tb
, 1);
1397 #ifdef DEBUG_TB_CHECK
1403 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1404 tb[1].tc_ptr. Return NULL if not found */
1405 static TranslationBlock
*tb_find_pc(uintptr_t tc_ptr
)
1407 int m_min
, m_max
, m
;
1409 TranslationBlock
*tb
;
1411 if (tcg_ctx
.tb_ctx
.nb_tbs
<= 0) {
1414 if (tc_ptr
< (uintptr_t)tcg_ctx
.code_gen_buffer
||
1415 tc_ptr
>= (uintptr_t)tcg_ctx
.code_gen_ptr
) {
1418 /* binary search (cf Knuth) */
1420 m_max
= tcg_ctx
.tb_ctx
.nb_tbs
- 1;
1421 while (m_min
<= m_max
) {
1422 m
= (m_min
+ m_max
) >> 1;
1423 tb
= &tcg_ctx
.tb_ctx
.tbs
[m
];
1424 v
= (uintptr_t)tb
->tc_ptr
;
1427 } else if (tc_ptr
< v
) {
1433 return &tcg_ctx
.tb_ctx
.tbs
[m_max
];
1436 #if !defined(CONFIG_USER_ONLY)
1437 void tb_invalidate_phys_addr(AddressSpace
*as
, hwaddr addr
)
1439 ram_addr_t ram_addr
;
1443 mr
= address_space_translate(as
, addr
, &addr
, &l
, false);
1444 if (!(memory_region_is_ram(mr
)
1445 || memory_region_is_romd(mr
))) {
1448 ram_addr
= (memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
)
1450 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1, 0);
1452 #endif /* !defined(CONFIG_USER_ONLY) */
1454 void tb_check_watchpoint(CPUState
*cpu
)
1456 TranslationBlock
*tb
;
1458 tb
= tb_find_pc(cpu
->mem_io_pc
);
1460 cpu_abort(cpu
, "check_watchpoint: could not find TB for pc=%p",
1461 (void *)cpu
->mem_io_pc
);
1463 cpu_restore_state_from_tb(cpu
, tb
, cpu
->mem_io_pc
);
1464 tb_phys_invalidate(tb
, -1);
1467 #ifndef CONFIG_USER_ONLY
1468 /* mask must never be zero, except for A20 change call */
1469 static void tcg_handle_interrupt(CPUState
*cpu
, int mask
)
1473 old_mask
= cpu
->interrupt_request
;
1474 cpu
->interrupt_request
|= mask
;
1477 * If called from iothread context, wake the target cpu in
1480 if (!qemu_cpu_is_self(cpu
)) {
1486 cpu
->icount_decr
.u16
.high
= 0xffff;
1487 if (!cpu_can_do_io(cpu
)
1488 && (mask
& ~old_mask
) != 0) {
1489 cpu_abort(cpu
, "Raised interrupt while not in I/O function");
1492 cpu
->tcg_exit_req
= 1;
1496 CPUInterruptHandler cpu_interrupt_handler
= tcg_handle_interrupt
;
1498 /* in deterministic execution mode, instructions doing device I/Os
1499 must be at the end of the TB */
1500 void cpu_io_recompile(CPUState
*cpu
, uintptr_t retaddr
)
1502 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
1503 CPUArchState
*env
= cpu
->env_ptr
;
1505 TranslationBlock
*tb
;
1507 target_ulong pc
, cs_base
;
1510 tb
= tb_find_pc(retaddr
);
1512 cpu_abort(cpu
, "cpu_io_recompile: could not find TB for pc=%p",
1515 n
= cpu
->icount_decr
.u16
.low
+ tb
->icount
;
1516 cpu_restore_state_from_tb(cpu
, tb
, retaddr
);
1517 /* Calculate how many instructions had been executed before the fault
1519 n
= n
- cpu
->icount_decr
.u16
.low
;
1520 /* Generate a new TB ending on the I/O insn. */
1522 /* On MIPS and SH, delay slot instructions can only be restarted if
1523 they were already the first instruction in the TB. If this is not
1524 the first instruction in a TB then re-execute the preceding
1526 #if defined(TARGET_MIPS)
1527 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0 && n
> 1) {
1528 env
->active_tc
.PC
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
1529 cpu
->icount_decr
.u16
.low
++;
1530 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1532 #elif defined(TARGET_SH4)
1533 if ((env
->flags
& ((DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
))) != 0
1536 cpu
->icount_decr
.u16
.low
++;
1537 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
);
1540 /* This should never happen. */
1541 if (n
> CF_COUNT_MASK
) {
1542 cpu_abort(cpu
, "TB too big during recompile");
1545 cflags
= n
| CF_LAST_IO
;
1547 cs_base
= tb
->cs_base
;
1549 tb_phys_invalidate(tb
, -1);
1550 /* FIXME: In theory this could raise an exception. In practice
1551 we have already translated the block once so it's probably ok. */
1552 tb_gen_code(cpu
, pc
, cs_base
, flags
, cflags
);
1553 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
1554 the first in the TB) then we end up generating a whole new TB and
1555 repeating the fault, which is horribly inefficient.
1556 Better would be to execute just this insn uncached, or generate a
1558 cpu_resume_from_signal(cpu
, NULL
);
1561 void tb_flush_jmp_cache(CPUState
*cpu
, target_ulong addr
)
1565 /* Discard jump cache entries for any tb which might potentially
1566 overlap the flushed page. */
1567 i
= tb_jmp_cache_hash_page(addr
- TARGET_PAGE_SIZE
);
1568 memset(&cpu
->tb_jmp_cache
[i
], 0,
1569 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1571 i
= tb_jmp_cache_hash_page(addr
);
1572 memset(&cpu
->tb_jmp_cache
[i
], 0,
1573 TB_JMP_PAGE_SIZE
* sizeof(TranslationBlock
*));
1576 void dump_exec_info(FILE *f
, fprintf_function cpu_fprintf
)
1578 int i
, target_code_size
, max_target_code_size
;
1579 int direct_jmp_count
, direct_jmp2_count
, cross_page
;
1580 TranslationBlock
*tb
;
1582 target_code_size
= 0;
1583 max_target_code_size
= 0;
1585 direct_jmp_count
= 0;
1586 direct_jmp2_count
= 0;
1587 for (i
= 0; i
< tcg_ctx
.tb_ctx
.nb_tbs
; i
++) {
1588 tb
= &tcg_ctx
.tb_ctx
.tbs
[i
];
1589 target_code_size
+= tb
->size
;
1590 if (tb
->size
> max_target_code_size
) {
1591 max_target_code_size
= tb
->size
;
1593 if (tb
->page_addr
[1] != -1) {
1596 if (tb
->tb_next_offset
[0] != 0xffff) {
1598 if (tb
->tb_next_offset
[1] != 0xffff) {
1599 direct_jmp2_count
++;
1603 /* XXX: avoid using doubles ? */
1604 cpu_fprintf(f
, "Translation buffer state:\n");
1605 cpu_fprintf(f
, "gen code size %td/%zd\n",
1606 tcg_ctx
.code_gen_ptr
- tcg_ctx
.code_gen_buffer
,
1607 tcg_ctx
.code_gen_buffer_max_size
);
1608 cpu_fprintf(f
, "TB count %d/%d\n",
1609 tcg_ctx
.tb_ctx
.nb_tbs
, tcg_ctx
.code_gen_max_blocks
);
1610 cpu_fprintf(f
, "TB avg target size %d max=%d bytes\n",
1611 tcg_ctx
.tb_ctx
.nb_tbs
? target_code_size
/
1612 tcg_ctx
.tb_ctx
.nb_tbs
: 0,
1613 max_target_code_size
);
1614 cpu_fprintf(f
, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
1615 tcg_ctx
.tb_ctx
.nb_tbs
? (tcg_ctx
.code_gen_ptr
-
1616 tcg_ctx
.code_gen_buffer
) /
1617 tcg_ctx
.tb_ctx
.nb_tbs
: 0,
1618 target_code_size
? (double) (tcg_ctx
.code_gen_ptr
-
1619 tcg_ctx
.code_gen_buffer
) /
1620 target_code_size
: 0);
1621 cpu_fprintf(f
, "cross page TB count %d (%d%%)\n", cross_page
,
1622 tcg_ctx
.tb_ctx
.nb_tbs
? (cross_page
* 100) /
1623 tcg_ctx
.tb_ctx
.nb_tbs
: 0);
1624 cpu_fprintf(f
, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
1626 tcg_ctx
.tb_ctx
.nb_tbs
? (direct_jmp_count
* 100) /
1627 tcg_ctx
.tb_ctx
.nb_tbs
: 0,
1629 tcg_ctx
.tb_ctx
.nb_tbs
? (direct_jmp2_count
* 100) /
1630 tcg_ctx
.tb_ctx
.nb_tbs
: 0);
1631 cpu_fprintf(f
, "\nStatistics:\n");
1632 cpu_fprintf(f
, "TB flush count %d\n", tcg_ctx
.tb_ctx
.tb_flush_count
);
1633 cpu_fprintf(f
, "TB invalidate count %d\n",
1634 tcg_ctx
.tb_ctx
.tb_phys_invalidate_count
);
1635 cpu_fprintf(f
, "TLB flush count %d\n", tlb_flush_count
);
1636 tcg_dump_info(f
, cpu_fprintf
);
1639 void dump_opcount_info(FILE *f
, fprintf_function cpu_fprintf
)
1641 tcg_dump_op_count(f
, cpu_fprintf
);
1644 #else /* CONFIG_USER_ONLY */
1646 void cpu_interrupt(CPUState
*cpu
, int mask
)
1648 cpu
->interrupt_request
|= mask
;
1649 cpu
->tcg_exit_req
= 1;
1653 * Walks guest process memory "regions" one by one
1654 * and calls callback function 'fn' for each region.
1656 struct walk_memory_regions_data
{
1657 walk_memory_regions_fn fn
;
1663 static int walk_memory_regions_end(struct walk_memory_regions_data
*data
,
1664 target_ulong end
, int new_prot
)
1666 if (data
->start
!= -1u) {
1667 int rc
= data
->fn(data
->priv
, data
->start
, end
, data
->prot
);
1673 data
->start
= (new_prot
? end
: -1u);
1674 data
->prot
= new_prot
;
1679 static int walk_memory_regions_1(struct walk_memory_regions_data
*data
,
1680 target_ulong base
, int level
, void **lp
)
1686 return walk_memory_regions_end(data
, base
, 0);
1692 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
1693 int prot
= pd
[i
].flags
;
1695 pa
= base
| (i
<< TARGET_PAGE_BITS
);
1696 if (prot
!= data
->prot
) {
1697 rc
= walk_memory_regions_end(data
, pa
, prot
);
1706 for (i
= 0; i
< V_L2_SIZE
; ++i
) {
1707 pa
= base
| ((target_ulong
)i
<<
1708 (TARGET_PAGE_BITS
+ V_L2_BITS
* level
));
1709 rc
= walk_memory_regions_1(data
, pa
, level
- 1, pp
+ i
);
1719 int walk_memory_regions(void *priv
, walk_memory_regions_fn fn
)
1721 struct walk_memory_regions_data data
;
1729 for (i
= 0; i
< V_L1_SIZE
; i
++) {
1730 int rc
= walk_memory_regions_1(&data
, (target_ulong
)i
<< (V_L1_SHIFT
+ TARGET_PAGE_BITS
),
1731 V_L1_SHIFT
/ V_L2_BITS
- 1, l1_map
+ i
);
1737 return walk_memory_regions_end(&data
, 0, 0);
1740 static int dump_region(void *priv
, target_ulong start
,
1741 target_ulong end
, unsigned long prot
)
1743 FILE *f
= (FILE *)priv
;
1745 (void) fprintf(f
, TARGET_FMT_lx
"-"TARGET_FMT_lx
1746 " "TARGET_FMT_lx
" %c%c%c\n",
1747 start
, end
, end
- start
,
1748 ((prot
& PAGE_READ
) ? 'r' : '-'),
1749 ((prot
& PAGE_WRITE
) ? 'w' : '-'),
1750 ((prot
& PAGE_EXEC
) ? 'x' : '-'));
1755 /* dump memory mappings */
1756 void page_dump(FILE *f
)
1758 const int length
= sizeof(target_ulong
) * 2;
1759 (void) fprintf(f
, "%-*s %-*s %-*s %s\n",
1760 length
, "start", length
, "end", length
, "size", "prot");
1761 walk_memory_regions(f
, dump_region
);
1764 int page_get_flags(target_ulong address
)
1768 p
= page_find(address
>> TARGET_PAGE_BITS
);
1775 /* Modify the flags of a page and invalidate the code if necessary.
1776 The flag PAGE_WRITE_ORG is positioned automatically depending
1777 on PAGE_WRITE. The mmap_lock should already be held. */
1778 void page_set_flags(target_ulong start
, target_ulong end
, int flags
)
1780 target_ulong addr
, len
;
1782 /* This function should never be called with addresses outside the
1783 guest address space. If this assert fires, it probably indicates
1784 a missing call to h2g_valid. */
1785 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
1786 assert(end
< ((target_ulong
)1 << L1_MAP_ADDR_SPACE_BITS
));
1788 assert(start
< end
);
1790 start
= start
& TARGET_PAGE_MASK
;
1791 end
= TARGET_PAGE_ALIGN(end
);
1793 if (flags
& PAGE_WRITE
) {
1794 flags
|= PAGE_WRITE_ORG
;
1797 for (addr
= start
, len
= end
- start
;
1799 len
-= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
1800 PageDesc
*p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
, 1);
1802 /* If the write protection bit is set, then we invalidate
1804 if (!(p
->flags
& PAGE_WRITE
) &&
1805 (flags
& PAGE_WRITE
) &&
1807 tb_invalidate_phys_page(addr
, 0, NULL
, false);
1813 int page_check_range(target_ulong start
, target_ulong len
, int flags
)
1819 /* This function should never be called with addresses outside the
1820 guest address space. If this assert fires, it probably indicates
1821 a missing call to h2g_valid. */
1822 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
1823 assert(start
< ((target_ulong
)1 << L1_MAP_ADDR_SPACE_BITS
));
1829 if (start
+ len
- 1 < start
) {
1830 /* We've wrapped around. */
1834 /* must do before we loose bits in the next step */
1835 end
= TARGET_PAGE_ALIGN(start
+ len
);
1836 start
= start
& TARGET_PAGE_MASK
;
1838 for (addr
= start
, len
= end
- start
;
1840 len
-= TARGET_PAGE_SIZE
, addr
+= TARGET_PAGE_SIZE
) {
1841 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1845 if (!(p
->flags
& PAGE_VALID
)) {
1849 if ((flags
& PAGE_READ
) && !(p
->flags
& PAGE_READ
)) {
1852 if (flags
& PAGE_WRITE
) {
1853 if (!(p
->flags
& PAGE_WRITE_ORG
)) {
1856 /* unprotect the page if it was put read-only because it
1857 contains translated code */
1858 if (!(p
->flags
& PAGE_WRITE
)) {
1859 if (!page_unprotect(addr
, 0, NULL
)) {
1868 /* called from signal handler: invalidate the code and unprotect the
1869 page. Return TRUE if the fault was successfully handled. */
1870 int page_unprotect(target_ulong address
, uintptr_t pc
, void *puc
)
1874 target_ulong host_start
, host_end
, addr
;
1876 /* Technically this isn't safe inside a signal handler. However we
1877 know this only ever happens in a synchronous SEGV handler, so in
1878 practice it seems to be ok. */
1881 p
= page_find(address
>> TARGET_PAGE_BITS
);
1887 /* if the page was really writable, then we change its
1888 protection back to writable */
1889 if ((p
->flags
& PAGE_WRITE_ORG
) && !(p
->flags
& PAGE_WRITE
)) {
1890 host_start
= address
& qemu_host_page_mask
;
1891 host_end
= host_start
+ qemu_host_page_size
;
1894 for (addr
= host_start
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
1895 p
= page_find(addr
>> TARGET_PAGE_BITS
);
1896 p
->flags
|= PAGE_WRITE
;
1899 /* and since the content will be modified, we must invalidate
1900 the corresponding translated code. */
1901 tb_invalidate_phys_page(addr
, pc
, puc
, true);
1902 #ifdef DEBUG_TB_CHECK
1903 tb_invalidate_check(addr
);
1906 mprotect((void *)g2h(host_start
), qemu_host_page_size
,
1915 #endif /* CONFIG_USER_ONLY */