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
osy 4e91fc77d5 coroutine: add libucontext as external library
iOS does not support ucontext natively for aarch64 and the sigaltstack is also unsupported (even worse, it fails silently, see: https://openradar.appspot.com/13002712 )
2020-02-08 20:29:32 -08:00
osy 1474e37eae build: Added support for building QEMU as a shared library 2020-02-08 20:29:22 -08:00
osy feb8266224 build: Add support for iOS host
Fix build errors with clang
Block driver modified to handle iOS
Slirp driver modified to handle iOS
Calls to system() conditioned on not iOS build
Support iOS icache flushing
2020-01-31 13:19:07 -08:00
28 changed files with 619 additions and 32 deletions

No files matched your search

+3
View File
@@ -61,3 +61,6 @@
[submodule "roms/qboot"]
path = roms/qboot
url = https://github.com/bonzini/qboot
[submodule "libucontext"]
path = libucontext
url = https://github.com/halts/libucontext.git
+32
View File
@@ -513,6 +513,9 @@ CAP_CFLAGS += -DCAPSTONE_HAS_ARM64
CAP_CFLAGS += -DCAPSTONE_HAS_POWERPC
CAP_CFLAGS += -DCAPSTONE_HAS_X86
LIBUCONTEXT_CFLAGS = $(CFLAGS) $(QEMU_CFLAGS)
LIBUCONTEXT_CFLAGS += -DCUSTOM_IMPL
.PHONY: capstone/all
capstone/all: .git-submodule-status
$(call quiet-command,$(MAKE) -C $(SRC_PATH)/capstone CAPSTONE_SHARED=no BUILDDIR="$(BUILD_DIR)/capstone" CC="$(CC)" AR="$(AR)" LD="$(LD)" RANLIB="$(RANLIB)" CFLAGS="$(CAP_CFLAGS)" $(SUBDIR_MAKEFLAGS) $(BUILD_DIR)/capstone/$(LIBCAPSTONE))
@@ -525,11 +528,19 @@ slirp/all: .git-submodule-status
CC="$(CC)" AR="$(AR)" LD="$(LD)" RANLIB="$(RANLIB)" \
CFLAGS="$(QEMU_CFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)")
.PHONY: libucontext/all
libucontext/all: .git-submodule-status
$(call quiet-command,$(MAKE) -C $(SRC_PATH)/libucontext \
ARCH="$(ARCH)" BUILD_DIR="$(BUILD_DIR)/libucontext" \
CC="$(CC)" AR="$(AR)" LD="$(LD)" RANLIB="$(RANLIB)" \
CFLAGS="$(LIBUCONTEXT_CFLAGS)")
# Compatibility gunk to keep make working across the rename of targets
# for recursion, to be removed some time after 4.1.
subdir-dtc: dtc/all
subdir-capstone: capstone/all
subdir-slirp: slirp/all
subdir-libucontext: libucontext/all
$(filter %/all, $(TARGET_DIRS_RULES)): libqemuutil.a $(common-obj-y) \
$(qom-obj-y) $(crypto-user-obj-$(CONFIG_USER_ONLY))
@@ -558,6 +569,16 @@ Makefile: $(version-obj-y)
libqemuutil.a: $(util-obj-y) $(trace-obj-y) $(stub-obj-y)
libvhost-user.a: $(libvhost-user-obj-y) $(util-obj-y) $(stub-obj-y)
ifdef CONFIG_DARWIN
QEMU_IMG_SHARED_LIBRARY=libqemu-img.dylib
else
QEMU_IMG_SHARED_LIBRARY=libqemu-img.so
endif
SOBJS=$(QEMU_IMG_SHARED_LIBRARY)
ifdef CONFIG_SHARED_LIB
all: $(SOBJS)
endif
######################################################################
COMMON_LDADDS = libqemuutil.a
@@ -565,6 +586,11 @@ COMMON_LDADDS = libqemuutil.a
qemu-img.o: qemu-img-cmds.h
qemu-img$(EXESUF): qemu-img.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
$(QEMU_IMG_SHARED_LIBRARY): LDFLAGS += $(LDFLAGS_SHARED)
$(QEMU_IMG_SHARED_LIBRARY): qemu-img.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
$(call LINK, $(filter-out %.mak, $^))
qemu-nbd$(EXESUF): qemu-nbd.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
qemu-io$(EXESUF): qemu-io.o $(authz-obj-y) $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
@@ -712,6 +738,7 @@ clean: recurse-clean
-exec rm {} +
rm -f $(edk2-decompressed)
rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) TAGS cscope.* *.pod *~ */*~
rm -f $(SOBJS)
rm -f fsdev/*.pod scsi/*.pod
rm -f qemu-img-cmds.h
rm -f ui/shader/*-vert.h ui/shader/*-frag.h
@@ -873,6 +900,11 @@ install: all $(if $(BUILD_DOCS),install-doc) \
ifneq ($(TOOLS),)
$(call install-prog,$(TOOLS),$(DESTDIR)$(bindir))
endif
ifdef CONFIG_SHARED_LIB
ifneq ($(SOBJS),)
$(call install-so,$(SOBJS),$(DESTDIR)$(libdir))
endif
endif
ifneq ($(CONFIG_MODULES),)
$(INSTALL_DIR) "$(DESTDIR)$(qemu_moddir)"
for s in $(modules-m:.mo=$(DSOSUF)); do \
+23 -1
View File
@@ -37,6 +37,14 @@ endif
endif
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
ifdef CONFIG_DARWIN
QEMU_SHARED_LIBRARY=lib$(QEMU_PROG).dylib
else
QEMU_SHARED_LIBRARY=lib$(QEMU_PROG).so
endif
SOBJS=$(QEMU_SHARED_LIBRARY)
STPFILES=
config-target.h: config-target.h-timestamp
@@ -98,7 +106,11 @@ stap:
endif
.PHONY: stap
ifdef CONFIG_SHARED_LIB
all: $(SOBJS) stap
else
all: $(PROGS) stap
endif
# Dummy command so that make thinks it has done something
@true
@@ -211,6 +223,10 @@ ifdef CONFIG_DARWIN
$(call quiet-command,SetFile -a C $@,"SETFILE","$(TARGET_DIR)$@")
endif
$(QEMU_SHARED_LIBRARY): LDFLAGS += $(LDFLAGS_SHARED)
$(QEMU_SHARED_LIBRARY): $(all-obj-y) $(COMMON_LDADDS)
$(call LINK, $(filter-out %.mak, $^))
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"GEN","$(TARGET_DIR)$@")
@@ -221,7 +237,7 @@ hmp-commands-info.h: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$(TARGET_DIR)$@")
clean: clean-target
rm -f *.a *~ $(PROGS)
rm -f *.a *~ $(PROGS) $(SOBJS)
rm -f $(shell find . -name '*.[od]')
rm -f hmp-commands.h gdbstub-xml.c
rm -f trace/generated-helpers.c trace/generated-helpers.c-timestamp
@@ -230,9 +246,15 @@ ifdef CONFIG_TRACE_SYSTEMTAP
endif
install: all
ifdef CONFIG_SHARED_LIB
ifneq ($(SOBJS),)
$(call install-so,$(SOBJS),$(DESTDIR)$(libdir))
endif
else
ifneq ($(PROGS),)
$(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
endif
endif
ifdef CONFIG_TRACE_SYSTEMTAP
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"
+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)
+1 -1
View File
@@ -52,7 +52,7 @@
#ifdef CONFIG_BSD
#include <sys/ioctl.h>
#include <sys/queue.h>
#ifndef __DragonFly__
#if !defined(__DragonFly__) && !defined(CONFIG_IOS)
#include <sys/disk.h>
#endif
#endif
+25 -13
View File
@@ -40,7 +40,7 @@
#include "scsi/pr-manager.h"
#include "scsi/constants.h"
#if defined(__APPLE__) && (__MACH__)
#if defined(CONFIG_HOST_DEVICE) && defined(__APPLE__) && (__MACH__)
#include <paths.h>
#include <sys/param.h>
#include <IOKit/IOKitLib.h>
@@ -177,7 +177,16 @@ typedef struct BDRVRawReopenState {
bool check_cache_dropped;
} BDRVRawReopenState;
static int fd_open(BlockDriverState *bs);
static int fd_open(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
/* this is just to ensure s->fd is sane (its called by io ops) */
if (s->fd >= 0)
return 0;
return -EIO;
}
static int64_t raw_getlength(BlockDriverState *bs);
typedef struct RawPosixAIOData {
@@ -248,6 +257,12 @@ static int raw_normalize_devicepath(const char **filename, Error **errp)
}
#endif
#if defined(CONFIG_IOS)
static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
{
return -ENOTSUP; // not supported on iOS
}
#else /* CONFIG_IOS */
/*
* Get logical block size via ioctl. On success store it in @sector_size_p.
*/
@@ -280,6 +295,7 @@ static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
return success ? 0 : -errno;
}
#endif /* !CONFIG_IOS */
/**
* Get physical block size of @fd.
@@ -2169,7 +2185,7 @@ again:
}
if (size == 0)
#endif
#if defined(__APPLE__) && defined(__MACH__)
#if !defined(CONFIG_IOS) && defined(__APPLE__) && defined(__MACH__)
{
uint64_t sectors = 0;
uint32_t sector_size = 0;
@@ -3045,6 +3061,8 @@ BlockDriver bdrv_file = {
/***********************************************/
/* host device */
#if defined(CONFIG_HOST_DEVICE)
#if defined(__APPLE__) && defined(__MACH__)
static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
CFIndex maxPathSize, int flags);
@@ -3381,16 +3399,6 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
}
#endif /* linux */
static int fd_open(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
/* this is just to ensure s->fd is sane (its called by io ops) */
if (s->fd >= 0)
return 0;
return -EIO;
}
static coroutine_fn int
hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
{
@@ -3778,6 +3786,8 @@ static BlockDriver bdrv_host_cdrom = {
};
#endif /* __FreeBSD__ */
#endif /* CONFIG_HOST_DEVICE */
static void bdrv_file_init(void)
{
/*
@@ -3785,6 +3795,7 @@ static void bdrv_file_init(void)
* registered last will get probed first.
*/
bdrv_register(&bdrv_file);
#if defined(CONFIG_HOST_DEVICE)
bdrv_register(&bdrv_host_device);
#ifdef __linux__
bdrv_register(&bdrv_host_cdrom);
@@ -3792,6 +3803,7 @@ static void bdrv_file_init(void)
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
bdrv_register(&bdrv_host_cdrom);
#endif
#endif /* CONFIG_HOST_DEVICE */
}
block_init(bdrv_file_init);
Vendored
+90 -3
View File
@@ -504,6 +504,8 @@ debug_mutex="no"
libpmem=""
default_devices="yes"
plugins="no"
host_device_support="yes"
shared_lib="no"
supported_cpu="no"
supported_os="no"
@@ -619,6 +621,19 @@ EOF
compile_object
}
check_ios() {
cat > $TMPC <<EOF
#ifdef __APPLE__
#import "TargetConditionals.h"
#if !TARGET_OS_IPHONE
#error TARGET_OS_IPHONE not true
#endif
#endif
int main(void) { return 0; }
EOF
compile_object
}
check_include() {
cat > $TMPC <<EOF
#include <$1>
@@ -652,7 +667,11 @@ elif check_define __DragonFly__ ; then
elif check_define __NetBSD__; then
targetos='NetBSD'
elif check_define __APPLE__; then
targetos='Darwin'
if check_ios ; then
targetos='iOS'
else
targetos='Darwin'
fi
else
# This is a fatal error, but don't report it yet, because we
# might be going to just print the --help text, or it might
@@ -841,6 +860,7 @@ Darwin)
darwin="yes"
hax="yes"
hvf="yes"
DSOSUF=".dylib"
LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
if [ "$cpu" = "x86_64" ] ; then
QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
@@ -857,6 +877,28 @@ Darwin)
HOST_VARIANT_DIR="darwin"
supported_os="yes"
;;
iOS)
bsd="yes"
darwin="yes"
ios="yes"
hax="yes"
hvf="yes"
DSOSUF=".dylib"
LDFLAGS_SHARED="-dynamiclib"
if [ "$cpu" = "x86_64" ] ; then
QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
LDFLAGS="-arch x86_64 $LDFLAGS"
fi
host_device_support="no"
audio_drv_list=""
audio_possible_drivers=""
LDFLAGS="-framework CoreFoundation $LDFLAGS"
# Disable attempts to use ObjectiveC features in os/object.h since they
# won't work when we're compiling with gcc as a C compiler.
QEMU_CFLAGS="-DOS_OBJECT_USE_OBJC=0 $QEMU_CFLAGS"
HOST_VARIANT_DIR="ios"
supported_os="yes"
;;
SunOS)
solaris="yes"
make="${MAKE-gmake}"
@@ -1539,6 +1581,14 @@ for opt do
;;
--disable-plugins) plugins="no"
;;
--enable-shared-lib)
shared_lib=yes
want_tools=no
QEMU_CFLAGS="$QEMU_CFLAGS -fPIC"
DSOSUF="-module$DSOSUF" # fix for make 2.81 not building target order correctly
;;
--disable-shared-lib) shared_lib=no
;;
*)
echo "ERROR: unknown option $opt"
echo "Try '$0 --help' for more information"
@@ -1709,7 +1759,7 @@ Advanced options (experts only):
--oss-lib path to OSS library
--cpu=CPU Build for host CPU [$cpu]
--with-coroutine=BACKEND coroutine backend. Supported options:
ucontext, sigaltstack, windows
ucontext, libucontext, sigaltstack, windows
--enable-gcov enable test coverage analysis with gcov
--gcov=GCOV use specified gcov [$gcov_tool]
--disable-blobs disable installing provided firmware blobs
@@ -1722,6 +1772,7 @@ Advanced options (experts only):
track the maximum stack usage of stacks created by qemu_alloc_stack
--enable-plugins
enable plugins via shared library loading
--enable-shared-lib build QEMU as a shared library instead of an application
Optional features, enabled with --enable-FEATURE and
disabled with --disable-FEATURE, default is enabled if available:
@@ -5290,6 +5341,8 @@ if test "$coroutine" = ""; then
coroutine=win32
elif test "$ucontext_works" = "yes"; then
coroutine=ucontext
elif test "$ios" = "yes"; then
coroutine=libucontext
else
coroutine=sigaltstack
fi
@@ -5313,12 +5366,29 @@ else
error_exit "only the 'windows' coroutine backend is valid for Windows"
fi
;;
libucontext)
;;
*)
error_exit "unknown coroutine backend $coroutine"
;;
esac
fi
case $coroutine in
libucontext)
git_submodules="${git_submodules} libucontext"
mkdir -p libucontext
QEMU_CFLAGS="$QEMU_CFLAGS -I\$(SRC_PATH)/libucontext/include"
LIBS="-L\$(BUILD_DIR)/libucontext -lucontext $LIBS"
;;
esac
if test "$coroutine" == "libucontext"; then
coroutine_impl=ucontext
else
coroutine_impl=$coroutine
fi
if test "$coroutine_pool" = ""; then
coroutine_pool=yes
fi
@@ -6599,6 +6669,8 @@ echo "libpmem support $libpmem"
echo "libudev $libudev"
echo "default devices $default_devices"
echo "plugin support $plugins"
echo "host device support $host_device_support"
echo "build shared lib $shared_lib"
if test "$supported_cpu" = "no"; then
echo
@@ -6675,6 +6747,12 @@ if test "$default_devices" = "yes" ; then
else
echo "CONFIG_MINIKCONF_MODE=--allnoconfig" >> $config_host_mak
fi
if test "$host_device_support" = "yes" ; then
echo "CONFIG_HOST_DEVICE=y" >> $config_host_mak
fi
if test "$shared_lib" = "yes" ; then
echo "CONFIG_SHARED_LIB=y" >> $config_host_mak
fi
if test "$debug_tcg" = "yes" ; then
echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak
fi
@@ -6724,6 +6802,11 @@ if test "$darwin" = "yes" ; then
echo "CONFIG_DARWIN=y" >> $config_host_mak
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
echo "CONFIG_SOLARIS=y" >> $config_host_mak
fi
@@ -7183,7 +7266,7 @@ if test "$rbd" = "yes" ; then
echo "RBD_LIBS=$rbd_libs" >> $config_host_mak
fi
echo "CONFIG_COROUTINE_BACKEND=$coroutine" >> $config_host_mak
echo "CONFIG_COROUTINE_BACKEND=$coroutine_impl" >> $config_host_mak
if test "$coroutine_pool" = "yes" ; then
echo "CONFIG_COROUTINE_POOL=1" >> $config_host_mak
else
@@ -7977,6 +8060,10 @@ fi
if [ "$capstone" = "git" -o "$capstone" = "internal" ]; then
echo "config-host.h: capstone/all" >> $config_host_mak
fi
if [ "$coroutine" = "libucontext" ]; then
echo "config-host.h: subdir-libucontext" >> $config_host_mak
echo "CONFIG_LIBUCONTEXT=y" >> $config_host_mak
fi
if test -n "$LIBCAPSTONE"; then
echo "LIBCAPSTONE=$LIBCAPSTONE" >> $config_host_mak
fi
+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 &&
Submodule
+1
Submodule libucontext added at 148ec4c5b6.
+8 -8
View File
@@ -27,7 +27,7 @@
#include "net/slirp.h"
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
#include <pwd.h>
#include <sys/wait.h>
#endif
@@ -90,7 +90,7 @@ typedef struct SlirpState {
Slirp *slirp;
Notifier poll_notifier;
Notifier exit_notifier;
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
gchar *smb_dir;
#endif
GSList *fwd;
@@ -103,7 +103,7 @@ static QTAILQ_HEAD(, SlirpState) slirp_stacks =
static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
static int slirp_smb(SlirpState *s, const char *exported_dir,
struct in_addr vserver_addr, Error **errp);
static void slirp_smb_cleanup(SlirpState *s);
@@ -368,7 +368,7 @@ static int net_slirp_init(NetClientState *peer, const char *model,
struct in6_addr ip6_prefix;
struct in6_addr ip6_host;
struct in6_addr ip6_dns;
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
struct in_addr smbsrv = { .s_addr = 0 };
#endif
NetClientState *nc;
@@ -478,7 +478,7 @@ static int net_slirp_init(NetClientState *peer, const char *model,
return -1;
}
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
error_setg(errp, "Failed to parse SMB address");
return -1;
@@ -593,7 +593,7 @@ static int net_slirp_init(NetClientState *peer, const char *model,
}
}
}
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
if (smb_export) {
if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
goto error;
@@ -805,7 +805,7 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
}
#ifndef _WIN32
#if !defined(_WIN32) && !defined(CONFIG_IOS)
/* automatic user mode samba server configuration */
static void slirp_smb_cleanup(SlirpState *s)
@@ -920,7 +920,7 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
return 0;
}
#endif /* !defined(_WIN32) */
#endif /* !defined(_WIN32) && !defined(CONFIG_IOS) */
static int guestfwd_can_read(void *opaque)
{
+5
View File
@@ -49,6 +49,11 @@
#include "crypto/init.h"
#include "trace/control.h"
#if defined(CONFIG_SHARED_LIB)
#undef main
#define main qemu_img_main
#endif
#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
"\n" QEMU_COPYRIGHT "\n"
+6
View File
@@ -33,6 +33,12 @@
#ifndef CONFIG_HAS_ENVIRON
#ifdef __APPLE__
#include "TargetConditionals.h"
#if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
#define APPLE_USE_CRT_EXTERNS
#endif
#endif
#ifdef APPLE_USE_CRT_EXTERNS
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#else
+6
View File
@@ -154,6 +154,12 @@ define install-prog
$(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
endef
# install-so list, dir
define install-so
$(INSTALL_DIR) "$2"
$(INSTALL_LIB) $1 "$2"
endef
# find-in-path
# Usage: $(call find-in-path, prog)
# Looks in the PATH if the argument contains no slash, else only considers one
+2
View File
@@ -882,6 +882,7 @@ target_ulong do_arm_semihosting(CPUARMState *env)
return clock() / (CLOCKS_PER_SEC / 100);
case TARGET_SYS_TIME:
return set_swi_errno(env, time(NULL));
#if !defined(CONFIG_IOS) // iOS does not have system()
case TARGET_SYS_SYSTEM:
GET_ARG(0);
GET_ARG(1);
@@ -898,6 +899,7 @@ target_ulong do_arm_semihosting(CPUARMState *env)
unlock_user(s, arg0, 0);
return ret;
}
#endif /* CONFIG_IOS */
case TARGET_SYS_ERRNO:
return get_swi_errno(env);
case TARGET_SYS_GET_CMDLINE:
+2
View File
@@ -402,6 +402,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
result = isatty(arg0);
}
break;
#if !defined(CONFIG_IOS) // iOS does not have system()
case HOSTED_SYSTEM:
GET_ARG(0);
GET_ARG(1);
@@ -420,6 +421,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
}
}
break;
#endif /* CONFIG_IOS */
case HOSTED_INIT_SIM:
#if defined(CONFIG_USER_ONLY)
{
+2
View File
@@ -426,6 +426,7 @@ void do_nios2_semihosting(CPUNios2State *env)
result = isatty(arg0);
}
break;
#if !defined(CONFIG_IOS) // iOS does not have system()
case HOSTED_SYSTEM:
GET_ARG(0);
GET_ARG(1);
@@ -444,6 +445,7 @@ void do_nios2_semihosting(CPUNios2State *env)
}
}
break;
#endif
default:
qemu_log_mask(LOG_GUEST_ERROR, "nios2-semihosting: unsupported "
"semihosting syscall %d\n", nr);
+15
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
@@ -146,9 +151,19 @@ typedef enum {
#define TCG_TARGET_DEFAULT_MO (0)
#define TCG_TARGET_HAS_MEMORY_BSWAP 1
#ifdef __APPLE__
void sys_icache_invalidate(void *start, size_t len);
#endif
static inline void flush_icache_range(uintptr_t start, uintptr_t stop)
{
#if defined(__APPLE__)
sys_icache_invalidate((char *)start, stop-start);
#elif defined(__GNUC__)
__builtin___clear_cache((char *)start, (char *)stop);
#else
#error "Missing builtin to flush instruction cache"
#endif
}
void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t);
+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;
+9
View File
@@ -23,7 +23,16 @@
#undef _FORTIFY_SOURCE
#endif
#include "qemu/osdep.h"
#if defined(CONFIG_LIBUCONTEXT)
#include <libucontext.h>
#define ucontext_t libucontext_ucontext_t
#define getcontext libucontext_getcontext
#define setcontext libucontext_setcontext
#define swapcontext libucontext_swapcontext
#define makecontext libucontext_makecontext
#else
#include <ucontext.h>
#endif
#include "qemu/coroutine_int.h"
#ifdef CONFIG_VALGRIND_H
+1 -1
View File
@@ -49,7 +49,7 @@ int main(int argc, char **argv)
#endif
#endif /* CONFIG_SDL */
#ifdef CONFIG_COCOA
#if defined(CONFIG_COCOA) || defined(CONFIG_SHARED_LIB)
#undef main
#define main qemu_main
#endif /* CONFIG_COCOA */