2 * Memory region management for Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qemu/madvise.h"
28 #include "qemu/mprotect.h"
29 #include "qemu/memalign.h"
30 #include "qemu/cacheinfo.h"
31 #include "qapi/error.h"
32 #include "exec/exec-all.h"
34 #include "tcg-internal.h"
37 struct tcg_region_tree
{
40 /* padding to avoid false sharing is computed at run-time */
44 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
45 * dynamically allocate from as demand dictates. Given appropriate region
46 * sizing, this minimizes flushes even when some TCG threads generate a lot
47 * more code than others.
49 struct tcg_region_state
{
52 /* fields set at init time */
56 size_t size
; /* size of one region */
57 size_t stride
; /* .size + guard size */
58 size_t total_size
; /* size of entire buffer, >= n * stride */
60 /* fields protected by the lock */
61 size_t current
; /* current region index */
62 size_t agg_size_full
; /* aggregate size of full regions */
65 static struct tcg_region_state region
;
68 * This is an array of struct tcg_region_tree's, with padding.
69 * We use void * to simplify the computation of region_trees[i]; each
70 * struct is found every tree_size bytes.
72 static void *region_trees
;
73 static size_t tree_size
;
75 bool in_code_gen_buffer(const void *p
)
78 * Much like it is valid to have a pointer to the byte past the
79 * end of an array (so long as you don't dereference it), allow
80 * a pointer to the byte past the end of the code gen buffer.
82 return (size_t)(p
- region
.start_aligned
) <= region
.total_size
;
85 #ifdef CONFIG_DEBUG_TCG
86 const void *tcg_splitwx_to_rx(void *rw
)
88 /* Pass NULL pointers unchanged. */
90 g_assert(in_code_gen_buffer(rw
));
91 rw
+= tcg_splitwx_diff
;
96 void *tcg_splitwx_to_rw(const void *rx
)
98 /* Pass NULL pointers unchanged. */
100 rx
-= tcg_splitwx_diff
;
101 /* Assert that we end with a pointer in the rw region. */
102 g_assert(in_code_gen_buffer(rx
));
106 #endif /* CONFIG_DEBUG_TCG */
108 /* compare a pointer @ptr and a tb_tc @s */
109 static int ptr_cmp_tb_tc(const void *ptr
, const struct tb_tc
*s
)
111 if (ptr
>= s
->ptr
+ s
->size
) {
113 } else if (ptr
< s
->ptr
) {
119 static gint
tb_tc_cmp(gconstpointer ap
, gconstpointer bp
, gpointer userdata
)
121 const struct tb_tc
*a
= ap
;
122 const struct tb_tc
*b
= bp
;
125 * When both sizes are set, we know this isn't a lookup.
126 * This is the most likely case: every TB must be inserted; lookups
127 * are a lot less frequent.
129 if (likely(a
->size
&& b
->size
)) {
130 if (a
->ptr
> b
->ptr
) {
132 } else if (a
->ptr
< b
->ptr
) {
135 /* a->ptr == b->ptr should happen only on deletions */
136 g_assert(a
->size
== b
->size
);
140 * All lookups have either .size field set to 0.
141 * From the glib sources we see that @ap is always the lookup key. However
142 * the docs provide no guarantee, so we just mark this case as likely.
144 if (likely(a
->size
== 0)) {
145 return ptr_cmp_tb_tc(a
->ptr
, b
);
147 return ptr_cmp_tb_tc(b
->ptr
, a
);
150 static void tb_destroy(gpointer value
)
152 TranslationBlock
*tb
= value
;
153 qemu_spin_destroy(&tb
->jmp_lock
);
156 static void tcg_region_trees_init(void)
160 tree_size
= ROUND_UP(sizeof(struct tcg_region_tree
), qemu_dcache_linesize
);
161 region_trees
= qemu_memalign(qemu_dcache_linesize
, region
.n
* tree_size
);
162 for (i
= 0; i
< region
.n
; i
++) {
163 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
165 qemu_mutex_init(&rt
->lock
);
166 rt
->tree
= g_tree_new_full(tb_tc_cmp
, NULL
, NULL
, tb_destroy
);
170 static struct tcg_region_tree
*tc_ptr_to_region_tree(const void *p
)
175 * Like tcg_splitwx_to_rw, with no assert. The pc may come from
176 * a signal handler over which the caller has no control.
178 if (!in_code_gen_buffer(p
)) {
179 p
-= tcg_splitwx_diff
;
180 if (!in_code_gen_buffer(p
)) {
185 if (p
< region
.start_aligned
) {
188 ptrdiff_t offset
= p
- region
.start_aligned
;
190 if (offset
> region
.stride
* (region
.n
- 1)) {
191 region_idx
= region
.n
- 1;
193 region_idx
= offset
/ region
.stride
;
196 return region_trees
+ region_idx
* tree_size
;
199 void tcg_tb_insert(TranslationBlock
*tb
)
201 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
203 g_assert(rt
!= NULL
);
204 qemu_mutex_lock(&rt
->lock
);
205 g_tree_insert(rt
->tree
, &tb
->tc
, tb
);
206 qemu_mutex_unlock(&rt
->lock
);
209 void tcg_tb_remove(TranslationBlock
*tb
)
211 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
213 g_assert(rt
!= NULL
);
214 qemu_mutex_lock(&rt
->lock
);
215 g_tree_remove(rt
->tree
, &tb
->tc
);
216 qemu_mutex_unlock(&rt
->lock
);
220 * Find the TB 'tb' such that
221 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
222 * Return NULL if not found.
224 TranslationBlock
*tcg_tb_lookup(uintptr_t tc_ptr
)
226 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree((void *)tc_ptr
);
227 TranslationBlock
*tb
;
228 struct tb_tc s
= { .ptr
= (void *)tc_ptr
};
234 qemu_mutex_lock(&rt
->lock
);
235 tb
= g_tree_lookup(rt
->tree
, &s
);
236 qemu_mutex_unlock(&rt
->lock
);
240 static void tcg_region_tree_lock_all(void)
244 for (i
= 0; i
< region
.n
; i
++) {
245 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
247 qemu_mutex_lock(&rt
->lock
);
251 static void tcg_region_tree_unlock_all(void)
255 for (i
= 0; i
< region
.n
; i
++) {
256 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
258 qemu_mutex_unlock(&rt
->lock
);
262 void tcg_tb_foreach(GTraverseFunc func
, gpointer user_data
)
266 tcg_region_tree_lock_all();
267 for (i
= 0; i
< region
.n
; i
++) {
268 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
270 g_tree_foreach(rt
->tree
, func
, user_data
);
272 tcg_region_tree_unlock_all();
275 size_t tcg_nb_tbs(void)
280 tcg_region_tree_lock_all();
281 for (i
= 0; i
< region
.n
; i
++) {
282 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
284 nb_tbs
+= g_tree_nnodes(rt
->tree
);
286 tcg_region_tree_unlock_all();
290 static void tcg_region_tree_reset_all(void)
294 tcg_region_tree_lock_all();
295 for (i
= 0; i
< region
.n
; i
++) {
296 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
298 /* Increment the refcount first so that destroy acts as a reset */
299 g_tree_ref(rt
->tree
);
300 g_tree_destroy(rt
->tree
);
302 tcg_region_tree_unlock_all();
305 static void tcg_region_bounds(size_t curr_region
, void **pstart
, void **pend
)
309 start
= region
.start_aligned
+ curr_region
* region
.stride
;
310 end
= start
+ region
.size
;
312 if (curr_region
== 0) {
313 start
= region
.after_prologue
;
315 /* The final region may have a few extra pages due to earlier rounding. */
316 if (curr_region
== region
.n
- 1) {
317 end
= region
.start_aligned
+ region
.total_size
;
324 static void tcg_region_assign(TCGContext
*s
, size_t curr_region
)
328 tcg_region_bounds(curr_region
, &start
, &end
);
330 s
->code_gen_buffer
= start
;
331 s
->code_gen_ptr
= start
;
332 s
->code_gen_buffer_size
= end
- start
;
333 s
->code_gen_highwater
= end
- TCG_HIGHWATER
;
336 static bool tcg_region_alloc__locked(TCGContext
*s
)
338 if (region
.current
== region
.n
) {
341 tcg_region_assign(s
, region
.current
);
347 * Request a new region once the one in use has filled up.
348 * Returns true on error.
350 bool tcg_region_alloc(TCGContext
*s
)
353 /* read the region size now; alloc__locked will overwrite it on success */
354 size_t size_full
= s
->code_gen_buffer_size
;
356 qemu_mutex_lock(®ion
.lock
);
357 err
= tcg_region_alloc__locked(s
);
359 region
.agg_size_full
+= size_full
- TCG_HIGHWATER
;
361 qemu_mutex_unlock(®ion
.lock
);
366 * Perform a context's first region allocation.
367 * This function does _not_ increment region.agg_size_full.
369 static void tcg_region_initial_alloc__locked(TCGContext
*s
)
371 bool err
= tcg_region_alloc__locked(s
);
375 void tcg_region_initial_alloc(TCGContext
*s
)
377 qemu_mutex_lock(®ion
.lock
);
378 tcg_region_initial_alloc__locked(s
);
379 qemu_mutex_unlock(®ion
.lock
);
382 /* Call from a safe-work context */
383 void tcg_region_reset_all(void)
385 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
388 qemu_mutex_lock(®ion
.lock
);
390 region
.agg_size_full
= 0;
392 for (i
= 0; i
< n_ctxs
; i
++) {
393 TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
394 tcg_region_initial_alloc__locked(s
);
396 qemu_mutex_unlock(®ion
.lock
);
398 tcg_region_tree_reset_all();
401 static size_t tcg_n_regions(size_t tb_size
, unsigned max_cpus
)
403 #ifdef CONFIG_USER_ONLY
409 * It is likely that some vCPUs will translate more code than others,
410 * so we first try to set more regions than max_cpus, with those regions
411 * being of reasonable size. If that's not possible we make do by evenly
412 * dividing the code_gen_buffer among the vCPUs.
414 /* Use a single region if all we have is one vCPU thread */
415 if (max_cpus
== 1 || !qemu_tcg_mttcg_enabled()) {
420 * Try to have more regions than max_cpus, with each region being >= 2 MB.
421 * If we can't, then just allocate one region per vCPU thread.
423 n_regions
= tb_size
/ (2 * MiB
);
424 if (n_regions
<= max_cpus
) {
427 return MIN(n_regions
, max_cpus
* 8);
432 * Minimum size of the code gen buffer. This number is randomly chosen,
433 * but not so small that we can't have a fair number of TB's live.
435 * Maximum size, MAX_CODE_GEN_BUFFER_SIZE, is defined in tcg-target.h.
436 * Unless otherwise indicated, this is constrained by the range of
437 * direct branches on the host cpu, as used by the TCG implementation
440 #define MIN_CODE_GEN_BUFFER_SIZE (1 * MiB)
442 #if TCG_TARGET_REG_BITS == 32
443 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32 * MiB)
444 #ifdef CONFIG_USER_ONLY
446 * For user mode on smaller 32 bit systems we may run into trouble
447 * allocating big chunks of data in the right place. On these systems
448 * we utilise a static code generation buffer directly in the binary.
450 #define USE_STATIC_CODE_GEN_BUFFER
452 #else /* TCG_TARGET_REG_BITS == 64 */
453 #ifdef CONFIG_USER_ONLY
455 * As user-mode emulation typically means running multiple instances
456 * of the translator don't go too nuts with our default code gen
457 * buffer lest we make things too hard for the OS.
459 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (128 * MiB)
462 * We expect most system emulation to run one or two guests per host.
463 * Users running large scale system emulation may want to tweak their
464 * runtime setup via the tb-size control on the command line.
466 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (1 * GiB)
470 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
471 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
472 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
474 #ifdef USE_STATIC_CODE_GEN_BUFFER
475 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
]
476 __attribute__((aligned(CODE_GEN_ALIGN
)));
478 static int alloc_code_gen_buffer(size_t tb_size
, int splitwx
, Error
**errp
)
484 error_setg(errp
, "jit split-wx not supported");
488 /* page-align the beginning and end of the buffer */
489 buf
= static_code_gen_buffer
;
490 end
= static_code_gen_buffer
+ sizeof(static_code_gen_buffer
);
491 buf
= QEMU_ALIGN_PTR_UP(buf
, qemu_real_host_page_size());
492 end
= QEMU_ALIGN_PTR_DOWN(end
, qemu_real_host_page_size());
496 /* Honor a command-line option limiting the size of the buffer. */
497 if (size
> tb_size
) {
498 size
= QEMU_ALIGN_DOWN(tb_size
, qemu_real_host_page_size());
501 region
.start_aligned
= buf
;
502 region
.total_size
= size
;
504 return PROT_READ
| PROT_WRITE
;
506 #elif defined(_WIN32)
507 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
512 error_setg(errp
, "jit split-wx not supported");
516 buf
= VirtualAlloc(NULL
, size
, MEM_RESERVE
| MEM_COMMIT
,
517 PAGE_EXECUTE_READWRITE
);
519 error_setg_win32(errp
, GetLastError(),
520 "allocate %zu bytes for jit buffer", size
);
524 region
.start_aligned
= buf
;
525 region
.total_size
= size
;
527 return PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
530 static int alloc_code_gen_buffer_anon(size_t size
, int prot
,
531 int flags
, Error
**errp
)
535 buf
= mmap(NULL
, size
, prot
, flags
, -1, 0);
536 if (buf
== MAP_FAILED
) {
537 error_setg_errno(errp
, errno
,
538 "allocate %zu bytes for jit buffer", size
);
542 region
.start_aligned
= buf
;
543 region
.total_size
= size
;
547 #ifndef CONFIG_TCG_INTERPRETER
549 #include "qemu/memfd.h"
551 static int alloc_code_gen_buffer_splitwx_memfd(size_t size
, Error
**errp
)
553 void *buf_rw
= NULL
, *buf_rx
= MAP_FAILED
;
556 buf_rw
= qemu_memfd_alloc("tcg-jit", size
, 0, &fd
, errp
);
557 if (buf_rw
== NULL
) {
561 buf_rx
= mmap(NULL
, size
, PROT_READ
| PROT_EXEC
, MAP_SHARED
, fd
, 0);
562 if (buf_rx
== MAP_FAILED
) {
567 region
.start_aligned
= buf_rw
;
568 region
.total_size
= size
;
569 tcg_splitwx_diff
= buf_rx
- buf_rw
;
571 return PROT_READ
| PROT_WRITE
;
574 error_setg_errno(errp
, errno
, "failed to map shared memory for execute");
576 if (buf_rx
!= MAP_FAILED
) {
577 munmap(buf_rx
, size
);
580 munmap(buf_rw
, size
);
587 #endif /* CONFIG_POSIX */
590 #include <mach/mach.h>
592 extern kern_return_t
mach_vm_remap(vm_map_t target_task
,
593 mach_vm_address_t
*target_address
,
595 mach_vm_offset_t mask
,
598 mach_vm_address_t src_address
,
600 vm_prot_t
*cur_protection
,
601 vm_prot_t
*max_protection
,
602 vm_inherit_t inheritance
);
604 static int alloc_code_gen_buffer_splitwx_vmremap(size_t size
, Error
**errp
)
607 mach_vm_address_t buf_rw
, buf_rx
;
608 vm_prot_t cur_prot
, max_prot
;
610 /* Map the read-write portion via normal anon memory. */
611 if (!alloc_code_gen_buffer_anon(size
, PROT_READ
| PROT_WRITE
,
612 MAP_PRIVATE
| MAP_ANONYMOUS
, errp
)) {
616 buf_rw
= (mach_vm_address_t
)region
.start_aligned
;
618 ret
= mach_vm_remap(mach_task_self(),
629 if (ret
!= KERN_SUCCESS
) {
630 /* TODO: Convert "ret" to a human readable error message. */
631 error_setg(errp
, "vm_remap for jit splitwx failed");
632 munmap((void *)buf_rw
, size
);
636 if (mprotect((void *)buf_rx
, size
, PROT_READ
| PROT_EXEC
) != 0) {
637 error_setg_errno(errp
, errno
, "mprotect for jit splitwx");
638 munmap((void *)buf_rx
, size
);
639 munmap((void *)buf_rw
, size
);
643 tcg_splitwx_diff
= buf_rx
- buf_rw
;
644 return PROT_READ
| PROT_WRITE
;
646 #endif /* CONFIG_DARWIN */
647 #endif /* CONFIG_TCG_INTERPRETER */
649 static int alloc_code_gen_buffer_splitwx(size_t size
, Error
**errp
)
651 #ifndef CONFIG_TCG_INTERPRETER
652 # ifdef CONFIG_DARWIN
653 return alloc_code_gen_buffer_splitwx_vmremap(size
, errp
);
656 return alloc_code_gen_buffer_splitwx_memfd(size
, errp
);
659 error_setg(errp
, "jit split-wx not supported");
663 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
669 prot
= alloc_code_gen_buffer_splitwx(size
, errp
);
674 * If splitwx force-on (1), fail;
675 * if splitwx default-on (-1), fall through to splitwx off.
680 error_free_or_abort(errp
);
684 * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect
685 * rejects a permission change from RWX -> NONE when reserving the
686 * guard pages later. We can go the other way with the same number
687 * of syscalls, so always begin with PROT_NONE.
690 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
692 /* Applicable to both iOS and macOS (Apple Silicon). */
698 return alloc_code_gen_buffer_anon(size
, prot
, flags
, errp
);
700 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
703 * Initializes region partitioning.
705 * Called at init time from the parent thread (i.e. the one calling
706 * tcg_context_init), after the target's TCG globals have been set.
708 * Region partitioning works by splitting code_gen_buffer into separate regions,
709 * and then assigning regions to TCG threads so that the threads can translate
710 * code in parallel without synchronization.
712 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
713 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
714 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
715 * must have been parsed before calling this function, since it calls
716 * qemu_tcg_mttcg_enabled().
718 * In user-mode we use a single region. Having multiple regions in user-mode
719 * is not supported, because the number of vCPU threads (recall that each thread
720 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
721 * OS, and usually this number is huge (tens of thousands is not uncommon).
722 * Thus, given this large bound on the number of vCPU threads and the fact
723 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
724 * that the availability of at least one region per vCPU thread.
726 * However, this user-mode limitation is unlikely to be a significant problem
727 * in practice. Multi-threaded guests share most if not all of their translated
728 * code, which makes parallel code generation less appealing than in softmmu.
730 void tcg_region_init(size_t tb_size
, int splitwx
, unsigned max_cpus
)
732 const size_t page_size
= qemu_real_host_page_size();
734 int have_prot
, need_prot
;
736 /* Size the buffer. */
738 size_t phys_mem
= qemu_get_host_physmem();
740 tb_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
742 tb_size
= QEMU_ALIGN_DOWN(phys_mem
/ 8, page_size
);
743 tb_size
= MIN(DEFAULT_CODE_GEN_BUFFER_SIZE
, tb_size
);
746 if (tb_size
< MIN_CODE_GEN_BUFFER_SIZE
) {
747 tb_size
= MIN_CODE_GEN_BUFFER_SIZE
;
749 if (tb_size
> MAX_CODE_GEN_BUFFER_SIZE
) {
750 tb_size
= MAX_CODE_GEN_BUFFER_SIZE
;
753 have_prot
= alloc_code_gen_buffer(tb_size
, splitwx
, &error_fatal
);
754 assert(have_prot
>= 0);
756 /* Request large pages for the buffer and the splitwx. */
757 qemu_madvise(region
.start_aligned
, region
.total_size
, QEMU_MADV_HUGEPAGE
);
758 if (tcg_splitwx_diff
) {
759 qemu_madvise(region
.start_aligned
+ tcg_splitwx_diff
,
760 region
.total_size
, QEMU_MADV_HUGEPAGE
);
764 * Make region_size a multiple of page_size, using aligned as the start.
765 * As a result of this we might end up with a few extra pages at the end of
766 * the buffer; we will assign those to the last region.
768 region
.n
= tcg_n_regions(tb_size
, max_cpus
);
769 region_size
= tb_size
/ region
.n
;
770 region_size
= QEMU_ALIGN_DOWN(region_size
, page_size
);
772 /* A region must have at least 2 pages; one code, one guard */
773 g_assert(region_size
>= 2 * page_size
);
774 region
.stride
= region_size
;
776 /* Reserve space for guard pages. */
777 region
.size
= region_size
- page_size
;
778 region
.total_size
-= page_size
;
781 * The first region will be smaller than the others, via the prologue,
782 * which has yet to be allocated. For now, the first region begins at
785 region
.after_prologue
= region
.start_aligned
;
787 /* init the region struct */
788 qemu_mutex_init(®ion
.lock
);
791 * Set guard pages in the rw buffer, as that's the one into which
792 * buffer overruns could occur. Do not set guard pages in the rx
793 * buffer -- let that one use hugepages throughout.
794 * Work with the page protections set up with the initial mapping.
796 need_prot
= PAGE_READ
| PAGE_WRITE
;
797 #ifndef CONFIG_TCG_INTERPRETER
798 if (tcg_splitwx_diff
== 0) {
799 need_prot
|= PAGE_EXEC
;
802 for (size_t i
= 0, n
= region
.n
; i
< n
; i
++) {
805 tcg_region_bounds(i
, &start
, &end
);
806 if (have_prot
!= need_prot
) {
809 if (need_prot
== (PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
)) {
810 rc
= qemu_mprotect_rwx(start
, end
- start
);
811 } else if (need_prot
== (PAGE_READ
| PAGE_WRITE
)) {
812 rc
= qemu_mprotect_rw(start
, end
- start
);
814 g_assert_not_reached();
817 error_setg_errno(&error_fatal
, errno
,
818 "mprotect of jit buffer");
821 if (have_prot
!= 0) {
822 /* Guard pages are nice for bug detection but are not essential. */
823 (void)qemu_mprotect_none(end
, page_size
);
827 tcg_region_trees_init();
830 * Leave the initial context initialized to the first region.
831 * This will be the context into which we generate the prologue.
832 * It is also the only context for CONFIG_USER_ONLY.
834 tcg_region_initial_alloc__locked(&tcg_init_ctx
);
837 void tcg_region_prologue_set(TCGContext
*s
)
839 /* Deduct the prologue from the first region. */
840 g_assert(region
.start_aligned
== s
->code_gen_buffer
);
841 region
.after_prologue
= s
->code_ptr
;
843 /* Recompute boundaries of the first region. */
844 tcg_region_assign(s
, 0);
846 /* Register the balance of the buffer with gdb. */
847 tcg_register_jit(tcg_splitwx_to_rx(region
.after_prologue
),
848 region
.start_aligned
+ region
.total_size
-
849 region
.after_prologue
);
853 * Returns the size (in bytes) of all translated code (i.e. from all regions)
854 * currently in the cache.
855 * See also: tcg_code_capacity()
856 * Do not confuse with tcg_current_code_size(); that one applies to a single
859 size_t tcg_code_size(void)
861 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
865 qemu_mutex_lock(®ion
.lock
);
866 total
= region
.agg_size_full
;
867 for (i
= 0; i
< n_ctxs
; i
++) {
868 const TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
871 size
= qatomic_read(&s
->code_gen_ptr
) - s
->code_gen_buffer
;
872 g_assert(size
<= s
->code_gen_buffer_size
);
875 qemu_mutex_unlock(®ion
.lock
);
880 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
882 * See also: tcg_code_size()
884 size_t tcg_code_capacity(void)
886 size_t guard_size
, capacity
;
888 /* no need for synchronization; these variables are set at init time */
889 guard_size
= region
.stride
- region
.size
;
890 capacity
= region
.total_size
;
891 capacity
-= (region
.n
- 1) * guard_size
;
892 capacity
-= region
.n
* TCG_HIGHWATER
;