From patchwork Mon Jul 25 01:44:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 106585 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 21207B6F7F for ; Mon, 25 Jul 2011 11:46:49 +1000 (EST) Received: from localhost ([::1]:58467 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QlAFl-0007Hw-60 for incoming@patchwork.ozlabs.org; Sun, 24 Jul 2011 21:46:41 -0400 Received: from eggs.gnu.org ([140.186.70.92]:42439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QlAEL-0004An-NI for qemu-devel@nongnu.org; Sun, 24 Jul 2011 21:45:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QlAEH-0005Yo-RN for qemu-devel@nongnu.org; Sun, 24 Jul 2011 21:45:13 -0400 Received: from e5.ny.us.ibm.com ([32.97.182.145]:56268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QlAEH-0005XL-0e for qemu-devel@nongnu.org; Sun, 24 Jul 2011 21:45:09 -0400 Received: from d01relay05.pok.ibm.com (d01relay05.pok.ibm.com [9.56.227.237]) by e5.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p6P1G4nY011415 for ; Sun, 24 Jul 2011 21:16:04 -0400 Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay05.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p6P1j8vS154400 for ; Sun, 24 Jul 2011 21:45:08 -0400 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p6P1j7Hh007948 for ; Sun, 24 Jul 2011 21:45:07 -0400 Received: from titi.austin.rr.com (sig-9-65-207-230.mts.ibm.com [9.65.207.230]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p6P1itKu006824; Sun, 24 Jul 2011 21:45:07 -0400 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Sun, 24 Jul 2011 20:44:47 -0500 Message-Id: <1311558293-5855-16-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com> References: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.182.145 Cc: Anthony Liguori Subject: [Qemu-devel] [PATCH 15/21] qom: add Device class 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 Device is meant to replace DeviceState as the root class for the device model. This is included here merely as a RFC. Device adds a couple of useful features. 1) Default hard reset. Device will literally call finalize on the object and then reinitialize it in place. This means that most devices don't have to worry about implementing reset logic. Reset preserves the current state of properties which makes it equivalent to taking the properties of the current device and then initializing a new object using those properties. 2) Full object serialization as a property. The 'state' property is special in that it will invoke a visit method that's intended to be overridden by a subclass. The visit method will visit the full object. This exists to enable live migration. Notice that there is no mention of compatibility, versioning, subsections, etc. The idea behind supporting robust migration is that the device model is only responsible for generating the state of the current device model, the structure of the device model, and the current set of properties. The expectation is that another layer will perform transformations of the resulting tree to preserve migration compatibility. Tree transformation is a powerful mechanism and even with a totally different device model, this should enable us to support migration compatibility even to the current VMState based migration code. Signed-off-by: Anthony Liguori --- Makefile.qom | 3 + Qconfig | 1 + configure | 2 +- devices/Makefile | 1 + devices/Qconfig | 6 ++ devices/device.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/device.h | 28 +++++++++++ 7 files changed, 165 insertions(+), 1 deletions(-) create mode 100644 devices/Makefile create mode 100644 devices/Qconfig create mode 100644 devices/device.c create mode 100644 include/qemu/device.h diff --git a/Makefile.qom b/Makefile.qom index 34f1f91..c694cbb 100644 --- a/Makefile.qom +++ b/Makefile.qom @@ -13,3 +13,6 @@ common-obj-y += $(addprefix qapi/,$(qapi-obj-y)) include $(SRC_PATH)/qom/Makefile common-obj-y += $(addprefix qom/,$(qom-obj-y)) +include $(SRC_PATH)/devices/Makefile +common-obj-y += $(addprefix devices/,$(devices-obj-y)) + diff --git a/Qconfig b/Qconfig index 889dfa6..03f2a87 100644 --- a/Qconfig +++ b/Qconfig @@ -1,2 +1,3 @@ source qapi/Qconfig source qom/Qconfig +source devices/Qconfig diff --git a/configure b/configure index 93e5e97..6ec1020 100755 --- a/configure +++ b/configure @@ -3516,7 +3516,7 @@ DIRS="$DIRS pc-bios/spapr-rtas" DIRS="$DIRS roms/seabios roms/vgabios" DIRS="$DIRS fsdev ui" DIRS="$DIRS qapi" -DIRS="$DIRS qga qom" +DIRS="$DIRS qga qom devices" FILES="Makefile tests/Makefile" FILES="$FILES tests/cris/Makefile tests/cris/.gdbinit" FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps" diff --git a/devices/Makefile b/devices/Makefile new file mode 100644 index 0000000..fbb0b82 --- /dev/null +++ b/devices/Makefile @@ -0,0 +1 @@ +devices-obj-$(CONFIG_DEVICE) := device.o diff --git a/devices/Qconfig b/devices/Qconfig new file mode 100644 index 0000000..6a06417 --- /dev/null +++ b/devices/Qconfig @@ -0,0 +1,6 @@ +config DEVICE + bool "QOM based device model" + default y + depends on QOM && QAPI_QMP + help + Device model diff --git a/devices/device.c b/devices/device.c new file mode 100644 index 0000000..5d289fc --- /dev/null +++ b/devices/device.c @@ -0,0 +1,125 @@ +#include "qemu/device.h" +#include "qapi/qmp-output-visitor.h" +#include "qapi/qmp-input-visitor.h" + +static void device_state_accessor(Plug *plug, const char *name, Visitor *v, void *opaque, Error **errp) +{ + DeviceClass *class = DEVICE_GET_CLASS(DEVICE(plug)); + return class->visit(DEVICE(plug), v, name, errp); +} + +static void device_initfn(TypeInstance *obj) +{ + Device *device = DEVICE(obj); + + plug_add_property_full(PLUG(device), "state", + device_state_accessor, + device_state_accessor, + NULL, + NULL, + type_get_type(obj), + PROP_F_READWRITE); +} + +void device_visit(Device *device, Visitor *v, const char *name, Error **errp) +{ + visit_start_struct(v, (void **)&device, "Device", name, 0, errp); + visit_end_struct(v, errp); +} + +static void device_visit_properties(Device *device, bool is_input, const char *name, Visitor *v, Error **errp); + +typedef struct DeviceVisitPropertyData +{ + Visitor *v; + Error **errp; + bool is_input; +} DeviceVisitPropertyData; + +static void device_visit_property(Plug *plug, const char *name, const char *typename, int flags, void *opaque) +{ + DeviceVisitPropertyData *data = opaque; + + if (strcmp(name, "state") == 0 || strcmp(name, "realized") == 0) { + return; + } + + if (strstart(typename, "plug<", NULL)) { + Device *value = DEVICE(plug_get_property_plug(plug, NULL, name)); + device_visit_properties(value, data->is_input, name, data->v, data->errp); + } else if (data->is_input) { + if ((flags & PROP_F_READ) && (flags & PROP_F_WRITE)) { + plug_set_property(plug, name, data->v, data->errp); + } + } else { + if (flags & PROP_F_READ) { + plug_get_property(plug, name, data->v, data->errp); + } + } +} + +static void device_visit_properties(Device *device, bool is_input, const char *name, Visitor *v, Error **errp) +{ + DeviceVisitPropertyData data = { + .v = v, + .errp = errp, + .is_input = is_input, + }; + + visit_start_struct(v, (void **)&device, "Device", name, sizeof(Device), errp); + plug_foreach_property(PLUG(device), device_visit_property, &data); + visit_end_struct(v, errp); +} + +static void device_unrealize(Plug *plug) +{ + Device *device = DEVICE(plug); + const char *typename; + char id[MAX_ID]; + QmpOutputVisitor *qov; + QmpInputVisitor *qiv; + Error *local_err = NULL; // FIXME + + snprintf(id, sizeof(id), "%s", type_get_id(TYPE_INSTANCE(device))); + typename = type_get_type(TYPE_INSTANCE(device)); + + qov = qmp_output_visitor_new(); + + device_visit_properties(device, false, id, qmp_output_get_visitor(qov), &local_err); + + type_finalize(device); + type_initialize(device, typename, id); + + qiv = qmp_input_visitor_new(qmp_output_get_qobject(qov)); + + device_visit_properties(device, true, id, qmp_input_get_visitor(qiv), &local_err); + + qmp_input_visitor_cleanup(qiv); + qmp_output_visitor_cleanup(qov); +} + +static void device_class_initfn(TypeClass *type_class) +{ + PlugClass *plug_class = PLUG_CLASS(type_class); + DeviceClass *class = DEVICE_CLASS(type_class); + + plug_class->unrealize = device_unrealize; + class->visit = device_visit; +} + +static const TypeInfo device_type_info = { + .name = TYPE_DEVICE, + .parent = TYPE_PLUG, + .instance_size = sizeof(Device), + .class_size = sizeof(DeviceClass), + .class_init = device_class_initfn, + .instance_init = device_initfn, + .abstract = true, +}; + +static void register_devices(void) +{ + type_register_static(&device_type_info); +} + +device_init(register_devices); diff --git a/include/qemu/device.h b/include/qemu/device.h new file mode 100644 index 0000000..8e15232 --- /dev/null +++ b/include/qemu/device.h @@ -0,0 +1,28 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include "qemu/plug.h" + +typedef struct Device +{ + Plug parent; +} Device; + +typedef void (DeviceVisitor)(Device *device, Visitor *v, const char *name, Error **errp); + +typedef struct DeviceClass +{ + PlugClass parent_class; + + /* public */ + DeviceVisitor *visit; +} DeviceClass; + +#define TYPE_DEVICE "device" +#define DEVICE(obj) TYPE_CHECK(Device, obj, TYPE_DEVICE) +#define DEVICE_CLASS(class) TYPE_CLASS_CHECK(DeviceClass, class, TYPE_DEVICE) +#define DEVICE_GET_CLASS(obj) TYPE_GET_CLASS(DeviceClass, obj, TYPE_DEVICE) + +void device_visit(Device *device, Visitor *v, const char *name, Error **errp); + +#endif