Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / accel / tcg / translate-all.c
bloba867579be348e6b4a9b7f230781fd1df7515bd31
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 "qemu/main-loop.h"
60 #include "exec/log.h"
61 #include "sysemu/cpus.h"
63 /* #define DEBUG_TB_INVALIDATE */
64 /* #define DEBUG_TB_FLUSH */
65 /* make various TB consistency checks */
66 /* #define DEBUG_TB_CHECK */
68 #if !defined(CONFIG_USER_ONLY)
69 /* TB consistency checks only implemented for usermode emulation. */
70 #undef DEBUG_TB_CHECK
71 #endif
73 /* Access to the various translations structures need to be serialised via locks
74 * for consistency. This is automatic for SoftMMU based system
75 * emulation due to its single threaded nature. In user-mode emulation
76 * access to the memory related structures are protected with the
77 * mmap_lock.
79 #ifdef CONFIG_SOFTMMU
80 #define assert_memory_lock() tcg_debug_assert(have_tb_lock)
81 #else
82 #define assert_memory_lock() tcg_debug_assert(have_mmap_lock())
83 #endif
85 #define SMC_BITMAP_USE_THRESHOLD 10
87 typedef struct PageDesc {
88 /* list of TBs intersecting this ram page */
89 TranslationBlock *first_tb;
90 #ifdef CONFIG_SOFTMMU
91 /* in order to optimize self modifying code, we count the number
92 of lookups we do to a given page to use a bitmap */
93 unsigned int code_write_count;
94 unsigned long *code_bitmap;
95 #else
96 unsigned long flags;
97 #endif
98 } PageDesc;
100 /* In system mode we want L1_MAP to be based on ram offsets,
101 while in user mode we want it to be based on virtual addresses. */
102 #if !defined(CONFIG_USER_ONLY)
103 #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
104 # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
105 #else
106 # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
107 #endif
108 #else
109 # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
110 #endif
112 /* Size of the L2 (and L3, etc) page tables. */
113 #define V_L2_BITS 10
114 #define V_L2_SIZE (1 << V_L2_BITS)
116 uintptr_t qemu_host_page_size;
117 intptr_t qemu_host_page_mask;
120 * L1 Mapping properties
122 static int v_l1_size;
123 static int v_l1_shift;
124 static int v_l2_levels;
126 /* The bottom level has pointers to PageDesc, and is indexed by
127 * anything from 4 to (V_L2_BITS + 3) bits, depending on target page size.
129 #define V_L1_MIN_BITS 4
130 #define V_L1_MAX_BITS (V_L2_BITS + 3)
131 #define V_L1_MAX_SIZE (1 << V_L1_MAX_BITS)
133 static void *l1_map[V_L1_MAX_SIZE];
135 /* code generation context */
136 TCGContext tcg_ctx;
137 bool parallel_cpus;
139 /* translation block context */
140 __thread int have_tb_lock;
142 static void page_table_config_init(void)
144 uint32_t v_l1_bits;
146 assert(TARGET_PAGE_BITS);
147 /* The bits remaining after N lower levels of page tables. */
148 v_l1_bits = (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % V_L2_BITS;
149 if (v_l1_bits < V_L1_MIN_BITS) {
150 v_l1_bits += V_L2_BITS;
153 v_l1_size = 1 << v_l1_bits;
154 v_l1_shift = L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - v_l1_bits;
155 v_l2_levels = v_l1_shift / V_L2_BITS - 1;
157 assert(v_l1_bits <= V_L1_MAX_BITS);
158 assert(v_l1_shift % V_L2_BITS == 0);
159 assert(v_l2_levels >= 0);
162 #define assert_tb_locked() tcg_debug_assert(have_tb_lock)
163 #define assert_tb_unlocked() tcg_debug_assert(!have_tb_lock)
165 void tb_lock(void)
167 assert_tb_unlocked();
168 qemu_mutex_lock(&tcg_ctx.tb_ctx.tb_lock);
169 have_tb_lock++;
172 void tb_unlock(void)
174 assert_tb_locked();
175 have_tb_lock--;
176 qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
179 void tb_lock_reset(void)
181 if (have_tb_lock) {
182 qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
183 have_tb_lock = 0;
187 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr);
189 void cpu_gen_init(void)
191 tcg_context_init(&tcg_ctx);
194 /* Encode VAL as a signed leb128 sequence at P.
195 Return P incremented past the encoded value. */
196 static uint8_t *encode_sleb128(uint8_t *p, target_long val)
198 int more, byte;
200 do {
201 byte = val & 0x7f;
202 val >>= 7;
203 more = !((val == 0 && (byte & 0x40) == 0)
204 || (val == -1 && (byte & 0x40) != 0));
205 if (more) {
206 byte |= 0x80;
208 *p++ = byte;
209 } while (more);
211 return p;
214 /* Decode a signed leb128 sequence at *PP; increment *PP past the
215 decoded value. Return the decoded value. */
216 static target_long decode_sleb128(uint8_t **pp)
218 uint8_t *p = *pp;
219 target_long val = 0;
220 int byte, shift = 0;
222 do {
223 byte = *p++;
224 val |= (target_ulong)(byte & 0x7f) << shift;
225 shift += 7;
226 } while (byte & 0x80);
227 if (shift < TARGET_LONG_BITS && (byte & 0x40)) {
228 val |= -(target_ulong)1 << shift;
231 *pp = p;
232 return val;
235 /* Encode the data collected about the instructions while compiling TB.
236 Place the data at BLOCK, and return the number of bytes consumed.
238 The logical table consisits of TARGET_INSN_START_WORDS target_ulong's,
239 which come from the target's insn_start data, followed by a uintptr_t
240 which comes from the host pc of the end of the code implementing the insn.
242 Each line of the table is encoded as sleb128 deltas from the previous
243 line. The seed for the first line is { tb->pc, 0..., tb->tc_ptr }.
244 That is, the first column is seeded with the guest pc, the last column
245 with the host pc, and the middle columns with zeros. */
247 static int encode_search(TranslationBlock *tb, uint8_t *block)
249 uint8_t *highwater = tcg_ctx.code_gen_highwater;
250 uint8_t *p = block;
251 int i, j, n;
253 tb->tc_search = block;
255 for (i = 0, n = tb->icount; i < n; ++i) {
256 target_ulong prev;
258 for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
259 if (i == 0) {
260 prev = (j == 0 ? tb->pc : 0);
261 } else {
262 prev = tcg_ctx.gen_insn_data[i - 1][j];
264 p = encode_sleb128(p, tcg_ctx.gen_insn_data[i][j] - prev);
266 prev = (i == 0 ? 0 : tcg_ctx.gen_insn_end_off[i - 1]);
267 p = encode_sleb128(p, tcg_ctx.gen_insn_end_off[i] - prev);
269 /* Test for (pending) buffer overflow. The assumption is that any
270 one row beginning below the high water mark cannot overrun
271 the buffer completely. Thus we can test for overflow after
272 encoding a row without having to check during encoding. */
273 if (unlikely(p > highwater)) {
274 return -1;
278 return p - block;
281 /* The cpu state corresponding to 'searched_pc' is restored.
282 * Called with tb_lock held.
284 static int cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
285 uintptr_t searched_pc)
287 target_ulong data[TARGET_INSN_START_WORDS] = { tb->pc };
288 uintptr_t host_pc = (uintptr_t)tb->tc_ptr;
289 CPUArchState *env = cpu->env_ptr;
290 uint8_t *p = tb->tc_search;
291 int i, j, num_insns = tb->icount;
292 #ifdef CONFIG_PROFILER
293 int64_t ti = profile_getclock();
294 #endif
296 searched_pc -= GETPC_ADJ;
298 if (searched_pc < host_pc) {
299 return -1;
302 /* Reconstruct the stored insn data while looking for the point at
303 which the end of the insn exceeds the searched_pc. */
304 for (i = 0; i < num_insns; ++i) {
305 for (j = 0; j < TARGET_INSN_START_WORDS; ++j) {
306 data[j] += decode_sleb128(&p);
308 host_pc += decode_sleb128(&p);
309 if (host_pc > searched_pc) {
310 goto found;
313 return -1;
315 found:
316 if (tb->cflags & CF_USE_ICOUNT) {
317 assert(use_icount);
318 /* Reset the cycle counter to the start of the block. */
319 cpu->icount_decr.u16.low += num_insns;
320 /* Clear the IO flag. */
321 cpu->can_do_io = 0;
323 cpu->icount_decr.u16.low -= i;
324 restore_state_to_opc(env, tb, data);
326 #ifdef CONFIG_PROFILER
327 tcg_ctx.restore_time += profile_getclock() - ti;
328 tcg_ctx.restore_count++;
329 #endif
330 return 0;
333 bool cpu_restore_state(CPUState *cpu, uintptr_t retaddr)
335 TranslationBlock *tb;
336 bool r = false;
338 /* A retaddr of zero is invalid so we really shouldn't have ended
339 * up here. The target code has likely forgotten to check retaddr
340 * != 0 before attempting to restore state. We return early to
341 * avoid blowing up on a recursive tb_lock(). The target must have
342 * previously survived a failed cpu_restore_state because
343 * tb_find_pc(0) would have failed anyway. It still should be
344 * fixed though.
347 if (!retaddr) {
348 return r;
351 tb_lock();
352 tb = tb_find_pc(retaddr);
353 if (tb) {
354 cpu_restore_state_from_tb(cpu, tb, retaddr);
355 if (tb->cflags & CF_NOCACHE) {
356 /* one-shot translation, invalidate it immediately */
357 tb_phys_invalidate(tb, -1);
358 tb_free(tb);
360 r = true;
362 tb_unlock();
364 return r;
367 void page_size_init(void)
369 /* NOTE: we can always suppose that qemu_host_page_size >=
370 TARGET_PAGE_SIZE */
371 qemu_real_host_page_size = getpagesize();
372 qemu_real_host_page_mask = -(intptr_t)qemu_real_host_page_size;
373 if (qemu_host_page_size == 0) {
374 qemu_host_page_size = qemu_real_host_page_size;
376 if (qemu_host_page_size < TARGET_PAGE_SIZE) {
377 qemu_host_page_size = TARGET_PAGE_SIZE;
379 qemu_host_page_mask = -(intptr_t)qemu_host_page_size;
382 static void page_init(void)
384 page_size_init();
385 page_table_config_init();
387 #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
389 #ifdef HAVE_KINFO_GETVMMAP
390 struct kinfo_vmentry *freep;
391 int i, cnt;
393 freep = kinfo_getvmmap(getpid(), &cnt);
394 if (freep) {
395 mmap_lock();
396 for (i = 0; i < cnt; i++) {
397 unsigned long startaddr, endaddr;
399 startaddr = freep[i].kve_start;
400 endaddr = freep[i].kve_end;
401 if (h2g_valid(startaddr)) {
402 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
404 if (h2g_valid(endaddr)) {
405 endaddr = h2g(endaddr);
406 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
407 } else {
408 #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
409 endaddr = ~0ul;
410 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
411 #endif
415 free(freep);
416 mmap_unlock();
418 #else
419 FILE *f;
421 last_brk = (unsigned long)sbrk(0);
423 f = fopen("/compat/linux/proc/self/maps", "r");
424 if (f) {
425 mmap_lock();
427 do {
428 unsigned long startaddr, endaddr;
429 int n;
431 n = fscanf(f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
433 if (n == 2 && h2g_valid(startaddr)) {
434 startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
436 if (h2g_valid(endaddr)) {
437 endaddr = h2g(endaddr);
438 } else {
439 endaddr = ~0ul;
441 page_set_flags(startaddr, endaddr, PAGE_RESERVED);
443 } while (!feof(f));
445 fclose(f);
446 mmap_unlock();
448 #endif
450 #endif
453 /* If alloc=1:
454 * Called with tb_lock held for system emulation.
455 * Called with mmap_lock held for user-mode emulation.
457 static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
459 PageDesc *pd;
460 void **lp;
461 int i;
463 if (alloc) {
464 assert_memory_lock();
467 /* Level 1. Always allocated. */
468 lp = l1_map + ((index >> v_l1_shift) & (v_l1_size - 1));
470 /* Level 2..N-1. */
471 for (i = v_l2_levels; i > 0; i--) {
472 void **p = atomic_rcu_read(lp);
474 if (p == NULL) {
475 if (!alloc) {
476 return NULL;
478 p = g_new0(void *, V_L2_SIZE);
479 atomic_rcu_set(lp, p);
482 lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
485 pd = atomic_rcu_read(lp);
486 if (pd == NULL) {
487 if (!alloc) {
488 return NULL;
490 pd = g_new0(PageDesc, V_L2_SIZE);
491 atomic_rcu_set(lp, pd);
494 return pd + (index & (V_L2_SIZE - 1));
497 static inline PageDesc *page_find(tb_page_addr_t index)
499 return page_find_alloc(index, 0);
502 #if defined(CONFIG_USER_ONLY)
503 /* Currently it is not recommended to allocate big chunks of data in
504 user mode. It will change when a dedicated libc will be used. */
505 /* ??? 64-bit hosts ought to have no problem mmaping data outside the
506 region in which the guest needs to run. Revisit this. */
507 #define USE_STATIC_CODE_GEN_BUFFER
508 #endif
510 /* Minimum size of the code gen buffer. This number is randomly chosen,
511 but not so small that we can't have a fair number of TB's live. */
512 #define MIN_CODE_GEN_BUFFER_SIZE (1024u * 1024)
514 /* Maximum size of the code gen buffer we'd like to use. Unless otherwise
515 indicated, this is constrained by the range of direct branches on the
516 host cpu, as used by the TCG implementation of goto_tb. */
517 #if defined(__x86_64__)
518 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
519 #elif defined(__sparc__)
520 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
521 #elif defined(__powerpc64__)
522 # define MAX_CODE_GEN_BUFFER_SIZE (2ul * 1024 * 1024 * 1024)
523 #elif defined(__powerpc__)
524 # define MAX_CODE_GEN_BUFFER_SIZE (32u * 1024 * 1024)
525 #elif defined(__aarch64__)
526 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
527 #elif defined(__s390x__)
528 /* We have a +- 4GB range on the branches; leave some slop. */
529 # define MAX_CODE_GEN_BUFFER_SIZE (3ul * 1024 * 1024 * 1024)
530 #elif defined(__mips__)
531 /* We have a 256MB branch region, but leave room to make sure the
532 main executable is also within that region. */
533 # define MAX_CODE_GEN_BUFFER_SIZE (128ul * 1024 * 1024)
534 #else
535 # define MAX_CODE_GEN_BUFFER_SIZE ((size_t)-1)
536 #endif
538 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32u * 1024 * 1024)
540 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
541 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
542 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
544 static inline size_t size_code_gen_buffer(size_t tb_size)
546 /* Size the buffer. */
547 if (tb_size == 0) {
548 #ifdef USE_STATIC_CODE_GEN_BUFFER
549 tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
550 #else
551 /* ??? Needs adjustments. */
552 /* ??? If we relax the requirement that CONFIG_USER_ONLY use the
553 static buffer, we could size this on RESERVED_VA, on the text
554 segment size of the executable, or continue to use the default. */
555 tb_size = (unsigned long)(ram_size / 4);
556 #endif
558 if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
559 tb_size = MIN_CODE_GEN_BUFFER_SIZE;
561 if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
562 tb_size = MAX_CODE_GEN_BUFFER_SIZE;
564 return tb_size;
567 #ifdef __mips__
568 /* In order to use J and JAL within the code_gen_buffer, we require
569 that the buffer not cross a 256MB boundary. */
570 static inline bool cross_256mb(void *addr, size_t size)
572 return ((uintptr_t)addr ^ ((uintptr_t)addr + size)) & ~0x0ffffffful;
575 /* We weren't able to allocate a buffer without crossing that boundary,
576 so make do with the larger portion of the buffer that doesn't cross.
577 Returns the new base of the buffer, and adjusts code_gen_buffer_size. */
578 static inline void *split_cross_256mb(void *buf1, size_t size1)
580 void *buf2 = (void *)(((uintptr_t)buf1 + size1) & ~0x0ffffffful);
581 size_t size2 = buf1 + size1 - buf2;
583 size1 = buf2 - buf1;
584 if (size1 < size2) {
585 size1 = size2;
586 buf1 = buf2;
589 tcg_ctx.code_gen_buffer_size = size1;
590 return buf1;
592 #endif
594 #ifdef USE_STATIC_CODE_GEN_BUFFER
595 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
596 __attribute__((aligned(CODE_GEN_ALIGN)));
598 # ifdef _WIN32
599 static inline void do_protect(void *addr, long size, int prot)
601 DWORD old_protect;
602 VirtualProtect(addr, size, prot, &old_protect);
605 static inline void map_exec(void *addr, long size)
607 do_protect(addr, size, PAGE_EXECUTE_READWRITE);
610 static inline void map_none(void *addr, long size)
612 do_protect(addr, size, PAGE_NOACCESS);
614 # else
615 static inline void do_protect(void *addr, long size, int prot)
617 uintptr_t start, end;
619 start = (uintptr_t)addr;
620 start &= qemu_real_host_page_mask;
622 end = (uintptr_t)addr + size;
623 end = ROUND_UP(end, qemu_real_host_page_size);
625 mprotect((void *)start, end - start, prot);
628 static inline void map_exec(void *addr, long size)
630 do_protect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
633 static inline void map_none(void *addr, long size)
635 do_protect(addr, size, PROT_NONE);
637 # endif /* WIN32 */
639 static inline void *alloc_code_gen_buffer(void)
641 void *buf = static_code_gen_buffer;
642 size_t full_size, size;
644 /* The size of the buffer, rounded down to end on a page boundary. */
645 full_size = (((uintptr_t)buf + sizeof(static_code_gen_buffer))
646 & qemu_real_host_page_mask) - (uintptr_t)buf;
648 /* Reserve a guard page. */
649 size = full_size - qemu_real_host_page_size;
651 /* Honor a command-line option limiting the size of the buffer. */
652 if (size > tcg_ctx.code_gen_buffer_size) {
653 size = (((uintptr_t)buf + tcg_ctx.code_gen_buffer_size)
654 & qemu_real_host_page_mask) - (uintptr_t)buf;
656 tcg_ctx.code_gen_buffer_size = size;
658 #ifdef __mips__
659 if (cross_256mb(buf, size)) {
660 buf = split_cross_256mb(buf, size);
661 size = tcg_ctx.code_gen_buffer_size;
663 #endif
665 map_exec(buf, size);
666 map_none(buf + size, qemu_real_host_page_size);
667 qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
669 return buf;
671 #elif defined(_WIN32)
672 static inline void *alloc_code_gen_buffer(void)
674 size_t size = tcg_ctx.code_gen_buffer_size;
675 void *buf1, *buf2;
677 /* Perform the allocation in two steps, so that the guard page
678 is reserved but uncommitted. */
679 buf1 = VirtualAlloc(NULL, size + qemu_real_host_page_size,
680 MEM_RESERVE, PAGE_NOACCESS);
681 if (buf1 != NULL) {
682 buf2 = VirtualAlloc(buf1, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
683 assert(buf1 == buf2);
686 return buf1;
688 #else
689 static inline void *alloc_code_gen_buffer(void)
691 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
692 uintptr_t start = 0;
693 size_t size = tcg_ctx.code_gen_buffer_size;
694 void *buf;
696 /* Constrain the position of the buffer based on the host cpu.
697 Note that these addresses are chosen in concert with the
698 addresses assigned in the relevant linker script file. */
699 # if defined(__PIE__) || defined(__PIC__)
700 /* Don't bother setting a preferred location if we're building
701 a position-independent executable. We're more likely to get
702 an address near the main executable if we let the kernel
703 choose the address. */
704 # elif defined(__x86_64__) && defined(MAP_32BIT)
705 /* Force the memory down into low memory with the executable.
706 Leave the choice of exact location with the kernel. */
707 flags |= MAP_32BIT;
708 /* Cannot expect to map more than 800MB in low memory. */
709 if (size > 800u * 1024 * 1024) {
710 tcg_ctx.code_gen_buffer_size = size = 800u * 1024 * 1024;
712 # elif defined(__sparc__)
713 start = 0x40000000ul;
714 # elif defined(__s390x__)
715 start = 0x90000000ul;
716 # elif defined(__mips__)
717 # if _MIPS_SIM == _ABI64
718 start = 0x128000000ul;
719 # else
720 start = 0x08000000ul;
721 # endif
722 # endif
724 buf = mmap((void *)start, size + qemu_real_host_page_size,
725 PROT_NONE, flags, -1, 0);
726 if (buf == MAP_FAILED) {
727 return NULL;
730 #ifdef __mips__
731 if (cross_256mb(buf, size)) {
732 /* Try again, with the original still mapped, to avoid re-acquiring
733 that 256mb crossing. This time don't specify an address. */
734 size_t size2;
735 void *buf2 = mmap(NULL, size + qemu_real_host_page_size,
736 PROT_NONE, flags, -1, 0);
737 switch ((int)(buf2 != MAP_FAILED)) {
738 case 1:
739 if (!cross_256mb(buf2, size)) {
740 /* Success! Use the new buffer. */
741 munmap(buf, size + qemu_real_host_page_size);
742 break;
744 /* Failure. Work with what we had. */
745 munmap(buf2, size + qemu_real_host_page_size);
746 /* fallthru */
747 default:
748 /* Split the original buffer. Free the smaller half. */
749 buf2 = split_cross_256mb(buf, size);
750 size2 = tcg_ctx.code_gen_buffer_size;
751 if (buf == buf2) {
752 munmap(buf + size2 + qemu_real_host_page_size, size - size2);
753 } else {
754 munmap(buf, size - size2);
756 size = size2;
757 break;
759 buf = buf2;
761 #endif
763 /* Make the final buffer accessible. The guard page at the end
764 will remain inaccessible with PROT_NONE. */
765 mprotect(buf, size, PROT_WRITE | PROT_READ | PROT_EXEC);
767 /* Request large pages for the buffer. */
768 qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
770 return buf;
772 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
774 static inline void code_gen_alloc(size_t tb_size)
776 tcg_ctx.code_gen_buffer_size = size_code_gen_buffer(tb_size);
777 tcg_ctx.code_gen_buffer = alloc_code_gen_buffer();
778 if (tcg_ctx.code_gen_buffer == NULL) {
779 fprintf(stderr, "Could not allocate dynamic translator buffer\n");
780 exit(1);
783 /* size this conservatively -- realloc later if needed */
784 tcg_ctx.tb_ctx.tbs_size =
785 tcg_ctx.code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE / 8;
786 if (unlikely(!tcg_ctx.tb_ctx.tbs_size)) {
787 tcg_ctx.tb_ctx.tbs_size = 64 * 1024;
789 tcg_ctx.tb_ctx.tbs = g_new(TranslationBlock *, tcg_ctx.tb_ctx.tbs_size);
791 qemu_mutex_init(&tcg_ctx.tb_ctx.tb_lock);
794 static void tb_htable_init(void)
796 unsigned int mode = QHT_MODE_AUTO_RESIZE;
798 qht_init(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE, mode);
801 /* Must be called before using the QEMU cpus. 'tb_size' is the size
802 (in bytes) allocated to the translation buffer. Zero means default
803 size. */
804 void tcg_exec_init(uintptr_t tb_size)
806 cpu_gen_init();
807 page_init();
808 tb_htable_init();
809 code_gen_alloc(tb_size);
810 #if defined(CONFIG_SOFTMMU)
811 /* There's no guest base to take into account, so go ahead and
812 initialize the prologue now. */
813 tcg_prologue_init(&tcg_ctx);
814 #endif
817 bool tcg_enabled(void)
819 return tcg_ctx.code_gen_buffer != NULL;
823 * Allocate a new translation block. Flush the translation buffer if
824 * too many translation blocks or too much generated code.
826 * Called with tb_lock held.
828 static TranslationBlock *tb_alloc(target_ulong pc)
830 TranslationBlock *tb;
831 TBContext *ctx;
833 assert_tb_locked();
835 tb = tcg_tb_alloc(&tcg_ctx);
836 if (unlikely(tb == NULL)) {
837 return NULL;
839 ctx = &tcg_ctx.tb_ctx;
840 if (unlikely(ctx->nb_tbs == ctx->tbs_size)) {
841 ctx->tbs_size *= 2;
842 ctx->tbs = g_renew(TranslationBlock *, ctx->tbs, ctx->tbs_size);
844 ctx->tbs[ctx->nb_tbs++] = tb;
845 return tb;
848 /* Called with tb_lock held. */
849 void tb_free(TranslationBlock *tb)
851 assert_tb_locked();
853 /* In practice this is mostly used for single use temporary TB
854 Ignore the hard cases and just back up if this TB happens to
855 be the last one generated. */
856 if (tcg_ctx.tb_ctx.nb_tbs > 0 &&
857 tb == tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) {
858 size_t struct_size = ROUND_UP(sizeof(*tb), qemu_icache_linesize);
860 tcg_ctx.code_gen_ptr = tb->tc_ptr - struct_size;
861 tcg_ctx.tb_ctx.nb_tbs--;
865 static inline void invalidate_page_bitmap(PageDesc *p)
867 #ifdef CONFIG_SOFTMMU
868 g_free(p->code_bitmap);
869 p->code_bitmap = NULL;
870 p->code_write_count = 0;
871 #endif
874 /* Set to NULL all the 'first_tb' fields in all PageDescs. */
875 static void page_flush_tb_1(int level, void **lp)
877 int i;
879 if (*lp == NULL) {
880 return;
882 if (level == 0) {
883 PageDesc *pd = *lp;
885 for (i = 0; i < V_L2_SIZE; ++i) {
886 pd[i].first_tb = NULL;
887 invalidate_page_bitmap(pd + i);
889 } else {
890 void **pp = *lp;
892 for (i = 0; i < V_L2_SIZE; ++i) {
893 page_flush_tb_1(level - 1, pp + i);
898 static void page_flush_tb(void)
900 int i, l1_sz = v_l1_size;
902 for (i = 0; i < l1_sz; i++) {
903 page_flush_tb_1(v_l2_levels, l1_map + i);
907 /* flush all the translation blocks */
908 static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
910 tb_lock();
912 /* If it is already been done on request of another CPU,
913 * just retry.
915 if (tcg_ctx.tb_ctx.tb_flush_count != tb_flush_count.host_int) {
916 goto done;
919 #if defined(DEBUG_TB_FLUSH)
920 printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
921 (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer),
922 tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.tb_ctx.nb_tbs > 0 ?
923 ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)) /
924 tcg_ctx.tb_ctx.nb_tbs : 0);
925 #endif
926 if ((unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer)
927 > tcg_ctx.code_gen_buffer_size) {
928 cpu_abort(cpu, "Internal error: code buffer overflow\n");
931 CPU_FOREACH(cpu) {
932 int i;
934 for (i = 0; i < TB_JMP_CACHE_SIZE; ++i) {
935 atomic_set(&cpu->tb_jmp_cache[i], NULL);
939 tcg_ctx.tb_ctx.nb_tbs = 0;
940 qht_reset_size(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
941 page_flush_tb();
943 tcg_ctx.code_gen_ptr = tcg_ctx.code_gen_buffer;
944 /* XXX: flush processor icache at this point if cache flush is
945 expensive */
946 atomic_mb_set(&tcg_ctx.tb_ctx.tb_flush_count,
947 tcg_ctx.tb_ctx.tb_flush_count + 1);
949 done:
950 tb_unlock();
953 void tb_flush(CPUState *cpu)
955 if (tcg_enabled()) {
956 unsigned tb_flush_count = atomic_mb_read(&tcg_ctx.tb_ctx.tb_flush_count);
957 async_safe_run_on_cpu(cpu, do_tb_flush,
958 RUN_ON_CPU_HOST_INT(tb_flush_count));
962 #ifdef DEBUG_TB_CHECK
964 static void
965 do_tb_invalidate_check(struct qht *ht, void *p, uint32_t hash, void *userp)
967 TranslationBlock *tb = p;
968 target_ulong addr = *(target_ulong *)userp;
970 if (!(addr + TARGET_PAGE_SIZE <= tb->pc || addr >= tb->pc + tb->size)) {
971 printf("ERROR invalidate: address=" TARGET_FMT_lx
972 " PC=%08lx size=%04x\n", addr, (long)tb->pc, tb->size);
976 /* verify that all the pages have correct rights for code
978 * Called with tb_lock held.
980 static void tb_invalidate_check(target_ulong address)
982 address &= TARGET_PAGE_MASK;
983 qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_invalidate_check, &address);
986 static void
987 do_tb_page_check(struct qht *ht, void *p, uint32_t hash, void *userp)
989 TranslationBlock *tb = p;
990 int flags1, flags2;
992 flags1 = page_get_flags(tb->pc);
993 flags2 = page_get_flags(tb->pc + tb->size - 1);
994 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
995 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
996 (long)tb->pc, tb->size, flags1, flags2);
1000 /* verify that all the pages have correct rights for code */
1001 static void tb_page_check(void)
1003 qht_iter(&tcg_ctx.tb_ctx.htable, do_tb_page_check, NULL);
1006 #endif
1008 static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
1010 TranslationBlock *tb1;
1011 unsigned int n1;
1013 for (;;) {
1014 tb1 = *ptb;
1015 n1 = (uintptr_t)tb1 & 3;
1016 tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
1017 if (tb1 == tb) {
1018 *ptb = tb1->page_next[n1];
1019 break;
1021 ptb = &tb1->page_next[n1];
1025 /* remove the TB from a list of TBs jumping to the n-th jump target of the TB */
1026 static inline void tb_remove_from_jmp_list(TranslationBlock *tb, int n)
1028 TranslationBlock *tb1;
1029 uintptr_t *ptb, ntb;
1030 unsigned int n1;
1032 ptb = &tb->jmp_list_next[n];
1033 if (*ptb) {
1034 /* find tb(n) in circular list */
1035 for (;;) {
1036 ntb = *ptb;
1037 n1 = ntb & 3;
1038 tb1 = (TranslationBlock *)(ntb & ~3);
1039 if (n1 == n && tb1 == tb) {
1040 break;
1042 if (n1 == 2) {
1043 ptb = &tb1->jmp_list_first;
1044 } else {
1045 ptb = &tb1->jmp_list_next[n1];
1048 /* now we can suppress tb(n) from the list */
1049 *ptb = tb->jmp_list_next[n];
1051 tb->jmp_list_next[n] = (uintptr_t)NULL;
1055 /* reset the jump entry 'n' of a TB so that it is not chained to
1056 another TB */
1057 static inline void tb_reset_jump(TranslationBlock *tb, int n)
1059 uintptr_t addr = (uintptr_t)(tb->tc_ptr + tb->jmp_reset_offset[n]);
1060 tb_set_jmp_target(tb, n, addr);
1063 /* remove any jumps to the TB */
1064 static inline void tb_jmp_unlink(TranslationBlock *tb)
1066 TranslationBlock *tb1;
1067 uintptr_t *ptb, ntb;
1068 unsigned int n1;
1070 ptb = &tb->jmp_list_first;
1071 for (;;) {
1072 ntb = *ptb;
1073 n1 = ntb & 3;
1074 tb1 = (TranslationBlock *)(ntb & ~3);
1075 if (n1 == 2) {
1076 break;
1078 tb_reset_jump(tb1, n1);
1079 *ptb = tb1->jmp_list_next[n1];
1080 tb1->jmp_list_next[n1] = (uintptr_t)NULL;
1084 /* invalidate one TB
1086 * Called with tb_lock held.
1088 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
1090 CPUState *cpu;
1091 PageDesc *p;
1092 uint32_t h;
1093 tb_page_addr_t phys_pc;
1095 assert_tb_locked();
1097 atomic_set(&tb->invalid, true);
1099 /* remove the TB from the hash list */
1100 phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1101 h = tb_hash_func(phys_pc, tb->pc, tb->flags);
1102 qht_remove(&tcg_ctx.tb_ctx.htable, tb, h);
1104 /* remove the TB from the page list */
1105 if (tb->page_addr[0] != page_addr) {
1106 p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
1107 tb_page_remove(&p->first_tb, tb);
1108 invalidate_page_bitmap(p);
1110 if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
1111 p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
1112 tb_page_remove(&p->first_tb, tb);
1113 invalidate_page_bitmap(p);
1116 /* remove the TB from the hash list */
1117 h = tb_jmp_cache_hash_func(tb->pc);
1118 CPU_FOREACH(cpu) {
1119 if (atomic_read(&cpu->tb_jmp_cache[h]) == tb) {
1120 atomic_set(&cpu->tb_jmp_cache[h], NULL);
1124 /* suppress this TB from the two jump lists */
1125 tb_remove_from_jmp_list(tb, 0);
1126 tb_remove_from_jmp_list(tb, 1);
1128 /* suppress any remaining jumps to this TB */
1129 tb_jmp_unlink(tb);
1131 tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
1134 #ifdef CONFIG_SOFTMMU
1135 static void build_page_bitmap(PageDesc *p)
1137 int n, tb_start, tb_end;
1138 TranslationBlock *tb;
1140 p->code_bitmap = bitmap_new(TARGET_PAGE_SIZE);
1142 tb = p->first_tb;
1143 while (tb != NULL) {
1144 n = (uintptr_t)tb & 3;
1145 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1146 /* NOTE: this is subtle as a TB may span two physical pages */
1147 if (n == 0) {
1148 /* NOTE: tb_end may be after the end of the page, but
1149 it is not a problem */
1150 tb_start = tb->pc & ~TARGET_PAGE_MASK;
1151 tb_end = tb_start + tb->size;
1152 if (tb_end > TARGET_PAGE_SIZE) {
1153 tb_end = TARGET_PAGE_SIZE;
1155 } else {
1156 tb_start = 0;
1157 tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1159 bitmap_set(p->code_bitmap, tb_start, tb_end - tb_start);
1160 tb = tb->page_next[n];
1163 #endif
1165 /* add the tb in the target page and protect it if necessary
1167 * Called with mmap_lock held for user-mode emulation.
1169 static inline void tb_alloc_page(TranslationBlock *tb,
1170 unsigned int n, tb_page_addr_t page_addr)
1172 PageDesc *p;
1173 #ifndef CONFIG_USER_ONLY
1174 bool page_already_protected;
1175 #endif
1177 assert_memory_lock();
1179 tb->page_addr[n] = page_addr;
1180 p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
1181 tb->page_next[n] = p->first_tb;
1182 #ifndef CONFIG_USER_ONLY
1183 page_already_protected = p->first_tb != NULL;
1184 #endif
1185 p->first_tb = (TranslationBlock *)((uintptr_t)tb | n);
1186 invalidate_page_bitmap(p);
1188 #if defined(CONFIG_USER_ONLY)
1189 if (p->flags & PAGE_WRITE) {
1190 target_ulong addr;
1191 PageDesc *p2;
1192 int prot;
1194 /* force the host page as non writable (writes will have a
1195 page fault + mprotect overhead) */
1196 page_addr &= qemu_host_page_mask;
1197 prot = 0;
1198 for (addr = page_addr; addr < page_addr + qemu_host_page_size;
1199 addr += TARGET_PAGE_SIZE) {
1201 p2 = page_find(addr >> TARGET_PAGE_BITS);
1202 if (!p2) {
1203 continue;
1205 prot |= p2->flags;
1206 p2->flags &= ~PAGE_WRITE;
1208 mprotect(g2h(page_addr), qemu_host_page_size,
1209 (prot & PAGE_BITS) & ~PAGE_WRITE);
1210 #ifdef DEBUG_TB_INVALIDATE
1211 printf("protecting code page: 0x" TARGET_FMT_lx "\n",
1212 page_addr);
1213 #endif
1215 #else
1216 /* if some code is already present, then the pages are already
1217 protected. So we handle the case where only the first TB is
1218 allocated in a physical page */
1219 if (!page_already_protected) {
1220 tlb_protect_code(page_addr);
1222 #endif
1225 /* add a new TB and link it to the physical page tables. phys_page2 is
1226 * (-1) to indicate that only one page contains the TB.
1228 * Called with mmap_lock held for user-mode emulation.
1230 static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,
1231 tb_page_addr_t phys_page2)
1233 uint32_t h;
1235 assert_memory_lock();
1237 /* add in the page list */
1238 tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
1239 if (phys_page2 != -1) {
1240 tb_alloc_page(tb, 1, phys_page2);
1241 } else {
1242 tb->page_addr[1] = -1;
1245 /* add in the hash table */
1246 h = tb_hash_func(phys_pc, tb->pc, tb->flags);
1247 qht_insert(&tcg_ctx.tb_ctx.htable, tb, h);
1249 #ifdef DEBUG_TB_CHECK
1250 tb_page_check();
1251 #endif
1254 /* Called with mmap_lock held for user mode emulation. */
1255 TranslationBlock *tb_gen_code(CPUState *cpu,
1256 target_ulong pc, target_ulong cs_base,
1257 uint32_t flags, int cflags)
1259 CPUArchState *env = cpu->env_ptr;
1260 TranslationBlock *tb;
1261 tb_page_addr_t phys_pc, phys_page2;
1262 target_ulong virt_page2;
1263 tcg_insn_unit *gen_code_buf;
1264 int gen_code_size, search_size;
1265 #ifdef CONFIG_PROFILER
1266 int64_t ti;
1267 #endif
1268 assert_memory_lock();
1270 phys_pc = get_page_addr_code(env, pc);
1271 if (use_icount && !(cflags & CF_IGNORE_ICOUNT)) {
1272 cflags |= CF_USE_ICOUNT;
1275 tb = tb_alloc(pc);
1276 if (unlikely(!tb)) {
1277 buffer_overflow:
1278 /* flush must be done */
1279 tb_flush(cpu);
1280 mmap_unlock();
1281 /* Make the execution loop process the flush as soon as possible. */
1282 cpu->exception_index = EXCP_INTERRUPT;
1283 cpu_loop_exit(cpu);
1286 gen_code_buf = tcg_ctx.code_gen_ptr;
1287 tb->tc_ptr = gen_code_buf;
1288 tb->pc = pc;
1289 tb->cs_base = cs_base;
1290 tb->flags = flags;
1291 tb->cflags = cflags;
1292 tb->invalid = false;
1294 #ifdef CONFIG_PROFILER
1295 tcg_ctx.tb_count1++; /* includes aborted translations because of
1296 exceptions */
1297 ti = profile_getclock();
1298 #endif
1300 tcg_func_start(&tcg_ctx);
1302 tcg_ctx.cpu = ENV_GET_CPU(env);
1303 gen_intermediate_code(env, tb);
1304 tcg_ctx.cpu = NULL;
1306 trace_translate_block(tb, tb->pc, tb->tc_ptr);
1308 /* generate machine code */
1309 tb->jmp_reset_offset[0] = TB_JMP_RESET_OFFSET_INVALID;
1310 tb->jmp_reset_offset[1] = TB_JMP_RESET_OFFSET_INVALID;
1311 tcg_ctx.tb_jmp_reset_offset = tb->jmp_reset_offset;
1312 #ifdef USE_DIRECT_JUMP
1313 tcg_ctx.tb_jmp_insn_offset = tb->jmp_insn_offset;
1314 tcg_ctx.tb_jmp_target_addr = NULL;
1315 #else
1316 tcg_ctx.tb_jmp_insn_offset = NULL;
1317 tcg_ctx.tb_jmp_target_addr = tb->jmp_target_addr;
1318 #endif
1320 #ifdef CONFIG_PROFILER
1321 tcg_ctx.tb_count++;
1322 tcg_ctx.interm_time += profile_getclock() - ti;
1323 tcg_ctx.code_time -= profile_getclock();
1324 #endif
1326 /* ??? Overflow could be handled better here. In particular, we
1327 don't need to re-do gen_intermediate_code, nor should we re-do
1328 the tcg optimization currently hidden inside tcg_gen_code. All
1329 that should be required is to flush the TBs, allocate a new TB,
1330 re-initialize it per above, and re-do the actual code generation. */
1331 gen_code_size = tcg_gen_code(&tcg_ctx, tb);
1332 if (unlikely(gen_code_size < 0)) {
1333 goto buffer_overflow;
1335 search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size);
1336 if (unlikely(search_size < 0)) {
1337 goto buffer_overflow;
1340 #ifdef CONFIG_PROFILER
1341 tcg_ctx.code_time += profile_getclock();
1342 tcg_ctx.code_in_len += tb->size;
1343 tcg_ctx.code_out_len += gen_code_size;
1344 tcg_ctx.search_out_len += search_size;
1345 #endif
1347 #ifdef DEBUG_DISAS
1348 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
1349 qemu_log_in_addr_range(tb->pc)) {
1350 qemu_log_lock();
1351 qemu_log("OUT: [size=%d]\n", gen_code_size);
1352 log_disas(tb->tc_ptr, gen_code_size);
1353 qemu_log("\n");
1354 qemu_log_flush();
1355 qemu_log_unlock();
1357 #endif
1359 tcg_ctx.code_gen_ptr = (void *)
1360 ROUND_UP((uintptr_t)gen_code_buf + gen_code_size + search_size,
1361 CODE_GEN_ALIGN);
1363 #if defined(CONFIG_USER_ONLY) && defined(TARGET_X86_64)
1364 /* if we are doing vsyscall don't link the page as it lies in high memory
1365 and tb_alloc_page will abort due to page_l1_map returning NULL */
1366 if (unlikely(phys_pc >= TARGET_VSYSCALL_START
1367 && phys_pc < TARGET_VSYSCALL_END))
1368 return tb;
1369 #endif
1371 /* init jump list */
1372 assert(((uintptr_t)tb & 3) == 0);
1373 tb->jmp_list_first = (uintptr_t)tb | 2;
1374 tb->jmp_list_next[0] = (uintptr_t)NULL;
1375 tb->jmp_list_next[1] = (uintptr_t)NULL;
1377 /* init original jump addresses wich has been set during tcg_gen_code() */
1378 if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) {
1379 tb_reset_jump(tb, 0);
1381 if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) {
1382 tb_reset_jump(tb, 1);
1385 /* check next page if needed */
1386 virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
1387 phys_page2 = -1;
1388 if ((pc & TARGET_PAGE_MASK) != virt_page2) {
1389 phys_page2 = get_page_addr_code(env, virt_page2);
1391 /* As long as consistency of the TB stuff is provided by tb_lock in user
1392 * mode and is implicit in single-threaded softmmu emulation, no explicit
1393 * memory barrier is required before tb_link_page() makes the TB visible
1394 * through the physical hash table and physical page list.
1396 tb_link_page(tb, phys_pc, phys_page2);
1397 return tb;
1401 * Invalidate all TBs which intersect with the target physical address range
1402 * [start;end[. NOTE: start and end may refer to *different* physical pages.
1403 * 'is_cpu_write_access' should be true if called from a real cpu write
1404 * access: the virtual CPU will exit the current TB if code is modified inside
1405 * this TB.
1407 * Called with mmap_lock held for user-mode emulation, grabs tb_lock
1408 * Called with tb_lock held for system-mode emulation
1410 static void tb_invalidate_phys_range_1(tb_page_addr_t start, tb_page_addr_t end)
1412 while (start < end) {
1413 tb_invalidate_phys_page_range(start, end, 0);
1414 start &= TARGET_PAGE_MASK;
1415 start += TARGET_PAGE_SIZE;
1419 #ifdef CONFIG_SOFTMMU
1420 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)
1422 assert_tb_locked();
1423 tb_invalidate_phys_range_1(start, end);
1425 #else
1426 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)
1428 assert_memory_lock();
1429 tb_lock();
1430 tb_invalidate_phys_range_1(start, end);
1431 tb_unlock();
1433 #endif
1435 * Invalidate all TBs which intersect with the target physical address range
1436 * [start;end[. NOTE: start and end must refer to the *same* physical page.
1437 * 'is_cpu_write_access' should be true if called from a real cpu write
1438 * access: the virtual CPU will exit the current TB if code is modified inside
1439 * this TB.
1441 * Called with tb_lock/mmap_lock held for user-mode emulation
1442 * Called with tb_lock held for system-mode emulation
1444 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
1445 int is_cpu_write_access)
1447 TranslationBlock *tb, *tb_next;
1448 #if defined(TARGET_HAS_PRECISE_SMC)
1449 CPUState *cpu = current_cpu;
1450 CPUArchState *env = NULL;
1451 #endif
1452 tb_page_addr_t tb_start, tb_end;
1453 PageDesc *p;
1454 int n;
1455 #ifdef TARGET_HAS_PRECISE_SMC
1456 int current_tb_not_found = is_cpu_write_access;
1457 TranslationBlock *current_tb = NULL;
1458 int current_tb_modified = 0;
1459 target_ulong current_pc = 0;
1460 target_ulong current_cs_base = 0;
1461 uint32_t current_flags = 0;
1462 #endif /* TARGET_HAS_PRECISE_SMC */
1464 assert_memory_lock();
1465 assert_tb_locked();
1467 p = page_find(start >> TARGET_PAGE_BITS);
1468 if (!p) {
1469 return;
1471 #if defined(TARGET_HAS_PRECISE_SMC)
1472 if (cpu != NULL) {
1473 env = cpu->env_ptr;
1475 #endif
1477 /* we remove all the TBs in the range [start, end[ */
1478 /* XXX: see if in some cases it could be faster to invalidate all
1479 the code */
1480 tb = p->first_tb;
1481 while (tb != NULL) {
1482 n = (uintptr_t)tb & 3;
1483 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1484 tb_next = tb->page_next[n];
1485 /* NOTE: this is subtle as a TB may span two physical pages */
1486 if (n == 0) {
1487 /* NOTE: tb_end may be after the end of the page, but
1488 it is not a problem */
1489 tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
1490 tb_end = tb_start + tb->size;
1491 } else {
1492 tb_start = tb->page_addr[1];
1493 tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
1495 if (!(tb_end <= start || tb_start >= end)) {
1496 #ifdef TARGET_HAS_PRECISE_SMC
1497 if (current_tb_not_found) {
1498 current_tb_not_found = 0;
1499 current_tb = NULL;
1500 if (cpu->mem_io_pc) {
1501 /* now we have a real cpu fault */
1502 current_tb = tb_find_pc(cpu->mem_io_pc);
1505 if (current_tb == tb &&
1506 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1507 /* If we are modifying the current TB, we must stop
1508 its execution. We could be more precise by checking
1509 that the modification is after the current PC, but it
1510 would require a specialized function to partially
1511 restore the CPU state */
1513 current_tb_modified = 1;
1514 cpu_restore_state_from_tb(cpu, current_tb, cpu->mem_io_pc);
1515 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1516 &current_flags);
1518 #endif /* TARGET_HAS_PRECISE_SMC */
1519 tb_phys_invalidate(tb, -1);
1521 tb = tb_next;
1523 #if !defined(CONFIG_USER_ONLY)
1524 /* if no code remaining, no need to continue to use slow writes */
1525 if (!p->first_tb) {
1526 invalidate_page_bitmap(p);
1527 tlb_unprotect_code(start);
1529 #endif
1530 #ifdef TARGET_HAS_PRECISE_SMC
1531 if (current_tb_modified) {
1532 /* we generate a block containing just the instruction
1533 modifying the memory. It will ensure that it cannot modify
1534 itself */
1535 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1536 cpu_loop_exit_noexc(cpu);
1538 #endif
1541 #ifdef CONFIG_SOFTMMU
1542 /* len must be <= 8 and start must be a multiple of len.
1543 * Called via softmmu_template.h when code areas are written to with
1544 * iothread mutex not held.
1546 void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
1548 PageDesc *p;
1550 #if 0
1551 if (1) {
1552 qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1553 cpu_single_env->mem_io_vaddr, len,
1554 cpu_single_env->eip,
1555 cpu_single_env->eip +
1556 (intptr_t)cpu_single_env->segs[R_CS].base);
1558 #endif
1559 assert_memory_lock();
1561 p = page_find(start >> TARGET_PAGE_BITS);
1562 if (!p) {
1563 return;
1565 if (!p->code_bitmap &&
1566 ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD) {
1567 /* build code bitmap. FIXME: writes should be protected by
1568 * tb_lock, reads by tb_lock or RCU.
1570 build_page_bitmap(p);
1572 if (p->code_bitmap) {
1573 unsigned int nr;
1574 unsigned long b;
1576 nr = start & ~TARGET_PAGE_MASK;
1577 b = p->code_bitmap[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG - 1));
1578 if (b & ((1 << len) - 1)) {
1579 goto do_invalidate;
1581 } else {
1582 do_invalidate:
1583 tb_invalidate_phys_page_range(start, start + len, 1);
1586 #else
1587 /* Called with mmap_lock held. If pc is not 0 then it indicates the
1588 * host PC of the faulting store instruction that caused this invalidate.
1589 * Returns true if the caller needs to abort execution of the current
1590 * TB (because it was modified by this store and the guest CPU has
1591 * precise-SMC semantics).
1593 static bool tb_invalidate_phys_page(tb_page_addr_t addr, uintptr_t pc)
1595 TranslationBlock *tb;
1596 PageDesc *p;
1597 int n;
1598 #ifdef TARGET_HAS_PRECISE_SMC
1599 TranslationBlock *current_tb = NULL;
1600 CPUState *cpu = current_cpu;
1601 CPUArchState *env = NULL;
1602 int current_tb_modified = 0;
1603 target_ulong current_pc = 0;
1604 target_ulong current_cs_base = 0;
1605 uint32_t current_flags = 0;
1606 #endif
1608 assert_memory_lock();
1610 addr &= TARGET_PAGE_MASK;
1611 p = page_find(addr >> TARGET_PAGE_BITS);
1612 if (!p) {
1613 return false;
1616 tb_lock();
1617 tb = p->first_tb;
1618 #ifdef TARGET_HAS_PRECISE_SMC
1619 if (tb && pc != 0) {
1620 current_tb = tb_find_pc(pc);
1622 if (cpu != NULL) {
1623 env = cpu->env_ptr;
1625 #endif
1626 while (tb != NULL) {
1627 n = (uintptr_t)tb & 3;
1628 tb = (TranslationBlock *)((uintptr_t)tb & ~3);
1629 #ifdef TARGET_HAS_PRECISE_SMC
1630 if (current_tb == tb &&
1631 (current_tb->cflags & CF_COUNT_MASK) != 1) {
1632 /* If we are modifying the current TB, we must stop
1633 its execution. We could be more precise by checking
1634 that the modification is after the current PC, but it
1635 would require a specialized function to partially
1636 restore the CPU state */
1638 current_tb_modified = 1;
1639 cpu_restore_state_from_tb(cpu, current_tb, pc);
1640 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
1641 &current_flags);
1643 #endif /* TARGET_HAS_PRECISE_SMC */
1644 tb_phys_invalidate(tb, addr);
1645 tb = tb->page_next[n];
1647 p->first_tb = NULL;
1648 #ifdef TARGET_HAS_PRECISE_SMC
1649 if (current_tb_modified) {
1650 /* we generate a block containing just the instruction
1651 modifying the memory. It will ensure that it cannot modify
1652 itself */
1653 tb_gen_code(cpu, current_pc, current_cs_base, current_flags, 1);
1654 /* tb_lock will be reset after cpu_loop_exit_noexc longjmps
1655 * back into the cpu_exec loop. */
1656 return true;
1658 #endif
1659 tb_unlock();
1661 return false;
1663 #endif
1665 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1666 tb[1].tc_ptr. Return NULL if not found */
1667 static TranslationBlock *tb_find_pc(uintptr_t tc_ptr)
1669 int m_min, m_max, m;
1670 uintptr_t v;
1671 TranslationBlock *tb;
1673 if (tcg_ctx.tb_ctx.nb_tbs <= 0) {
1674 return NULL;
1676 if (tc_ptr < (uintptr_t)tcg_ctx.code_gen_buffer ||
1677 tc_ptr >= (uintptr_t)tcg_ctx.code_gen_ptr) {
1678 return NULL;
1680 /* binary search (cf Knuth) */
1681 m_min = 0;
1682 m_max = tcg_ctx.tb_ctx.nb_tbs - 1;
1683 while (m_min <= m_max) {
1684 m = (m_min + m_max) >> 1;
1685 tb = tcg_ctx.tb_ctx.tbs[m];
1686 v = (uintptr_t)tb->tc_ptr;
1687 if (v == tc_ptr) {
1688 return tb;
1689 } else if (tc_ptr < v) {
1690 m_max = m - 1;
1691 } else {
1692 m_min = m + 1;
1695 return tcg_ctx.tb_ctx.tbs[m_max];
1698 #if !defined(CONFIG_USER_ONLY)
1699 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
1701 ram_addr_t ram_addr;
1702 MemoryRegion *mr;
1703 hwaddr l = 1;
1705 rcu_read_lock();
1706 mr = address_space_translate(as, addr, &addr, &l, false);
1707 if (!(memory_region_is_ram(mr)
1708 || memory_region_is_romd(mr))) {
1709 rcu_read_unlock();
1710 return;
1712 ram_addr = memory_region_get_ram_addr(mr) + addr;
1713 tb_lock();
1714 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
1715 tb_unlock();
1716 rcu_read_unlock();
1718 #endif /* !defined(CONFIG_USER_ONLY) */
1720 /* Called with tb_lock held. */
1721 void tb_check_watchpoint(CPUState *cpu)
1723 TranslationBlock *tb;
1725 tb = tb_find_pc(cpu->mem_io_pc);
1726 if (tb) {
1727 /* We can use retranslation to find the PC. */
1728 cpu_restore_state_from_tb(cpu, tb, cpu->mem_io_pc);
1729 tb_phys_invalidate(tb, -1);
1730 } else {
1731 /* The exception probably happened in a helper. The CPU state should
1732 have been saved before calling it. Fetch the PC from there. */
1733 CPUArchState *env = cpu->env_ptr;
1734 target_ulong pc, cs_base;
1735 tb_page_addr_t addr;
1736 uint32_t flags;
1738 cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
1739 addr = get_page_addr_code(env, pc);
1740 tb_invalidate_phys_range(addr, addr + 1);
1744 #ifndef CONFIG_USER_ONLY
1745 /* in deterministic execution mode, instructions doing device I/Os
1746 * must be at the end of the TB.
1748 * Called by softmmu_template.h, with iothread mutex not held.
1750 void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr)
1752 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
1753 CPUArchState *env = cpu->env_ptr;
1754 #endif
1755 TranslationBlock *tb;
1756 uint32_t n, cflags;
1757 target_ulong pc, cs_base;
1758 uint32_t flags;
1760 tb_lock();
1761 tb = tb_find_pc(retaddr);
1762 if (!tb) {
1763 cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p",
1764 (void *)retaddr);
1766 n = cpu->icount_decr.u16.low + tb->icount;
1767 cpu_restore_state_from_tb(cpu, tb, retaddr);
1768 /* Calculate how many instructions had been executed before the fault
1769 occurred. */
1770 n = n - cpu->icount_decr.u16.low;
1771 /* Generate a new TB ending on the I/O insn. */
1772 n++;
1773 /* On MIPS and SH, delay slot instructions can only be restarted if
1774 they were already the first instruction in the TB. If this is not
1775 the first instruction in a TB then re-execute the preceding
1776 branch. */
1777 #if defined(TARGET_MIPS)
1778 if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
1779 env->active_tc.PC -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4);
1780 cpu->icount_decr.u16.low++;
1781 env->hflags &= ~MIPS_HFLAG_BMASK;
1783 #elif defined(TARGET_SH4)
1784 if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
1785 && n > 1) {
1786 env->pc -= 2;
1787 cpu->icount_decr.u16.low++;
1788 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
1790 #endif
1791 /* This should never happen. */
1792 if (n > CF_COUNT_MASK) {
1793 cpu_abort(cpu, "TB too big during recompile");
1796 cflags = n | CF_LAST_IO;
1797 pc = tb->pc;
1798 cs_base = tb->cs_base;
1799 flags = tb->flags;
1800 tb_phys_invalidate(tb, -1);
1801 if (tb->cflags & CF_NOCACHE) {
1802 if (tb->orig_tb) {
1803 /* Invalidate original TB if this TB was generated in
1804 * cpu_exec_nocache() */
1805 tb_phys_invalidate(tb->orig_tb, -1);
1807 tb_free(tb);
1809 /* FIXME: In theory this could raise an exception. In practice
1810 we have already translated the block once so it's probably ok. */
1811 tb_gen_code(cpu, pc, cs_base, flags, cflags);
1813 /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
1814 * the first in the TB) then we end up generating a whole new TB and
1815 * repeating the fault, which is horribly inefficient.
1816 * Better would be to execute just this insn uncached, or generate a
1817 * second new TB.
1819 * cpu_loop_exit_noexc will longjmp back to cpu_exec where the
1820 * tb_lock gets reset.
1822 cpu_loop_exit_noexc(cpu);
1825 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr)
1827 unsigned int i;
1829 /* Discard jump cache entries for any tb which might potentially
1830 overlap the flushed page. */
1831 i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1832 memset(&cpu->tb_jmp_cache[i], 0,
1833 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1835 i = tb_jmp_cache_hash_page(addr);
1836 memset(&cpu->tb_jmp_cache[i], 0,
1837 TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1840 static void print_qht_statistics(FILE *f, fprintf_function cpu_fprintf,
1841 struct qht_stats hst)
1843 uint32_t hgram_opts;
1844 size_t hgram_bins;
1845 char *hgram;
1847 if (!hst.head_buckets) {
1848 return;
1850 cpu_fprintf(f, "TB hash buckets %zu/%zu (%0.2f%% head buckets used)\n",
1851 hst.used_head_buckets, hst.head_buckets,
1852 (double)hst.used_head_buckets / hst.head_buckets * 100);
1854 hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS;
1855 hgram_opts |= QDIST_PR_100X | QDIST_PR_PERCENT;
1856 if (qdist_xmax(&hst.occupancy) - qdist_xmin(&hst.occupancy) == 1) {
1857 hgram_opts |= QDIST_PR_NODECIMAL;
1859 hgram = qdist_pr(&hst.occupancy, 10, hgram_opts);
1860 cpu_fprintf(f, "TB hash occupancy %0.2f%% avg chain occ. Histogram: %s\n",
1861 qdist_avg(&hst.occupancy) * 100, hgram);
1862 g_free(hgram);
1864 hgram_opts = QDIST_PR_BORDER | QDIST_PR_LABELS;
1865 hgram_bins = qdist_xmax(&hst.chain) - qdist_xmin(&hst.chain);
1866 if (hgram_bins > 10) {
1867 hgram_bins = 10;
1868 } else {
1869 hgram_bins = 0;
1870 hgram_opts |= QDIST_PR_NODECIMAL | QDIST_PR_NOBINRANGE;
1872 hgram = qdist_pr(&hst.chain, hgram_bins, hgram_opts);
1873 cpu_fprintf(f, "TB hash avg chain %0.3f buckets. Histogram: %s\n",
1874 qdist_avg(&hst.chain), hgram);
1875 g_free(hgram);
1878 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
1880 int i, target_code_size, max_target_code_size;
1881 int direct_jmp_count, direct_jmp2_count, cross_page;
1882 TranslationBlock *tb;
1883 struct qht_stats hst;
1885 tb_lock();
1887 target_code_size = 0;
1888 max_target_code_size = 0;
1889 cross_page = 0;
1890 direct_jmp_count = 0;
1891 direct_jmp2_count = 0;
1892 for (i = 0; i < tcg_ctx.tb_ctx.nb_tbs; i++) {
1893 tb = tcg_ctx.tb_ctx.tbs[i];
1894 target_code_size += tb->size;
1895 if (tb->size > max_target_code_size) {
1896 max_target_code_size = tb->size;
1898 if (tb->page_addr[1] != -1) {
1899 cross_page++;
1901 if (tb->jmp_reset_offset[0] != TB_JMP_RESET_OFFSET_INVALID) {
1902 direct_jmp_count++;
1903 if (tb->jmp_reset_offset[1] != TB_JMP_RESET_OFFSET_INVALID) {
1904 direct_jmp2_count++;
1908 /* XXX: avoid using doubles ? */
1909 cpu_fprintf(f, "Translation buffer state:\n");
1910 cpu_fprintf(f, "gen code size %td/%zd\n",
1911 tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer,
1912 tcg_ctx.code_gen_highwater - tcg_ctx.code_gen_buffer);
1913 cpu_fprintf(f, "TB count %d\n", tcg_ctx.tb_ctx.nb_tbs);
1914 cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
1915 tcg_ctx.tb_ctx.nb_tbs ? target_code_size /
1916 tcg_ctx.tb_ctx.nb_tbs : 0,
1917 max_target_code_size);
1918 cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
1919 tcg_ctx.tb_ctx.nb_tbs ? (tcg_ctx.code_gen_ptr -
1920 tcg_ctx.code_gen_buffer) /
1921 tcg_ctx.tb_ctx.nb_tbs : 0,
1922 target_code_size ? (double) (tcg_ctx.code_gen_ptr -
1923 tcg_ctx.code_gen_buffer) /
1924 target_code_size : 0);
1925 cpu_fprintf(f, "cross page TB count %d (%d%%)\n", cross_page,
1926 tcg_ctx.tb_ctx.nb_tbs ? (cross_page * 100) /
1927 tcg_ctx.tb_ctx.nb_tbs : 0);
1928 cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
1929 direct_jmp_count,
1930 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp_count * 100) /
1931 tcg_ctx.tb_ctx.nb_tbs : 0,
1932 direct_jmp2_count,
1933 tcg_ctx.tb_ctx.nb_tbs ? (direct_jmp2_count * 100) /
1934 tcg_ctx.tb_ctx.nb_tbs : 0);
1936 qht_statistics_init(&tcg_ctx.tb_ctx.htable, &hst);
1937 print_qht_statistics(f, cpu_fprintf, hst);
1938 qht_statistics_destroy(&hst);
1940 cpu_fprintf(f, "\nStatistics:\n");
1941 cpu_fprintf(f, "TB flush count %u\n",
1942 atomic_read(&tcg_ctx.tb_ctx.tb_flush_count));
1943 cpu_fprintf(f, "TB invalidate count %d\n",
1944 tcg_ctx.tb_ctx.tb_phys_invalidate_count);
1945 cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
1946 tcg_dump_info(f, cpu_fprintf);
1948 tb_unlock();
1951 void dump_opcount_info(FILE *f, fprintf_function cpu_fprintf)
1953 tcg_dump_op_count(f, cpu_fprintf);
1956 #else /* CONFIG_USER_ONLY */
1958 void cpu_interrupt(CPUState *cpu, int mask)
1960 g_assert(qemu_mutex_iothread_locked());
1961 cpu->interrupt_request |= mask;
1962 cpu->icount_decr.u16.high = -1;
1966 * Walks guest process memory "regions" one by one
1967 * and calls callback function 'fn' for each region.
1969 struct walk_memory_regions_data {
1970 walk_memory_regions_fn fn;
1971 void *priv;
1972 target_ulong start;
1973 int prot;
1976 static int walk_memory_regions_end(struct walk_memory_regions_data *data,
1977 target_ulong end, int new_prot)
1979 if (data->start != -1u) {
1980 int rc = data->fn(data->priv, data->start, end, data->prot);
1981 if (rc != 0) {
1982 return rc;
1986 data->start = (new_prot ? end : -1u);
1987 data->prot = new_prot;
1989 return 0;
1992 static int walk_memory_regions_1(struct walk_memory_regions_data *data,
1993 target_ulong base, int level, void **lp)
1995 target_ulong pa;
1996 int i, rc;
1998 if (*lp == NULL) {
1999 return walk_memory_regions_end(data, base, 0);
2002 if (level == 0) {
2003 PageDesc *pd = *lp;
2005 for (i = 0; i < V_L2_SIZE; ++i) {
2006 int prot = pd[i].flags;
2008 pa = base | (i << TARGET_PAGE_BITS);
2009 if (prot != data->prot) {
2010 rc = walk_memory_regions_end(data, pa, prot);
2011 if (rc != 0) {
2012 return rc;
2016 } else {
2017 void **pp = *lp;
2019 for (i = 0; i < V_L2_SIZE; ++i) {
2020 pa = base | ((target_ulong)i <<
2021 (TARGET_PAGE_BITS + V_L2_BITS * level));
2022 rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
2023 if (rc != 0) {
2024 return rc;
2029 return 0;
2032 int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
2034 struct walk_memory_regions_data data;
2035 uintptr_t i, l1_sz = v_l1_size;
2037 data.fn = fn;
2038 data.priv = priv;
2039 data.start = -1u;
2040 data.prot = 0;
2042 for (i = 0; i < l1_sz; i++) {
2043 target_ulong base = i << (v_l1_shift + TARGET_PAGE_BITS);
2044 int rc = walk_memory_regions_1(&data, base, v_l2_levels, l1_map + i);
2045 if (rc != 0) {
2046 return rc;
2050 return walk_memory_regions_end(&data, 0, 0);
2053 static int dump_region(void *priv, target_ulong start,
2054 target_ulong end, abi_ulong prot)
2056 FILE *f = (FILE *)priv;
2058 (void) fprintf(f, TARGET_FMT_lx"-"TARGET_FMT_lx
2059 " "TARGET_FMT_lx" %c%c%c\n",
2060 start, end, end - start,
2061 ((prot & PAGE_READ) ? 'r' : '-'),
2062 ((prot & PAGE_WRITE) ? 'w' : '-'),
2063 ((prot & PAGE_EXEC) ? 'x' : '-'));
2065 return 0;
2068 /* dump memory mappings */
2069 void page_dump(FILE *f)
2071 const int length = sizeof(target_ulong) * 2;
2072 (void) fprintf(f, "%-*s %-*s %-*s %s\n",
2073 length, "start", length, "end", length, "size", "prot");
2074 walk_memory_regions(f, dump_region);
2077 int page_get_flags(target_ulong address)
2079 PageDesc *p;
2081 p = page_find(address >> TARGET_PAGE_BITS);
2082 if (!p) {
2083 return 0;
2085 return p->flags;
2088 /* Modify the flags of a page and invalidate the code if necessary.
2089 The flag PAGE_WRITE_ORG is positioned automatically depending
2090 on PAGE_WRITE. The mmap_lock should already be held. */
2091 void page_set_flags(target_ulong start, target_ulong end, int flags)
2093 target_ulong addr, len;
2095 /* This function should never be called with addresses outside the
2096 guest address space. If this assert fires, it probably indicates
2097 a missing call to h2g_valid. */
2098 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2099 assert(end < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2100 #endif
2101 assert(start < end);
2102 assert_memory_lock();
2104 start = start & TARGET_PAGE_MASK;
2105 end = TARGET_PAGE_ALIGN(end);
2107 if (flags & PAGE_WRITE) {
2108 flags |= PAGE_WRITE_ORG;
2111 for (addr = start, len = end - start;
2112 len != 0;
2113 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2114 PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
2116 /* If the write protection bit is set, then we invalidate
2117 the code inside. */
2118 if (!(p->flags & PAGE_WRITE) &&
2119 (flags & PAGE_WRITE) &&
2120 p->first_tb) {
2121 tb_invalidate_phys_page(addr, 0);
2123 p->flags = flags;
2127 int page_check_range(target_ulong start, target_ulong len, int flags)
2129 PageDesc *p;
2130 target_ulong end;
2131 target_ulong addr;
2133 /* This function should never be called with addresses outside the
2134 guest address space. If this assert fires, it probably indicates
2135 a missing call to h2g_valid. */
2136 #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2137 assert(start < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2138 #endif
2140 if (len == 0) {
2141 return 0;
2143 if (start + len - 1 < start) {
2144 /* We've wrapped around. */
2145 return -1;
2148 /* must do before we loose bits in the next step */
2149 end = TARGET_PAGE_ALIGN(start + len);
2150 start = start & TARGET_PAGE_MASK;
2152 for (addr = start, len = end - start;
2153 len != 0;
2154 len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
2155 p = page_find(addr >> TARGET_PAGE_BITS);
2156 if (!p) {
2157 return -1;
2159 if (!(p->flags & PAGE_VALID)) {
2160 return -1;
2163 if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) {
2164 return -1;
2166 if (flags & PAGE_WRITE) {
2167 if (!(p->flags & PAGE_WRITE_ORG)) {
2168 return -1;
2170 /* unprotect the page if it was put read-only because it
2171 contains translated code */
2172 if (!(p->flags & PAGE_WRITE)) {
2173 if (!page_unprotect(addr, 0)) {
2174 return -1;
2179 return 0;
2182 /* called from signal handler: invalidate the code and unprotect the
2183 * page. Return 0 if the fault was not handled, 1 if it was handled,
2184 * and 2 if it was handled but the caller must cause the TB to be
2185 * immediately exited. (We can only return 2 if the 'pc' argument is
2186 * non-zero.)
2188 int page_unprotect(target_ulong address, uintptr_t pc)
2190 unsigned int prot;
2191 bool current_tb_invalidated;
2192 PageDesc *p;
2193 target_ulong host_start, host_end, addr;
2195 /* Technically this isn't safe inside a signal handler. However we
2196 know this only ever happens in a synchronous SEGV handler, so in
2197 practice it seems to be ok. */
2198 mmap_lock();
2200 p = page_find(address >> TARGET_PAGE_BITS);
2201 if (!p) {
2202 mmap_unlock();
2203 return 0;
2206 /* if the page was really writable, then we change its
2207 protection back to writable */
2208 if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
2209 host_start = address & qemu_host_page_mask;
2210 host_end = host_start + qemu_host_page_size;
2212 prot = 0;
2213 current_tb_invalidated = false;
2214 for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
2215 p = page_find(addr >> TARGET_PAGE_BITS);
2216 p->flags |= PAGE_WRITE;
2217 prot |= p->flags;
2219 /* and since the content will be modified, we must invalidate
2220 the corresponding translated code. */
2221 current_tb_invalidated |= tb_invalidate_phys_page(addr, pc);
2222 #ifdef DEBUG_TB_CHECK
2223 tb_invalidate_check(addr);
2224 #endif
2226 mprotect((void *)g2h(host_start), qemu_host_page_size,
2227 prot & PAGE_BITS);
2229 mmap_unlock();
2230 /* If current TB was invalidated return to main loop */
2231 return current_tb_invalidated ? 2 : 1;
2233 mmap_unlock();
2234 return 0;
2236 #endif /* CONFIG_USER_ONLY */