iOS and Apple Silicon host support
Subsystems (3)
v5 cover iOS and Apple Silicon host support
Based-on: [email protected] ([PATCH v3 00/41] Mirror map JIT memory for TCG)
These set of changes brings QEMU TCG to iOS devices and future Apple Silicon devices. They were originally developed last year and have been working in the UTM app. Recently, we ported the changes to master, re-wrote a lot of the build script changes for meson, and broke up the patches into more distinct units.
A summary of the changes:
* CONFIG_IOS defined when building for iOS and iOS specific changes (as well as unsupported code) are gated behind it.
* A new dependency, libucontext is added since iOS does not have native ucontext and broken support for sigaltstack. libucontext is available as a new option for coroutine backend.
* For (recent) jailbroken iOS devices as well as upcoming Apple Silicon devices, there are new rules for applications supporting JIT (with the proper entitlement). These rules are implemented as well.
Since v5:
* Fixed some more instances of QAPI define of CONFIG_HOST_BLOCK_DEVICE
* Fixed libucontext build on newer version of GCC
Since v4:
* Updated QAPI schema for CONFIG_HOST_BLOCK_DEVICE
* Updated maintainers file for iOS host support
* Moved system() changes to osdep.h
* Fixed typo in libucontext meson.build change
Since v3:
* Moved mirror JIT support to a different patch set.
* Removed dependency on pthread_jit_write_protect_np because it was redundent and also crashes if called on a non-jailbroken iOS device.
* Removed --enable-cross-compile option
* Fixed checkpatch errors
* Fixed iOS build on master due to new test recently added which calls system()
Since v2:
* Changed getting mirror pointer from a macro to inline functions
* Split constification of TCG code pointers to separate patch
* Removed slirp updates (will send future patch once slirp changes are in)
* Removed shared library patch (will send future patch)
-j
Joelle van Dyne (7):
configure: option to disable host block devices
configure: cross-compiling with empty cross_prefix
qemu: add support for iOS host
coroutine: add libucontext as external library
slirp: update build flags for iOS resolv fix
tcg: implement JIT for iOS and Apple Silicon
block: check availablity for preadv/pwritev on mac
docs/devel/index.rst | 1 + docs/devel/ios.rst | 28 ++++++++++++ configure | 76 +++++++++++++++++++++++++++++--- meson.build | 33 +++++++++++++- qapi/block-core.json | 10 +++-- include/exec/exec-all.h | 2 + include/qemu/osdep.h | 11 +++++ include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ block.c | 2 +- block/file-posix.c | 53 +++++++++++++++++------ net/slirp.c | 16 +++---- qga/commands-posix.c | 6 +++ tcg/tcg.c | 4 ++ util/coroutine-ucontext.c | 9 ++++ .gitmodules | 3 ++ MAINTAINERS | 7 +++ libucontext | 1 + meson_options.txt | 2 + tests/qtest/meson.build | 7 ++- 23 files changed, 373 insertions(+), 37 deletions(-) create mode 100644 docs/devel/ios.rst create mode 100644 include/tcg/tcg-apple-jit.h create mode 160000 libucontext
Signature · 1 line
2.28.0
PATCH v5 1/7 configure: option to disable host block devices
Some hosts (iOS) have a sandboxed filesystem and do not provide low-level APIs for interfacing with host block devices.
--- configure | 4 ++++ meson.build | 1 + qapi/block-core.json | 10 +++++++--- block/file-posix.c | 10 +++++++++- 4 files changed, 21 insertions(+), 4 deletions(-)
configure +4 -0
@@ -448,6 +448,7 @@ meson="" ninja="" skip_meson=no gettext="" +host_block_device_support="yes" bogus_os="no" malloc_trim="auto"
@@ -5907,6 +5908,9 @@ if test "$default_devices" = "yes" ; then else echo "CONFIG_MINIKCONF_MODE=--allnoconfig" >> $config_host_mak fi +if test "$host_block_device_support" = "yes" ; then + echo "CONFIG_HOST_BLOCK_DEVICE=y" >> $config_host_mak +fi if test "$debug_tcg" = "yes" ; then echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak fi
meson.build +1 -0
@@ -2166,6 +2166,7 @@ summary_info += {'vvfat support': config_host.has_key('CONFIG_VVFAT')} summary_info += {'qed support': config_host.has_key('CONFIG_QED')} summary_info += {'parallels support': config_host.has_key('CONFIG_PARALLELS')} summary_info += {'sheepdog support': config_host.has_key('CONFIG_SHEEPDOG')} +summary_info += {'host block dev support': config_host.has_key('CONFIG_HOST_BLOCK_DEVICE')} summary_info += {'capstone': capstone_opt == 'disabled' ? false : capstone_opt} summary_info += {'libpmem support': config_host.has_key('CONFIG_LIBPMEM')} summary_info += {'libdaxctl support': config_host.has_key('CONFIG_LIBDAXCTL')}
qapi/block-core.json +7 -3
@@ -980,7 +980,8 @@ 'discriminator': 'driver', 'data': { 'file': 'BlockStatsSpecificFile', - 'host_device': 'BlockStatsSpecificFile', + 'host_device': { 'type': 'BlockStatsSpecificFile', + 'if': 'defined(CONFIG_HOST_BLOCK_DEVICE)' }, 'nvme': 'BlockStatsSpecificNvme' } } ##
@@ -2848,7 +2849,9 @@ { 'enum': 'BlockdevDriver', 'data': [ 'blkdebug', 'blklogwrites', 'blkreplay', 'blkverify', 'bochs', 'cloop', 'compress', 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', - 'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', + 'gluster', 'host_cdrom', + {'name': 'host_device', 'if': 'defined(CONFIG_HOST_BLOCK_DEVICE)' }, + 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd', { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' },
@@ -4016,7 +4019,8 @@ 'ftps': 'BlockdevOptionsCurlFtps', 'gluster': 'BlockdevOptionsGluster', 'host_cdrom': 'BlockdevOptionsFile', - 'host_device':'BlockdevOptionsFile', + 'host_device': { 'type': 'BlockdevOptionsFile', + 'if': 'defined(CONFIG_HOST_BLOCK_DEVICE)' }, 'http': 'BlockdevOptionsCurlHttp', 'https': 'BlockdevOptionsCurlHttps', 'iscsi': 'BlockdevOptionsIscsi',
block/file-posix.c +9 -1
@@ -41,7 +41,7 @@#include "scsi/pr-manager.h" #include "scsi/constants.h" -#if defined(__APPLE__) && (__MACH__) +#if defined(CONFIG_HOST_BLOCK_DEVICE ) && defined(__APPLE__) && (__MACH__)#include <paths.h> #include <sys/param.h> #include <IOKit/IOKitLib.h>
@@ -3014,6 +3014,7 @@ static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)return stats; } +#if defined(CONFIG_HOST_BLOCK_DEVICE )static BlockStatsSpecific *hdev_get_specific_stats (BlockDriverState *bs) {BlockStatsSpecific *stats =g_new (BlockStatsSpecific,1 );
@@ -3023,6 +3024,7 @@ static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)return stats; } +#endif /* CONFIG_HOST_BLOCK_DEVICE */ static QemuOptsList raw_create_opts = { .name ="raw-create-opts" ,
@@ -3247,6 +3249,8 @@ BlockDriver bdrv_file = {/***********************************************/ /* host device */ +#if defined(CONFIG_HOST_BLOCK_DEVICE ) +#if defined(__APPLE__) && defined(__MACH__)static kern_return_t GetBSDPath (io_iterator_t mediaIterator,char *bsdPath,CFIndex maxPathSize,int flags);
@@ -3872,6 +3876,8 @@ static BlockDriver bdrv_host_cdrom = { };#endif /* __FreeBSD__ */ +#endif /* CONFIG_HOST_BLOCK_DEVICE */ +static void bdrv_file_init (void ) {/*
@@ -3879,6 +3885,7 @@ static void bdrv_file_init(void)* registered last will get probed first. */ bdrv_register (&bdrv_file); +#if defined(CONFIG_HOST_BLOCK_DEVICE )bdrv_register (&bdrv_host_device);#ifdef __linux__bdrv_register (&bdrv_host_cdrom);
@@ -3886,6 +3893,7 @@ static void bdrv_file_init(void)#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)bdrv_register (&bdrv_host_cdrom);#endif +#endif /* CONFIG_HOST_BLOCK_DEVICE */ }block_init (bdrv_file_init);
Signature · 1 line
2.28.0
PATCH v5 2/7 configure: cross-compiling with empty cross_prefix
In reply to Joelle van DyneThe iOS toolchain does not use the host prefix naming convention. So we need to enable cross-compile options while allowing the PREFIX to be blank.
--- configure | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
configure +4 -2
@@ -234,6 +234,7 @@ cpu="" iasl="iasl" interp_prefix="/usr/gnemul/qemu-%M" static="no" +cross_compile="no" cross_prefix="" audio_drv_list="" block_drv_rw_whitelist=""
@@ -458,6 +459,7 @@ for opt do optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)') case "$opt" in --cross-prefix=*) cross_prefix="$optarg" + cross_compile="yes" ;; --cc=*) CC="$optarg" ;;
@@ -1657,7 +1659,7 @@ $(echo Available targets: $default_target_list | \ --target-list-exclude=LIST exclude a set of targets from the default target-list Advanced options (experts only): - --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix] + --cross-prefix=PREFIX use PREFIX for compile tools, PREFIX can be blank [$cross_prefix] --cc=CC use C compiler CC [$cc] --iasl=IASL use ACPI compiler IASL [$iasl] --host-cc=CC use C compiler CC [$host_cc] for code run at
@@ -6933,7 +6935,7 @@ if has $sdl2_config; then fi echo "strip = [$(meson_quote $strip)]" >> $cross echo "windres = [$(meson_quote $windres)]" >> $cross -if test -n "$cross_prefix"; then +if test "$cross_compile" = "yes"; then cross_arg="--cross-file config-meson.cross" echo "[host_machine]" >> $cross if test "$mingw32" = "yes" ; then
Signature · 1 line
2.28.0
Re: [PATCH v5 2/7] configure: cross-compiling with empty cross_prefix
Quoted 7 lines · Joelle van Dyne
The iOS toolchain does not use the host prefix naming convention. So we need to enable cross-compile options while allowing the PREFIX to be blank.
Signed-off-by Joelle van Dyne <[email protected]>--- configure | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
PATCH v5 3/7 qemu: add support for iOS host
In reply to Joelle van DyneThis introduces support for building for iOS hosts. When the correct Xcode toolchain is used, iOS host will be detected automatically.
* block: disable features not supported by iOS sandbox
* slirp: disable SMB features for iOS
* osdep: disable system() calls for iOS
--- docs/devel/index.rst | 1 + docs/devel/ios.rst | 28 +++++++++++++++++++++++++++ configure | 43 ++++++++++++++++++++++++++++++++++++++++- meson.build | 2 +- include/qemu/osdep.h | 11 +++++++++++ block.c | 2 +- block/file-posix.c | 31 +++++++++++++++++------------ net/slirp.c | 16 +++++++-------- qga/commands-posix.c | 6 ++++++ MAINTAINERS | 7 +++++++ tests/qtest/meson.build | 7 +++---- 11 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 docs/devel/ios.rst
docs/devel/index.rst +1 -0
@@ -34,3 +34,4 @@ Contents: clocks qom block-coroutine-wrapper + ios
docs/devel/ios.rst new file +28 -0
@@ -0,0 +1,28 @@ +=========== +iOS Support +=========== + +To run qemu on the iOS platform, some modifications were required. Most of the +modifications are conditioned on the ``CONFIG_IOS`` and configuration variable. + +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 it has been replaced +with an assertion error. There should be no code path that call system() from +iOS. + +``ucontext`` support is broken on iOS. The implementation from ``libucontext`` +is used instead. + +JIT support +----------- + +On iOS, allocating RWX pages require special entitlements not usually granted to +apps. However, it is possible to use `bulletproof JIT`_ with a development +certificate. This means that we need to allocate one chunk of memory with RX +permissions and then mirror map the same memory with RW permissions. We generate +code to the mirror mapping and execute the original mapping. + +.. _bulletproof JIT: https://www.blackhat.com/docs/us-16/materials/us-16-Krstic.pdf
configure +42 -1
79 lines not on this page · raw message
meson.build +1 -1
@@ -176,7 +176,7 @@ if targetos == 'windows' include_directories: include_directories('.')) elif targetos == 'darwin' coref = dependency('appleframeworks', modules: 'CoreFoundation') - iokit = dependency('appleframeworks', modules: 'IOKit') + iokit = dependency('appleframeworks', modules: 'IOKit', required: 'CONFIG_IOS' not in config_host) cocoa = dependency('appleframeworks', modules: 'Cocoa', required: get_option('cocoa')) elif targetos == 'sunos' socket = [cc.find_library('socket'),
include/qemu/osdep.h +11 -0
@@ -686,4 +686,15 @@ char *qemu_get_host_name(Error **errp);*/ size_t qemu_get_host_physmem (void ); +/** +* iOS does not support system() so we replace it with an assertion failure. +*/ +#ifdef CONFIG_IOS +#define system ios_does_not_support_system +static inline int ios_does_not_support_system (const char *command) +{ +assert (0 ); +} +#endif /* CONFIG_IOS */ +#endif
block.c +1 -1
@@ -53,7 +53,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
block/file-posix.c +19 -12
@@ -181,7 +181,17 @@ 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 {
@@ -252,6 +262,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. */
@@ -284,6 +300,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.
@@ -2306,7 +2323,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 ;
@@ -3543,16 +3560,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 inthdev_co_pdiscard (BlockDriverState *bs,int64_t offset,int bytes) {
net/slirp.c +8 -8
@@ -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;
@@ -785,7 +785,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)
@@ -900,7 +900,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) {
qga/commands-posix.c +6 -0
@@ -34,6 +34,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
MAINTAINERS +7 -0
@@ -528,6 +528,13 @@ F: include/*/*win32* X: qga/*win32* F: qemu.nsi +IOS +M: Joelle van Dyne <[email protected]> +S: Maintained +K: ^Subject:.*(?i)iOS +F: docs/devel/ios.rst +F: include/tcg/tcg-apple-jit.h + Alpha Machines -------------- M: Richard Henderson <[email protected]>
tests/qtest/meson.build +3 -4
@@ -46,12 +46,11 @@ qtests_i386 = \ (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-test'] : []) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_RTL8139_PCI') ? ['rtl8139-test'] : []) + \ + (not config_host.has_key('CONFIG_IOS') ? ['bios-tables-test', 'hd-geo-test'] : []) + \ qtests_pci + \ ['fdc-test', 'ide-test', - 'hd-geo-test', 'boot-order-test', - 'bios-tables-test', 'rtc-test', 'i440fx-test', 'fuzz-test',
@@ -148,9 +147,9 @@ qtests_arm = \ 'boot-serial-test', 'hexloader-test'] -# TODO: once aarch64 TCG is fixed on ARM 32 bit host, make bios-tables-test unconditional +# TODO: once aarch64 TCG is fixed on ARM 32 bit host, make bios-tables-test unconditional (except on iOS) qtests_aarch64 = \ - (cpu != 'arm' ? ['bios-tables-test'] : []) + \ + (cpu != 'arm' and not config_host.has_key('CONFIG_IOS') ? ['bios-tables-test'] : []) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-test'] : []) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-swtpm-test'] : []) + \ ['arm-cpu-features',
Signature · 1 line
2.28.0
PATCH v5 4/7 coroutine: add libucontext as external library
In reply to Joelle van DyneiOS does not support ucontext natively for aarch64 and the sigaltstack is also unsupported (even worse, it fails silently, see:
https://openradar.appspot.com/13002712 )
As a workaround we include a library implementation of ucontext and add it as a build option.
--- configure | 23 ++++++++++++++++++++--- meson.build | 28 +++++++++++++++++++++++++++- util/coroutine-ucontext.c | 9 +++++++++ .gitmodules | 3 +++ libucontext | 1 + meson_options.txt | 2 ++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 160000 libucontext
configure +20 -3
@@ -1750,7 +1750,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 --disable-blobs disable installing provided firmware blobs --with-vss-sdk=SDK-path enable Windows VSS support in QEMU Guest Agent
@@ -4883,6 +4883,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
@@ -4906,12 +4908,27 @@ 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 + coroutine_impl=ucontext + libucontext="enabled" + ;; +*) + coroutine_impl=$coroutine + libucontext="disabled" + ;; +esac + if test "$coroutine_pool" = ""; then coroutine_pool=yes fi
@@ -6457,7 +6474,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
@@ -7035,7 +7052,7 @@ NINJA=$ninja $meson setup \ -Dcocoa=$cocoa -Dmpath=$mpath -Dsdl=$sdl -Dsdl_image=$sdl_image \ -Dvnc=$vnc -Dvnc_sasl=$vnc_sasl -Dvnc_jpeg=$vnc_jpeg -Dvnc_png=$vnc_png \ -Dgettext=$gettext -Dxkbcommon=$xkbcommon -Du2f=$u2f -Dvirtiofsd=$virtiofsd \ - -Dcapstone=$capstone -Dslirp=$slirp -Dfdt=$fdt \ + -Dcapstone=$capstone -Dslirp=$slirp -Dfdt=$fdt -Ducontext=$libucontext \ -Diconv=$iconv -Dcurses=$curses -Dlibudev=$libudev\ -Ddocs=$docs -Dsphinx_build=$sphinx_build -Dinstall_blobs=$blobs \ $cross_arg \
meson.build +27 -1
@@ -1259,9 +1259,34 @@ if not fdt.found() and fdt_required.length() > 0 error('fdt not available but required by targets ' + ', '.join(fdt_required)) endif +ucontext = not_found +if get_option('ucontext').enabled() + arch = host_machine.cpu() + if not fs.is_dir(meson.current_source_dir() / 'libucontext/arch' / arch) + error('libucontext is wanted but not implemented for host ' + arch) + endif + ucontext_cargs = ['-DG_LOG_DOMAIN="ucontext"', '-DCUSTOM_IMPL'] + ucontext_files = [ + 'libucontext/arch' / arch / 'getcontext.S', + 'libucontext/arch' / arch / 'setcontext.S', + 'libucontext/arch' / arch / 'makecontext.c', + 'libucontext/arch' / arch / 'startcontext.S', + 'libucontext/arch' / arch / 'swapcontext.S', + ] + + ucontext_inc = include_directories('libucontext/include') + libucontext = static_library('ucontext', + sources: ucontext_files, + c_args: ucontext_cargs, + include_directories: ucontext_inc) + ucontext = declare_dependency(link_with: libucontext, + include_directories: ucontext_inc) +endif + config_host_data.set('CONFIG_CAPSTONE', capstone.found()) config_host_data.set('CONFIG_FDT', fdt.found()) config_host_data.set('CONFIG_SLIRP', slirp.found()) +config_host_data.set('CONFIG_LIBUCONTEXT', ucontext.found()) ##################### # Generated sources #
@@ -1477,7 +1502,7 @@ util_ss.add_all(trace_ss) util_ss = util_ss.apply(config_all, strict: false) libqemuutil = static_library('qemuutil', sources: util_ss.sources() + stub_ss.sources() + genh, - dependencies: [util_ss.dependencies(), m, glib, socket, malloc]) + dependencies: [util_ss.dependencies(), m, glib, socket, malloc, ucontext]) qemuutil = declare_dependency(link_with: libqemuutil, sources: genh + version_res)
@@ -2135,6 +2160,7 @@ if targetos == 'windows' summary_info += {'QGA MSI support': config_host.has_key('CONFIG_QGA_MSI')} endif summary_info += {'seccomp support': config_host.has_key('CONFIG_SECCOMP')} +summary_info += {'libucontext support': ucontext.found()} summary_info += {'coroutine backend': config_host['CONFIG_COROUTINE_BACKEND']} summary_info += {'coroutine pool': config_host['CONFIG_COROUTINE_POOL'] == '1'} summary_info += {'debug stack usage': config_host.has_key('CONFIG_DEBUG_STACK_USAGE')}
util/coroutine-ucontext.c +9 -0
@@ -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
.gitmodules +3 -0
@@ -64,3 +64,6 @@ [submodule "roms/vbootrom"] path = roms/vbootrom url = https://git.qemu.org/git/vbootrom.git +[submodule "libucontext"] + path = libucontext + url = https://github.com/utmapp/libucontext.git
libucontext new file +1 -0
@@ -0,0 +1 @@ +Subproject commit 455ecd495f706d5b57be3ff5b572c120c2a7a5a2
meson_options.txt +2 -0
@@ -64,6 +64,8 @@ option('xkbcommon', type : 'feature', value : 'auto', description: 'xkbcommon support') option('virtiofsd', type: 'feature', value: 'auto', description: 'build virtiofs daemon (virtiofsd)') +option('ucontext', type : 'feature', value : 'disabled', + description: 'libucontext support') option('capstone', type: 'combo', value: 'auto', choices: ['disabled', 'enabled', 'auto', 'system', 'internal'],
Signature · 1 line
2.28.0
Re: [PATCH v5 4/7] coroutine: add libucontext as external library
Quoted 8 lines · Joelle van Dyne
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 )As a workaround we include a library implementation of ucontext and add it as a build option.
Signed-off-by Joelle van Dyne <[email protected]>
Quoted 8 lines
--- configure | 23 ++++++++++++++++++++--- meson.build | 28 +++++++++++++++++++++++++++- util/coroutine-ucontext.c | 9 +++++++++ .gitmodules | 3 +++ libucontext | 1 + meson_options.txt | 2 ++ 6 files changed, 62 insertions(+), 4 deletions(-)
PATCH v5 5/7 slirp: update build flags for iOS resolv fix
In reply to Joelle van DyneA future libslirp update will use libresolv on Darwin systems, so we add the flags in QEMU build now.
--- meson.build | 2 ++ 1 file changed, 2 insertions(+)
meson.build +2 -0
@@ -1156,6 +1156,8 @@ if have_system slirp_deps = [] if targetos == 'windows' slirp_deps = cc.find_library('iphlpapi') + elif targetos == 'darwin' + slirp_deps = cc.find_library('resolv') endif slirp_conf = configuration_data() slirp_conf.set('SLIRP_MAJOR_VERSION', meson.project_version().split('.')[0])
Signature · 1 line
2.28.0
PATCH v5 6/7 tcg: implement JIT for iOS and Apple Silicon
In reply to Joelle van DyneWhen entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled before tcg_qemu_tb_exec()
* JIT write-protect is disabled after tcg_qemu_tb_exec() returns
* JIT write-protect is disabled inside do_tb_phys_invalidate() but if it is called inside of tcg_qemu_tb_exec() then write-protect will be enabled again before returning.
* JIT write-protect is disabled by cpu_loop_exit() for interrupt handling.
* JIT write-protect is disabled everywhere else.
--- include/exec/exec-all.h | 2 + include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++ 7 files changed, 145 insertions(+) create mode 100644 include/tcg/tcg-apple-jit.h
include/exec/exec-all.h +2 -0
@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void );/* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER )
include/tcg/tcg-apple-jit.h new file +86 -0
86 lines not on this page · raw message
include/tcg/tcg.h +3 -0
@@ -625,6 +625,9 @@ struct TCGContext {size_t code_gen_buffer_size;void *code_gen_ptr;void *data_gen_ptr; +#if defined(CONFIG_DARWIN ) && !defined(CONFIG_TCG_INTERPRETER ) +bool code_gen_locked;/* on Darwin each thread tracks W^X flags */ +#endif /* Threshold to flush the translated code buffer. */ void *code_gen_highwater;
accel/tcg/cpu-exec-common.c +2 -0
@@ -64,6 +64,8 @@ void cpu_reloading_memory_map(void)void cpu_loop_exit (CPUState *cpu) { +/* Unlock JIT write protect if applicable. */ +tb_exec_unlock ();/* Undo the setting in cpu_tb_exec. */ cpu->can_do_io =1 ;siglongjmp (cpu->jmp_env,1 );
accel/tcg/cpu-exec.c +2 -0
@@ -176,7 +176,9 @@ static inline TranslationBlock *cpu_tb_exec(CPUState *cpu, }#endif /* DEBUG_DISAS */ +tb_exec_lock (); ret =tcg_qemu_tb_exec (env, tb_ptr); +tb_exec_unlock (); cpu->can_do_io =1 ;/* * TODO: Delay swapping back to the read-write region of the TB
accel/tcg/translate-all.c +46 -0
87 lines not on this page · raw message
tcg/tcg.c +4 -0
@@ -809,6 +809,8 @@ static void alloc_tcg_plugin_context(TCGContext *s)void tcg_register_thread (void ) { tcg_ctx = &tcg_init_ctx; + +tb_exec_unlock (); }#else void tcg_register_thread (void )
@@ -843,6 +845,8 @@ void tcg_register_thread(void) err =tcg_region_initial_alloc__locked (tcg_ctx);g_assert (!err);qemu_mutex_unlock (®ion.lock); + + tb_exec_unlock(); }#endif /* !CONFIG_USER_ONLY */
Signature · 1 line
2.28.0
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 108 lines · Joelle van Dyne
When entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled beforetcg_qemu_tb_exec()
* JIT write-protect is disabled aftertcg_qemu_tb_exec()returns
* JIT write-protect is disabled insidedo_tb_phys_invalidate()but if it is called inside oftcg_qemu_tb_exec()then write-protect will be enabled again before returning.
* JIT write-protect is disabled bycpu_loop_exit()for interrupt handling.
* JIT write-protect is disabled everywhere else.Signed-off-by Joelle van Dyne <[email protected]>--- include/exec/exec-all.h | 2 + include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++ 7 files changed, 145 insertions(+)create mode 100644
include/tcg/tcg-apple-jit.hinclude/exec/exec-all.h +2
-0@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void );/* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER )include/tcg/tcg-apple-jit.h new file +61
-0@@ -0,0 +1,86 @@ +/* +* Apple Silicon functions for JIT handling +* +* Copyright (c) 2020 osy +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef TCG_APPLE_JIT_H +#define TCG_APPLE_JIT_H + +/* + * APRR handling + * Credits to: https://siguza.github.io/APRR/ + * Reversed from /usr/lib/system/libsystem_pthread.dylib + */ + +#if defined(__aarch64__) && defined(CONFIG_DARWIN) + +#define _COMM_PAGE_START_ADDRESS (0x0000000FFFFFC000ULL) /* In TTBR0 */ +#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) +#define _COMM_PAGE_APPR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) +#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS + 0x118) + +static __attribute__((__always_inline__)) bool jit_write_protect_supported(void) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + return aprr_support > 0; +} + +/* write protect enable = write disable */ +static __attribute__((__always_inline__)) void jit_write_protect(int enabled) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + if (aprr_support == 0 || aprr_support > 3) { + return; + } else if (aprr_support == 1) { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_4_c15_c2_7, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0" + ); + } else { + __asm__ __volatile__ (
include/tcg/tcg-apple-jit.h line 71
Quoted 10 lines
include/tcg/tcg-apple-jit.h new file +10
-0+"mov x0, %0\n" +"ldr x0, [x0]\n" +"msr S3_6_c15_c1_5, x0\n" +"isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0" + ); + } +}
Is there a particular reason you're not just calling pthread_jit_write_protect_np()? That would remove the dependency on anything reverse engineered.
Quoted 20 lines
include/tcg/tcg-apple-jit.h new file +15
-0+ +#else /* defined(__aarch64__) && defined(CONFIG_DARWIN) */ + +static __attribute__((__always_inline__))bool jit_write_protect_supported (void ) +{ +return false; +} + +static __attribute__((__always_inline__))void jit_write_protect (int enabled) +{ +} + +#endif + +#endif /* define TCG_APPLE_JIT_H */ include/tcg/tcg.h +0
-0
include/tcg/tcg.h struct TCGContext { · line 629
Quoted 5 lines
include/tcg/tcg.h +2
-0@@ -625,6 +625,9 @@ struct TCGContext {size_t code_gen_buffer_size;void *code_gen_ptr;void *data_gen_ptr; +#if defined(CONFIG_DARWIN ) && !defined(CONFIG_TCG_INTERPRETER ) +bool code_gen_locked;/* on Darwin each thread tracks W^X flags */
I don't quite understand why you need to keep track of whether you're in locked state or not. If you just always keep in locked state and unlock around the few parts that modify the code gen region, you should be fine, no?
Quoted 9 lines
include/tcg/tcg.h +1
-0+#endif /* Threshold to flush the translated code buffer. */ void *code_gen_highwater;accel/tcg/cpu-exec-common.c +0
-0
accel/tcg/cpu-exec-common.c void cpu_reloading_memory_map(void) · line 68
Quoted 5 lines
accel/tcg/cpu-exec-common.c +2
-0@@ -64,6 +64,8 @@ void cpu_reloading_memory_map(void)void cpu_loop_exit (CPUState *cpu) { +/* Unlock JIT write protect if applicable. */ +tb_exec_unlock ();
Why do you need to unlock here? I think in general this patch is trying to keep the state RW always and only flip to RX when actually executing code, right?
I think it would be much easier and cleaner to do it reverse: Keep it in RX always and flip to RW when you need to modify.
Also, shouldn't the code gen buffer be allocated with MAP_JIT according to the porting guide?
Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 154 lines · Alexander Graf
Quoted 117 lines · Joelle van Dyne
When entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled beforetcg_qemu_tb_exec()
* JIT write-protect is disabled aftertcg_qemu_tb_exec()returns
* JIT write-protect is disabled insidedo_tb_phys_invalidate()but if it is called inside oftcg_qemu_tb_exec()then write-protect will be enabled again before returning.
* JIT write-protect is disabled bycpu_loop_exit()for interrupt handling.
* JIT write-protect is disabled everywhere else.Signed-off-by Joelle van Dyne <[email protected]>---
include/exec/exec-all.h | 2 +include/tcg/tcg-apple-jit.h| 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++accel/tcg/cpu-exec-common.c| 2 +
accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++7 files changed, 145 insertions(+)create mode 100644
include/tcg/tcg-apple-jit.hinclude/exec/exec-all.h +2
-0@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu,target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask); void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void ); /* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER)include/tcg/tcg-apple-jit.h new file +17
-0@@ -0,0 +1,86 @@ +/* +* Apple Silicon functions for JIT handling +* +* Copyright (c) 2020 osy +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */include/tcg/tcg-apple-jit.h new file +13
-0+ +#ifndef TCG_APPLE_JIT_H +#define TCG_APPLE_JIT_H + +/* +* APRR handling +* Credits to: https://siguza.github.io/APRR/ +* Reversed from /usr/lib/system/libsystem_pthread.dylib +*/ + +#if defined(__aarch64__) && defined(CONFIG_DARWIN ) + +#define _COMM_PAGE_START_ADDRESS (0x0000000FFFFFC000ULL)/* In TTBR0 */
include/tcg/tcg-apple-jit.h new file +3
-0+#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) +#define _COMM_PAGE_APPR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) +#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS +
0x118)include/tcg/tcg-apple-jit.h new file +2
-0+ +static __attribute__((__always_inline__)) bool
jit_write_protect_supported(void)
+{
+ /* Access shared kernel page at fixed memory location. */ +uint8_taprr_support= *(volatileuint8_t*)_COMM_PAGE_APRR_SUPPORT;
+ returnaprr_support> 0;include/tcg/tcg-apple-jit.h new file +4
-0+} + +/* write protect enable = write disable */ +static __attribute__((__always_inline__)) void jit_write_protect(intenabled)
+{
+ /* Access shared kernel page at fixed memory location. */ +uint8_taprr_support= *(volatileuint8_t*)_COMM_PAGE_APRR_SUPPORT;
+ if (aprr_support== 0 ||aprr_support> 3) {
+ return;
+ } else if (aprr_support== 1) {
+__asm____volatile__(
+ "mov x0, %0\n"
+ "ldr x0, [x0]\n"
+ "msrS3_4_c15_c2_7, x0\n"
+ "isb sy\n"
+ :: "r" (enabled ?_COMM_PAGE_APRR_WRITE_DISABLE+ :_COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0"
+ );
+ } else {
+__asm____volatile__(
+ "mov x0, %0\n"
+ "ldr x0, [x0]\n"Quoted 8 lines
+ "msr
S3_6_c15_c1_5, x0\n"
+ "isb sy\n"
+ :: "r" (enabled ?_COMM_PAGE_APRR_WRITE_DISABLE+ :_COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0"
+ );
+ }
+}Is there a particular reason you're not just calling
pthread_jit_write_protect_np()? That would remove the dependency on anything reverse engineered.Quoted 22 lines
include/tcg/tcg-apple-jit.h new file +4
-0+ +#else /* defined(__aarch64__) && defined(CONFIG_DARWIN) */ + +static __attribute__((__always_inline__)) bool
jit_write_protect_supported(void)include/tcg/tcg-apple-jit.h new file +5
-0+{ + return false; +} + +static __attribute__((__always_inline__)) void jit_write_protect(intenabled)
include/tcg/tcg-apple-jit.h new file +6
-0+{ +} + +#endif + +#endif /* define TCG_APPLE_JIT_H */ include/tcg/tcg.h +0
-0@@ -625,6 +625,9 @@ struct TCGContext {
Quoted 8 lines
include/tcg/tcg.h struct TCGContext { · line 629
Quoted 5 lines
include/tcg/tcg.h +2
-0size_t code_gen_buffer_size; void *code_gen_ptr; void *data_gen_ptr; +#if defined(CONFIG_DARWIN ) && !defined(CONFIG_TCG_INTERPRETER ) + bool code_gen_locked; /* on Darwin each thread tracks W^X flags */I don't quite understand why you need to keep track of whether you're in locked state or not. If you just always keep in locked state and unlock around the few parts that modify the code gen region, you should be fine, no?
I take this bit back. After fiddling with setting the flags the other way around, I think what you do here is better. Especially when it gets to exception handling, always treating the code region as writeable is better.
Quoted 18 lines
Quoted 10 lines
include/tcg/tcg.h +1
-0+#endif /* Threshold to flush the translated code buffer. */ void *code_gen_highwater;accel/tcg/cpu-exec-common.c +0
-0accel/tcg/cpu-exec-common.c void cpu_reloading_memory_map(void) · line 67
Quoted 4 lines
accel/tcg/cpu-exec-common.c +2
-0@@ -64,6 +64,8 @@ void cpu_reloading_memory_map(void) void cpu_loop_exit(CPUState *cpu) { + /* Unlock JIT write protect if applicable. */ + tb_exec_unlock();Why do you need to unlock here? I think in general this patch is trying to keep the state RW always and only flip to RX when actually executing code, right?
Quoted 3 lines
I think it would be much easier and cleaner to do it reverse: Keep it in RX always and flip to RW when you need to modify.
Also, shouldn't the code gen buffer be allocated with
MAP_JITaccording to the porting guide?
MAP_JIT is definitely missing to make it work on macos.
Also, I would prefer if you find a better name for the lock/unlock function. How about "tcg_set_codegen_mutable(bool)"? You can easily map that to the pthread call then.
Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 1 line · Alexander Graf
MAP_JITis definitely missing to make it work on macos.
As per the cover,
Based-on: [email protected] ([PATCH v3 00/41] Mirror map JIT memory for TCG)
which contains
https://lists.nongnu.org/archive/html/qemu-devel/2020-11/msg01766.html
which sets MAP_JIT.
r~
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
In reply to Alexander GrafQuoted 118 lines · Alexander Graf
Quoted 115 lines · Joelle van Dyne
When entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled beforetcg_qemu_tb_exec()
* JIT write-protect is disabled aftertcg_qemu_tb_exec()returns
* JIT write-protect is disabled insidedo_tb_phys_invalidate()but if it is called inside oftcg_qemu_tb_exec()then write-protect will be enabled again before returning.
* JIT write-protect is disabled bycpu_loop_exit()for interrupt handling.
* JIT write-protect is disabled everywhere else.Signed-off-by Joelle van Dyne <[email protected]>--- include/exec/exec-all.h | 2 + include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++ 7 files changed, 145 insertions(+)create mode 100644
include/tcg/tcg-apple-jit.hinclude/exec/exec-all.h +2
-0@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void );/* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER )include/tcg/tcg-apple-jit.h new file +68
-0@@ -0,0 +1,86 @@ +/* +* Apple Silicon functions for JIT handling +* +* Copyright (c) 2020 osy +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef TCG_APPLE_JIT_H +#define TCG_APPLE_JIT_H + +/* + * APRR handling + * Credits to: https://siguza.github.io/APRR/ + * Reversed from /usr/lib/system/libsystem_pthread.dylib + */ + +#if defined(__aarch64__) && defined(CONFIG_DARWIN) + +#define _COMM_PAGE_START_ADDRESS (0x0000000FFFFFC000ULL) /* In TTBR0 */ +#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) +#define _COMM_PAGE_APPR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) +#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS + 0x118) + +static __attribute__((__always_inline__)) bool jit_write_protect_supported(void) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + return aprr_support > 0; +} + +/* write protect enable = write disable */ +static __attribute__((__always_inline__)) void jit_write_protect(int enabled) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + if (aprr_support == 0 || aprr_support > 3) { + return; + } else if (aprr_support == 1) { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_4_c15_c2_7, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0" + ); + } else { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_6_c15_c1_5, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0"
Quoted 8 lines
include/tcg/tcg-apple-jit.h line 71
Quoted 3 lines
include/tcg/tcg-apple-jit.h new file +3
-0+ ); + } +}Is there a particular reason you're not just calling
pthread_jit_write_protect_np()? That would remove the dependency on anything reverse engineered.
Those APIs are not available on iOS 13 or below, which has the same APRR requirements. If for legal reasons we cannot include this code, then it is fine to remove this file and replace the calls with the APIs, but we would lose support on lower iOS versions.
Quoted 25 lines
Quoted 25 lines
include/tcg/tcg-apple-jit.h new file +15
-0+ +#else /* defined(__aarch64__) && defined(CONFIG_DARWIN) */ + +static __attribute__((__always_inline__))bool jit_write_protect_supported (void ) +{ +return false; +} + +static __attribute__((__always_inline__))void jit_write_protect (int enabled) +{ +} + +#endif + +#endif /* define TCG_APPLE_JIT_H */ include/tcg/tcg.h +0
-0@@ -625,6 +625,9 @@ struct TCGContext {size_t code_gen_buffer_size;void *code_gen_ptr;void *data_gen_ptr;
Quoted 8 lines
include/tcg/tcg.h struct TCGContext { · line 629
Quoted 2 lines
include/tcg/tcg.h +2
-0+#if defined(CONFIG_DARWIN ) && !defined(CONFIG_TCG_INTERPRETER ) +bool code_gen_locked;/* on Darwin each thread tracks W^X flags */ I don't quite understand why you need to keep track of whether you're in locked state or not. If you just always keep in locked state and unlock around the few parts that modify the code gen region, you should be fine, no?
I thought so at first, but do_tb_phys_invalidate() can be called in either state and even when looking at all the callers it's not possible to easily derive the lock state without storing this somewhere. If someone knows of a way, then this flag can be removed.
Quoted 12 lines
Quoted 12 lines
include/tcg/tcg.h +1
-0+#endif /* Threshold to flush the translated code buffer. */ void *code_gen_highwater;accel/tcg/cpu-exec-common.c +0
-0@@ -64,6 +64,8 @@ void cpu_reloading_memory_map(void)
Quoted 9 lines
accel/tcg/cpu-exec-common.c void cpu_reloading_memory_map(void) · line 68
Quoted 4 lines
accel/tcg/cpu-exec-common.c +2
-0void cpu_loop_exit (CPUState *cpu) { +/* Unlock JIT write protect if applicable. */ +tb_exec_unlock ();Why do you need to unlock here? I think in general this patch is trying to keep the state RW always and only flip to RX when actually executing code, right?
Yes, this point is when the code exits due to an interrupt or some other async means. Otherwise, the unlock would be matched after tcg_qemu_tb_exec.
-j
Quoted 9 lines
I think it would be much easier and cleaner to do it reverse: Keep it in RX always and flip to RW when you need to modify.
Also, shouldn't the code gen buffer be allocated with
MAP_JITaccording to the porting guide?Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 125 lines · Joelle van Dyne
Quoted 116 lines · Alexander Graf
Quoted 114 lines · Joelle van Dyne
When entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled beforetcg_qemu_tb_exec()
* JIT write-protect is disabled aftertcg_qemu_tb_exec()returns
* JIT write-protect is disabled insidedo_tb_phys_invalidate()but if it is called inside oftcg_qemu_tb_exec()then write-protect will be enabled again before returning.
* JIT write-protect is disabled bycpu_loop_exit()for interrupt handling.
* JIT write-protect is disabled everywhere else.Signed-off-by Joelle van Dyne <[email protected]>--- include/exec/exec-all.h | 2 + include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++ 7 files changed, 145 insertions(+)create mode 100644
include/tcg/tcg-apple-jit.hinclude/exec/exec-all.h +2
-0@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void );/* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER )include/tcg/tcg-apple-jit.h new file +67
-0@@ -0,0 +1,86 @@ +/* +* Apple Silicon functions for JIT handling +* +* Copyright (c) 2020 osy +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef TCG_APPLE_JIT_H +#define TCG_APPLE_JIT_H + +/* + * APRR handling + * Credits to: https://siguza.github.io/APRR/ + * Reversed from /usr/lib/system/libsystem_pthread.dylib + */ + +#if defined(__aarch64__) && defined(CONFIG_DARWIN) + +#define _COMM_PAGE_START_ADDRESS (0x0000000FFFFFC000ULL) /* In TTBR0 */ +#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) +#define _COMM_PAGE_APPR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) +#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS + 0x118) + +static __attribute__((__always_inline__)) bool jit_write_protect_supported(void) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + return aprr_support > 0; +} + +/* write protect enable = write disable */ +static __attribute__((__always_inline__)) void jit_write_protect(int enabled) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + if (aprr_support == 0 || aprr_support > 3) { + return; + } else if (aprr_support == 1) { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_4_c15_c2_7, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0" + ); + } else { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_6_c15_c1_5, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE)Quoted 8 lines
include/tcg/tcg-apple-jit.h line 71
Quoted 4 lines
include/tcg/tcg-apple-jit.h new file +4
-0+ : "memory", "x0" + ); + } +}Is there a particular reason you're not just calling
pthread_jit_write_protect_np()? That would remove the dependency on anything reverse engineered.
Quoted 4 lines
Those APIs are not available on iOS 13 or below, which has the same APRR requirements. If for legal reasons we cannot include this code, then it is fine to remove this file and replace the calls with the APIs, but we would lose support on lower iOS versions.
I don't think we realistically care about running QEMU on iOS13, do we? Let's just focus on making the code maintainable for anything going forward from now :).
Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
A lot of users of UTM are on iOS 13 (a large number of devices only have jailbreak for iOS 13 and below), but if the QEMU community thinks it's better that way, we are willing to compromise.
-j
Quoted 142 lines · Alexander Graf
Quoted 125 lines · Joelle van Dyne
Quoted 116 lines · Alexander Graf
Quoted 114 lines · Joelle van Dyne
When entitlements are available (macOS or jailbroken iOS), a hardware feature called APRR exists on newer Apple Silicon that can cheaply mark JIT pages as either RX or RW. Reverse engineered functions from libsystem_pthread.dylib are implemented to handle this.
The following rules apply for JIT write protect:
* JIT write-protect is enabled beforetcg_qemu_tb_exec()
* JIT write-protect is disabled aftertcg_qemu_tb_exec()returns
* JIT write-protect is disabled insidedo_tb_phys_invalidate()but if it is called inside oftcg_qemu_tb_exec()then write-protect will be enabled again before returning.
* JIT write-protect is disabled bycpu_loop_exit()for interrupt handling.
* JIT write-protect is disabled everywhere else.Signed-off-by Joelle van Dyne <[email protected]>--- include/exec/exec-all.h | 2 + include/tcg/tcg-apple-jit.h | 86 +++++++++++++++++++++++++++++++++++++ include/tcg/tcg.h | 3 ++ accel/tcg/cpu-exec-common.c | 2 + accel/tcg/cpu-exec.c | 2 + accel/tcg/translate-all.c | 46 ++++++++++++++++++++ tcg/tcg.c | 4 ++ 7 files changed, 145 insertions(+)create mode 100644
include/tcg/tcg-apple-jit.hinclude/exec/exec-all.h +2
-0@@ -549,6 +549,8 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, uint32_t cf_mask);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr); +void tb_exec_lock (void ); +void tb_exec_unlock (void );/* GETPC is the true target of the return instruction that we'll execute. */ #if defined(CONFIG_TCG_INTERPRETER )include/tcg/tcg-apple-jit.h new file +67
-0@@ -0,0 +1,86 @@ +/* +* Apple Silicon functions for JIT handling +* +* Copyright (c) 2020 osy +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef TCG_APPLE_JIT_H +#define TCG_APPLE_JIT_H + +/* + * APRR handling + * Credits to: https://siguza.github.io/APRR/ + * Reversed from /usr/lib/system/libsystem_pthread.dylib + */ + +#if defined(__aarch64__) && defined(CONFIG_DARWIN) + +#define _COMM_PAGE_START_ADDRESS (0x0000000FFFFFC000ULL) /* In TTBR0 */ +#define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) +#define _COMM_PAGE_APPR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) +#define _COMM_PAGE_APRR_WRITE_DISABLE (_COMM_PAGE_START_ADDRESS + 0x118) + +static __attribute__((__always_inline__)) bool jit_write_protect_supported(void) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + return aprr_support > 0; +} + +/* write protect enable = write disable */ +static __attribute__((__always_inline__)) void jit_write_protect(int enabled) +{ + /* Access shared kernel page at fixed memory location. */ + uint8_t aprr_support = *(volatile uint8_t *)_COMM_PAGE_APRR_SUPPORT; + if (aprr_support == 0 || aprr_support > 3) { + return; + } else if (aprr_support == 1) { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_4_c15_c2_7, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE) + : "memory", "x0" + ); + } else { + __asm__ __volatile__ ( + "mov x0, %0\n" + "ldr x0, [x0]\n" + "msr S3_6_c15_c1_5, x0\n" + "isb sy\n" + :: "r" (enabled ? _COMM_PAGE_APRR_WRITE_DISABLE + : _COMM_PAGE_APPR_WRITE_ENABLE)Quoted 8 lines
include/tcg/tcg-apple-jit.h line 71
Quoted 4 lines
include/tcg/tcg-apple-jit.h new file +4
-0+ : "memory", "x0" + ); + } +}Is there a particular reason you're not just calling
pthread_jit_write_protect_np()? That would remove the dependency on anything reverse engineered.Quoted 4 lines
Those APIs are not available on iOS 13 or below, which has the same APRR requirements. If for legal reasons we cannot include this code, then it is fine to remove this file and replace the calls with the APIs, but we would lose support on lower iOS versions.
I don't think we realistically care about running QEMU on iOS13, do we? Let's just focus on making the code maintainable for anything going forward from now :).
Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 3 lines · Joelle van Dyne
A lot of users of UTM are on iOS 13 (a large number of devices only have jailbreak for iOS 13 and below), but if the QEMU community thinks it's better that way, we are willing to compromise.
I think it would make merging much more straight forward if we could keep RWX toggling to the publicly released API. So yes, please adapt it. In UTM, you can still carry a tiny downstream patch that implements the API through your reverse engineered code for iOS 13, no?
Alex
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Quoted 10 lines · Alexander Graf
Quoted 3 lines · Joelle van Dyne
A lot of users of UTM are on iOS 13 (a large number of devices only have jailbreak for iOS 13 and below), but if the QEMU community thinks it's better that way, we are willing to compromise.
I think it would make merging much more straight forward if we could keep RWX toggling to the publicly released API. So yes, please adapt it. In UTM, you can still carry a tiny downstream patch that implements the API through your reverse engineered code for iOS 13, no?
Alex, you're awesome! Thanks for finding a way to avoid the reverse-engineered code. With that change we don't need to go through a legal review and it makes merging this much simpler.
Stefan
Re: [PATCH v5 6/7] tcg: implement JIT for iOS and Apple Silicon
Sounds good, I will make that change in the next version.
-j
Quoted 18 lines · Stefan Hajnoczi
Quoted 10 lines · Alexander Graf
Quoted 3 lines · Joelle van Dyne
A lot of users of UTM are on iOS 13 (a large number of devices only have jailbreak for iOS 13 and below), but if the QEMU community thinks it's better that way, we are willing to compromise.
I think it would make merging much more straight forward if we could keep RWX toggling to the publicly released API. So yes, please adapt it. In UTM, you can still carry a tiny downstream patch that implements the API through your reverse engineered code for iOS 13, no?
Alex, you're awesome! Thanks for finding a way to avoid the reverse-engineered code. With that change we don't need to go through a legal review and it makes merging this much simpler.
Stefan
PATCH v5 7/7 block: check availablity for preadv/pwritev on mac
In reply to Joelle van DynemacOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure will succeed with CONFIG_PREADV even when targeting a lower OS version. We therefore need to check at run time if we can actually use these APIs.
--- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block/file-posix.c +12 -0
@@ -1394,12 +1394,24 @@ static bool preadv_present = true;static ssize_t qemu_preadv (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return preadv (fd, iov, nr_iov, offset); }static ssize_t qemu_pwritev (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return pwritev (fd, iov, nr_iov, offset); }
Signature · 1 line
2.28.0
Re: [PATCH v5 7/7] block: check availablity for preadv/pwritev on mac
Quoted 14 lines · Joelle van Dyne
macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure will succeed with
CONFIG_PREADVeven when targeting a lower OS version. We therefore need to check at run time if we can actually use these APIs.Signed-off-by Joelle van Dyne <[email protected]>--- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/file-posix.c +0
-0
block/file-posix.c static bool preadv_present = true; · line 1398
Quoted 5 lines
block/file-posix.c +2
-0@@ -1394,12 +1394,24 @@ static bool preadv_present = true;static ssize_t qemu_preadv (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {
Can we change the CONFIG_PREADV ifdef'ry to run this check once on macOS 11?
Quoted 20 lines
block/file-posix.c +10
-0+ preadv_present = false; + return -ENOSYS; + } else +#endif return preadv (fd, iov, nr_iov, offset); }static ssize_t qemu_pwritev (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return pwritev (fd, iov, nr_iov, offset); }
Re: [PATCH v5 7/7] block: check availablity for preadv/pwritev on mac
No, because if you build on a macOS 11 host but try to run it on macOS 10.15 then it will crash.
-j
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Joelle van Dyne
macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure will succeed with
CONFIG_PREADVeven when targeting a lower OS version. We therefore need to check at run time if we can actually use these APIs.Signed-off-by Joelle van Dyne <[email protected]>--- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/file-posix.c +0
-0block/file-posix.c static bool preadv_present = true; · line 1398
Quoted 5 lines
block/file-posix.c +2
-0@@ -1394,12 +1394,24 @@ static bool preadv_present = true;static ssize_t qemu_preadv (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {Can we change the
CONFIG_PREADVifdef'ry to run this check once on macOS 11?Quoted 20 lines
block/file-posix.c +10
-0+ preadv_present = false; + return -ENOSYS; + } else +#endif return preadv (fd, iov, nr_iov, offset); }static ssize_t qemu_pwritev (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return pwritev (fd, iov, nr_iov, offset); }
Re: [PATCH v5 7/7] block: check availablity for preadv/pwritev on mac
Quoted 2 lines · Joelle van Dyne
No, because if you build on a macOS 11 host but try to run it on macOS 10.15 then it will crash.
I'm not asking to move the check to configure/build time, but to do it only once at runtime...
Quoted 51 lines
-j
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Joelle van Dyne
macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure will succeed with
CONFIG_PREADVeven when targeting a lower OS version. We therefore need to check at run time if we can actually use these APIs.Signed-off-by Joelle van Dyne <[email protected]>--- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/file-posix.c +0
-0block/file-posix.c static bool preadv_present = true; · line 1398
Quoted 5 lines
block/file-posix.c +2
-0@@ -1394,12 +1394,24 @@ static bool preadv_present = true;static ssize_t qemu_preadv (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {Can we change the
CONFIG_PREADVifdef'ry to run this check once on macOS 11?Quoted 20 lines
block/file-posix.c +10
-0+ preadv_present = false; + return -ENOSYS; + } else +#endif return preadv (fd, iov, nr_iov, offset); }static ssize_t qemu_pwritev (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return pwritev (fd, iov, nr_iov, offset); }
Re: [PATCH v5 7/7] block: check availablity for preadv/pwritev on mac
How about this:
static bool preadv_present = true;
static bool preadv_checked = false;
static ssize_tqemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) {
#ifdef CONFIG_DARWIN
if (!preadv_checked) {
if (__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {
preadv_checked = true;
} else {
preadv_present = false;
return -ENOSYS;
}
}
#endif
return preadv(fd, iov, nr_iov, offset);
}-j
Quoted 59 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Joelle van Dyne
No, because if you build on a macOS 11 host but try to run it on macOS 10.15 then it will crash.
I'm not asking to move the check to configure/build time, but to do it only once at runtime...
Quoted 51 lines
-j
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Joelle van Dyne
macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure will succeed with
CONFIG_PREADVeven when targeting a lower OS version. We therefore need to check at run time if we can actually use these APIs.Signed-off-by Joelle van Dyne <[email protected]>--- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/file-posix.c +0
-0block/file-posix.c static bool preadv_present = true; · line 1398
Quoted 5 lines
block/file-posix.c +2
-0@@ -1394,12 +1394,24 @@ static bool preadv_present = true;static ssize_t qemu_preadv (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {Can we change the
CONFIG_PREADVifdef'ry to run this check once on macOS 11?Quoted 20 lines
block/file-posix.c +10
-0+ preadv_present = false; + return -ENOSYS; + } else +#endif return preadv (fd, iov, nr_iov, offset); }static ssize_t qemu_pwritev (int fd,const struct iovec *iov,int nr_iov,off_t offset) { +#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */ + if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) { + preadv_present = false; +return -ENOSYS ; + } else +#endif return pwritev (fd, iov, nr_iov, offset); }
Quoted 19 lines · Joelle van Dyne
Based-on: [email protected] ([PATCH v3 00/41] Mirror map JIT memory for TCG)
These set of changes brings QEMU TCG to iOS devices and future Apple Silicon devices. They were originally developed last year and have been working in the UTM app. Recently, we ported the changes to master, re-wrote a lot of the build script changes for meson, and broke up the patches into more distinct units.
A summary of the changes:
*
CONFIG_IOSdefined when building for iOS and iOS specific changes (as well as unsupported code) are gated behind it.
* A new dependency, libucontext is added since iOS does not have native ucontext and broken support for sigaltstack. libucontext is available as a new option for coroutine backend.
* For (recent) jailbroken iOS devices as well as upcoming Apple Silicon devices, there are new rules for applications supporting JIT (with the proper entitlement). These rules are implemented as well.
Quoted 4 lines
Since v5:
* Fixed some more instances of QAPI define of
CONFIG_HOST_BLOCK_DEVICE
* Fixed libucontext build on newer version of GCC
I have pinged Software Freedom Conservancy about an opinion regarding merging the reverse-engineered part. Hope to get a reply soon. You are on CC so you'll see it.
QEMU is currently in freeze (fixes only, no new features) for the upcoming 5.2 release. The development tree will open again at the start of December:
https://wiki.qemu.org/Planning/5.2
The code looks good to me. Thank you!