Compare commits

...
Author SHA1 Message Date
osy cd46296ce8 tcg (aarch64 target): fix indirect tb jumps
The LDR only works in a +/- 1MiB range. We expand this to a 4GiB range by using ADRP+LDR when needed.
2020-02-08 20:42:37 -08:00
osy 252beadf13 tcg: support exec memory locking
To support iOS which does not allow allocating RWX pages, we can only mmap
initially as RW and later mprotect it as RX. The full details are
documented in docs/devel/ios.rst

This largely reverts 6e3b2bfd6a

On iOS, because we must lock/unlock executable memory for writing, it does
not make sense to store the TBs in executable memory. Instead, we will use
the beginning of each tcg region for TBs.

In order to make the most effective use of space, after a TLB flush, we
find the average size of a code block in previous allocations and then set
aside enough space for future allocations.

This is less space efficient than storing the TB before the code block but
can be more space efficient than the static allocations before that
implementation.

With multiple TCG contexts there is an issue of one vCPU executing a TB
generated by another vCPU and therefore the executable page unlocking can
happen during the execution.

For each TB, we keep a pointer to the tcg_ctx.code_locked_top_page and can
check that a given TB is executable if either 1) it was generated in the
same host thread as the tcg_ctx (then we would be running it after locking
the memory) or 2) it is below the water level for locked exec page (for
that TCG context). We will only return an existing TB in a search if it
matches either condition.

We do this to prevent having to (mutex) lock while generating code and
ruin the advantages of having multiple TCG threads. However, there is now
the possibility of two vCPUs generating the same TB and we have to just
deal with that overhead.
2020-02-08 20:42:37 -08:00
13 changed files with 394 additions and 5 deletions

No files matched your search

+29 -1
View File
@@ -169,6 +169,18 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb)
}
#endif /* DEBUG_DISAS */
#if defined(CONFIG_NO_RWX)
/* We only have to lock the exec memory if we are trying to exec a
* block in a page that is potentially unlocked. If we are using a
* old block that is under the water mark for unlocked page, then
* it is fine to execute without locking anything. We must make
* sure that any cached jumps do not go into potentially unlocked
* memory here. */
if (*itb->p_code_locked_top_page < itb->tc.ptr + itb->tc.size) {
tb_exec_memory_lock();
}
#endif
ret = tcg_qemu_tb_exec(env, tb_ptr);
cpu->can_do_io = 1;
last_tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
@@ -297,6 +309,9 @@ static bool tb_lookup_cmp(const void *p, const void *d)
const struct tb_desc *desc = d;
if (tb->pc == desc->pc &&
#if defined(CONFIG_NO_RWX)
tb_is_exec(tb) &&
#endif
tb->page_addr[0] == desc->phys_page1 &&
tb->cs_base == desc->cs_base &&
tb->flags == desc->flags &&
@@ -418,8 +433,21 @@ static inline TranslationBlock *tb_find(CPUState *cpu,
}
#endif
/* See if we can patch the calling TB. */
if (last_tb) {
tb_add_jump(last_tb, tb_exit, tb);
#if defined(CONFIG_NO_RWX)
/* In case of exec locked/unlocked pages, we don't want to link to a page
* that can potentially be unlocked. Therefore, we delay the jump linking
* until the next time we fetch it and we know the unlocked watermark is
* above the tb OR if it ends in the same page as last_tb. */
if (((uintptr_t)(last_tb->tc.ptr + last_tb->tc.size - 1) & qemu_host_page_mask) ==
((uintptr_t)(tb->tc.ptr + tb->tc.size) & qemu_host_page_mask) ||
(*tb->p_code_locked_top_page >= tb->tc.ptr + tb->tc.size)) {
#endif // defined(CONFIG_NO_RWX)
tb_add_jump(last_tb, tb_exit, tb);
#if defined(CONFIG_NO_RWX)
}
#endif // defined(CONFIG_NO_RWX)
}
return tb;
}
+9
View File
@@ -155,6 +155,15 @@ void *HELPER(lookup_tb_ptr)(CPUArchState *env)
if (tb == NULL) {
return tcg_ctx->code_gen_epilogue;
}
#if defined(CONFIG_NO_RWX)
/* In the corner case that we're executing WITHOUT locking the exec memory
* because the first TB was below the locked mem water level, then we have
* to lock the mem here. */
if (tcg_ctx->code_locked_top_page == *tb->p_code_locked_top_page &&
tcg_ctx->code_locked_top_page < tb->tc.ptr + tb->tc.size) {
tb_exec_memory_lock();
}
#endif
qemu_log_mask_and_addr(CPU_LOG_EXEC, pc,
"Chain %d: %p ["
TARGET_FMT_lx "/" TARGET_FMT_lx "/%#x] %s\n",
+40
View File
@@ -1030,7 +1030,11 @@ static inline void *alloc_code_gen_buffer(void)
#else
static inline void *alloc_code_gen_buffer(void)
{
#if defined(CONFIG_NO_RWX) // iOS requires W^X
int prot = PROT_WRITE | PROT_READ;
#else
int prot = PROT_WRITE | PROT_READ | PROT_EXEC;
#endif
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
uintptr_t start = 0;
size_t size = tcg_ctx->code_gen_buffer_size;
@@ -1124,6 +1128,9 @@ static bool tb_cmp(const void *ap, const void *bp)
const TranslationBlock *b = bp;
return a->pc == b->pc &&
#if defined(CONFIG_NO_RWX) // check if from different regions
a->p_code_locked_top_page == b->p_code_locked_top_page &&
#endif
a->cs_base == b->cs_base &&
a->flags == b->flags &&
(tb_cflags(a) & CF_HASH_MASK) == (tb_cflags(b) & CF_HASH_MASK) &&
@@ -1754,6 +1761,10 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
ti = profile_getclock();
#endif
#if defined(CONFIG_NO_RWX)
tcg_exec_memory_unlock(tcg_ctx);
#endif
gen_code_size = tcg_gen_code(tcg_ctx, tb);
if (unlikely(gen_code_size < 0)) {
switch (gen_code_size) {
@@ -1868,9 +1879,19 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
if (unlikely(existing_tb != tb)) {
uintptr_t orig_aligned = (uintptr_t)gen_code_buf;
#if defined(CONFIG_NO_RWX)
g_assert(tcg_ctx->nb_tbs > 0);
atomic_dec(&tcg_ctx->nb_tbs);
#else // if TB is allocated in the code buffer
orig_aligned -= ROUND_UP(sizeof(*tb), qemu_icache_linesize);
#endif
atomic_set(&tcg_ctx->code_gen_ptr, (void *)orig_aligned);
return existing_tb;
} else {
#if defined(CONFIG_NO_RWX)
/* count total size of code blocks */
tcg_ctx->tb_total_sizes += ((uintptr_t)tcg_ctx->code_gen_ptr - (uintptr_t)gen_code_buf);
#endif
}
tcg_tb_insert(tb);
return tb;
@@ -2674,3 +2695,22 @@ void tcg_flush_softmmu_tlb(CPUState *cs)
tlb_flush(cs);
#endif
}
#if defined(CONFIG_NO_RWX)
void tb_exec_memory_lock(void)
{
tcg_exec_memory_lock(tcg_ctx);
}
/* We can execute a TB if it's either 1) running on the same thread
* as the current tcg_ctx or 2) below the exec locked water mark.
*/
bool tb_is_exec(const TranslationBlock *tb)
{
const void *code_locked_top_page;
code_locked_top_page = atomic_read(tb->p_code_locked_top_page);
return code_locked_top_page == tcg_ctx->code_locked_top_page ||
code_locked_top_page >= tb->tc.ptr + tb->tc.size;
}
#endif // defined(CONFIG_NO_RWX)
Vendored
+1
View File
@@ -6804,6 +6804,7 @@ fi
if test "$ios" = "yes" ; then
echo "CONFIG_IOS=y" >> $config_host_mak
echo "CONFIG_NO_RWX=y" >> $config_host_mak
fi
if test "$solaris" = "yes" ; then
+1
View File
@@ -23,3 +23,4 @@ Contents:
secure-coding-practices
tcg
tcg-plugins
ios
+120
View File
@@ -0,0 +1,120 @@
===========
iOS Support
===========
To run qemu on the iOS platform, some modifications were required. Most of the
modifications are conditioned on the ``CONFIG_IOS`` and ``CONFIG_NO_RWX``
configuration variables.
Build support
-------------
For the code to compile, certain changes in the block driver and the slirp
driver had to be made. There is no ``system()`` call, so code requiring it had
to be disabled.
``ucontext`` support is broken on iOS. The implementation from ``libucontext``
is used instead.
Because ``fork()`` is not allowed on iOS apps, the option to build qemu and the
utilities as shared libraries is added. Note that because qemu does not perform
resource cleanup in most cases (open files, allocated memory, etc), it is
advisable that the user implements a proxy layer for syscalls so resources can
be kept track by the app that uses qemu as a shared library.
Executable memory locking
-------------------------
The iOS kernel does not permit ``mmap()`` pages with
``PROT_READ | PROT_WRITE | PROT_EXEC``. However, it does allow allocating pages
with only ``PROT_READ | PROT_WRITE`` and then later calling ``mprotect()`` with
``PROT_READ | PROT_EXEC``. A page can never be both writable and executable.
In this document, we will refer to a page that is read-writable as "unlocked"
and a page that is read-executable as "locked." Because ``mprotect()`` is an
expensive call, we try to defer calling it until we need to and also try to
avoid calling it unless it is absolutely needed.
One approach would be to unlock the entire TCG region when a TB translation is
being done and then lock the entire region when a TB is about to be executed.
This would require thousands of pages to be locked and unlocked all the time.
Additionally, it means that different vCPU threads cannot share the same TLB
cache.
TB allocation changes
---------------------
To improve the performance, we first notice that ``tcg_tb_alloc()`` returns a
chunk of memory that must be unlocked. A recent change in qemu places the TB
structure close to the code buffer in order to improve both cache locality and
reduce code size and memory usage. Unfortunately, we have to regress this
improvement as any benefit from it is negated with the need to unlock the memory
whenever we need to mutate the TB structure.
We go back to the old method of statically allocating a large buffer for all
TBs in a region. However a few improvements are made. First, we try to respect
the locality by making this buffer close to the code. Second, whenever we flush
the TB cache, we will use the average size of code blocks to divide up the TCG
region into space for TB structures and space for code blocks.
Locked memory water level
-------------------------
By moving the TB allocation, we made it such that the memory only needs to be
unlocked in the context of ``tb_gen_code()``. Because the code buffer pointer
only grows downwards (we do not ever "free" code blocks and have holes), we
only ever need to unlock at most one page.
We can think of the entire TCG region divided into two sections: the locked
section and the unlocked section. At the start, the entire region is unlocked.
As more and more code blocks are generated, the allocation pointer moves
upwards. We can then lock the memory of any memory below the allocation pointer
as the code generated is immutable. Therefore we can keep a second pointer to
the highest page boundary the allocation pointer passes and keep all the memory
below that pointer (all the way to the start of the region) locked and all the
memory above it unlocked. This pointer is our locked water level.
That way, assuming all pages are unlocked at the start, we will progressively
lock more pages as more code is generated. The only page we ever need to unlock
would be the page pointed to by our locked water level pointer.
In ``tb_gen_code()`` we will call ``mprotect()`` on at most one page in order to
unlock the top of the water level (if it is currently locked). In
``cpu_tb_exec()`` we will call ``mprotect()`` on all pages below the water
level that are currently unlocked. This will, in most cases, be one or zero
pages with the exception being if multiple pages of code were generated without
being executed.
Multiple threads
----------------
Additional consideration is needed to handle multiple threads. We do not permit
one vCPU from executing code on another vCPU if the end of the code is located
at it's TCG region's locked water level. The reason is that without having
synchronization between threads, we cannot guarantee if the page at the water
level is locked or unlocked.
There are multiple places this may happen: when a TB is being looked up in the
main loop, when a TB is being looked up as part of ``goto_tb``, and the TB chain
caches (where after lookup, we encode a jump so a future call to the first TB
will immediately jump into the second TB without lookup).
Since adding synchronization is expensive (holding on thread idle while another
one is generating code defeats the purpose of parallel TCG contexts), we
implement a lock-less solution. In each TB, we store a pointer to the water
level pointer. Whenever a TB is looked up, we check that either 1) the TB
belongs to the current thread and therefore we can ensure the memory is locked
during execution or 2) the water level of the TCG context that the TB belongs to
is beyond the end of the TB's code block. This does mean that there might be
redundant code generation done by multiple TCG contexts if multiple vCPUs all
decide to execute the same block of code at the same time. This should not
happen too often.
Similarly, for the TB chain cache, we will only chain a TB if either 1) both
TBs' code buffer end pointer resides in the same page and therefore if the
memory is locked to execute the first TB, we can jump to the second TB without
issue, or 2) the second TB's code block fully resides below the locked water
level of its TCG context. This means in some cases (such as two newly minted
TBs from two threads happen to be chained), we will not chain the TB when we
see it initially but will only chain it after a few subsequent executions and
the locked water level has risen.
+11 -1
View File
@@ -430,7 +430,11 @@ struct TranslationBlock {
uintptr_t jmp_list_head;
uintptr_t jmp_list_next[2];
uintptr_t jmp_dest[2];
};
#if defined(CONFIG_NO_RWX)
/* Reference to code_locked_top_page in tcg_ctx */
void * const *p_code_locked_top_page;
#endif
} __attribute__((aligned(TB_EXIT_MASK+1)));
extern bool parallel_cpus;
@@ -579,6 +583,12 @@ void tlb_set_dirty(CPUState *cpu, target_ulong vaddr);
/* exec.c */
void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr);
/* translate-all.c */
#if defined(CONFIG_NO_RWX)
void tb_exec_memory_lock(void);
bool tb_is_exec(const TranslationBlock *tb);
#endif
MemoryRegionSection *
address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
hwaddr *xlat, hwaddr *plen,
+3
View File
@@ -33,6 +33,9 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base,
cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
if (likely(tb &&
#if defined(CONFIG_NO_RWX)
tb_is_exec(tb) &&
#endif
tb->pc == *pc &&
tb->cs_base == *cs_base &&
tb->flags == *flags &&
+5
View File
@@ -122,7 +122,12 @@ typedef enum {
#define TCG_TARGET_HAS_muls2_i64 0
#define TCG_TARGET_HAS_muluh_i64 1
#define TCG_TARGET_HAS_mulsh_i64 1
#if defined(CONFIG_NO_RWX) // In iOS, we have to unlock exe memory which is very slow
#define TCG_TARGET_HAS_direct_jump 0
#else
#define TCG_TARGET_HAS_direct_jump 1
#endif
#define TCG_TARGET_HAS_v64 1
#define TCG_TARGET_HAS_v128 1
+9 -2
View File
@@ -1898,8 +1898,15 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
} else {
/* !TCG_TARGET_HAS_direct_jump */
tcg_debug_assert(s->tb_jmp_target_addr != NULL);
intptr_t offset = tcg_pcrel_diff(s, (s->tb_jmp_target_addr + a0)) >> 2;
tcg_out_insn(s, 3305, LDR, offset, TCG_REG_TMP);
uintptr_t target_addr = (uintptr_t)(s->tb_jmp_target_addr + a0);
ptrdiff_t offset = tcg_pcrel_diff(s, (void *)target_addr);
if (offset < 0x100000 && offset > -0x100000) { // check it's in +/- 1MiB range
tcg_out_insn(s, 3305, LDR, offset >> 2, TCG_REG_TMP);
} else { // TODO: what if +/- 4GiB is not enough?
offset = (target_addr >> 12) - ((uintptr_t)s->code_ptr >> 12);
tcg_out_insn(s, 3406, ADRP, TCG_REG_TMP, offset);
tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP, TCG_REG_TMP, target_addr & 0xfff);
}
}
tcg_out_insn(s, 3207, BR, TCG_REG_TMP);
set_jmp_reset_offset(s, a0);
+4
View File
@@ -133,7 +133,11 @@ extern bool have_avx2;
#define TCG_TARGET_HAS_muluh_i32 0
#define TCG_TARGET_HAS_mulsh_i32 0
#define TCG_TARGET_HAS_goto_ptr 1
#if defined(CONFIG_NO_RWX) // In iOS, we have to unlock exe memory which is very slow
#define TCG_TARGET_HAS_direct_jump 0
#else
#define TCG_TARGET_HAS_direct_jump 1
#endif
#if TCG_TARGET_REG_BITS == 64
/* Keep target addresses zero-extended in a register. */
+137 -1
View File
@@ -157,6 +157,10 @@ static int tcg_out_ldst_finalize(TCGContext *s);
#define TCG_HIGHWATER 1024
/* For computing the average TB code size, if we seen below this threshold,
then the average is not meaningful and we ignore it. */
#define TCG_TB_SEEN_THRESHOLD 10
static TCGContext **tcg_ctxs;
static unsigned int n_tcg_ctxs;
TCGv_env cpu_env = 0;
@@ -535,6 +539,36 @@ static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
*pend = end;
}
#if defined(CONFIG_NO_RWX)
/*
* Called before clearing the TBs from last flush.
* Uses the average code block size from the last flush
* to set aside a chunk of memory for future translations.
* `buffer` must be page aligned
*/
static void tcg_tbs_alloc(TCGContext *s)
{
size_t average_block_size;
if (s->nb_tbs < TCG_TB_SEEN_THRESHOLD) {
average_block_size = CODE_GEN_AVG_BLOCK_SIZE;
} else {
average_block_size = s->tb_total_sizes / s->nb_tbs;
}
s->code_gen_max_blocks = s->code_gen_buffer_size / (sizeof(TranslationBlock) + average_block_size);
s->tbs = s->code_gen_ptr;
/* if buffer is start of region, it should already be aligned */
g_assert(((uintptr_t)s->tbs & ~qemu_host_page_mask) == 0);
/* Sanity check since we need bottom 2 bits zero. page_mask should cover this. */
g_assert(((uintptr_t)s->tbs & TB_EXIT_MASK) == 0);
s->code_gen_ptr = (void *)ROUND_UP((uintptr_t)&s->tbs[s->code_gen_max_blocks], qemu_host_page_size);
g_assert(s->code_gen_ptr < s->code_gen_highwater);
s->nb_tbs = 0;
s->tb_total_sizes = 0;
}
#endif // defined(CONFIG_NO_RWX)
static void tcg_region_assign(TCGContext *s, size_t curr_region)
{
void *start, *end;
@@ -545,6 +579,13 @@ static void tcg_region_assign(TCGContext *s, size_t curr_region)
s->code_gen_ptr = start;
s->code_gen_buffer_size = end - start;
s->code_gen_highwater = end - TCG_HIGHWATER;
#if defined(CONFIG_NO_RWX)
tcg_tbs_alloc(s);
s->code_locked = 0;
g_assert(((uintptr_t)s->code_gen_ptr & ~qemu_host_page_mask) == 0);
s->code_locked_top_page = s->code_gen_ptr;
#endif
}
static bool tcg_region_alloc__locked(TCGContext *s)
@@ -594,6 +635,10 @@ void tcg_region_reset_all(void)
qemu_mutex_lock(&region.lock);
region.current = 0;
region.agg_size_full = 0;
#if defined(CONFIG_NO_RWX)
/* unlock any locked pages from before */
tcg_exec_memory_destroy(region.start, region.end - region.start);
#endif
for (i = 0; i < n_ctxs; i++) {
TCGContext *s = atomic_read(&tcg_ctxs[i]);
@@ -688,6 +733,11 @@ void tcg_region_init(void)
n_regions = tcg_n_regions();
#if defined(CONFIG_NO_RWX)
/* We shouldn't be translating code yet, so it should not be locked */
g_assert(!tcg_init_ctx.code_locked);
#endif
/* The first region will be 'aligned - buf' bytes larger than the others */
aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
@@ -1018,6 +1068,30 @@ void tcg_context_init(TCGContext *s)
*/
TranslationBlock *tcg_tb_alloc(TCGContext *s)
{
#if defined(CONFIG_NO_RWX)
TranslationBlock *tb;
int nb_tbs;
retry:
/* artifical limit of TBs buffer reached OR we hit highwater mark */
nb_tbs = atomic_fetch_inc(&s->nb_tbs);
if (unlikely(nb_tbs >= s->code_gen_max_blocks ||
s->code_gen_ptr > s->code_gen_highwater)) {
// lock this region before moving to the next one
tcg_exec_memory_lock(s);
if (tcg_region_alloc(s)) {
return NULL;
}
goto retry;
}
/* we have a tb to work with */
tb = &s->tbs[nb_tbs];
tb->p_code_locked_top_page = &s->code_locked_top_page;
/* bottom two bits used in exit_tb, must be zero */
g_assert(((uintptr_t)tb & TB_EXIT_MASK) == 0);
s->data_gen_ptr = NULL;
return tb;
#else // !defined(CONFIG_NO_RWX)
uintptr_t align = qemu_icache_linesize;
TranslationBlock *tb;
void *next;
@@ -1035,6 +1109,7 @@ TranslationBlock *tcg_tb_alloc(TCGContext *s)
atomic_set(&s->code_gen_ptr, next);
s->data_gen_ptr = NULL;
return tb;
#endif // defined(CONFIG_NO_RWX)
}
void tcg_prologue_init(TCGContext *s)
@@ -1070,17 +1145,34 @@ void tcg_prologue_init(TCGContext *s)
}
#endif
#if defined(CONFIG_NO_RWX)
/* We need to mark prolog page as W^X, so align to page */
s->code_ptr = (void *)ROUND_UP((uintptr_t)s->code_ptr, qemu_host_page_size);
#endif
buf1 = s->code_ptr;
prologue_size = tcg_current_code_size(s);
flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
/* Deduct the prologue from the buffer. */
prologue_size = tcg_current_code_size(s);
s->code_gen_ptr = buf1;
s->code_gen_buffer = buf1;
s->code_buf = buf1;
total_size -= prologue_size;
s->code_gen_buffer_size = total_size;
#if defined(CONFIG_NO_RWX)
tcg_tbs_alloc(s);
/* Mark prologue as executable */
if (mprotect(buf0, prologue_size, PROT_READ | PROT_EXEC) < 0) {
fprintf(stderr, "Failed to lock exec memory\n");
exit(1);
}
g_assert(((uintptr_t)buf1 & ~qemu_host_page_mask) == 0);
s->code_locked = 0;
s->code_locked_top_page = buf1;
#endif
tcg_register_jit(s->code_gen_buffer, total_size);
#ifdef DEBUG_DISAS
@@ -4569,3 +4661,47 @@ void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
g_assert_not_reached();
}
#endif
#if defined(CONFIG_NO_RWX)
void tcg_exec_memory_unlock(TCGContext *s)
{
/* We only need to unlock the top page as all the pages above it are
still unlocked */
if (s->code_locked)
{
if (mprotect(s->code_locked_top_page, qemu_host_page_size, PROT_READ | PROT_WRITE) < 0)
{
fprintf(stderr, "Failed to lock exec memory\n");
exit(1);
}
s->code_locked = 0;
}
}
void tcg_exec_memory_lock(TCGContext *s)
{
if (!s->code_locked)
{
void *old_top = s->code_locked_top_page;
void *new_top = (void *)((uintptr_t)s->code_gen_ptr & qemu_host_page_mask);
if (mprotect(old_top, (new_top - old_top) + qemu_host_page_size, PROT_READ | PROT_EXEC) < 0)
{
fprintf(stderr, "Failed to unlock exec memory\n");
exit(1);
}
s->code_locked_top_page = new_top;
s->code_locked = 1;
}
}
void tcg_exec_memory_destroy(void *start, size_t len)
{
g_assert(((uintptr_t)start & ~qemu_host_page_mask) == 0);
g_assert((len & ~qemu_host_page_mask) == 0);
if (mprotect(start, len, PROT_READ | PROT_WRITE) < 0)
{
fprintf(stderr, "Failed to unlock exec memory\n");
exit(1);
}
}
#endif // defined(CONFIG_NO_RWX)
+25
View File
@@ -625,6 +625,25 @@ struct TCGContext {
void *code_gen_ptr;
void *data_gen_ptr;
#if defined(CONFIG_NO_RWX)
/* Code memory locking/unlocking. On W^X platforms, we need to keep
track of when a page is RW or RX. We take advantage of the fact
that generated code only grows downwards so at any point in time
we only need to have 1 or 0 page unlocked. */
int code_locked;
void *code_locked_top_page;
/* On iOS we cannot store TB before the code buffer because of W^X
therefore we need to allocate from some other region of memory. */
TranslationBlock *tbs;
size_t nb_tbs;
size_t code_gen_max_blocks;
/* We can compute the average size of TB blocks last flush and use
that to compute the number of blocks needed next flush. */
uint64_t tb_total_sizes;
#endif
/* Threshold to flush the translated code buffer. */
void *code_gen_highwater;
@@ -1216,6 +1235,12 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr);
void tcg_register_jit(void *buf, size_t buf_size);
#if defined(CONFIG_NO_RWX)
void tcg_exec_memory_unlock(TCGContext *s);
void tcg_exec_memory_lock(TCGContext *s);
void tcg_exec_memory_destroy(void *start, size_t len);
#endif
#if TCG_TARGET_MAYBE_vec
/* Return zero if the tuple (opc, type, vece) is unsupportable;
return > 0 if it is directly supportable;