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/cacheinfo.h"
30 #include "qapi/error.h"
31 #include "exec/exec-all.h"
33 #include "tcg-internal.h"
36 struct tcg_region_tree
{
39 /* padding to avoid false sharing is computed at run-time */
43 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
44 * dynamically allocate from as demand dictates. Given appropriate region
45 * sizing, this minimizes flushes even when some TCG threads generate a lot
46 * more code than others.
48 struct tcg_region_state
{
51 /* fields set at init time */
55 size_t size
; /* size of one region */
56 size_t stride
; /* .size + guard size */
57 size_t total_size
; /* size of entire buffer, >= n * stride */
59 /* fields protected by the lock */
60 size_t current
; /* current region index */
61 size_t agg_size_full
; /* aggregate size of full regions */
64 static struct tcg_region_state region
;
67 * This is an array of struct tcg_region_tree's, with padding.
68 * We use void * to simplify the computation of region_trees[i]; each
69 * struct is found every tree_size bytes.
71 static void *region_trees
;
72 static size_t tree_size
;
74 bool in_code_gen_buffer(const void *p
)
77 * Much like it is valid to have a pointer to the byte past the
78 * end of an array (so long as you don't dereference it), allow
79 * a pointer to the byte past the end of the code gen buffer.
81 return (size_t)(p
- region
.start_aligned
) <= region
.total_size
;
84 #ifdef CONFIG_DEBUG_TCG
85 const void *tcg_splitwx_to_rx(void *rw
)
87 /* Pass NULL pointers unchanged. */
89 g_assert(in_code_gen_buffer(rw
));
90 rw
+= tcg_splitwx_diff
;
95 void *tcg_splitwx_to_rw(const void *rx
)
97 /* Pass NULL pointers unchanged. */
99 rx
-= tcg_splitwx_diff
;
100 /* Assert that we end with a pointer in the rw region. */
101 g_assert(in_code_gen_buffer(rx
));
105 #endif /* CONFIG_DEBUG_TCG */
107 /* compare a pointer @ptr and a tb_tc @s */
108 static int ptr_cmp_tb_tc(const void *ptr
, const struct tb_tc
*s
)
110 if (ptr
>= s
->ptr
+ s
->size
) {
112 } else if (ptr
< s
->ptr
) {
118 static gint
tb_tc_cmp(gconstpointer ap
, gconstpointer bp
, gpointer userdata
)
120 const struct tb_tc
*a
= ap
;
121 const struct tb_tc
*b
= bp
;
124 * When both sizes are set, we know this isn't a lookup.
125 * This is the most likely case: every TB must be inserted; lookups
126 * are a lot less frequent.
128 if (likely(a
->size
&& b
->size
)) {
129 if (a
->ptr
> b
->ptr
) {
131 } else if (a
->ptr
< b
->ptr
) {
134 /* a->ptr == b->ptr should happen only on deletions */
135 g_assert(a
->size
== b
->size
);
139 * All lookups have either .size field set to 0.
140 * From the glib sources we see that @ap is always the lookup key. However
141 * the docs provide no guarantee, so we just mark this case as likely.
143 if (likely(a
->size
== 0)) {
144 return ptr_cmp_tb_tc(a
->ptr
, b
);
146 return ptr_cmp_tb_tc(b
->ptr
, a
);
149 static void tb_destroy(gpointer value
)
151 TranslationBlock
*tb
= value
;
152 qemu_spin_destroy(&tb
->jmp_lock
);
155 static void tcg_region_trees_init(void)
159 tree_size
= ROUND_UP(sizeof(struct tcg_region_tree
), qemu_dcache_linesize
);
160 region_trees
= qemu_memalign(qemu_dcache_linesize
, region
.n
* tree_size
);
161 for (i
= 0; i
< region
.n
; i
++) {
162 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
164 qemu_mutex_init(&rt
->lock
);
165 rt
->tree
= g_tree_new_full(tb_tc_cmp
, NULL
, NULL
, tb_destroy
);
169 static struct tcg_region_tree
*tc_ptr_to_region_tree(const void *p
)
174 * Like tcg_splitwx_to_rw, with no assert. The pc may come from
175 * a signal handler over which the caller has no control.
177 if (!in_code_gen_buffer(p
)) {
178 p
-= tcg_splitwx_diff
;
179 if (!in_code_gen_buffer(p
)) {
184 if (p
< region
.start_aligned
) {
187 ptrdiff_t offset
= p
- region
.start_aligned
;
189 if (offset
> region
.stride
* (region
.n
- 1)) {
190 region_idx
= region
.n
- 1;
192 region_idx
= offset
/ region
.stride
;
195 return region_trees
+ region_idx
* tree_size
;
198 void tcg_tb_insert(TranslationBlock
*tb
)
200 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
202 g_assert(rt
!= NULL
);
203 qemu_mutex_lock(&rt
->lock
);
204 g_tree_insert(rt
->tree
, &tb
->tc
, tb
);
205 qemu_mutex_unlock(&rt
->lock
);
208 void tcg_tb_remove(TranslationBlock
*tb
)
210 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
212 g_assert(rt
!= NULL
);
213 qemu_mutex_lock(&rt
->lock
);
214 g_tree_remove(rt
->tree
, &tb
->tc
);
215 qemu_mutex_unlock(&rt
->lock
);
219 * Find the TB 'tb' such that
220 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
221 * Return NULL if not found.
223 TranslationBlock
*tcg_tb_lookup(uintptr_t tc_ptr
)
225 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree((void *)tc_ptr
);
226 TranslationBlock
*tb
;
227 struct tb_tc s
= { .ptr
= (void *)tc_ptr
};
233 qemu_mutex_lock(&rt
->lock
);
234 tb
= g_tree_lookup(rt
->tree
, &s
);
235 qemu_mutex_unlock(&rt
->lock
);
239 static void tcg_region_tree_lock_all(void)
243 for (i
= 0; i
< region
.n
; i
++) {
244 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
246 qemu_mutex_lock(&rt
->lock
);
250 static void tcg_region_tree_unlock_all(void)
254 for (i
= 0; i
< region
.n
; i
++) {
255 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
257 qemu_mutex_unlock(&rt
->lock
);
261 void tcg_tb_foreach(GTraverseFunc func
, gpointer user_data
)
265 tcg_region_tree_lock_all();
266 for (i
= 0; i
< region
.n
; i
++) {
267 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
269 g_tree_foreach(rt
->tree
, func
, user_data
);
271 tcg_region_tree_unlock_all();
274 size_t tcg_nb_tbs(void)
279 tcg_region_tree_lock_all();
280 for (i
= 0; i
< region
.n
; i
++) {
281 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
283 nb_tbs
+= g_tree_nnodes(rt
->tree
);
285 tcg_region_tree_unlock_all();
289 static void tcg_region_tree_reset_all(void)
293 tcg_region_tree_lock_all();
294 for (i
= 0; i
< region
.n
; i
++) {
295 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
297 /* Increment the refcount first so that destroy acts as a reset */
298 g_tree_ref(rt
->tree
);
299 g_tree_destroy(rt
->tree
);
301 tcg_region_tree_unlock_all();
304 static void tcg_region_bounds(size_t curr_region
, void **pstart
, void **pend
)
308 start
= region
.start_aligned
+ curr_region
* region
.stride
;
309 end
= start
+ region
.size
;
311 if (curr_region
== 0) {
312 start
= region
.after_prologue
;
314 /* The final region may have a few extra pages due to earlier rounding. */
315 if (curr_region
== region
.n
- 1) {
316 end
= region
.start_aligned
+ region
.total_size
;
323 static void tcg_region_assign(TCGContext
*s
, size_t curr_region
)
327 tcg_region_bounds(curr_region
, &start
, &end
);
329 s
->code_gen_buffer
= start
;
330 s
->code_gen_ptr
= start
;
331 s
->code_gen_buffer_size
= end
- start
;
332 s
->code_gen_highwater
= end
- TCG_HIGHWATER
;
335 static bool tcg_region_alloc__locked(TCGContext
*s
)
337 if (region
.current
== region
.n
) {
340 tcg_region_assign(s
, region
.current
);
346 * Request a new region once the one in use has filled up.
347 * Returns true on error.
349 bool tcg_region_alloc(TCGContext
*s
)
352 /* read the region size now; alloc__locked will overwrite it on success */
353 size_t size_full
= s
->code_gen_buffer_size
;
355 qemu_mutex_lock(®ion
.lock
);
356 err
= tcg_region_alloc__locked(s
);
358 region
.agg_size_full
+= size_full
- TCG_HIGHWATER
;
360 qemu_mutex_unlock(®ion
.lock
);
365 * Perform a context's first region allocation.
366 * This function does _not_ increment region.agg_size_full.
368 static void tcg_region_initial_alloc__locked(TCGContext
*s
)
370 bool err
= tcg_region_alloc__locked(s
);
374 void tcg_region_initial_alloc(TCGContext
*s
)
376 qemu_mutex_lock(®ion
.lock
);
377 tcg_region_initial_alloc__locked(s
);
378 qemu_mutex_unlock(®ion
.lock
);
381 /* Call from a safe-work context */
382 void tcg_region_reset_all(void)
384 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
387 qemu_mutex_lock(®ion
.lock
);
389 region
.agg_size_full
= 0;
391 for (i
= 0; i
< n_ctxs
; i
++) {
392 TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
393 tcg_region_initial_alloc__locked(s
);
395 qemu_mutex_unlock(®ion
.lock
);
397 tcg_region_tree_reset_all();
400 static size_t tcg_n_regions(size_t tb_size
, unsigned max_cpus
)
402 #ifdef CONFIG_USER_ONLY
408 * It is likely that some vCPUs will translate more code than others,
409 * so we first try to set more regions than max_cpus, with those regions
410 * being of reasonable size. If that's not possible we make do by evenly
411 * dividing the code_gen_buffer among the vCPUs.
413 /* Use a single region if all we have is one vCPU thread */
414 if (max_cpus
== 1 || !qemu_tcg_mttcg_enabled()) {
419 * Try to have more regions than max_cpus, with each region being >= 2 MB.
420 * If we can't, then just allocate one region per vCPU thread.
422 n_regions
= tb_size
/ (2 * MiB
);
423 if (n_regions
<= max_cpus
) {
426 return MIN(n_regions
, max_cpus
* 8);
431 * Minimum size of the code gen buffer. This number is randomly chosen,
432 * but not so small that we can't have a fair number of TB's live.
434 * Maximum size, MAX_CODE_GEN_BUFFER_SIZE, is defined in tcg-target.h.
435 * Unless otherwise indicated, this is constrained by the range of
436 * direct branches on the host cpu, as used by the TCG implementation
439 #define MIN_CODE_GEN_BUFFER_SIZE (1 * MiB)
441 #if TCG_TARGET_REG_BITS == 32
442 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32 * MiB)
443 #ifdef CONFIG_USER_ONLY
445 * For user mode on smaller 32 bit systems we may run into trouble
446 * allocating big chunks of data in the right place. On these systems
447 * we utilise a static code generation buffer directly in the binary.
449 #define USE_STATIC_CODE_GEN_BUFFER
451 #else /* TCG_TARGET_REG_BITS == 64 */
452 #ifdef CONFIG_USER_ONLY
454 * As user-mode emulation typically means running multiple instances
455 * of the translator don't go too nuts with our default code gen
456 * buffer lest we make things too hard for the OS.
458 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (128 * MiB)
461 * We expect most system emulation to run one or two guests per host.
462 * Users running large scale system emulation may want to tweak their
463 * runtime setup via the tb-size control on the command line.
465 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (1 * GiB)
469 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
470 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
471 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
473 #ifdef USE_STATIC_CODE_GEN_BUFFER
474 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
]
475 __attribute__((aligned(CODE_GEN_ALIGN
)));
477 static int alloc_code_gen_buffer(size_t tb_size
, int splitwx
, Error
**errp
)
483 error_setg(errp
, "jit split-wx not supported");
487 /* page-align the beginning and end of the buffer */
488 buf
= static_code_gen_buffer
;
489 end
= static_code_gen_buffer
+ sizeof(static_code_gen_buffer
);
490 buf
= QEMU_ALIGN_PTR_UP(buf
, qemu_real_host_page_size
);
491 end
= QEMU_ALIGN_PTR_DOWN(end
, qemu_real_host_page_size
);
495 /* Honor a command-line option limiting the size of the buffer. */
496 if (size
> tb_size
) {
497 size
= QEMU_ALIGN_DOWN(tb_size
, qemu_real_host_page_size
);
500 region
.start_aligned
= buf
;
501 region
.total_size
= size
;
503 return PROT_READ
| PROT_WRITE
;
505 #elif defined(_WIN32)
506 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
511 error_setg(errp
, "jit split-wx not supported");
515 buf
= VirtualAlloc(NULL
, size
, MEM_RESERVE
| MEM_COMMIT
,
516 PAGE_EXECUTE_READWRITE
);
518 error_setg_win32(errp
, GetLastError(),
519 "allocate %zu bytes for jit buffer", size
);
523 region
.start_aligned
= buf
;
524 region
.total_size
= size
;
526 return PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
529 static int alloc_code_gen_buffer_anon(size_t size
, int prot
,
530 int flags
, Error
**errp
)
534 buf
= mmap(NULL
, size
, prot
, flags
, -1, 0);
535 if (buf
== MAP_FAILED
) {
536 error_setg_errno(errp
, errno
,
537 "allocate %zu bytes for jit buffer", size
);
541 region
.start_aligned
= buf
;
542 region
.total_size
= size
;
546 #ifndef CONFIG_TCG_INTERPRETER
548 #include "qemu/memfd.h"
550 static bool alloc_code_gen_buffer_splitwx_memfd(size_t size
, Error
**errp
)
552 void *buf_rw
= NULL
, *buf_rx
= MAP_FAILED
;
555 buf_rw
= qemu_memfd_alloc("tcg-jit", size
, 0, &fd
, errp
);
556 if (buf_rw
== NULL
) {
560 buf_rx
= mmap(NULL
, size
, PROT_READ
| PROT_EXEC
, MAP_SHARED
, fd
, 0);
561 if (buf_rx
== MAP_FAILED
) {
566 region
.start_aligned
= buf_rw
;
567 region
.total_size
= size
;
568 tcg_splitwx_diff
= buf_rx
- buf_rw
;
570 return PROT_READ
| PROT_WRITE
;
573 error_setg_errno(errp
, errno
, "failed to map shared memory for execute");
575 if (buf_rx
!= MAP_FAILED
) {
576 munmap(buf_rx
, size
);
579 munmap(buf_rw
, size
);
586 #endif /* CONFIG_POSIX */
589 #include <mach/mach.h>
591 extern kern_return_t
mach_vm_remap(vm_map_t target_task
,
592 mach_vm_address_t
*target_address
,
594 mach_vm_offset_t mask
,
597 mach_vm_address_t src_address
,
599 vm_prot_t
*cur_protection
,
600 vm_prot_t
*max_protection
,
601 vm_inherit_t inheritance
);
603 static int alloc_code_gen_buffer_splitwx_vmremap(size_t size
, Error
**errp
)
606 mach_vm_address_t buf_rw
, buf_rx
;
607 vm_prot_t cur_prot
, max_prot
;
609 /* Map the read-write portion via normal anon memory. */
610 if (!alloc_code_gen_buffer_anon(size
, PROT_READ
| PROT_WRITE
,
611 MAP_PRIVATE
| MAP_ANONYMOUS
, errp
)) {
615 buf_rw
= (mach_vm_address_t
)region
.start_aligned
;
617 ret
= mach_vm_remap(mach_task_self(),
628 if (ret
!= KERN_SUCCESS
) {
629 /* TODO: Convert "ret" to a human readable error message. */
630 error_setg(errp
, "vm_remap for jit splitwx failed");
631 munmap((void *)buf_rw
, size
);
635 if (mprotect((void *)buf_rx
, size
, PROT_READ
| PROT_EXEC
) != 0) {
636 error_setg_errno(errp
, errno
, "mprotect for jit splitwx");
637 munmap((void *)buf_rx
, size
);
638 munmap((void *)buf_rw
, size
);
642 tcg_splitwx_diff
= buf_rx
- buf_rw
;
643 return PROT_READ
| PROT_WRITE
;
645 #endif /* CONFIG_DARWIN */
646 #endif /* CONFIG_TCG_INTERPRETER */
648 static int alloc_code_gen_buffer_splitwx(size_t size
, Error
**errp
)
650 #ifndef CONFIG_TCG_INTERPRETER
651 # ifdef CONFIG_DARWIN
652 return alloc_code_gen_buffer_splitwx_vmremap(size
, errp
);
655 return alloc_code_gen_buffer_splitwx_memfd(size
, errp
);
658 error_setg(errp
, "jit split-wx not supported");
662 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
668 prot
= alloc_code_gen_buffer_splitwx(size
, errp
);
673 * If splitwx force-on (1), fail;
674 * if splitwx default-on (-1), fall through to splitwx off.
679 error_free_or_abort(errp
);
683 * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect
684 * rejects a permission change from RWX -> NONE when reserving the
685 * guard pages later. We can go the other way with the same number
686 * of syscalls, so always begin with PROT_NONE.
689 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
691 /* Applicable to both iOS and macOS (Apple Silicon). */
697 return alloc_code_gen_buffer_anon(size
, prot
, flags
, errp
);
699 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
702 * Initializes region partitioning.
704 * Called at init time from the parent thread (i.e. the one calling
705 * tcg_context_init), after the target's TCG globals have been set.
707 * Region partitioning works by splitting code_gen_buffer into separate regions,
708 * and then assigning regions to TCG threads so that the threads can translate
709 * code in parallel without synchronization.
711 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
712 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
713 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
714 * must have been parsed before calling this function, since it calls
715 * qemu_tcg_mttcg_enabled().
717 * In user-mode we use a single region. Having multiple regions in user-mode
718 * is not supported, because the number of vCPU threads (recall that each thread
719 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
720 * OS, and usually this number is huge (tens of thousands is not uncommon).
721 * Thus, given this large bound on the number of vCPU threads and the fact
722 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
723 * that the availability of at least one region per vCPU thread.
725 * However, this user-mode limitation is unlikely to be a significant problem
726 * in practice. Multi-threaded guests share most if not all of their translated
727 * code, which makes parallel code generation less appealing than in softmmu.
729 void tcg_region_init(size_t tb_size
, int splitwx
, unsigned max_cpus
)
731 const size_t page_size
= qemu_real_host_page_size
;
733 int have_prot
, need_prot
;
735 /* Size the buffer. */
737 size_t phys_mem
= qemu_get_host_physmem();
739 tb_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
741 tb_size
= QEMU_ALIGN_DOWN(phys_mem
/ 8, page_size
);
742 tb_size
= MIN(DEFAULT_CODE_GEN_BUFFER_SIZE
, tb_size
);
745 if (tb_size
< MIN_CODE_GEN_BUFFER_SIZE
) {
746 tb_size
= MIN_CODE_GEN_BUFFER_SIZE
;
748 if (tb_size
> MAX_CODE_GEN_BUFFER_SIZE
) {
749 tb_size
= MAX_CODE_GEN_BUFFER_SIZE
;
752 have_prot
= alloc_code_gen_buffer(tb_size
, splitwx
, &error_fatal
);
753 assert(have_prot
>= 0);
755 /* Request large pages for the buffer and the splitwx. */
756 qemu_madvise(region
.start_aligned
, region
.total_size
, QEMU_MADV_HUGEPAGE
);
757 if (tcg_splitwx_diff
) {
758 qemu_madvise(region
.start_aligned
+ tcg_splitwx_diff
,
759 region
.total_size
, QEMU_MADV_HUGEPAGE
);
763 * Make region_size a multiple of page_size, using aligned as the start.
764 * As a result of this we might end up with a few extra pages at the end of
765 * the buffer; we will assign those to the last region.
767 region
.n
= tcg_n_regions(tb_size
, max_cpus
);
768 region_size
= tb_size
/ region
.n
;
769 region_size
= QEMU_ALIGN_DOWN(region_size
, page_size
);
771 /* A region must have at least 2 pages; one code, one guard */
772 g_assert(region_size
>= 2 * page_size
);
773 region
.stride
= region_size
;
775 /* Reserve space for guard pages. */
776 region
.size
= region_size
- page_size
;
777 region
.total_size
-= page_size
;
780 * The first region will be smaller than the others, via the prologue,
781 * which has yet to be allocated. For now, the first region begins at
784 region
.after_prologue
= region
.start_aligned
;
786 /* init the region struct */
787 qemu_mutex_init(®ion
.lock
);
790 * Set guard pages in the rw buffer, as that's the one into which
791 * buffer overruns could occur. Do not set guard pages in the rx
792 * buffer -- let that one use hugepages throughout.
793 * Work with the page protections set up with the initial mapping.
795 need_prot
= PAGE_READ
| PAGE_WRITE
;
796 #ifndef CONFIG_TCG_INTERPRETER
797 if (tcg_splitwx_diff
== 0) {
798 need_prot
|= PAGE_EXEC
;
801 for (size_t i
= 0, n
= region
.n
; i
< n
; i
++) {
804 tcg_region_bounds(i
, &start
, &end
);
805 if (have_prot
!= need_prot
) {
808 if (need_prot
== (PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
)) {
809 rc
= qemu_mprotect_rwx(start
, end
- start
);
810 } else if (need_prot
== (PAGE_READ
| PAGE_WRITE
)) {
811 rc
= qemu_mprotect_rw(start
, end
- start
);
813 g_assert_not_reached();
816 error_setg_errno(&error_fatal
, errno
,
817 "mprotect of jit buffer");
820 if (have_prot
!= 0) {
821 /* Guard pages are nice for bug detection but are not essential. */
822 (void)qemu_mprotect_none(end
, page_size
);
826 tcg_region_trees_init();
829 * Leave the initial context initialized to the first region.
830 * This will be the context into which we generate the prologue.
831 * It is also the only context for CONFIG_USER_ONLY.
833 tcg_region_initial_alloc__locked(&tcg_init_ctx
);
836 void tcg_region_prologue_set(TCGContext
*s
)
838 /* Deduct the prologue from the first region. */
839 g_assert(region
.start_aligned
== s
->code_gen_buffer
);
840 region
.after_prologue
= s
->code_ptr
;
842 /* Recompute boundaries of the first region. */
843 tcg_region_assign(s
, 0);
845 /* Register the balance of the buffer with gdb. */
846 tcg_register_jit(tcg_splitwx_to_rx(region
.after_prologue
),
847 region
.start_aligned
+ region
.total_size
-
848 region
.after_prologue
);
852 * Returns the size (in bytes) of all translated code (i.e. from all regions)
853 * currently in the cache.
854 * See also: tcg_code_capacity()
855 * Do not confuse with tcg_current_code_size(); that one applies to a single
858 size_t tcg_code_size(void)
860 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
864 qemu_mutex_lock(®ion
.lock
);
865 total
= region
.agg_size_full
;
866 for (i
= 0; i
< n_ctxs
; i
++) {
867 const TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
870 size
= qatomic_read(&s
->code_gen_ptr
) - s
->code_gen_buffer
;
871 g_assert(size
<= s
->code_gen_buffer_size
);
874 qemu_mutex_unlock(®ion
.lock
);
879 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
881 * See also: tcg_code_size()
883 size_t tcg_code_capacity(void)
885 size_t guard_size
, capacity
;
887 /* no need for synchronization; these variables are set at init time */
888 guard_size
= region
.stride
- region
.size
;
889 capacity
= region
.total_size
;
890 capacity
-= (region
.n
- 1) * guard_size
;
891 capacity
-= region
.n
* TCG_HIGHWATER
;