Browse Source

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 )
osy 6 years ago
parent
commit
4e91fc77d5
5 changed files with 49 additions and 2 deletions
  1. 3 0
      .gitmodules
  2. 11 0
      Makefile
  3. 25 2
      configure
  4. 1 0
      libucontext
  5. 9 0
      util/coroutine-ucontext.c

+ 3 - 0
.gitmodules

@@ -61,3 +61,6 @@
 [submodule "roms/qboot"]
 [submodule "roms/qboot"]
 	path = roms/qboot
 	path = roms/qboot
 	url = https://github.com/bonzini/qboot
 	url = https://github.com/bonzini/qboot
+[submodule "libucontext"]
+	path = libucontext
+	url = https://github.com/halts/libucontext.git

+ 11 - 0
Makefile

@@ -513,6 +513,9 @@ CAP_CFLAGS += -DCAPSTONE_HAS_ARM64
 CAP_CFLAGS += -DCAPSTONE_HAS_POWERPC
 CAP_CFLAGS += -DCAPSTONE_HAS_POWERPC
 CAP_CFLAGS += -DCAPSTONE_HAS_X86
 CAP_CFLAGS += -DCAPSTONE_HAS_X86
 
 
+LIBUCONTEXT_CFLAGS = $(CFLAGS) $(QEMU_CFLAGS)
+LIBUCONTEXT_CFLAGS += -DCUSTOM_IMPL
+
 .PHONY: capstone/all
 .PHONY: capstone/all
 capstone/all: .git-submodule-status
 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))
 	$(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)"	\
 		CC="$(CC)" AR="$(AR)" 	LD="$(LD)" RANLIB="$(RANLIB)"	\
 		CFLAGS="$(QEMU_CFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)")
 		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
 # Compatibility gunk to keep make working across the rename of targets
 # for recursion, to be removed some time after 4.1.
 # for recursion, to be removed some time after 4.1.
 subdir-dtc: dtc/all
 subdir-dtc: dtc/all
 subdir-capstone: capstone/all
 subdir-capstone: capstone/all
 subdir-slirp: slirp/all
 subdir-slirp: slirp/all
+subdir-libucontext: libucontext/all
 
 
 $(filter %/all, $(TARGET_DIRS_RULES)): libqemuutil.a $(common-obj-y) \
 $(filter %/all, $(TARGET_DIRS_RULES)): libqemuutil.a $(common-obj-y) \
 	$(qom-obj-y) $(crypto-user-obj-$(CONFIG_USER_ONLY))
 	$(qom-obj-y) $(crypto-user-obj-$(CONFIG_USER_ONLY))

+ 25 - 2
configure

@@ -1759,7 +1759,7 @@ Advanced options (experts only):
   --oss-lib                path to OSS library
   --oss-lib                path to OSS library
   --cpu=CPU                Build for host CPU [$cpu]
   --cpu=CPU                Build for host CPU [$cpu]
   --with-coroutine=BACKEND coroutine backend. Supported options:
   --with-coroutine=BACKEND coroutine backend. Supported options:
-                           ucontext, sigaltstack, windows
+                           ucontext, libucontext, sigaltstack, windows
   --enable-gcov            enable test coverage analysis with gcov
   --enable-gcov            enable test coverage analysis with gcov
   --gcov=GCOV              use specified gcov [$gcov_tool]
   --gcov=GCOV              use specified gcov [$gcov_tool]
   --disable-blobs          disable installing provided firmware blobs
   --disable-blobs          disable installing provided firmware blobs
@@ -5341,6 +5341,8 @@ if test "$coroutine" = ""; then
     coroutine=win32
     coroutine=win32
   elif test "$ucontext_works" = "yes"; then
   elif test "$ucontext_works" = "yes"; then
     coroutine=ucontext
     coroutine=ucontext
+  elif test "$ios" = "yes"; then
+    coroutine=libucontext
   else
   else
     coroutine=sigaltstack
     coroutine=sigaltstack
   fi
   fi
@@ -5364,12 +5366,29 @@ else
       error_exit "only the 'windows' coroutine backend is valid for Windows"
       error_exit "only the 'windows' coroutine backend is valid for Windows"
     fi
     fi
     ;;
     ;;
+  libucontext)
+  ;;
   *)
   *)
     error_exit "unknown coroutine backend $coroutine"
     error_exit "unknown coroutine backend $coroutine"
     ;;
     ;;
   esac
   esac
 fi
 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
 if test "$coroutine_pool" = ""; then
   coroutine_pool=yes
   coroutine_pool=yes
 fi
 fi
@@ -7246,7 +7265,7 @@ if test "$rbd" = "yes" ; then
   echo "RBD_LIBS=$rbd_libs" >> $config_host_mak
   echo "RBD_LIBS=$rbd_libs" >> $config_host_mak
 fi
 fi
 
 
-echo "CONFIG_COROUTINE_BACKEND=$coroutine" >> $config_host_mak
+echo "CONFIG_COROUTINE_BACKEND=$coroutine_impl" >> $config_host_mak
 if test "$coroutine_pool" = "yes" ; then
 if test "$coroutine_pool" = "yes" ; then
   echo "CONFIG_COROUTINE_POOL=1" >> $config_host_mak
   echo "CONFIG_COROUTINE_POOL=1" >> $config_host_mak
 else
 else
@@ -8040,6 +8059,10 @@ fi
 if [ "$capstone" = "git" -o "$capstone" = "internal" ]; then
 if [ "$capstone" = "git" -o "$capstone" = "internal" ]; then
   echo "config-host.h: capstone/all" >> $config_host_mak
   echo "config-host.h: capstone/all" >> $config_host_mak
 fi
 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
 if test -n "$LIBCAPSTONE"; then
   echo "LIBCAPSTONE=$LIBCAPSTONE" >> $config_host_mak
   echo "LIBCAPSTONE=$LIBCAPSTONE" >> $config_host_mak
 fi
 fi

+ 1 - 0
libucontext

@@ -0,0 +1 @@
+Subproject commit 148ec4c5b653a4f602d1f02ff5b02cfaf39f46f9

+ 9 - 0
util/coroutine-ucontext.c

@@ -23,7 +23,16 @@
 #undef _FORTIFY_SOURCE
 #undef _FORTIFY_SOURCE
 #endif
 #endif
 #include "qemu/osdep.h"
 #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>
 #include <ucontext.h>
+#endif
 #include "qemu/coroutine_int.h"
 #include "qemu/coroutine_int.h"
 
 
 #ifdef CONFIG_VALGRIND_H
 #ifdef CONFIG_VALGRIND_H