Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / translate-all.c
blob02d1ef3457ae7cb58281cc33d26b5dbf8db333e8
1 /*
2 * Host code generation
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/>.
19 #ifdef _WIN32
20 #include <windows.h>
21 #endif
22 #include "qemu/osdep.h"
24 #include "qemu-common.h"
25 #define NO_CPU_IO_DEFS
26 #include "cpu.h"
27 #include "trace.h"
28 #include "disas/disas.h"
29 #include "exec/exec-all.h"
30 #include "tcg.h"
31 #if defined(CONFIG_USER_ONLY)
32 #include "qemu.h"
33 #if defined(TARGET_X86_64)
34 #include "vsyscall.h"
35 #endif
36 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
37 #include <sys/param.h>
38 #if __FreeBSD_version >= 700104
39 #define HAVE_KINFO_GETVMMAP
40 #define sigqueue sigqueue_freebsd /* avoid redefinition */
41 #include <sys/proc.h>
42 #include <machine/profile.h>
43 #define _KERNEL
44 #include <sys/user.h>
45 #undef _KERNEL
46 #undef sigqueue
47 #include <libutil.h>
48 #endif
49 #endif
50 #else
51 #include "exec/address-spaces.h"
52 #endif
54 #include "exec/cputlb.h"
55 #include "exec/tb-hash.h"
56 #include "translate-all.h"
57 #include "qemu/bitmap.h"
58 #include "qemu/timer.h"
59 #include "exec/log.h"
61 /* #define DEBUG_TB_INVALIDATE */
62 /* #define DEBUG_TB_FLUSH */
63 /* #define DEBUG_LOCKING */
64 /* make various TB consistency checks */
65 /* #define DEBUG_TB_CHECK */
67 #if !defined(CONFIG_USER_ONLY)
68 /* TB consistency checks only implemented for usermode emulation. */
69 #undef DEBUG_TB_CHECK
70 #endif
72 /* Access to the various translations structures need to be serialised via locks
73 * for consistency. This is automatic for SoftMMU based system
74 * emulation due to its single threaded nature. In user-mode emulation
75 * access to the memory related structures are protected with the
76 * mmap_lock.
78 #ifdef DEBUG_LOCKING
79 #define DEBUG_MEM_LOCKS 1
80 #else
81 #define DEBUG_MEM_LOCKS 0
82 #endif
84 #ifdef CONFIG_SOFTMMU
85 #define assert_memory_lock() do { /* nothing */ } while (0)
86 #else
87 #define assert_memory_lock() do { \
88 if (DEBUG_MEM_LOCKS) { \
89 g_assert(have_mmap_lock()); \
90 } \
91 } while (0)
92 #endif
94 #define SMC_BITMAP_USE_THRESHOLD 10
96 typedef struct PageDesc {
97 /* list of TBs intersecting this ram page */
98 TranslationBlock *first_tb;
99 #ifdef CONFIG_SOFTMMU
100 /* in order to optimize self modifying code, we count the number
101 of lookups we do to a given page to use a bitmap */
102 unsigned int code_write_count;
103 unsigned long *code_bitmap;
104 #else
105 unsigned long flags;
106 #endif
107 } PageDesc;
109 /* In system mode we want L1_MAP to be based on ram offsets,
110 while in user mode we want it to be based on virtual addresses. */
111 #if !defined(CONFIG_USER_ONLY)
112 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
113 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
114 #else
115 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
116 #endif
117 #else
118 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
119 #endif
121 /* Size of the L2 (and L3, etc) page tables. */
122 #define V_L2_BITS 10
123 #define V_L2_SIZE (1 << V_L2_BITS)
125 uintptr_t qemu_host_page_size;
126 intptr_t qemu_host_page_mask;
129 * L1 Mapping properties
131 static int v_l1_size;
132 static int v_l1_shift;
133 static int v_l2_levels;
135 /* The bottom level has pointers to PageDesc, and is indexed by
136 * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
138 #define V_L1_MIN_BITS 4
139 #define V_L1_MAX_BITS (V_L2_BITS + 3)
140 #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
142 static void *l1_map[V_L1_MAX_SIZE];
144 /* code generation context */
145 TCGContext tcg_ctx;
146 bool parallel_cpus;
148 /* translation block context */
149 #ifdef CONFIG_USER_ONLY
150 __thread int have_tb_lock;
151 #endif
153 static void page_table_config_init(void)
155 uint32_t v_l1_bits;
157 assert(TARGET_PAGE_BITS);
158 /* The bits remaining after N lower levels of page tables. */
159 v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
160 if (v_l1_bits < V_L1_MIN_BITS) {
161 v_l1_bits += V_L2_BITS;
164 v_l1_size = 1 << v_l1_bits;
165 v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
166 v_l2_levels = v_l1_shift / V_L2_BITS - 1;
168 assert(v_l1_bits <= V_L1_MAX_BITS);
169 assert(v_l1_shift % V_L2_BITS == 0);
170 assert(v_l2_levels >= 0);
173 void tb_lock(void)
175 #ifdef CONFIG_USER_ONLY
176 assert(!have_tb_lock);
177 qemu_mutex_lock(&tcg_ctx.tb_ctx.tb_lock);
178 have_tb_lock++;
179 #endif
182 void tb_unlock(void)
184 #ifdef CONFIG_USER_ONLY
185 assert(have_tb_lock);
186 have_tb_lock--;
187 qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
188 #endif
191 void tb_lock_reset(void)
193 #ifdef CONFIG_USER_ONLY
194 if (have_tb_lock) {
195 qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
196 have_tb_lock = 0;
198 #endif
201 #ifdef DEBUG_LOCKING
202 #define DEBUG_TB_LOCKS 1
203 #else
204 #define DEBUG_TB_LOCKS 0
205 #endif
207 #ifdef CONFIG_SOFTMMU
208 #define assert_tb_lock() do { /* nothing */ } while (0)
209 #else
210 #define assert_tb_lock() do { \
211 if (DEBUG_TB_LOCKS) { \
212 g_assert(have_tb_lock); \
214 } while (0)
215 #endif
218 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr);
220 void cpu_gen_init(void)
222 tcg_context_init(&tcg_ctx);
225 /* Encode VAL as a signed leb128 sequence at P.
226 Return P incremented past the encoded value. */
227 static uint8_t *encode_sleb128(uint8_t *p, target_long val)
229 int more, byte;
231 do {
232 byte = val & 0x7f;
233 val >>= 7;
234 more = !((val == 0 && (byte & 0x40) == 0)
235 || (val == -1 && (byte & 0x40) != 0));
236 if (more) {
237 byte |= 0x80;
239 *p++ = byte;
240 } while (more);
242 return p;
245 /* Decode a signed leb128 sequence at *PP; increment *PP past the
246 decoded value. Return the decoded value. */
247 static target_long decode_sleb128(uint8_t **pp)
249 uint8_t *p = *pp;
250 target_long val = 0;
251 int byte, shift = 0;
253 do {
254 byte = *p++;
255 val |= (target_ulong)(byte & 0x7f) << shift;
256 shift += 7;
257 } while (byte & 0x80);
258 if (shift < TARGET_LONG_BITS && (byte & 0x40)) {
259 val |= -(target_ulong)1 << shift;
262 *pp = p;
263 return val;
266 /* Encode the data collected about the instructions while compiling TB.
267 Place the data at BLOCK, and return the number of bytes consumed.
269 The logical table consisits of TARGET_INSN_START_WORDS target_ulong's,
270 which come from the target's insn_start data, followed by a uintptr_t
271 which comes from the host pc of the end of the code implementing the insn.
273 Each line of the table is encoded as sleb128 deltas from the previous
274 line. The seed for the first line is { tb->pc, 0..., tb->tc_ptr }.
275 That is, the first column is seeded with the guest pc, the last column
276 with the host pc, and the middle columns with zeros. */
278 static int encode_search(TranslationBlock *tb, uint8_t *block)
280 uint8_t *highwater = tcg_ctx.code_gen_highwater;
281 uint8_t *p = block;
282 int i, j, n;
284 tb->tc_search = block;
286 for (i = 0, n = tb->icount; i < n; ++i) {
287 target_ulong prev;
289 for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
290 if (i == 0) {
291 prev = (j == 0 ? tb->pc : 0);
292 } else {
293 prev = tcg_ctx.gen_insn_data[i - 1][j];
295 p = encode_sleb128(p, tcg_ctx.gen_insn_data[i][j] - prev);
297 prev = (i == 0 ? 0 : tcg_ctx.gen_insn_end_off[i - 1]);
298 p = encode_sleb128(p, tcg_ctx.gen_insn_end_off[i] - prev);
300 /* Test for (pending) buffer overflow. The assumption is that any
301 one row beginning below the high water mark cannot overrun
302 the buffer completely. Thus we can test for overflow after
303 encoding a row without having to check during encoding. */
304 if (unlikely(p > highwater)) {
305 return -1;
309 return p - block;
312 /* The cpu state corresponding to 'searched_pc' is restored.
313 * Called with tb_lock held.
315 static int cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
316 uintptr_t searched_pc)
318 target_ulong data[TARGET_INSN_START_WORDS] = { tb->pc };
319 uintptr_t host_pc = (uintptr_t)tb->tc_ptr;
320 CPUArchState *env = cpu->env_ptr;
321 uint8_t *p = tb->tc_search;
322 int i, j, num_insns = tb->icount;
323 #ifdef CONFIG_PROFILER
324 int64_t ti = profile_getclock();
325 #endif
327 searched_pc -= GETPC_ADJ;
329 if (searched_pc < host_pc) {
330 return -1;
333 /* Reconstruct the stored insn data while looking for the point at
334 which the end of the insn exceeds the searched_pc. */
335 for (i = 0; i < num_insns; ++i) {
336 for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
337 data[j] += decode_sleb128(&p);
339 host_pc += decode_sleb128(&p);
340 if (host_pc > searched_pc) {
341 goto found;
344 return -1;
346 found:
347 if (tb->cflags & CF_USE_ICOUNT) {
348 assert(use_icount);
349 /* Reset the cycle counter to the start of the block. */
350 cpu->icount_decr.u16.low += num_insns;
351 /* Clear the IO flag. */
352 cpu->can_do_io = 0;
354 cpu->icount_decr.u16.low -= i;
355 restore_state_to_opc(env, tb, data);
357 #ifdef CONFIG_PROFILER
358 tcg_ctx.restore_time += profile_getclock() - ti;
359 tcg_ctx.restore_count++;
360 #endif
361 return 0;
364 bool cpu_restore_state(CPUState *cpu, uintptr_t retaddr)
366 TranslationBlock *tb;
367 bool r = false;
369 tb_lock();
370 tb = tb_find_pc(retaddr);
371 if (tb) {
372 cpu_restore_state_from_tb(cpu, tb, retaddr);
373 if (tb->cflags & CF_NOCACHE) {
374 /* one-shot translation, invalidate it immediately */
375 tb_phys_invalidate(tb, -1);
376 tb_free(tb);
378 r = true;
380 tb_unlock();
382 return r;
385 void page_size_init(void)
387 /* NOTE: we can always suppose that qemu_host_page_size >=
388 TARGET_PAGE_SIZE */
389 qemu_real_host_page_size = getpagesize();
390 qemu_real_host_page_mask = -(intptr_t)qemu_real_host_page_size;
391 if (qemu_host_page_size == 0) {
392 qemu_host_page_size = qemu_real_host_page_size;
394 if (qemu_host_page_size < TARGET_PAGE_SIZE) {
395 qemu_host_page_size = TARGET_PAGE_SIZE;
397 qemu_host_page_mask = -(intptr_t)qemu_host_page_size;
400 static void page_init(void)
402 page_size_init();
403 page_table_config_init();
405 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
407 #ifdef HAVE_KINFO_GETVMMAP
408 struct kinfo_vmentry *freep;
409 int i, cnt;
411 freep = kinfo_getvmmap(getpid(), &cnt);
412 if (freep) {
413 mmap_lock();
414 for (i = 0; i < cnt; i++) {
415 unsigned long startaddr, endaddr;
417 startaddr = freep[i].kve_start;
418 endaddr = freep[i].kve_end;
419 if (h2g_valid(startaddr)) {
420 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
422 if (h2g_valid(endaddr)) {
423 endaddr = h2g(endaddr);
424 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
425 } else {
426 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
427 endaddr = ~0ul;
428 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
429 #endif
433 free(freep);
434 mmap_unlock();
436 #else
437 FILE *f;
439 last_brk = (unsigned long)sbrk(0);
441 f = fopen("/compat/linux/proc/self/maps", "r");
442 if (f) {
443 mmap_lock();
445 do {
446 unsigned long startaddr, endaddr;
447 int n;
449 n = fscanf(f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
451 if (n == 2 && h2g_valid(startaddr)) {
452 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
454 if (h2g_valid(endaddr)) {
455 endaddr = h2g(endaddr);
456 } else {
457 endaddr = ~0ul;
459 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
461 } while (!feof(f));
463 fclose(f);
464 mmap_unlock();
466 #endif
468 #endif
471 /* If alloc=1:
472 * Called with tb_lock held for system emulation.
473 * Called with mmap_lock held for user-mode emulation.
475 static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
477 PageDesc *pd;
478 void **lp;
479 int i;
481 if (alloc) {
482 assert_memory_lock();
485 /* Level 1. Always allocated. */
486 lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
488 /* Level 2..N-1. */
489 for (i = v_l2_levels; i > 0; i--) {
490 void **p = atomic_rcu_read(lp);
492 if (p == NULL) {
493 if (!alloc) {
494 return NULL;
496 p = g_new0(void *, V_L2_SIZE);
497 atomic_rcu_set(lp, p);
500 lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
503 pd = atomic_rcu_read(lp);
504 if (pd == NULL) {
505 if (!alloc) {
506 return NULL;
508 pd = g_new0(PageDesc, V_L2_SIZE);
509 atomic_rcu_set(lp, pd);
512 return pd + (index & (V_L2_SIZE - 1));
515 static inline PageDesc *page_find(tb_page_addr_t index)
517 return page_find_alloc(index, 0);
520 #if defined(CONFIG_USER_ONLY)
521 /* Currently it is not recommended to allocate big chunks of data in
522 user mode. It will change when a dedicated libc will be used. */
523 /* ??? 64-bit hosts ought to have no problem mmaping data outside the
524 region in which the guest needs to run. Revisit this. */
525 #define USE_STATIC_CODE_GEN_BUFFER
526 #endif
528 /* Minimum size of the code gen buffer. This number is randomly chosen,
529 but not so small that we can't have a fair number of TB's live. */
530 #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024)
532 /* Maximum size of the code gen buffer we'd like to use. Unless otherwise
533 indicated, this is constrained by the range of direct branches on the
534 host cpu, as used by the TCG implementation of goto_tb. */
535 #if defined(__x86_64__)
536 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
537 #elif defined(__sparc__)
538 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
539 #elif defined(__powerpc64__)
540 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
541 #elif defined(__powerpc__)
542 # define MAX_CODE_GEN_BUFFER_SIZE (32u * 1024 * 1024)
543 #elif defined(__aarch64__)
544 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
545 #elif defined(__arm__)
546 # define MAX_CODE_GEN_BUFFER_SIZE (16u * 1024 * 1024)
547 #elif defined(__s390x__)
548 /* We have a +- 4GB range on the branches; leave some slop. */
549 # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
550 #elif defined(__mips__)
551 /* We have a 256MB branch region, but leave room to make sure the
552 main executable is also within that region. */
553 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
554 #else
555 # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
556 #endif
558 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024)
560 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
561 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
562 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
564 static inline size_t size_code_gen_buffer(size_t tb_size)
566 /* Size the buffer. */
567 if (tb_size == 0) {
568 #ifdef USE_STATIC_CODE_GEN_BUFFER
569 tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
570 #else
571 /* ??? Needs adjustments. */
572 /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
573 static buffer, we could size this on RESERVED_VA, on the text
574 segment size of the executable, or continue to use the default. */
575 tb_size = (unsigned long)(ram_size / 4);
576 #endif
578 if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
579 tb_size = MIN_CODE_GEN_BUFFER_SIZE;
581 if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
582 tb_size = MAX_CODE_GEN_BUFFER_SIZE;
584 return tb_size;
587 #ifdef __mips__
588 /* In order to use J and JAL within the code_gen_buffer, we require
589 that the buffer not cross a 256MB boundary. */
590 static inline bool cross_256mb(void *addr, size_t size)
592 return ((uintptr_t)addr ^ ((uintptr_t)addr + size)) & ~0x0ffffffful;
595 /* We weren't able to allocate a buffer without crossing that boundary,
596 so make do with the larger portion of the buffer that doesn't cross.
597 Returns the new base of the buffer, and adjusts code_gen_buffer_size. */
598 static inline void *split_cross_256mb(void *buf1, size_t size1)
600 void *buf2 = (void *)(((uintptr_t)buf1 + size1) & ~0x0ffffffful);
601 size_t size2 = buf1 + size1 - buf2;
603 size1 = buf2 - buf1;
604 if (size1 < size2) {
605 size1 = size2;
606 buf1 = buf2;
609 tcg_ctx.code_gen_buffer_size = size1;
610 return buf1;
612 #endif
614 #ifdef USE_STATIC_CODE_GEN_BUFFER
615 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
616 __attribute__((aligned(CODE_GEN_ALIGN)));
618 # ifdef _WIN32
619 static inline void do_protect(void *addr, long size, int prot)
621 DWORD old_protect;
622 VirtualProtect(addr, size, prot, &old_protect);
625 static inline void map_exec(void *addr, long size)
627 do_protect(addr, size, PAGE_EXECUTE_READWRITE);
630 static inline void map_none(void *addr, long size)
632 do_protect(addr, size, PAGE_NOACCESS);
634 # else
635 static inline void do_protect(void *addr, long size, int prot)
637 uintptr_t start, end;
639 start = (uintptr_t)addr;
640 start &= qemu_real_host_page_mask;
642 end = (uintptr_t)addr + size;
643 end = ROUND_UP(end, qemu_real_host_page_size);
645 mprotect((void *)start, end - start, prot);
648 static inline void map_exec(void *addr, long size)
650 do_protect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
653 static inline void map_none(void *addr, long size)
655 do_protect(addr, size, PROT_NONE);
657 # endif /* WIN32 */
659 static inline void *alloc_code_gen_buffer(void)
661 void *buf = static_code_gen_buffer;
662 size_t full_size, size;
664 /* The size of the buffer, rounded down to end on a page boundary. */
665 full_size = (((uintptr_t)buf + sizeof(static_code_gen_buffer))
666 & qemu_real_host_page_mask) - (uintptr_t)buf;
668 /* Reserve a guard page. */
669 size = full_size - qemu_real_host_page_size;
671 /* Honor a command-line option limiting the size of the buffer. */
672 if (size > tcg_ctx.code_gen_buffer_size) {
673 size = (((uintptr_t)buf + tcg_ctx.code_gen_buffer_size)
674 & qemu_real_host_page_mask) - (uintptr_t)buf;
676 tcg_ctx.code_gen_buffer_size = size;
678 #ifdef __mips__
679 if (cross_256mb(buf, size)) {
680 buf = split_cross_256mb(buf, size);
681 size = tcg_ctx.code_gen_buffer_size;
683 #endif
685 map_exec(buf, size);
686 map_none(buf + size, qemu_real_host_page_size);
687 qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
689 return buf;
691 #elif defined(_WIN32)
692 static inline void *alloc_code_gen_buffer(void)
694 size_t size = tcg_ctx.code_gen_buffer_size;
695 void *buf1, *buf2;
697 /* Perform the allocation in two steps, so that the guard page
698 is reserved but uncommitted. */
699 buf1 = VirtualAlloc(NULL, size + qemu_real_host_page_size,
700 MEM_RESERVE, PAGE_NOACCESS);
701 if (buf1 != NULL) {
702 buf2 = VirtualAlloc(buf1, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
703 assert(buf1 == buf2);
706 return buf1;
708 #else
709 static inline void *alloc_code_gen_buffer(void)
711 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
712 uintptr_t start = 0;
713 size_t size = tcg_ctx.code_gen_buffer_size;
714 void *buf;
716 /* Constrain the position of the buffer based on the host cpu.
717 Note that these addresses are chosen in concert with the
718 addresses assigned in the relevant linker script file. */
719 # if defined(__PIE__) || defined(__PIC__)
720 /* Don't bother setting a preferred location if we're building
721 a position-independent executable. We're more likely to get
722 an address near the main executable if we let the kernel
723 choose the address. */
724 # elif defined(__x86_64__) && defined(MAP_32BIT)
725 /* Force the memory down into low memory with the executable.
726 Leave the choice of exact location with the kernel. */
727 flags |= MAP_32BIT;
728 /* Cannot expect to map more than 800MB in low memory. */
729 if (size > 800u * 1024 * 1024) {
730 tcg_ctx.code_gen_buffer_size = size = 800u * 1024 * 1024;
732 # elif defined(__sparc__)
733 start = 0x40000000ul;
734 # elif defined(__s390x__)
735 start = 0x90000000ul;
736 # elif defined(__mips__)
737 # if _MIPS_SIM == _ABI64
738 start = 0x128000000ul;
739 # else
740 start = 0x08000000ul;
741 # endif
742 # endif
744 buf = mmap((void *)start, size + qemu_real_host_page_size,
745 PROT_NONE, flags, -1, 0);
746 if (buf == MAP_FAILED) {
747 return NULL;
750 #ifdef __mips__
751 if (cross_256mb(buf, size)) {
752 /* Try again, with the original still mapped, to avoid re-acquiring
753 that 256mb crossing. This time don't specify an address. */
754 size_t size2;
755 void *buf2 = mmap(NULL, size + qemu_real_host_page_size,
756 PROT_NONE, flags, -1, 0);
757 switch ((int)(buf2 != MAP_FAILED)) {
758 case 1:
759 if (!cross_256mb(buf2, size)) {
760 /* Success! Use the new buffer. */
761 munmap(buf, size + qemu_real_host_page_size);
762 break;
764 /* Failure. Work with what we had. */
765 munmap(buf2, size + qemu_real_host_page_size);
766 /* fallthru */
767 default:
768 /* Split the original buffer. Free the smaller half. */
769 buf2 = split_cross_256mb(buf, size);
770 size2 = tcg_ctx.code_gen_buffer_size;
771 if (buf == buf2) {
772 munmap(buf + size2 + qemu_real_host_page_size, size - size2);
773 } else {
774 munmap(buf, size - size2);
776 size = size2;
777 break;
779 buf = buf2;
781 #endif
783 /* Make the final buffer accessible. The guard page at the end
784 will remain inaccessible with PROT_NONE. */
785 mprotect(buf, size, PROT_WRITE | PROT_READ | PROT_EXEC);
787 /* Request large pages for the buffer. */
788 qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
790 return buf;
792 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
794 static inline void code_gen_alloc(size_t tb_size)
796 tcg_ctx.code_gen_buffer_size = size_code_gen_buffer(tb_size);
797 tcg_ctx.code_gen_buffer = alloc_code_gen_buffer();
798 if (tcg_ctx.code_gen_buffer == NULL) {
799 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
800 exit(1);
803 /* Estimate a good size for the number of TBs we can support. We
804 still haven't deducted the prologue from the buffer size here,
805 but that's minimal and won't affect the estimate much. */
806 tcg_ctx.code_gen_max_blocks
807 = tcg_ctx.code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
808 tcg_ctx.tb_ctx.tbs = g_new(TranslationBlock, tcg_ctx.code_gen_max_blocks);
810 qemu_mutex_init(&tcg_ctx.tb_ctx.tb_lock);
813 static void tb_htable_init(void)
815 unsigned int mode = QHT_MODE_AUTO_RESIZE;
817 qht_init(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE, mode);
820 /* Must be called before using the QEMU cpus. 'tb_size' is the size
821 (in bytes) allocated to the translation buffer. Zero means default
822 size. */
823 void tcg_exec_init(uintptr_t tb_size)
825 cpu_gen_init();
826 page_init();
827 tb_htable_init();
828 code_gen_alloc(tb_size);
829 #if defined(CONFIG_SOFTMMU)
830 /* There's no guest base to take into account, so go ahead and
831 initialize the prologue now. */
832 tcg_prologue_init(&tcg_ctx);
833 #endif
836 bool tcg_enabled(void)
838 return tcg_ctx.code_gen_buffer != NULL;
842 * Allocate a new translation block. Flush the translation buffer if
843 * too many translation blocks or too much generated code.
845 * Called with tb_lock held.
847 static TranslationBlock *tb_alloc(target_ulong pc)
849 TranslationBlock *tb;
851 assert_tb_lock();
853 if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks) {
854 return NULL;
856 tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++];
857 tb->pc = pc;
858 tb->cflags = 0;
859 tb->invalid = false;
860 return tb;
863 /* Called with tb_lock held. */
864 void tb_free(TranslationBlock *tb)
866 assert_tb_lock();
868 /* In practice this is mostly used for single use temporary TB
869 Ignore the hard cases and just back up if this TB happens to
870 be the last one generated. */
871 if (tcg_ctx.tb_ctx.nb_tbs > 0 &&
872 tb == &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) {
873 tcg_ctx.code_gen_ptr = tb->tc_ptr;
874 tcg_ctx.tb_ctx.nb_tbs--;
878 static inline void invalidate_page_bitmap(PageDesc *p)
880 #ifdef CONFIG_SOFTMMU
881 g_free(p->code_bitmap);
882 p->code_bitmap = NULL;
883 p->code_write_count = 0;
884 #endif
887 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
888 static void page_flush_tb_1(int level, void **lp)
890 int i;
892 if (*lp == NULL) {
893 return;
895 if (level == 0) {
896 PageDesc *pd = *lp;
898 for (i = 0; i < V_L2_SIZE; ++i) {
899 pd[i].first_tb = NULL;
900 invalidate_page_bitmap(pd + i);
902 } else {
903 void **pp = *lp;
905 for (i = 0; i < V_L2_SIZE; ++i) {
906 page_flush_tb_1(level - 1, pp + i);
911 static void page_flush_tb(void)
913 int i, l1_sz = v_l1_size;
915 for (i = 0; i < l1_sz; i++) {
916 page_flush_tb_1(v_l2_levels, l1_map + i);
920 /* flush all the translation blocks */
921 static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
923 tb_lock();
925 /* If it is already been done on request of another CPU,
926 * just retry.
928 if (tcg_ctx.tb_ctx.tb_flush_count != tb_flush_count.host_int) {
929 goto done;
932 #if defined(DEBUG_TB_FLUSH)
933 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
934 (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer),
935 tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.tb_ctx.nb_tbs > 0 ?
936 ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)) /
937 tcg_ctx.tb_ctx.nb_tbs : 0);
938 #endif
939 if ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)
940 > tcg_ctx.code_gen_buffer_size) {
941 cpu_abort(cpu, "Internal error: code buffer overflow\n");
944 CPU_FOREACH(cpu) {
945 int i;
947 for (i = 0; i < TB_JMP_CACHE_SIZE; ++i) {
948 atomic_set(&cpu->tb_jmp_cache[i], NULL);
952 tcg_ctx.tb_ctx.nb_tbs = 0;
953 qht_reset_size(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
954 page_flush_tb();
956 tcg_ctx.code_gen_ptr = tcg_ctx.code_gen_buffer;
957 /* XXX: flush processor icache at this point if cache flush is
958 expensive */
959 atomic_mb_set(&tcg_ctx.tb_ctx.tb_flush_count,
960 tcg_ctx.tb_ctx.tb_flush_count + 1);
962 done:
963 tb_unlock();
966 void tb_flush(CPUState *cpu)
968 if (tcg_enabled()) {
969 unsigned tb_flush_count = atomic_mb_read(&tcg_ctx.tb_ctx.tb_flush_count);
970 async_safe_run_on_cpu(cpu, do_tb_flush,
971 RUN_ON_CPU_HOST_INT(tb_flush_count));
975 #ifdef DEBUG_TB_CHECK
977 static void
978 do_tb_invalidate_check(struct qht *ht, void *p, uint32_t hash, void *userp)
980 TranslationBlock *tb = p;
981 target_ulong addr = *(target_ulong *)userp;
983 if (!(addr + TARGET_PAGE_SIZE <= tb->pc || addr >= tb->pc + tb->size)) {
984 printf("ERROR invalidate: address=" TARGET_FMT_lx
985 " PC=%08lx size=%04x\n", addr, (long)tb->pc, tb->size);
989 /* verify that all the pages have correct rights for code
991 * Called with tb_lock held.
993 static void tb_invalidate_check(target_ulong address)
995 address &= TARGET_PAGE_MASK;
996 qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_invalidate_check, &address);
999 static void
1000 do_tb_page_check(struct qht *ht, void *p, uint32_t hash, void *userp)
1002 TranslationBlock *tb = p;
1003 int flags1, flags2;
1005 flags1 = page_get_flags(tb->pc);
1006 flags2 = page_get_flags(tb->pc + tb->size - 1);
1007 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
1008 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
1009 (long)tb->pc, tb->size, flags1, flags2);
1013 /* verify that all the pages have correct rights for code */
1014 static void tb_page_check(void)
1016 qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_page_check, NULL);
1019 #endif
1021 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
1023 TranslationBlock *tb1;
1024 unsigned int n1;
1026 for (;;) {
1027 tb1 = *ptb;
1028 n1 = (uintptr_t)tb1 & 3;
1029 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
1030 if (tb1 == tb) {
1031 *ptb = tb1->page_next[n1];
1032 break;
1034 ptb = &tb1->page_next[n1];
1038 /* remove the TB from a list of TBs jumping to the n-th jump target of the TB */
1039 static inline void tb_remove_from_jmp_list(TranslationBlock *tb, int n)
1041 TranslationBlock *tb1;
1042 uintptr_t *ptb, ntb;
1043 unsigned int n1;
1045 ptb = &tb->jmp_list_next[n];
1046 if (*ptb) {
1047 /* find tb(n) in circular list */
1048 for (;;) {
1049 ntb = *ptb;
1050 n1 = ntb & 3;
1051 tb1 = (TranslationBlock *)(ntb & ~3);
1052 if (n1 == n && tb1 == tb) {
1053 break;
1055 if (n1 == 2) {
1056 ptb = &tb1->jmp_list_first;
1057 } else {
1058 ptb = &tb1->jmp_list_next[n1];
1061 /* now we can suppress tb(n) from the list */
1062 *ptb = tb->jmp_list_next[n];
1064 tb->jmp_list_next[n] = (uintptr_t)NULL;
1068 /* reset the jump entry 'n' of a TB so that it is not chained to
1069 another TB */
1070 static inline void tb_reset_jump(TranslationBlock *tb, int n)
1072 uintptr_t addr = (uintptr_t)(tb->tc_ptr + tb->jmp_reset_offset[n]);
1073 tb_set_jmp_target(tb, n, addr);
1076 /* remove any jumps to the TB */
1077 static inline void tb_jmp_unlink(TranslationBlock *tb)
1079 TranslationBlock *tb1;
1080 uintptr_t *ptb, ntb;
1081 unsigned int n1;
1083 ptb = &tb->jmp_list_first;
1084 for (;;) {
1085 ntb = *ptb;
1086 n1 = ntb & 3;
1087 tb1 = (TranslationBlock *)(ntb & ~3);
1088 if (n1 == 2) {
1089 break;
1091 tb_reset_jump(tb1, n1);
1092 *ptb = tb1->jmp_list_next[n1];
1093 tb1->jmp_list_next[n1] = (uintptr_t)NULL;
1097 /* invalidate one TB
1099 * Called with tb_lock held.
1101 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
1103 CPUState *cpu;
1104 PageDesc *p;
1105 uint32_t h;
1106 tb_page_addr_t phys_pc;
1108 assert_tb_lock();
1110 atomic_set(&tb->invalid, true);
1112 /* remove the TB from the hash list */
1113 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1114 h = tb_hash_func(phys_pc, tb->pc, tb->flags);
1115 qht_remove(&tcg_ctx.tb_ctx.htable, tb, h);
1117 /* remove the TB from the page list */
1118 if (tb->page_addr[0] != page_addr) {
1119 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
1120 tb_page_remove(&p->first_tb, tb);
1121 invalidate_page_bitmap(p);
1123 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
1124 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
1125 tb_page_remove(&p->first_tb, tb);
1126 invalidate_page_bitmap(p);
1129 /* remove the TB from the hash list */
1130 h = tb_jmp_cache_hash_func(tb->pc);
1131 CPU_FOREACH(cpu) {
1132 if (atomic_read(&cpu->tb_jmp_cache[h]) == tb) {
1133 atomic_set(&cpu->tb_jmp_cache[h], NULL);
1137 /* suppress this TB from the two jump lists */
1138 tb_remove_from_jmp_list(tb, 0);
1139 tb_remove_from_jmp_list(tb, 1);
1141 /* suppress any remaining jumps to this TB */
1142 tb_jmp_unlink(tb);
1144 tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
1147 #ifdef CONFIG_SOFTMMU
1148 static void build_page_bitmap(PageDesc *p)
1150 int n, tb_start, tb_end;
1151 TranslationBlock *tb;
1153 p->code_bitmap = bitmap_new(TARGET_PAGE_SIZE);
1155 tb = p->first_tb;
1156 while (tb != NULL) {
1157 n = (uintptr_t)tb & 3;
1158 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1159 /* NOTE: this is subtle as a TB may span two physical pages */
1160 if (n == 0) {
1161 /* NOTE: tb_end may be after the end of the page, but
1162 it is not a problem */
1163 tb_start = tb->pc & ~TARGET_PAGE_MASK;
1164 tb_end = tb_start + tb->size;
1165 if (tb_end > TARGET_PAGE_SIZE) {
1166 tb_end = TARGET_PAGE_SIZE;
1168 } else {
1169 tb_start = 0;
1170 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1172 bitmap_set(p->code_bitmap, tb_start, tb_end - tb_start);
1173 tb = tb->page_next[n];
1176 #endif
1178 /* add the tb in the target page and protect it if necessary
1180 * Called with mmap_lock held for user-mode emulation.
1182 static inline void tb_alloc_page(TranslationBlock *tb,
1183 unsigned int n, tb_page_addr_t page_addr)
1185 PageDesc *p;
1186 #ifndef CONFIG_USER_ONLY
1187 bool page_already_protected;
1188 #endif
1190 assert_memory_lock();
1192 tb->page_addr[n] = page_addr;
1193 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
1194 tb->page_next[n] = p->first_tb;
1195 #ifndef CONFIG_USER_ONLY
1196 page_already_protected = p->first_tb != NULL;
1197 #endif
1198 p->first_tb = (TranslationBlock *)((uintptr_t)tb | n);
1199 invalidate_page_bitmap(p);
1201 #if defined(CONFIG_USER_ONLY)
1202 if (p->flags & PAGE_WRITE) {
1203 target_ulong addr;
1204 PageDesc *p2;
1205 int prot;
1207 /* force the host page as non writable (writes will have a
1208 page fault + mprotect overhead) */
1209 page_addr &= qemu_host_page_mask;
1210 prot = 0;
1211 for (addr = page_addr; addr < page_addr + qemu_host_page_size;
1212 addr += TARGET_PAGE_SIZE) {
1214 p2 = page_find(addr >> TARGET_PAGE_BITS);
1215 if (!p2) {
1216 continue;
1218 prot |= p2->flags;
1219 p2->flags &= ~PAGE_WRITE;
1221 mprotect(g2h(page_addr), qemu_host_page_size,
1222 (prot & PAGE_BITS) & ~PAGE_WRITE);
1223 #ifdef DEBUG_TB_INVALIDATE
1224 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1225 page_addr);
1226 #endif
1228 #else
1229 /* if some code is already present, then the pages are already
1230 protected. So we handle the case where only the first TB is
1231 allocated in a physical page */
1232 if (!page_already_protected) {
1233 tlb_protect_code(page_addr);
1235 #endif
1238 /* add a new TB and link it to the physical page tables. phys_page2 is
1239 * (-1) to indicate that only one page contains the TB.
1241 * Called with mmap_lock held for user-mode emulation.
1243 static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
1244 tb_page_addr_t phys_page2)
1246 uint32_t h;
1248 assert_memory_lock();
1250 /* add in the page list */
1251 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1252 if (phys_page2 != -1) {
1253 tb_alloc_page(tb, 1, phys_page2);
1254 } else {
1255 tb->page_addr[1] = -1;
1258 /* add in the hash table */
1259 h = tb_hash_func(phys_pc, tb->pc, tb->flags);
1260 qht_insert(&tcg_ctx.tb_ctx.htable, tb, h);
1262 #ifdef DEBUG_TB_CHECK
1263 tb_page_check();
1264 #endif
1267 /* Called with mmap_lock held for user mode emulation. */
1268 TranslationBlock *tb_gen_code(CPUState *cpu,
1269 target_ulong pc, target_ulong cs_base,
1270 uint32_t flags, int cflags)
1272 CPUArchState *env = cpu->env_ptr;
1273 TranslationBlock *tb;
1274 tb_page_addr_t phys_pc, phys_page2;
1275 target_ulong virt_page2;
1276 tcg_insn_unit *gen_code_buf;
1277 int gen_code_size, search_size;
1278 #ifdef CONFIG_PROFILER
1279 int64_t ti;
1280 #endif
1281 assert_memory_lock();
1283 phys_pc = get_page_addr_code(env, pc);
1284 if (use_icount && !(cflags & CF_IGNORE_ICOUNT)) {
1285 cflags |= CF_USE_ICOUNT;
1288 tb = tb_alloc(pc);
1289 if (unlikely(!tb)) {
1290 buffer_overflow:
1291 /* flush must be done */
1292 tb_flush(cpu);
1293 mmap_unlock();
1294 cpu_loop_exit(cpu);
1297 gen_code_buf = tcg_ctx.code_gen_ptr;
1298 tb->tc_ptr = gen_code_buf;
1299 tb->cs_base = cs_base;
1300 tb->flags = flags;
1301 tb->cflags = cflags;
1303 #ifdef CONFIG_PROFILER
1304 tcg_ctx.tb_count1++; /* includes aborted translations because of
1305 exceptions */
1306 ti = profile_getclock();
1307 #endif
1309 tcg_func_start(&tcg_ctx);
1311 tcg_ctx.cpu = ENV_GET_CPU(env);
1312 gen_intermediate_code(env, tb);
1313 tcg_ctx.cpu = NULL;
1315 trace_translate_block(tb, tb->pc, tb->tc_ptr);
1317 /* generate machine code */
1318 tb->jmp_reset_offset[0] = TB_JMP_RESET_OFFSET_INVALID;
1319 tb->jmp_reset_offset[1] = TB_JMP_RESET_OFFSET_INVALID;
1320 tcg_ctx.tb_jmp_reset_offset = tb->jmp_reset_offset;
1321 #ifdef USE_DIRECT_JUMP
1322 tcg_ctx.tb_jmp_insn_offset = tb->jmp_insn_offset;
1323 tcg_ctx.tb_jmp_target_addr = NULL;
1324 #else
1325 tcg_ctx.tb_jmp_insn_offset = NULL;
1326 tcg_ctx.tb_jmp_target_addr = tb->jmp_target_addr;
1327 #endif
1329 #ifdef CONFIG_PROFILER
1330 tcg_ctx.tb_count++;
1331 tcg_ctx.interm_time += profile_getclock() - ti;
1332 tcg_ctx.code_time -= profile_getclock();
1333 #endif
1335 /* ??? Overflow could be handled better here. In particular, we
1336 don't need to re-do gen_intermediate_code, nor should we re-do
1337 the tcg optimization currently hidden inside tcg_gen_code. All
1338 that should be required is to flush the TBs, allocate a new TB,
1339 re-initialize it per above, and re-do the actual code generation. */
1340 gen_code_size = tcg_gen_code(&tcg_ctx, tb);
1341 if (unlikely(gen_code_size < 0)) {
1342 goto buffer_overflow;
1344 search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size);
1345 if (unlikely(search_size < 0)) {
1346 goto buffer_overflow;
1349 #ifdef CONFIG_PROFILER
1350 tcg_ctx.code_time += profile_getclock();
1351 tcg_ctx.code_in_len += tb->size;
1352 tcg_ctx.code_out_len += gen_code_size;
1353 tcg_ctx.search_out_len += search_size;
1354 #endif
1356 #ifdef DEBUG_DISAS
1357 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
1358 qemu_log_in_addr_range(tb->pc)) {
1359 qemu_log_lock();
1360 qemu_log("OUT: [size=%d]\n", gen_code_size);
1361 log_disas(tb->tc_ptr, gen_code_size);
1362 qemu_log("\n");
1363 qemu_log_flush();
1364 qemu_log_unlock();
1366 #endif
1368 tcg_ctx.code_gen_ptr = (void *)
1369 ROUND_UP((uintptr_t)gen_code_buf + gen_code_size + search_size,
1370 CODE_GEN_ALIGN);
1372 #if defined(CONFIG_USER_ONLY) && defined(TARGET_X86_64)
1373 /* if we are doing vsyscall don't link the page as it lies in high memory
1374 and tb_alloc_page will abort due to page_l1_map returning NULL */
1375 if (unlikely(phys_pc >= TARGET_VSYSCALL_START
1376 && phys_pc < TARGET_VSYSCALL_END))
1377 return tb;
1378 #endif
1380 /* init jump list */
1381 assert(((uintptr_t)tb & 3) == 0);
1382 tb->jmp_list_first = (uintptr_t)tb | 2;
1383 tb->jmp_list_next[0] = (uintptr_t)NULL;
1384 tb->jmp_list_next[1] = (uintptr_t)NULL;
1386 /* init original jump addresses wich has been set during tcg_gen_code() */
1387 if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) {
1388 tb_reset_jump(tb, 0);
1390 if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) {
1391 tb_reset_jump(tb, 1);
1394 /* check next page if needed */
1395 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
1396 phys_page2 = -1;
1397 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
1398 phys_page2 = get_page_addr_code(env, virt_page2);
1400 /* As long as consistency of the TB stuff is provided by tb_lock in user
1401 * mode and is implicit in single-threaded softmmu emulation, no explicit
1402 * memory barrier is required before tb_link_page() makes the TB visible
1403 * through the physical hash table and physical page list.
1405 tb_link_page(tb, phys_pc, phys_page2);
1406 return tb;
1410 * Invalidate all TBs which intersect with the target physical address range
1411 * [start;end[. NOTE: start and end may refer to *different* physical pages.
1412 * 'is_cpu_write_access' should be true if called from a real cpu write
1413 * access: the virtual CPU will exit the current TB if code is modified inside
1414 * this TB.
1416 * Called with mmap_lock held for user-mode emulation, grabs tb_lock
1417 * Called with tb_lock held for system-mode emulation
1419 static void tb_invalidate_phys_range_1(tb_page_addr_t start, tb_page_addr_t end)
1421 while (start < end) {
1422 tb_invalidate_phys_page_range(start, end, 0);
1423 start &= TARGET_PAGE_MASK;
1424 start += TARGET_PAGE_SIZE;
1428 #ifdef CONFIG_SOFTMMU
1429 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)
1431 assert_tb_lock();
1432 tb_invalidate_phys_range_1(start, end);
1434 #else
1435 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)
1437 assert_memory_lock();
1438 tb_lock();
1439 tb_invalidate_phys_range_1(start, end);
1440 tb_unlock();
1442 #endif
1444 * Invalidate all TBs which intersect with the target physical address range
1445 * [start;end[. NOTE: start and end must refer to the *same* physical page.
1446 * 'is_cpu_write_access' should be true if called from a real cpu write
1447 * access: the virtual CPU will exit the current TB if code is modified inside
1448 * this TB.
1450 * Called with tb_lock/mmap_lock held for user-mode emulation
1451 * Called with tb_lock held for system-mode emulation
1453 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
1454 int is_cpu_write_access)
1456 TranslationBlock *tb, *tb_next;
1457 #if defined(TARGET_HAS_PRECISE_SMC)
1458 CPUState *cpu = current_cpu;
1459 CPUArchState *env = NULL;
1460 #endif
1461 tb_page_addr_t tb_start, tb_end;
1462 PageDesc *p;
1463 int n;
1464 #ifdef TARGET_HAS_PRECISE_SMC
1465 int current_tb_not_found = is_cpu_write_access;
1466 TranslationBlock *current_tb = NULL;
1467 int current_tb_modified = 0;
1468 target_ulong current_pc = 0;
1469 target_ulong current_cs_base = 0;
1470 uint32_t current_flags = 0;
1471 #endif /* TARGET_HAS_PRECISE_SMC */
1473 assert_memory_lock();
1474 assert_tb_lock();
1476 p = page_find(start >> TARGET_PAGE_BITS);
1477 if (!p) {
1478 return;
1480 #if defined(TARGET_HAS_PRECISE_SMC)
1481 if (cpu != NULL) {
1482 env = cpu->env_ptr;
1484 #endif
1486 /* we remove all the TBs in the range [start, end[ */
1487 /* XXX: see if in some cases it could be faster to invalidate all
1488 the code */
1489 tb = p->first_tb;
1490 while (tb != NULL) {
1491 n = (uintptr_t)tb & 3;
1492 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1493 tb_next = tb->page_next[n];
1494 /* NOTE: this is subtle as a TB may span two physical pages */
1495 if (n == 0) {
1496 /* NOTE: tb_end may be after the end of the page, but
1497 it is not a problem */
1498 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1499 tb_end = tb_start + tb->size;
1500 } else {
1501 tb_start = tb->page_addr[1];
1502 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1504 if (!(tb_end <= start || tb_start >= end)) {
1505 #ifdef TARGET_HAS_PRECISE_SMC
1506 if (current_tb_not_found) {
1507 current_tb_not_found = 0;
1508 current_tb = NULL;
1509 if (cpu->mem_io_pc) {
1510 /* now we have a real cpu fault */
1511 current_tb = tb_find_pc(cpu->mem_io_pc);
1514 if (current_tb == tb &&
1515 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1516 /* If we are modifying the current TB, we must stop
1517 its execution. We could be more precise by checking
1518 that the modification is after the current PC, but it
1519 would require a specialized function to partially
1520 restore the CPU state */
1522 current_tb_modified = 1;
1523 cpu_restore_state_from_tb(cpu, current_tb, cpu->mem_io_pc);
1524 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1525 &current_flags);
1527 #endif /* TARGET_HAS_PRECISE_SMC */
1528 tb_phys_invalidate(tb, -1);
1530 tb = tb_next;
1532 #if !defined(CONFIG_USER_ONLY)
1533 /* if no code remaining, no need to continue to use slow writes */
1534 if (!p->first_tb) {
1535 invalidate_page_bitmap(p);
1536 tlb_unprotect_code(start);
1538 #endif
1539 #ifdef TARGET_HAS_PRECISE_SMC
1540 if (current_tb_modified) {
1541 /* we generate a block containing just the instruction
1542 modifying the memory. It will ensure that it cannot modify
1543 itself */
1544 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1545 cpu_loop_exit_noexc(cpu);
1547 #endif
1550 #ifdef CONFIG_SOFTMMU
1551 /* len must be <= 8 and start must be a multiple of len.
1552 * Called via softmmu_template.h when code areas are written to with
1553 * tb_lock held.
1555 void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
1557 PageDesc *p;
1559 #if 0
1560 if (1) {
1561 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1562 cpu_single_env->mem_io_vaddr, len,
1563 cpu_single_env->eip,
1564 cpu_single_env->eip +
1565 (intptr_t)cpu_single_env->segs[R_CS].base);
1567 #endif
1568 assert_memory_lock();
1570 p = page_find(start >> TARGET_PAGE_BITS);
1571 if (!p) {
1572 return;
1574 if (!p->code_bitmap &&
1575 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD) {
1576 /* build code bitmap. FIXME: writes should be protected by
1577 * tb_lock, reads by tb_lock or RCU.
1579 build_page_bitmap(p);
1581 if (p->code_bitmap) {
1582 unsigned int nr;
1583 unsigned long b;
1585 nr = start & ~TARGET_PAGE_MASK;
1586 b = p->code_bitmap[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG - 1));
1587 if (b & ((1 << len) - 1)) {
1588 goto do_invalidate;
1590 } else {
1591 do_invalidate:
1592 tb_invalidate_phys_page_range(start, start + len, 1);
1595 #else
1596 /* Called with mmap_lock held. If pc is not 0 then it indicates the
1597 * host PC of the faulting store instruction that caused this invalidate.
1598 * Returns true if the caller needs to abort execution of the current
1599 * TB (because it was modified by this store and the guest CPU has
1600 * precise-SMC semantics).
1602 static bool tb_invalidate_phys_page(tb_page_addr_t addr, uintptr_t pc)
1604 TranslationBlock *tb;
1605 PageDesc *p;
1606 int n;
1607 #ifdef TARGET_HAS_PRECISE_SMC
1608 TranslationBlock *current_tb = NULL;
1609 CPUState *cpu = current_cpu;
1610 CPUArchState *env = NULL;
1611 int current_tb_modified = 0;
1612 target_ulong current_pc = 0;
1613 target_ulong current_cs_base = 0;
1614 uint32_t current_flags = 0;
1615 #endif
1617 assert_memory_lock();
1619 addr &= TARGET_PAGE_MASK;
1620 p = page_find(addr >> TARGET_PAGE_BITS);
1621 if (!p) {
1622 return false;
1625 tb_lock();
1626 tb = p->first_tb;
1627 #ifdef TARGET_HAS_PRECISE_SMC
1628 if (tb && pc != 0) {
1629 current_tb = tb_find_pc(pc);
1631 if (cpu != NULL) {
1632 env = cpu->env_ptr;
1634 #endif
1635 while (tb != NULL) {
1636 n = (uintptr_t)tb & 3;
1637 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1638 #ifdef TARGET_HAS_PRECISE_SMC
1639 if (current_tb == tb &&
1640 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1641 /* If we are modifying the current TB, we must stop
1642 its execution. We could be more precise by checking
1643 that the modification is after the current PC, but it
1644 would require a specialized function to partially
1645 restore the CPU state */
1647 current_tb_modified = 1;
1648 cpu_restore_state_from_tb(cpu, current_tb, pc);
1649 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1650 &current_flags);
1652 #endif /* TARGET_HAS_PRECISE_SMC */
1653 tb_phys_invalidate(tb, addr);
1654 tb = tb->page_next[n];
1656 p->first_tb = NULL;
1657 #ifdef TARGET_HAS_PRECISE_SMC
1658 if (current_tb_modified) {
1659 /* we generate a block containing just the instruction
1660 modifying the memory. It will ensure that it cannot modify
1661 itself */
1662 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1663 /* tb_lock will be reset after cpu_loop_exit_noexc longjmps
1664 * back into the cpu_exec loop. */
1665 return true;
1667 #endif
1668 tb_unlock();
1670 return false;
1672 #endif
1674 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1675 tb[1].tc_ptr. Return NULL if not found */
1676 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr)
1678 int m_min, m_max, m;
1679 uintptr_t v;
1680 TranslationBlock *tb;
1682 if (tcg_ctx.tb_ctx.nb_tbs <= 0) {
1683 return NULL;
1685 if (tc_ptr < (uintptr_t)tcg_ctx.code_gen_buffer ||
1686 tc_ptr >= (uintptr_t)tcg_ctx.code_gen_ptr) {
1687 return NULL;
1689 /* binary search (cf Knuth) */
1690 m_min = 0;
1691 m_max = tcg_ctx.tb_ctx.nb_tbs - 1;
1692 while (m_min <= m_max) {
1693 m = (m_min + m_max) >> 1;
1694 tb = &tcg_ctx.tb_ctx.tbs[m];
1695 v = (uintptr_t)tb->tc_ptr;
1696 if (v == tc_ptr) {
1697 return tb;
1698 } else if (tc_ptr < v) {
1699 m_max = m - 1;
1700 } else {
1701 m_min = m + 1;
1704 return &tcg_ctx.tb_ctx.tbs[m_max];
1707 #if !defined(CONFIG_USER_ONLY)
1708 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
1710 ram_addr_t ram_addr;
1711 MemoryRegion *mr;
1712 hwaddr l = 1;
1714 rcu_read_lock();
1715 mr = address_space_translate(as, addr, &addr, &l, false);
1716 if (!(memory_region_is_ram(mr)
1717 || memory_region_is_romd(mr))) {
1718 rcu_read_unlock();
1719 return;
1721 ram_addr = memory_region_get_ram_addr(mr) + addr;
1722 tb_lock();
1723 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1724 tb_unlock();
1725 rcu_read_unlock();
1727 #endif /* !defined(CONFIG_USER_ONLY) */
1729 /* Called with tb_lock held. */
1730 void tb_check_watchpoint(CPUState *cpu)
1732 TranslationBlock *tb;
1734 tb = tb_find_pc(cpu->mem_io_pc);
1735 if (tb) {
1736 /* We can use retranslation to find the PC. */
1737 cpu_restore_state_from_tb(cpu, tb, cpu->mem_io_pc);
1738 tb_phys_invalidate(tb, -1);
1739 } else {
1740 /* The exception probably happened in a helper. The CPU state should
1741 have been saved before calling it. Fetch the PC from there. */
1742 CPUArchState *env = cpu->env_ptr;
1743 target_ulong pc, cs_base;
1744 tb_page_addr_t addr;
1745 uint32_t flags;
1747 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
1748 addr = get_page_addr_code(env, pc);
1749 tb_invalidate_phys_range(addr, addr + 1);
1753 #ifndef CONFIG_USER_ONLY
1754 /* in deterministic execution mode, instructions doing device I/Os
1755 must be at the end of the TB */
1756 void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)
1758 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
1759 CPUArchState *env = cpu->env_ptr;
1760 #endif
1761 TranslationBlock *tb;
1762 uint32_t n, cflags;
1763 target_ulong pc, cs_base;
1764 uint32_t flags;
1766 tb_lock();
1767 tb = tb_find_pc(retaddr);
1768 if (!tb) {
1769 cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p",
1770 (void *)retaddr);
1772 n = cpu->icount_decr.u16.low + tb->icount;
1773 cpu_restore_state_from_tb(cpu, tb, retaddr);
1774 /* Calculate how many instructions had been executed before the fault
1775 occurred. */
1776 n = n - cpu->icount_decr.u16.low;
1777 /* Generate a new TB ending on the I/O insn. */
1778 n++;
1779 /* On MIPS and SH, delay slot instructions can only be restarted if
1780 they were already the first instruction in the TB. If this is not
1781 the first instruction in a TB then re-execute the preceding
1782 branch. */
1783 #if defined(TARGET_MIPS)
1784 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
1785 env->active_tc.PC -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
1786 cpu->icount_decr.u16.low++;
1787 env->hflags &= ~MIPS_HFLAG_BMASK;
1789 #elif defined(TARGET_SH4)
1790 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
1791 && n > 1) {
1792 env->pc -= 2;
1793 cpu->icount_decr.u16.low++;
1794 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
1796 #endif
1797 /* This should never happen. */
1798 if (n > CF_COUNT_MASK) {
1799 cpu_abort(cpu, "TB too big during recompile");
1802 cflags = n | CF_LAST_IO;
1803 pc = tb->pc;
1804 cs_base = tb->cs_base;
1805 flags = tb->flags;
1806 tb_phys_invalidate(tb, -1);
1807 if (tb->cflags & CF_NOCACHE) {
1808 if (tb->orig_tb) {
1809 /* Invalidate original TB if this TB was generated in
1810 * cpu_exec_nocache() */
1811 tb_phys_invalidate(tb->orig_tb, -1);
1813 tb_free(tb);
1815 /* FIXME: In theory this could raise an exception. In practice
1816 we have already translated the block once so it's probably ok. */
1817 tb_gen_code(cpu, pc, cs_base, flags, cflags);
1819 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
1820 * the first in the TB) then we end up generating a whole new TB and
1821 * repeating the fault, which is horribly inefficient.
1822 * Better would be to execute just this insn uncached, or generate a
1823 * second new TB.
1825 * cpu_loop_exit_noexc will longjmp back to cpu_exec where the
1826 * tb_lock gets reset.
1828 cpu_loop_exit_noexc(cpu);
1831 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr)
1833 unsigned int i;
1835 /* Discard jump cache entries for any tb which might potentially
1836 overlap the flushed page. */
1837 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1838 memset(&cpu->tb_jmp_cache[i], 0,
1839 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1841 i = tb_jmp_cache_hash_page(addr);
1842 memset(&cpu->tb_jmp_cache[i], 0,
1843 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1846 static void print_qht_statistics(FILE *f, fprintf_function cpu_fprintf,
1847 struct qht_stats hst)
1849 uint32_t hgram_opts;
1850 size_t hgram_bins;
1851 char *hgram;
1853 if (!hst.head_buckets) {
1854 return;
1856 cpu_fprintf(f, "TB hash buckets %zu/%zu (%0.2f%% head buckets used)\n",
1857 hst.used_head_buckets, hst.head_buckets,
1858 (double)hst.used_head_buckets / hst.head_buckets * 100);
1860 hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS;
1861 hgram_opts |= QDIST_PR_100X | QDIST_PR_PERCENT;
1862 if (qdist_xmax(&hst.occupancy) - qdist_xmin(&hst.occupancy) == 1) {
1863 hgram_opts |= QDIST_PR_NODECIMAL;
1865 hgram = qdist_pr(&hst.occupancy, 10, hgram_opts);
1866 cpu_fprintf(f, "TB hash occupancy %0.2f%% avg chain occ. Histogram: %s\n",
1867 qdist_avg(&hst.occupancy) * 100, hgram);
1868 g_free(hgram);
1870 hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS;
1871 hgram_bins = qdist_xmax(&hst.chain) - qdist_xmin(&hst.chain);
1872 if (hgram_bins > 10) {
1873 hgram_bins = 10;
1874 } else {
1875 hgram_bins = 0;
1876 hgram_opts |= QDIST_PR_NODECIMAL | QDIST_PR_NOBINRANGE;
1878 hgram = qdist_pr(&hst.chain, hgram_bins, hgram_opts);
1879 cpu_fprintf(f, "TB hash avg chain %0.3f buckets. Histogram: %s\n",
1880 qdist_avg(&hst.chain), hgram);
1881 g_free(hgram);
1884 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
1886 int i, target_code_size, max_target_code_size;
1887 int direct_jmp_count, direct_jmp2_count, cross_page;
1888 TranslationBlock *tb;
1889 struct qht_stats hst;
1891 tb_lock();
1893 target_code_size = 0;
1894 max_target_code_size = 0;
1895 cross_page = 0;
1896 direct_jmp_count = 0;
1897 direct_jmp2_count = 0;
1898 for (i = 0; i < tcg_ctx.tb_ctx.nb_tbs; i++) {
1899 tb = &tcg_ctx.tb_ctx.tbs[i];
1900 target_code_size += tb->size;
1901 if (tb->size > max_target_code_size) {
1902 max_target_code_size = tb->size;
1904 if (tb->page_addr[1] != -1) {
1905 cross_page++;
1907 if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) {
1908 direct_jmp_count++;
1909 if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) {
1910 direct_jmp2_count++;
1914 /* XXX: avoid using doubles ? */
1915 cpu_fprintf(f, "Translation buffer state:\n");
1916 cpu_fprintf(f, "gen code size %td/%zd\n",
1917 tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer,
1918 tcg_ctx.code_gen_highwater - tcg_ctx.code_gen_buffer);
1919 cpu_fprintf(f, "TB count %d/%d\n",
1920 tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.code_gen_max_blocks);
1921 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
1922 tcg_ctx.tb_ctx.nb_tbs ? target_code_size /
1923 tcg_ctx.tb_ctx.nb_tbs : 0,
1924 max_target_code_size);
1925 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
1926 tcg_ctx.tb_ctx.nb_tbs ? (tcg_ctx.code_gen_ptr -
1927 tcg_ctx.code_gen_buffer) /
1928 tcg_ctx.tb_ctx.nb_tbs : 0,
1929 target_code_size ? (double) (tcg_ctx.code_gen_ptr -
1930 tcg_ctx.code_gen_buffer) /
1931 target_code_size : 0);
1932 cpu_fprintf(f, "cross page TB count %d (%d%%)\n", cross_page,
1933 tcg_ctx.tb_ctx.nb_tbs ? (cross_page * 100) /
1934 tcg_ctx.tb_ctx.nb_tbs : 0);
1935 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
1936 direct_jmp_count,
1937 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp_count * 100) /
1938 tcg_ctx.tb_ctx.nb_tbs : 0,
1939 direct_jmp2_count,
1940 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp2_count * 100) /
1941 tcg_ctx.tb_ctx.nb_tbs : 0);
1943 qht_statistics_init(&tcg_ctx.tb_ctx.htable, &hst);
1944 print_qht_statistics(f, cpu_fprintf, hst);
1945 qht_statistics_destroy(&hst);
1947 cpu_fprintf(f, "\nStatistics:\n");
1948 cpu_fprintf(f, "TB flush count %u\n",
1949 atomic_read(&tcg_ctx.tb_ctx.tb_flush_count));
1950 cpu_fprintf(f, "TB invalidate count %d\n",
1951 tcg_ctx.tb_ctx.tb_phys_invalidate_count);
1952 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
1953 tcg_dump_info(f, cpu_fprintf);
1955 tb_unlock();
1958 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf)
1960 tcg_dump_op_count(f, cpu_fprintf);
1963 #else /* CONFIG_USER_ONLY */
1965 void cpu_interrupt(CPUState *cpu, int mask)
1967 cpu->interrupt_request |= mask;
1968 cpu->tcg_exit_req = 1;
1972 * Walks guest process memory "regions" one by one
1973 * and calls callback function 'fn' for each region.
1975 struct walk_memory_regions_data {
1976 walk_memory_regions_fn fn;
1977 void *priv;
1978 target_ulong start;
1979 int prot;
1982 static int walk_memory_regions_end(struct walk_memory_regions_data *data,
1983 target_ulong end, int new_prot)
1985 if (data->start != -1u) {
1986 int rc = data->fn(data->priv, data->start, end, data->prot);
1987 if (rc != 0) {
1988 return rc;
1992 data->start = (new_prot ? end : -1u);
1993 data->prot = new_prot;
1995 return 0;
1998 static int walk_memory_regions_1(struct walk_memory_regions_data *data,
1999 target_ulong base, int level, void **lp)
2001 target_ulong pa;
2002 int i, rc;
2004 if (*lp == NULL) {
2005 return walk_memory_regions_end(data, base, 0);
2008 if (level == 0) {
2009 PageDesc *pd = *lp;
2011 for (i = 0; i < V_L2_SIZE; ++i) {
2012 int prot = pd[i].flags;
2014 pa = base | (i << TARGET_PAGE_BITS);
2015 if (prot != data->prot) {
2016 rc = walk_memory_regions_end(data, pa, prot);
2017 if (rc != 0) {
2018 return rc;
2022 } else {
2023 void **pp = *lp;
2025 for (i = 0; i < V_L2_SIZE; ++i) {
2026 pa = base | ((target_ulong)i <<
2027 (TARGET_PAGE_BITS + V_L2_BITS * level));
2028 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2029 if (rc != 0) {
2030 return rc;
2035 return 0;
2038 int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2040 struct walk_memory_regions_data data;
2041 uintptr_t i, l1_sz = v_l1_size;
2043 data.fn = fn;
2044 data.priv = priv;
2045 data.start = -1u;
2046 data.prot = 0;
2048 for (i = 0; i < l1_sz; i++) {
2049 target_ulong base = i << (v_l1_shift + TARGET_PAGE_BITS);
2050 int rc = walk_memory_regions_1(&data, base, v_l2_levels, l1_map + i);
2051 if (rc != 0) {
2052 return rc;
2056 return walk_memory_regions_end(&data, 0, 0);
2059 static int dump_region(void *priv, target_ulong start,
2060 target_ulong end, abi_ulong prot)
2062 FILE *f = (FILE *)priv;
2064 (void) fprintf(f, TARGET_FMT_lx"-"TARGET_FMT_lx
2065 " "TARGET_FMT_lx" %c%c%c\n",
2066 start, end, end - start,
2067 ((prot & PAGE_READ) ? 'r' : '-'),
2068 ((prot & PAGE_WRITE) ? 'w' : '-'),
2069 ((prot & PAGE_EXEC) ? 'x' : '-'));
2071 return 0;
2074 /* dump memory mappings */
2075 void page_dump(FILE *f)
2077 const int length = sizeof(target_ulong) * 2;
2078 (void) fprintf(f, "%-*s %-*s %-*s %s\n",
2079 length, "start", length, "end", length, "size", "prot");
2080 walk_memory_regions(f, dump_region);
2083 int page_get_flags(target_ulong address)
2085 PageDesc *p;
2087 p = page_find(address >> TARGET_PAGE_BITS);
2088 if (!p) {
2089 return 0;
2091 return p->flags;
2094 /* Modify the flags of a page and invalidate the code if necessary.
2095 The flag PAGE_WRITE_ORG is positioned automatically depending
2096 on PAGE_WRITE. The mmap_lock should already be held. */
2097 void page_set_flags(target_ulong start, target_ulong end, int flags)
2099 target_ulong addr, len;
2101 /* This function should never be called with addresses outside the
2102 guest address space. If this assert fires, it probably indicates
2103 a missing call to h2g_valid. */
2104 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2105 assert(end < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2106 #endif
2107 assert(start < end);
2108 assert_memory_lock();
2110 start = start & TARGET_PAGE_MASK;
2111 end = TARGET_PAGE_ALIGN(end);
2113 if (flags & PAGE_WRITE) {
2114 flags |= PAGE_WRITE_ORG;
2117 for (addr = start, len = end - start;
2118 len != 0;
2119 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2120 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2122 /* If the write protection bit is set, then we invalidate
2123 the code inside. */
2124 if (!(p->flags & PAGE_WRITE) &&
2125 (flags & PAGE_WRITE) &&
2126 p->first_tb) {
2127 tb_invalidate_phys_page(addr, 0);
2129 p->flags = flags;
2133 int page_check_range(target_ulong start, target_ulong len, int flags)
2135 PageDesc *p;
2136 target_ulong end;
2137 target_ulong addr;
2139 /* This function should never be called with addresses outside the
2140 guest address space. If this assert fires, it probably indicates
2141 a missing call to h2g_valid. */
2142 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2143 assert(start < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2144 #endif
2146 if (len == 0) {
2147 return 0;
2149 if (start + len - 1 < start) {
2150 /* We've wrapped around. */
2151 return -1;
2154 /* must do before we loose bits in the next step */
2155 end = TARGET_PAGE_ALIGN(start + len);
2156 start = start & TARGET_PAGE_MASK;
2158 for (addr = start, len = end - start;
2159 len != 0;
2160 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2161 p = page_find(addr >> TARGET_PAGE_BITS);
2162 if (!p) {
2163 return -1;
2165 if (!(p->flags & PAGE_VALID)) {
2166 return -1;
2169 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) {
2170 return -1;
2172 if (flags & PAGE_WRITE) {
2173 if (!(p->flags & PAGE_WRITE_ORG)) {
2174 return -1;
2176 /* unprotect the page if it was put read-only because it
2177 contains translated code */
2178 if (!(p->flags & PAGE_WRITE)) {
2179 if (!page_unprotect(addr, 0)) {
2180 return -1;
2185 return 0;
2188 /* called from signal handler: invalidate the code and unprotect the
2189 * page. Return 0 if the fault was not handled, 1 if it was handled,
2190 * and 2 if it was handled but the caller must cause the TB to be
2191 * immediately exited. (We can only return 2 if the 'pc' argument is
2192 * non-zero.)
2194 int page_unprotect(target_ulong address, uintptr_t pc)
2196 unsigned int prot;
2197 bool current_tb_invalidated;
2198 PageDesc *p;
2199 target_ulong host_start, host_end, addr;
2201 /* Technically this isn't safe inside a signal handler. However we
2202 know this only ever happens in a synchronous SEGV handler, so in
2203 practice it seems to be ok. */
2204 mmap_lock();
2206 p = page_find(address >> TARGET_PAGE_BITS);
2207 if (!p) {
2208 mmap_unlock();
2209 return 0;
2212 /* if the page was really writable, then we change its
2213 protection back to writable */
2214 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2215 host_start = address & qemu_host_page_mask;
2216 host_end = host_start + qemu_host_page_size;
2218 prot = 0;
2219 current_tb_invalidated = false;
2220 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2221 p = page_find(addr >> TARGET_PAGE_BITS);
2222 p->flags |= PAGE_WRITE;
2223 prot |= p->flags;
2225 /* and since the content will be modified, we must invalidate
2226 the corresponding translated code. */
2227 current_tb_invalidated |= tb_invalidate_phys_page(addr, pc);
2228 #ifdef DEBUG_TB_CHECK
2229 tb_invalidate_check(addr);
2230 #endif
2232 mprotect((void *)g2h(host_start), qemu_host_page_size,
2233 prot & PAGE_BITS);
2235 mmap_unlock();
2236 /* If current TB was invalidated return to main loop */
2237 return current_tb_invalidated ? 2 : 1;
2239 mmap_unlock();
2240 return 0;
2242 #endif /* CONFIG_USER_ONLY */