From patchwork Mon Sep 16 06:50:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 275119 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DAEC82C007B for ; Mon, 16 Sep 2013 16:53:46 +1000 (EST) Received: from localhost ([::1]:59985 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLSgp-0004Kh-Te for incoming@patchwork.ozlabs.org; Mon, 16 Sep 2013 02:53:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLSeJ-0000Ap-V9 for qemu-devel@nongnu.org; Mon, 16 Sep 2013 02:51:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VLSeD-0001U5-5O for qemu-devel@nongnu.org; Mon, 16 Sep 2013 02:51:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:21483) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLSeC-0001Tu-RS for qemu-devel@nongnu.org; Mon, 16 Sep 2013 02:51:01 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8G6oqYM027736 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 16 Sep 2013 02:50:53 -0400 Received: from T430s.nay.redhat.com ([10.66.6.17]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r8G6oSgN024634; Mon, 16 Sep 2013 02:50:49 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Mon, 16 Sep 2013 14:50:24 +0800 Message-Id: <1379314227-8855-6-git-send-email-famz@redhat.com> In-Reply-To: <1379314227-8855-1-git-send-email-famz@redhat.com> References: <1379314227-8855-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: peter.maydell@linaro.org, famz@redhat.com, mjt@tls.msk.ru, alex@alex.org.uk, pbonzini@redhat.com, vilanova@ac.upc.edu, rth@twiddle.net Subject: [Qemu-devel] [PATCH v10 5/8] module: implement module loading X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Added three types of modules: typedef enum { MODULE_LOAD_BLOCK = 0, MODULE_LOAD_UI, MODULE_LOAD_NET, MODULE_LOAD_MAX, } module_load_type; and their loading function: void module_load(module_load_type). which loads whitelisted ".so" files of the given type under ${MODDIR}. Modules of each type should be loaded in respective subsystem initialization code. The init function of dynamic module is no longer with __attribute__((constructor)) as static linked version, and need to be explicitly called once loaded. The function name is mangled with per configure fingerprint as: init_$(date +%s$$$RANDOM) Which is known to module_load function, and the loading fails if this symbol is not there. With this, modules built from a different tree/version/configure will not be loaded. The module loading code requires gmodule-2.0. Configure option "--enable-modules=L" can be used to restrict qemu to only build/load some whitelisted modules. Signed-off-by: Fam Zheng --- Makefile | 3 ++ block.c | 1 + configure | 44 +++++++++++++++++++++------- include/qemu/module.h | 23 +++++++++++++++ rules.mak | 9 ++++-- scripts/create_config | 22 ++++++++++++++ util/module.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ vl.c | 2 ++ 8 files changed, 172 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b59b49c..20167b8 100644 --- a/Makefile +++ b/Makefile @@ -196,6 +196,9 @@ Makefile: $(version-obj-y) $(version-lobj-y) libqemustub.a: $(stub-obj-y) libqemuutil.a: $(util-obj-y) qapi-types.o qapi-visit.o +default-whitelist = $(foreach o,$(modules-m),"$(subst /,-,$o)",) NULL +util/module.o-cflags = -D'CONFIG_MODULE_WHITELIST=$(default-whitelist)' + ###################################################################### qemu-img.o: qemu-img-cmds.h diff --git a/block.c b/block.c index a325efc..cd4f90e 100644 --- a/block.c +++ b/block.c @@ -3897,6 +3897,7 @@ BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, void bdrv_init(void) { + module_load(MODULE_LOAD_BLOCK); module_call_init(MODULE_INIT_BLOCK); } diff --git a/configure b/configure index 55a75d8..9cb9c7b 100755 --- a/configure +++ b/configure @@ -199,6 +199,7 @@ datadir="\${prefix}/share" qemu_docdir="\${prefix}/share/doc/qemu" bindir="\${prefix}/bin" libdir="\${prefix}/lib" +moddir="\${prefix}/lib/qemu" libexecdir="\${prefix}/libexec" includedir="\${prefix}/include" sysconfdir="\${prefix}/etc" @@ -664,7 +665,9 @@ for opt do ;; --disable-debug-info) ;; - --enable-modules) modules="yes" + --enable-modules|--enable-modules=*) + modules="yes" + module_list=`echo "$optarg" | sed -e 's/,/ /g'` ;; --cpu=*) ;; @@ -689,6 +692,8 @@ for opt do ;; --libdir=*) libdir="$optarg" ;; + --moddir=*) moddir="$optarg" + ;; --libexecdir=*) libexecdir="$optarg" ;; --includedir=*) includedir="$optarg" @@ -1078,10 +1083,14 @@ echo " --datadir=PATH install firmware in PATH$confsuffix" echo " --docdir=PATH install documentation in PATH$confsuffix" echo " --bindir=PATH install binaries in PATH" echo " --libdir=PATH install libraries in PATH" +echo " --moddir=PATH install modules in PATH" echo " --sysconfdir=PATH install config in PATH$confsuffix" echo " --localstatedir=PATH install local state in PATH (set at runtime on win32)" echo " --with-confsuffix=SUFFIX suffix for QEMU data inside datadir and sysconfdir [$confsuffix]" -echo " --enable-modules enable modules support" +echo " --enable-modules enable modules support and whitelist all modules" +echo " --enable-modules=L enable modules and provide a whitelist" +echo " Available modules: block-curl block-iscsi block-gluster" +echo " block-ssh block-rbd" echo " --enable-debug-tcg enable TCG debugging" echo " --disable-debug-tcg disable TCG debugging (default)" echo " --enable-debug-info enable debugging information (default)" @@ -2284,15 +2293,19 @@ if test "$mingw32" = yes; then else glib_req_ver=2.12 fi -if $pkg_config --atleast-version=$glib_req_ver gthread-2.0; then - glib_cflags=`$pkg_config --cflags gthread-2.0` - glib_libs=`$pkg_config --libs gthread-2.0` - CFLAGS="$glib_cflags $CFLAGS" - LIBS="$glib_libs $LIBS" - libs_qga="$glib_libs $libs_qga" -else - error_exit "glib-$glib_req_ver required to compile QEMU" -fi + +for i in gthread-2.0 gmodule-2.0; do + if $pkg_config --atleast-version=$glib_req_ver $i; then + glib_cflags=`$pkg_config --cflags $i` + glib_libs=`$pkg_config --libs $i` + CFLAGS="$glib_cflags $CFLAGS" + LIBS="$glib_libs $LIBS" + libs_qga="$glib_libs $libs_qga" + else + error_exit "glib-$glib_req_ver required to compile QEMU" + fi +done + ########################################## # pixman support probe @@ -3643,6 +3656,7 @@ echo "Install prefix $prefix" echo "BIOS directory `eval echo $qemu_datadir`" echo "binary directory `eval echo $bindir`" echo "library directory `eval echo $libdir`" +echo "module directory `eval echo $moddir`" echo "libexec directory `eval echo $libexecdir`" echo "include directory `eval echo $includedir`" echo "config directory `eval echo $sysconfdir`" @@ -3669,6 +3683,9 @@ if test "$slirp" = "yes" ; then echo "smbd $smbd" fi echo "module support $modules" +if test -n "$module_list"; then + echo "module whitelist $module_list" +fi echo "host CPU $cpu" echo "host big endian $bigendian" echo "target list $target_list" @@ -3769,6 +3786,7 @@ echo all: >> $config_host_mak echo "prefix=$prefix" >> $config_host_mak echo "bindir=$bindir" >> $config_host_mak echo "libdir=$libdir" >> $config_host_mak +echo "moddir=$moddir" >> $config_host_mak echo "libexecdir=$libexecdir" >> $config_host_mak echo "includedir=$includedir" >> $config_host_mak echo "mandir=$mandir" >> $config_host_mak @@ -3787,8 +3805,12 @@ echo "libs_softmmu=$libs_softmmu" >> $config_host_mak echo "ARCH=$ARCH" >> $config_host_mak +echo "CONFIG_FINGERPRINT=$(date +%s$$$RANDOM)" >> $config_host_mak if test "$modules" = "yes"; then echo "CONFIG_MODULES=y" >> $config_host_mak + if test -n "$module_list"; then + echo "CONFIG_MODULE_WHITELIST=$module_list" >> $config_host_mak + fi fi case "$cpu" in arm|i386|x86_64|x32|ppc|aarch64) diff --git a/include/qemu/module.h b/include/qemu/module.h index c4ccd57..6458d8f 100644 --- a/include/qemu/module.h +++ b/include/qemu/module.h @@ -14,11 +14,25 @@ #ifndef QEMU_MODULE_H #define QEMU_MODULE_H +#ifdef BUILD_DSO + +/* For error message, this function is an identification of qemu module */ +void qemu_module_do_init(void (*init)(void)); + +/* To restrict loading of arbitrary DSO, The init function name is changed per + * "./configure" to refuse unknown DSO file */ +void DSO_INIT_FUN(void); + +#define module_init(function, type) \ +void qemu_module_do_init(void (*init)(void)) { init(); } \ +void DSO_INIT_FUN(void) { qemu_module_do_init(function); } +#else /* This should not be used directly. Use block_init etc. instead. */ #define module_init(function, type) \ static void __attribute__((constructor)) do_qemu_init_ ## function(void) { \ register_module_init(function, type); \ } +#endif typedef enum { MODULE_INIT_BLOCK, @@ -37,4 +51,13 @@ void register_module_init(void (*fn)(void), module_init_type type); void module_call_init(module_init_type type); +typedef enum { + MODULE_LOAD_BLOCK = 0, + MODULE_LOAD_UI, + MODULE_LOAD_NET, + MODULE_LOAD_MAX, +} module_load_type; + +void module_load(module_load_type type); + #endif diff --git a/rules.mak b/rules.mak index 2ff2f16..dc9757a 100644 --- a/rules.mak +++ b/rules.mak @@ -69,7 +69,7 @@ endif %.o: %.dtrace $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@") -%$(DSOSUF): QEMU_CFLAGS += -fPIC +%$(DSOSUF): QEMU_CFLAGS += -fPIC -DBUILD_DSO %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED) %$(DSOSUF): %.mo libqemustub.a $(call LINK,$^) @@ -172,13 +172,18 @@ $(if $(nested-dirs), $(call unnest-vars-1)) endef +is-whitelisted = $(if $(CONFIG_MODULE_WHITELIST),$(strip \ + $(filter $(CONFIG_MODULE_WHITELIST),$(subst /,-,$(basename $1)))),\ + yes) define add-modules $(foreach o,$(filter %.o,$($1)), $(eval $(patsubst %.o,%.mo,$o): $o) \ $(eval $(patsubst %.o,%.mo,$o)-objs := $o)) $(foreach o,$(filter %.mo,$($1)),$(eval \ $o: $($o-objs))) -$(eval modules-m += $(patsubst %.o,%.mo,$($1))) +$(eval t := $(patsubst %.o,%.mo,$($1))) +$(foreach o,$t,$(if $(call is-whitelisted,$o),$(eval \ + modules-m += $o))) endef define unnest-vars diff --git a/scripts/create_config b/scripts/create_config index b1adbf5..6d47df7 100755 --- a/scripts/create_config +++ b/scripts/create_config @@ -26,6 +26,25 @@ case $line in # save for the next definitions prefix=${line#*=} ;; + moddir=*) + eval "moddir=\"${line#*=}\"" + echo "#define CONFIG_MODDIR \"$moddir\"" + ;; + CONFIG_FINGERPRINT=*) + echo "#define DSO_INIT_FUN init_${line#*=}" + echo "#define DSO_INIT_FUN_STR \"init_${line#*=}\"" + ;; + CONFIG_MODULES=*) + echo "#define CONFIG_MODULES \"${line#*=}\"" + ;; + CONFIG_MODULE_WHITELIST=*) + echo "#undef CONFIG_MODULE_WHITELIST" + echo "#define CONFIG_MODULE_WHITELIST\\" + for mod in ${line#*=}; do + echo " \"${mod}\",\\" + done + echo " NULL" + ;; CONFIG_AUDIO_DRIVERS=*) drivers=${line#*=} echo "#define CONFIG_AUDIO_DRIVERS \\" @@ -104,6 +123,9 @@ case $line in value=${line#*=} echo "#define $name $value" ;; + DSOSUF=*) + echo "#define HOST_DSOSUF \"${line#*=}\"" + ;; esac done # read diff --git a/util/module.c b/util/module.c index 7acc33d..70da1ca 100644 --- a/util/module.c +++ b/util/module.c @@ -13,6 +13,7 @@ * GNU GPL, version 2 or (at your option) any later version. */ +#include #include "qemu-common.h" #include "qemu/queue.h" #include "qemu/module.h" @@ -79,3 +80,83 @@ void module_call_init(module_init_type type) e->init(); } } + +#ifdef CONFIG_MODULES +static void module_load_file(const char *fname) +{ + GModule *g_module; + void (*init_fun)(void); + const char *dsosuf = HOST_DSOSUF; + int len = strlen(fname); + int suf_len = strlen(dsosuf); + + if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) { + /* wrong suffix */ + return; + } + if (access(fname, F_OK)) { + return; + } + + g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); + if (!g_module) { + fprintf(stderr, "Failed to load module: %s\n", + g_module_error()); + return; + } + if (!g_module_symbol(g_module, DSO_INIT_FUN_STR, (gpointer *)&init_fun)) { + fprintf(stderr, "Failed to initialize module: %s\n", + fname); + /* Print some info if this is a QEMU module (but from different build), + * this will make debugging user problems easier. */ + if (g_module_symbol(g_module, "qemu_module_do_init", + (gpointer *)&init_fun)) { + fprintf(stderr, + "Note: only modules from the same build can be loaded.\n"); + } + g_module_close(g_module); + return; + } + init_fun(); +} +#endif + +void module_load(module_load_type type) +{ +#ifdef CONFIG_MODULES + const char *prefix; + char *fname = NULL; + const char **mp; + static const char *module_whitelist[] = { + CONFIG_MODULE_WHITELIST + }; + + if (!g_module_supported()) { + return; + } + + switch (type) { + case MODULE_LOAD_BLOCK: + prefix = "block-"; + break; + case MODULE_LOAD_UI: + prefix = "ui-"; + break; + case MODULE_LOAD_NET: + prefix = "ui-"; + break; + default: + return; + } + + for (mp = &module_whitelist[0]; *mp; mp++) { + if (strncmp(prefix, *mp, strlen(prefix))) { + continue; + } + fname = g_strdup_printf("%s/%s%s", CONFIG_MODDIR, *mp, HOST_DSOSUF); + module_load_file(fname); + g_free(fname); + } + +#endif +} diff --git a/vl.c b/vl.c index 4e709d5..b20e7fb 100644 --- a/vl.c +++ b/vl.c @@ -2866,6 +2866,8 @@ int main(int argc, char **argv, char **envp) #endif } + module_load(MODULE_LOAD_UI); + module_load(MODULE_LOAD_NET); module_call_init(MODULE_INIT_QOM); qemu_add_opts(&qemu_drive_opts);