All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: dinechin@redhat.com, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>
Subject: [PATCH v4 1/7] qdev: add support for device module loading
Date: Mon, 22 Jun 2020 15:55:55 +0200	[thread overview]
Message-ID: <20200622135601.12433-2-kraxel@redhat.com> (raw)
In-Reply-To: <20200622135601.12433-1-kraxel@redhat.com>

When compiling devices as modules we'll need some infrastrtucture to
actually load those modules if needed.  This patch adds it.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 include/hw/qdev-core.h |  3 +++
 include/qemu/module.h  |  1 +
 hw/core/qdev.c         | 55 ++++++++++++++++++++++++++++++++++++++++++
 qdev-monitor.c         |  5 ++++
 qom/qom-qmp-cmds.c     |  5 ++++
 softmmu/vl.c           |  4 +++
 stubs/hw-module.c      | 10 ++++++++
 stubs/Makefile.objs    |  1 +
 8 files changed, 84 insertions(+)
 create mode 100644 stubs/hw-module.c

diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 7dc10be46f8c..0a33939f4855 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -557,4 +557,7 @@ void device_listener_unregister(DeviceListener *listener);
  */
 bool qdev_should_hide_device(QemuOpts *opts);
 
+void qdev_module_load_type(const char *type);
+void qdev_module_load_all(void);
+
 #endif
diff --git a/include/qemu/module.h b/include/qemu/module.h
index 011ae1ae7605..077a6b09bca7 100644
--- a/include/qemu/module.h
+++ b/include/qemu/module.h
@@ -64,6 +64,7 @@ typedef enum {
 #define block_module_load_one(lib) module_load_one("block-", lib)
 #define ui_module_load_one(lib) module_load_one("ui-", lib)
 #define audio_module_load_one(lib) module_load_one("audio-", lib)
+#define hw_module_load_one(lib) module_load_one("hw-", lib)
 
 void register_module_init(void (*fn)(void), module_init_type type);
 void register_dso_module_init(void (*fn)(void), module_init_type type);
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 2131c7f951dd..6119e25e0eeb 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -137,6 +137,9 @@ void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
  */
 DeviceState *qdev_new(const char *name)
 {
+    if (!object_class_by_name(name)) {
+        qdev_module_load_type(name);
+    }
     return DEVICE(object_new(name));
 }
 
@@ -147,6 +150,9 @@ DeviceState *qdev_new(const char *name)
  */
 DeviceState *qdev_try_new(const char *name)
 {
+    if (!object_class_by_name(name)) {
+        qdev_module_load_type(name);
+    }
     if (!object_class_by_name(name)) {
         return NULL;
     }
@@ -154,6 +160,55 @@ DeviceState *qdev_try_new(const char *name)
     return DEVICE(object_new(name));
 }
 
+/*
+ * Building devices modular is mostly useful in case they have
+ * dependencies to external shared libraries, so we can cut down the
+ * core qemu library dependencies.  Which is the case for only a very
+ * few devices.  So with the expectation that this will be rather the
+ * exception than to rule go with a simple hardcoded list for now
+ * (instead of generating it automatically somehow).
+ */
+static struct {
+    const char *type;
+    const char *mod;
+} const hwmodules[] = {
+};
+
+static bool qdev_module_loaded_all;
+
+void qdev_module_load_type(const char *type)
+{
+    int i;
+
+    if (qdev_module_loaded_all) {
+        return;
+    }
+    for (i = 0; i < ARRAY_SIZE(hwmodules); i++) {
+        if (strcmp(hwmodules[i].type, type) == 0) {
+            hw_module_load_one(hwmodules[i].mod);
+            return;
+        }
+    }
+}
+
+void qdev_module_load_all(void)
+{
+    int i;
+
+    if (qdev_module_loaded_all) {
+        return;
+    }
+    for (i = 0; i < ARRAY_SIZE(hwmodules); i++) {
+        if (i > 0 && strcmp(hwmodules[i - 1].mod,
+                            hwmodules[i].mod) == 0) {
+            /* one module implementing multiple devices -> load only once */
+            continue;
+        }
+        hw_module_load_one(hwmodules[i].mod);
+    }
+    qdev_module_loaded_all = true;
+}
+
 static QTAILQ_HEAD(, DeviceListener) device_listeners
     = QTAILQ_HEAD_INITIALIZER(device_listeners);
 
diff --git a/qdev-monitor.c b/qdev-monitor.c
index 22da107484c5..86f29daa7469 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -147,6 +147,7 @@ static void qdev_print_devinfos(bool show_no_user)
     int i;
     bool cat_printed;
 
+    qdev_module_load_all();
     list = object_class_get_list_sorted(TYPE_DEVICE, false);
 
     for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
@@ -224,6 +225,10 @@ static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
             oc = object_class_by_name(*driver);
         }
     }
+    if (!oc) {
+        qdev_module_load_type(*driver);
+        oc = object_class_by_name(*driver);
+    }
 
     if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
         if (*driver != original_name) {
diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
index c5249e44d020..4e0d7e53cd16 100644
--- a/qom/qom-qmp-cmds.c
+++ b/qom/qom-qmp-cmds.c
@@ -116,6 +116,7 @@ ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
 {
     ObjectTypeInfoList *ret = NULL;
 
+    qdev_module_load_all();
     object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
 
     return ret;
@@ -131,6 +132,10 @@ ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
     ObjectPropertyInfoList *prop_list = NULL;
 
     klass = object_class_by_name(typename);
+    if (klass == NULL) {
+        qdev_module_load_type(typename);
+        klass = object_class_by_name(typename);
+    }
     if (klass == NULL) {
         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                   "Device '%s' not found", typename);
diff --git a/softmmu/vl.c b/softmmu/vl.c
index f669c06ede4a..8d753ead5983 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -1770,6 +1770,10 @@ static bool vga_interface_available(VGAInterfaceType t)
 {
     const VGAInterfaceInfo *ti = &vga_interfaces[t];
 
+    if (ti->class_names[0] && !object_class_by_name(ti->class_names[0])) {
+        qdev_module_load_type(ti->class_names[0]);
+    }
+
     assert(t < VGA_TYPE_MAX);
     return !ti->class_names[0] ||
            object_class_by_name(ti->class_names[0]) ||
diff --git a/stubs/hw-module.c b/stubs/hw-module.c
new file mode 100644
index 000000000000..58b2160b97fe
--- /dev/null
+++ b/stubs/hw-module.c
@@ -0,0 +1,10 @@
+#include "qemu/osdep.h"
+#include "hw/qdev-core.h"
+
+void qdev_module_load_all(void)
+{
+}
+
+void qdev_module_load_type(const char *type)
+{
+}
diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs
index 28e48171d1f3..7a9f38f60b78 100644
--- a/stubs/Makefile.objs
+++ b/stubs/Makefile.objs
@@ -5,6 +5,7 @@ stub-obj-y += dump.o
 stub-obj-y += error-printf.o
 stub-obj-y += fdset.o
 stub-obj-y += gdbstub.o
+stub-obj-y += hw-module.o
 stub-obj-y += iothread-lock.o
 stub-obj-y += is-daemonized.o
 stub-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
-- 
2.18.4



  reply	other threads:[~2020-06-22 13:58 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-22 13:55 [PATCH v4 0/7] build some devices as modules Gerd Hoffmann
2020-06-22 13:55 ` Gerd Hoffmann [this message]
2020-06-26 18:46   ` [PATCH v4 1/7] qdev: add support for device module loading Richard Henderson
2020-06-22 13:55 ` [PATCH v4 2/7] build: fix device module builds Gerd Hoffmann
2020-06-22 13:55 ` [PATCH v4 3/7] ccid: build smartcard as module Gerd Hoffmann
2020-06-23 15:28   ` Philippe Mathieu-Daudé
2020-06-23 17:12     ` Gerd Hoffmann
2020-06-30  9:44       ` Philippe Mathieu-Daudé
2020-06-30 16:07         ` Gerd Hoffmann
2020-06-30 16:24           ` Daniel P. Berrangé
2020-06-22 13:55 ` [PATCH v4 4/7] usb: build usb-redir " Gerd Hoffmann
2020-06-22 13:55 ` [PATCH v4 5/7] vga: build qxl " Gerd Hoffmann
2020-06-22 13:56 ` [PATCH v4 6/7] vga: build virtio-gpu only once Gerd Hoffmann
2020-06-22 13:56 ` [PATCH v4 7/7] vga: build virtio-gpu as module Gerd Hoffmann
2020-06-23 15:04 ` [PATCH v4 0/7] build some devices as modules Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200622135601.12433-2-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=berrange@redhat.com \
    --cc=dinechin@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.