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 "qapi/error.h"
28 #include "exec/exec-all.h"
30 #include "tcg-internal.h"
33 struct tcg_region_tree
{
36 /* padding to avoid false sharing is computed at run-time */
40 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
41 * dynamically allocate from as demand dictates. Given appropriate region
42 * sizing, this minimizes flushes even when some TCG threads generate a lot
43 * more code than others.
45 struct tcg_region_state
{
48 /* fields set at init time */
52 size_t size
; /* size of one region */
53 size_t stride
; /* .size + guard size */
54 size_t total_size
; /* size of entire buffer, >= n * stride */
56 /* fields protected by the lock */
57 size_t current
; /* current region index */
58 size_t agg_size_full
; /* aggregate size of full regions */
61 static struct tcg_region_state region
;
64 * This is an array of struct tcg_region_tree's, with padding.
65 * We use void * to simplify the computation of region_trees[i]; each
66 * struct is found every tree_size bytes.
68 static void *region_trees
;
69 static size_t tree_size
;
71 bool in_code_gen_buffer(const void *p
)
74 * Much like it is valid to have a pointer to the byte past the
75 * end of an array (so long as you don't dereference it), allow
76 * a pointer to the byte past the end of the code gen buffer.
78 return (size_t)(p
- region
.start_aligned
) <= region
.total_size
;
81 #ifdef CONFIG_DEBUG_TCG
82 const void *tcg_splitwx_to_rx(void *rw
)
84 /* Pass NULL pointers unchanged. */
86 g_assert(in_code_gen_buffer(rw
));
87 rw
+= tcg_splitwx_diff
;
92 void *tcg_splitwx_to_rw(const void *rx
)
94 /* Pass NULL pointers unchanged. */
96 rx
-= tcg_splitwx_diff
;
97 /* Assert that we end with a pointer in the rw region. */
98 g_assert(in_code_gen_buffer(rx
));
102 #endif /* CONFIG_DEBUG_TCG */
104 /* compare a pointer @ptr and a tb_tc @s */
105 static int ptr_cmp_tb_tc(const void *ptr
, const struct tb_tc
*s
)
107 if (ptr
>= s
->ptr
+ s
->size
) {
109 } else if (ptr
< s
->ptr
) {
115 static gint
tb_tc_cmp(gconstpointer ap
, gconstpointer bp
, gpointer userdata
)
117 const struct tb_tc
*a
= ap
;
118 const struct tb_tc
*b
= bp
;
121 * When both sizes are set, we know this isn't a lookup.
122 * This is the most likely case: every TB must be inserted; lookups
123 * are a lot less frequent.
125 if (likely(a
->size
&& b
->size
)) {
126 if (a
->ptr
> b
->ptr
) {
128 } else if (a
->ptr
< b
->ptr
) {
131 /* a->ptr == b->ptr should happen only on deletions */
132 g_assert(a
->size
== b
->size
);
136 * All lookups have either .size field set to 0.
137 * From the glib sources we see that @ap is always the lookup key. However
138 * the docs provide no guarantee, so we just mark this case as likely.
140 if (likely(a
->size
== 0)) {
141 return ptr_cmp_tb_tc(a
->ptr
, b
);
143 return ptr_cmp_tb_tc(b
->ptr
, a
);
146 static void tb_destroy(gpointer value
)
148 TranslationBlock
*tb
= value
;
149 qemu_spin_destroy(&tb
->jmp_lock
);
152 static void tcg_region_trees_init(void)
156 tree_size
= ROUND_UP(sizeof(struct tcg_region_tree
), qemu_dcache_linesize
);
157 region_trees
= qemu_memalign(qemu_dcache_linesize
, region
.n
* tree_size
);
158 for (i
= 0; i
< region
.n
; i
++) {
159 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
161 qemu_mutex_init(&rt
->lock
);
162 rt
->tree
= g_tree_new_full(tb_tc_cmp
, NULL
, NULL
, tb_destroy
);
166 static struct tcg_region_tree
*tc_ptr_to_region_tree(const void *p
)
171 * Like tcg_splitwx_to_rw, with no assert. The pc may come from
172 * a signal handler over which the caller has no control.
174 if (!in_code_gen_buffer(p
)) {
175 p
-= tcg_splitwx_diff
;
176 if (!in_code_gen_buffer(p
)) {
181 if (p
< region
.start_aligned
) {
184 ptrdiff_t offset
= p
- region
.start_aligned
;
186 if (offset
> region
.stride
* (region
.n
- 1)) {
187 region_idx
= region
.n
- 1;
189 region_idx
= offset
/ region
.stride
;
192 return region_trees
+ region_idx
* tree_size
;
195 void tcg_tb_insert(TranslationBlock
*tb
)
197 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
199 g_assert(rt
!= NULL
);
200 qemu_mutex_lock(&rt
->lock
);
201 g_tree_insert(rt
->tree
, &tb
->tc
, tb
);
202 qemu_mutex_unlock(&rt
->lock
);
205 void tcg_tb_remove(TranslationBlock
*tb
)
207 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
209 g_assert(rt
!= NULL
);
210 qemu_mutex_lock(&rt
->lock
);
211 g_tree_remove(rt
->tree
, &tb
->tc
);
212 qemu_mutex_unlock(&rt
->lock
);
216 * Find the TB 'tb' such that
217 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
218 * Return NULL if not found.
220 TranslationBlock
*tcg_tb_lookup(uintptr_t tc_ptr
)
222 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree((void *)tc_ptr
);
223 TranslationBlock
*tb
;
224 struct tb_tc s
= { .ptr
= (void *)tc_ptr
};
230 qemu_mutex_lock(&rt
->lock
);
231 tb
= g_tree_lookup(rt
->tree
, &s
);
232 qemu_mutex_unlock(&rt
->lock
);
236 static void tcg_region_tree_lock_all(void)
240 for (i
= 0; i
< region
.n
; i
++) {
241 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
243 qemu_mutex_lock(&rt
->lock
);
247 static void tcg_region_tree_unlock_all(void)
251 for (i
= 0; i
< region
.n
; i
++) {
252 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
254 qemu_mutex_unlock(&rt
->lock
);
258 void tcg_tb_foreach(GTraverseFunc func
, gpointer user_data
)
262 tcg_region_tree_lock_all();
263 for (i
= 0; i
< region
.n
; i
++) {
264 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
266 g_tree_foreach(rt
->tree
, func
, user_data
);
268 tcg_region_tree_unlock_all();
271 size_t tcg_nb_tbs(void)
276 tcg_region_tree_lock_all();
277 for (i
= 0; i
< region
.n
; i
++) {
278 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
280 nb_tbs
+= g_tree_nnodes(rt
->tree
);
282 tcg_region_tree_unlock_all();
286 static void tcg_region_tree_reset_all(void)
290 tcg_region_tree_lock_all();
291 for (i
= 0; i
< region
.n
; i
++) {
292 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
294 /* Increment the refcount first so that destroy acts as a reset */
295 g_tree_ref(rt
->tree
);
296 g_tree_destroy(rt
->tree
);
298 tcg_region_tree_unlock_all();
301 static void tcg_region_bounds(size_t curr_region
, void **pstart
, void **pend
)
305 start
= region
.start_aligned
+ curr_region
* region
.stride
;
306 end
= start
+ region
.size
;
308 if (curr_region
== 0) {
309 start
= region
.after_prologue
;
311 /* The final region may have a few extra pages due to earlier rounding. */
312 if (curr_region
== region
.n
- 1) {
313 end
= region
.start_aligned
+ region
.total_size
;
320 static void tcg_region_assign(TCGContext
*s
, size_t curr_region
)
324 tcg_region_bounds(curr_region
, &start
, &end
);
326 s
->code_gen_buffer
= start
;
327 s
->code_gen_ptr
= start
;
328 s
->code_gen_buffer_size
= end
- start
;
329 s
->code_gen_highwater
= end
- TCG_HIGHWATER
;
332 static bool tcg_region_alloc__locked(TCGContext
*s
)
334 if (region
.current
== region
.n
) {
337 tcg_region_assign(s
, region
.current
);
343 * Request a new region once the one in use has filled up.
344 * Returns true on error.
346 bool tcg_region_alloc(TCGContext
*s
)
349 /* read the region size now; alloc__locked will overwrite it on success */
350 size_t size_full
= s
->code_gen_buffer_size
;
352 qemu_mutex_lock(®ion
.lock
);
353 err
= tcg_region_alloc__locked(s
);
355 region
.agg_size_full
+= size_full
- TCG_HIGHWATER
;
357 qemu_mutex_unlock(®ion
.lock
);
362 * Perform a context's first region allocation.
363 * This function does _not_ increment region.agg_size_full.
365 static void tcg_region_initial_alloc__locked(TCGContext
*s
)
367 bool err
= tcg_region_alloc__locked(s
);
371 void tcg_region_initial_alloc(TCGContext
*s
)
373 qemu_mutex_lock(®ion
.lock
);
374 tcg_region_initial_alloc__locked(s
);
375 qemu_mutex_unlock(®ion
.lock
);
378 /* Call from a safe-work context */
379 void tcg_region_reset_all(void)
381 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
384 qemu_mutex_lock(®ion
.lock
);
386 region
.agg_size_full
= 0;
388 for (i
= 0; i
< n_ctxs
; i
++) {
389 TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
390 tcg_region_initial_alloc__locked(s
);
392 qemu_mutex_unlock(®ion
.lock
);
394 tcg_region_tree_reset_all();
397 static size_t tcg_n_regions(size_t tb_size
, unsigned max_cpus
)
399 #ifdef CONFIG_USER_ONLY
405 * It is likely that some vCPUs will translate more code than others,
406 * so we first try to set more regions than max_cpus, with those regions
407 * being of reasonable size. If that's not possible we make do by evenly
408 * dividing the code_gen_buffer among the vCPUs.
410 /* Use a single region if all we have is one vCPU thread */
411 if (max_cpus
== 1 || !qemu_tcg_mttcg_enabled()) {
416 * Try to have more regions than max_cpus, with each region being >= 2 MB.
417 * If we can't, then just allocate one region per vCPU thread.
419 n_regions
= tb_size
/ (2 * MiB
);
420 if (n_regions
<= max_cpus
) {
423 return MIN(n_regions
, max_cpus
* 8);
428 * Minimum size of the code gen buffer. This number is randomly chosen,
429 * but not so small that we can't have a fair number of TB's live.
431 * Maximum size, MAX_CODE_GEN_BUFFER_SIZE, is defined in tcg-target.h.
432 * Unless otherwise indicated, this is constrained by the range of
433 * direct branches on the host cpu, as used by the TCG implementation
436 #define MIN_CODE_GEN_BUFFER_SIZE (1 * MiB)
438 #if TCG_TARGET_REG_BITS == 32
439 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32 * MiB)
440 #ifdef CONFIG_USER_ONLY
442 * For user mode on smaller 32 bit systems we may run into trouble
443 * allocating big chunks of data in the right place. On these systems
444 * we utilise a static code generation buffer directly in the binary.
446 #define USE_STATIC_CODE_GEN_BUFFER
448 #else /* TCG_TARGET_REG_BITS == 64 */
449 #ifdef CONFIG_USER_ONLY
451 * As user-mode emulation typically means running multiple instances
452 * of the translator don't go too nuts with our default code gen
453 * buffer lest we make things too hard for the OS.
455 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (128 * MiB)
458 * We expect most system emulation to run one or two guests per host.
459 * Users running large scale system emulation may want to tweak their
460 * runtime setup via the tb-size control on the command line.
462 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (1 * GiB)
466 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
467 (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
468 ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
470 #ifdef USE_STATIC_CODE_GEN_BUFFER
471 static uint8_t static_code_gen_buffer
[DEFAULT_CODE_GEN_BUFFER_SIZE
]
472 __attribute__((aligned(CODE_GEN_ALIGN
)));
474 static int alloc_code_gen_buffer(size_t tb_size
, int splitwx
, Error
**errp
)
480 error_setg(errp
, "jit split-wx not supported");
484 /* page-align the beginning and end of the buffer */
485 buf
= static_code_gen_buffer
;
486 end
= static_code_gen_buffer
+ sizeof(static_code_gen_buffer
);
487 buf
= QEMU_ALIGN_PTR_UP(buf
, qemu_real_host_page_size
);
488 end
= QEMU_ALIGN_PTR_DOWN(end
, qemu_real_host_page_size
);
492 /* Honor a command-line option limiting the size of the buffer. */
493 if (size
> tb_size
) {
494 size
= QEMU_ALIGN_DOWN(tb_size
, qemu_real_host_page_size
);
497 region
.start_aligned
= buf
;
498 region
.total_size
= size
;
500 return PROT_READ
| PROT_WRITE
;
502 #elif defined(_WIN32)
503 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
508 error_setg(errp
, "jit split-wx not supported");
512 buf
= VirtualAlloc(NULL
, size
, MEM_RESERVE
| MEM_COMMIT
,
513 PAGE_EXECUTE_READWRITE
);
515 error_setg_win32(errp
, GetLastError(),
516 "allocate %zu bytes for jit buffer", size
);
520 region
.start_aligned
= buf
;
521 region
.total_size
= size
;
523 return PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
526 static int alloc_code_gen_buffer_anon(size_t size
, int prot
,
527 int flags
, Error
**errp
)
531 buf
= mmap(NULL
, size
, prot
, flags
, -1, 0);
532 if (buf
== MAP_FAILED
) {
533 error_setg_errno(errp
, errno
,
534 "allocate %zu bytes for jit buffer", size
);
538 region
.start_aligned
= buf
;
539 region
.total_size
= size
;
543 #ifndef CONFIG_TCG_INTERPRETER
545 #include "qemu/memfd.h"
547 static bool alloc_code_gen_buffer_splitwx_memfd(size_t size
, Error
**errp
)
549 void *buf_rw
= NULL
, *buf_rx
= MAP_FAILED
;
552 buf_rw
= qemu_memfd_alloc("tcg-jit", size
, 0, &fd
, errp
);
553 if (buf_rw
== NULL
) {
557 buf_rx
= mmap(NULL
, size
, PROT_READ
| PROT_EXEC
, MAP_SHARED
, fd
, 0);
558 if (buf_rx
== MAP_FAILED
) {
563 region
.start_aligned
= buf_rw
;
564 region
.total_size
= size
;
565 tcg_splitwx_diff
= buf_rx
- buf_rw
;
567 return PROT_READ
| PROT_WRITE
;
570 error_setg_errno(errp
, errno
, "failed to map shared memory for execute");
572 if (buf_rx
!= MAP_FAILED
) {
573 munmap(buf_rx
, size
);
576 munmap(buf_rw
, size
);
583 #endif /* CONFIG_POSIX */
586 #include <mach/mach.h>
588 extern kern_return_t
mach_vm_remap(vm_map_t target_task
,
589 mach_vm_address_t
*target_address
,
591 mach_vm_offset_t mask
,
594 mach_vm_address_t src_address
,
596 vm_prot_t
*cur_protection
,
597 vm_prot_t
*max_protection
,
598 vm_inherit_t inheritance
);
600 static int alloc_code_gen_buffer_splitwx_vmremap(size_t size
, Error
**errp
)
603 mach_vm_address_t buf_rw
, buf_rx
;
604 vm_prot_t cur_prot
, max_prot
;
606 /* Map the read-write portion via normal anon memory. */
607 if (!alloc_code_gen_buffer_anon(size
, PROT_READ
| PROT_WRITE
,
608 MAP_PRIVATE
| MAP_ANONYMOUS
, errp
)) {
612 buf_rw
= (mach_vm_address_t
)region
.start_aligned
;
614 ret
= mach_vm_remap(mach_task_self(),
625 if (ret
!= KERN_SUCCESS
) {
626 /* TODO: Convert "ret" to a human readable error message. */
627 error_setg(errp
, "vm_remap for jit splitwx failed");
628 munmap((void *)buf_rw
, size
);
632 if (mprotect((void *)buf_rx
, size
, PROT_READ
| PROT_EXEC
) != 0) {
633 error_setg_errno(errp
, errno
, "mprotect for jit splitwx");
634 munmap((void *)buf_rx
, size
);
635 munmap((void *)buf_rw
, size
);
639 tcg_splitwx_diff
= buf_rx
- buf_rw
;
640 return PROT_READ
| PROT_WRITE
;
642 #endif /* CONFIG_DARWIN */
643 #endif /* CONFIG_TCG_INTERPRETER */
645 static int alloc_code_gen_buffer_splitwx(size_t size
, Error
**errp
)
647 #ifndef CONFIG_TCG_INTERPRETER
648 # ifdef CONFIG_DARWIN
649 return alloc_code_gen_buffer_splitwx_vmremap(size
, errp
);
652 return alloc_code_gen_buffer_splitwx_memfd(size
, errp
);
655 error_setg(errp
, "jit split-wx not supported");
659 static int alloc_code_gen_buffer(size_t size
, int splitwx
, Error
**errp
)
665 prot
= alloc_code_gen_buffer_splitwx(size
, errp
);
670 * If splitwx force-on (1), fail;
671 * if splitwx default-on (-1), fall through to splitwx off.
676 error_free_or_abort(errp
);
680 * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect
681 * rejects a permission change from RWX -> NONE when reserving the
682 * guard pages later. We can go the other way with the same number
683 * of syscalls, so always begin with PROT_NONE.
686 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
688 /* Applicable to both iOS and macOS (Apple Silicon). */
694 return alloc_code_gen_buffer_anon(size
, prot
, flags
, errp
);
696 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
699 * Initializes region partitioning.
701 * Called at init time from the parent thread (i.e. the one calling
702 * tcg_context_init), after the target's TCG globals have been set.
704 * Region partitioning works by splitting code_gen_buffer into separate regions,
705 * and then assigning regions to TCG threads so that the threads can translate
706 * code in parallel without synchronization.
708 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
709 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
710 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
711 * must have been parsed before calling this function, since it calls
712 * qemu_tcg_mttcg_enabled().
714 * In user-mode we use a single region. Having multiple regions in user-mode
715 * is not supported, because the number of vCPU threads (recall that each thread
716 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
717 * OS, and usually this number is huge (tens of thousands is not uncommon).
718 * Thus, given this large bound on the number of vCPU threads and the fact
719 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
720 * that the availability of at least one region per vCPU thread.
722 * However, this user-mode limitation is unlikely to be a significant problem
723 * in practice. Multi-threaded guests share most if not all of their translated
724 * code, which makes parallel code generation less appealing than in softmmu.
726 void tcg_region_init(size_t tb_size
, int splitwx
, unsigned max_cpus
)
728 const size_t page_size
= qemu_real_host_page_size
;
730 int have_prot
, need_prot
;
732 /* Size the buffer. */
734 size_t phys_mem
= qemu_get_host_physmem();
736 tb_size
= DEFAULT_CODE_GEN_BUFFER_SIZE
;
738 tb_size
= QEMU_ALIGN_DOWN(phys_mem
/ 8, page_size
);
739 tb_size
= MIN(DEFAULT_CODE_GEN_BUFFER_SIZE
, tb_size
);
742 if (tb_size
< MIN_CODE_GEN_BUFFER_SIZE
) {
743 tb_size
= MIN_CODE_GEN_BUFFER_SIZE
;
745 if (tb_size
> MAX_CODE_GEN_BUFFER_SIZE
) {
746 tb_size
= MAX_CODE_GEN_BUFFER_SIZE
;
749 have_prot
= alloc_code_gen_buffer(tb_size
, splitwx
, &error_fatal
);
750 assert(have_prot
>= 0);
752 /* Request large pages for the buffer and the splitwx. */
753 qemu_madvise(region
.start_aligned
, region
.total_size
, QEMU_MADV_HUGEPAGE
);
754 if (tcg_splitwx_diff
) {
755 qemu_madvise(region
.start_aligned
+ tcg_splitwx_diff
,
756 region
.total_size
, QEMU_MADV_HUGEPAGE
);
760 * Make region_size a multiple of page_size, using aligned as the start.
761 * As a result of this we might end up with a few extra pages at the end of
762 * the buffer; we will assign those to the last region.
764 region
.n
= tcg_n_regions(tb_size
, max_cpus
);
765 region_size
= tb_size
/ region
.n
;
766 region_size
= QEMU_ALIGN_DOWN(region_size
, page_size
);
768 /* A region must have at least 2 pages; one code, one guard */
769 g_assert(region_size
>= 2 * page_size
);
770 region
.stride
= region_size
;
772 /* Reserve space for guard pages. */
773 region
.size
= region_size
- page_size
;
774 region
.total_size
-= page_size
;
777 * The first region will be smaller than the others, via the prologue,
778 * which has yet to be allocated. For now, the first region begins at
781 region
.after_prologue
= region
.start_aligned
;
783 /* init the region struct */
784 qemu_mutex_init(®ion
.lock
);
787 * Set guard pages in the rw buffer, as that's the one into which
788 * buffer overruns could occur. Do not set guard pages in the rx
789 * buffer -- let that one use hugepages throughout.
790 * Work with the page protections set up with the initial mapping.
792 need_prot
= PAGE_READ
| PAGE_WRITE
;
793 #ifndef CONFIG_TCG_INTERPRETER
794 if (tcg_splitwx_diff
== 0) {
795 need_prot
|= PAGE_EXEC
;
798 for (size_t i
= 0, n
= region
.n
; i
< n
; i
++) {
801 tcg_region_bounds(i
, &start
, &end
);
802 if (have_prot
!= need_prot
) {
805 if (need_prot
== (PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
)) {
806 rc
= qemu_mprotect_rwx(start
, end
- start
);
807 } else if (need_prot
== (PAGE_READ
| PAGE_WRITE
)) {
808 rc
= qemu_mprotect_rw(start
, end
- start
);
810 g_assert_not_reached();
813 error_setg_errno(&error_fatal
, errno
,
814 "mprotect of jit buffer");
817 if (have_prot
!= 0) {
818 /* Guard pages are nice for bug detection but are not essential. */
819 (void)qemu_mprotect_none(end
, page_size
);
823 tcg_region_trees_init();
826 * Leave the initial context initialized to the first region.
827 * This will be the context into which we generate the prologue.
828 * It is also the only context for CONFIG_USER_ONLY.
830 tcg_region_initial_alloc__locked(&tcg_init_ctx
);
833 void tcg_region_prologue_set(TCGContext
*s
)
835 /* Deduct the prologue from the first region. */
836 g_assert(region
.start_aligned
== s
->code_gen_buffer
);
837 region
.after_prologue
= s
->code_ptr
;
839 /* Recompute boundaries of the first region. */
840 tcg_region_assign(s
, 0);
842 /* Register the balance of the buffer with gdb. */
843 tcg_register_jit(tcg_splitwx_to_rx(region
.after_prologue
),
844 region
.start_aligned
+ region
.total_size
-
845 region
.after_prologue
);
849 * Returns the size (in bytes) of all translated code (i.e. from all regions)
850 * currently in the cache.
851 * See also: tcg_code_capacity()
852 * Do not confuse with tcg_current_code_size(); that one applies to a single
855 size_t tcg_code_size(void)
857 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
861 qemu_mutex_lock(®ion
.lock
);
862 total
= region
.agg_size_full
;
863 for (i
= 0; i
< n_ctxs
; i
++) {
864 const TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
867 size
= qatomic_read(&s
->code_gen_ptr
) - s
->code_gen_buffer
;
868 g_assert(size
<= s
->code_gen_buffer_size
);
871 qemu_mutex_unlock(®ion
.lock
);
876 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
878 * See also: tcg_code_size()
880 size_t tcg_code_capacity(void)
882 size_t guard_size
, capacity
;
884 /* no need for synchronization; these variables are set at init time */
885 guard_size
= region
.stride
- region
.size
;
886 capacity
= region
.total_size
;
887 capacity
-= (region
.n
- 1) * guard_size
;
888 capacity
-= region
.n
* TCG_HIGHWATER
;