2023-04-24 12:22:48 +03:00
|
|
|
.. _qom:
|
|
|
|
|
2020-09-11 01:15:25 +03:00
|
|
|
===========================
|
|
|
|
The QEMU Object Model (QOM)
|
|
|
|
===========================
|
|
|
|
|
2020-09-23 19:21:39 +03:00
|
|
|
.. highlight:: c
|
|
|
|
|
|
|
|
The QEMU Object Model provides a framework for registering user creatable
|
|
|
|
types and instantiating objects from those types. QOM provides the following
|
|
|
|
features:
|
|
|
|
|
2020-10-03 05:54:20 +03:00
|
|
|
- System for dynamically registering types
|
|
|
|
- Support for single-inheritance of types
|
|
|
|
- Multiple inheritance of stateless interfaces
|
2023-06-30 21:04:12 +03:00
|
|
|
- Mapping internal members to publicly exposed properties
|
|
|
|
|
|
|
|
The root object class is TYPE_OBJECT which provides for the basic
|
|
|
|
object methods.
|
|
|
|
|
|
|
|
The QOM tree
|
|
|
|
============
|
|
|
|
|
|
|
|
The QOM tree is a composition tree which represents all of the objects
|
|
|
|
that make up a QEMU "machine". You can view this tree by running
|
|
|
|
``info qom-tree`` in the :ref:`QEMU monitor`. It will contain both
|
|
|
|
objects created by the machine itself as well those created due to
|
|
|
|
user configuration.
|
|
|
|
|
|
|
|
Creating a QOM class
|
|
|
|
====================
|
|
|
|
|
2023-07-30 21:03:29 +03:00
|
|
|
A simple minimal device implementation may look something like below:
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Creating a minimal type
|
|
|
|
|
|
|
|
#include "qdev.h"
|
|
|
|
|
|
|
|
#define TYPE_MY_DEVICE "my-device"
|
|
|
|
|
|
|
|
// No new virtual functions: we can reuse the typedef for the
|
|
|
|
// superclass.
|
|
|
|
typedef DeviceClass MyDeviceClass;
|
|
|
|
typedef struct MyDevice
|
|
|
|
{
|
2023-06-30 21:04:10 +03:00
|
|
|
DeviceState parent_obj;
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
int reg0, reg1, reg2;
|
|
|
|
} MyDevice;
|
|
|
|
|
|
|
|
static const TypeInfo my_device_info = {
|
|
|
|
.name = TYPE_MY_DEVICE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDevice),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void my_device_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&my_device_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(my_device_register_types)
|
|
|
|
|
|
|
|
In the above example, we create a simple type that is described by #TypeInfo.
|
|
|
|
#TypeInfo describes information about the type including what it inherits
|
|
|
|
from, the instance and class size, and constructor/destructor hooks.
|
|
|
|
|
2023-06-30 21:04:12 +03:00
|
|
|
The TYPE_DEVICE class is the parent class for all modern devices
|
|
|
|
implemented in QEMU and adds some specific methods to handle QEMU
|
|
|
|
device model. This includes managing the lifetime of devices from
|
|
|
|
creation through to when they become visible to the guest and
|
|
|
|
eventually unrealized.
|
|
|
|
|
2020-09-23 19:21:39 +03:00
|
|
|
Alternatively several static types could be registered using helper macro
|
|
|
|
DEFINE_TYPES()
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
static const TypeInfo device_types_info[] = {
|
|
|
|
{
|
|
|
|
.name = TYPE_MY_DEVICE_A,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDeviceA),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = TYPE_MY_DEVICE_B,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDeviceB),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_TYPES(device_types_info)
|
|
|
|
|
|
|
|
Every type has an #ObjectClass associated with it. #ObjectClass derivatives
|
|
|
|
are instantiated dynamically but there is only ever one instance for any
|
|
|
|
given type. The #ObjectClass typically holds a table of function pointers
|
|
|
|
for the virtual methods implemented by this type.
|
|
|
|
|
|
|
|
Using object_new(), a new #Object derivative will be instantiated. You can
|
|
|
|
cast an #Object to a subclass (or base-class) type using
|
|
|
|
object_dynamic_cast(). You typically want to define macro wrappers around
|
|
|
|
OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
|
|
|
|
specific type:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Typecasting macros
|
|
|
|
|
|
|
|
#define MY_DEVICE_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
|
|
|
|
#define MY_DEVICE_CLASS(klass) \
|
|
|
|
OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
|
|
|
|
#define MY_DEVICE(obj) \
|
|
|
|
OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
|
|
|
|
|
2021-06-24 13:38:24 +03:00
|
|
|
In case the ObjectClass implementation can be built as module a
|
|
|
|
module_obj() line must be added to make sure qemu loads the module
|
|
|
|
when the object is needed.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
module_obj(TYPE_MY_DEVICE);
|
|
|
|
|
2020-09-23 19:21:39 +03:00
|
|
|
Class Initialization
|
2023-06-30 21:04:12 +03:00
|
|
|
--------------------
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
Before an object is initialized, the class for the object must be
|
|
|
|
initialized. There is only one class object for all instance objects
|
|
|
|
that is created lazily.
|
|
|
|
|
|
|
|
Classes are initialized by first initializing any parent classes (if
|
|
|
|
necessary). After the parent class object has initialized, it will be
|
|
|
|
copied into the current class object and any additional storage in the
|
|
|
|
class object is zero filled.
|
|
|
|
|
|
|
|
The effect of this is that classes automatically inherit any virtual
|
|
|
|
function pointers that the parent class has already initialized. All
|
|
|
|
other fields will be zero filled.
|
|
|
|
|
|
|
|
Once all of the parent classes have been initialized, #TypeInfo::class_init
|
|
|
|
is called to let the class being instantiated provide default initialize for
|
|
|
|
its virtual functions. Here is how the above example might be modified
|
|
|
|
to introduce an overridden virtual function:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Overriding a virtual function
|
|
|
|
|
|
|
|
#include "qdev.h"
|
|
|
|
|
|
|
|
void my_device_class_init(ObjectClass *klass, void *class_data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
dc->reset = my_device_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo my_device_info = {
|
|
|
|
.name = TYPE_MY_DEVICE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDevice),
|
|
|
|
.class_init = my_device_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
Introducing new virtual methods requires a class to define its own
|
|
|
|
struct and to add a .class_size member to the #TypeInfo. Each method
|
|
|
|
will also have a wrapper function to call it easily:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Defining an abstract class
|
|
|
|
|
|
|
|
#include "qdev.h"
|
|
|
|
|
|
|
|
typedef struct MyDeviceClass
|
|
|
|
{
|
2023-06-30 21:04:10 +03:00
|
|
|
DeviceClass parent_class;
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
void (*frobnicate) (MyDevice *obj);
|
|
|
|
} MyDeviceClass;
|
|
|
|
|
|
|
|
static const TypeInfo my_device_info = {
|
|
|
|
.name = TYPE_MY_DEVICE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDevice),
|
|
|
|
.abstract = true, // or set a default in my_device_class_init
|
|
|
|
.class_size = sizeof(MyDeviceClass),
|
|
|
|
};
|
|
|
|
|
|
|
|
void my_device_frobnicate(MyDevice *obj)
|
|
|
|
{
|
|
|
|
MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
|
|
|
|
|
|
|
|
klass->frobnicate(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
Interfaces
|
2023-06-30 21:04:12 +03:00
|
|
|
----------
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
Interfaces allow a limited form of multiple inheritance. Instances are
|
|
|
|
similar to normal types except for the fact that are only defined by
|
|
|
|
their classes and never carry any state. As a consequence, a pointer to
|
|
|
|
an interface instance should always be of incomplete type in order to be
|
|
|
|
sure it cannot be dereferenced. That is, you should define the
|
|
|
|
'typedef struct SomethingIf SomethingIf' so that you can pass around
|
|
|
|
``SomethingIf *si`` arguments, but not define a ``struct SomethingIf { ... }``.
|
|
|
|
The only things you can validly do with a ``SomethingIf *`` are to pass it as
|
|
|
|
an argument to a method on its corresponding SomethingIfClass, or to
|
|
|
|
dynamically cast it to an object that implements the interface.
|
|
|
|
|
|
|
|
Methods
|
2023-06-30 21:04:12 +03:00
|
|
|
-------
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2020-10-03 05:54:22 +03:00
|
|
|
A *method* is a function within the namespace scope of
|
2020-09-23 19:21:39 +03:00
|
|
|
a class. It usually operates on the object instance by passing it as a
|
|
|
|
strongly-typed first argument.
|
|
|
|
If it does not operate on an object instance, it is dubbed
|
2020-10-03 05:54:22 +03:00
|
|
|
*class method*.
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
Methods cannot be overloaded. That is, the #ObjectClass and method name
|
|
|
|
uniquely identity the function to be called; the signature does not vary
|
|
|
|
except for trailing varargs.
|
|
|
|
|
2020-10-03 05:54:22 +03:00
|
|
|
Methods are always *virtual*. Overriding a method in
|
2020-09-23 19:21:39 +03:00
|
|
|
#TypeInfo.class_init of a subclass leads to any user of the class obtained
|
|
|
|
via OBJECT_GET_CLASS() accessing the overridden function.
|
|
|
|
The original function is not automatically invoked. It is the responsibility
|
|
|
|
of the overriding class to determine whether and when to invoke the method
|
|
|
|
being overridden.
|
|
|
|
|
|
|
|
To invoke the method being overridden, the preferred solution is to store
|
|
|
|
the original value in the overriding class before overriding the method.
|
|
|
|
This corresponds to ``{super,base}.method(...)`` in Java and C#
|
|
|
|
respectively; this frees the overriding class from hardcoding its parent
|
|
|
|
class, which someone might choose to change at some point.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Overriding a virtual method
|
|
|
|
|
|
|
|
typedef struct MyState MyState;
|
|
|
|
|
|
|
|
typedef void (*MyDoSomething)(MyState *obj);
|
|
|
|
|
|
|
|
typedef struct MyClass {
|
|
|
|
ObjectClass parent_class;
|
|
|
|
|
|
|
|
MyDoSomething do_something;
|
|
|
|
} MyClass;
|
|
|
|
|
|
|
|
static void my_do_something(MyState *obj)
|
|
|
|
{
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
|
|
|
|
static void my_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
MyClass *mc = MY_CLASS(oc);
|
|
|
|
|
|
|
|
mc->do_something = my_do_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo my_type_info = {
|
|
|
|
.name = TYPE_MY,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(MyState),
|
|
|
|
.class_size = sizeof(MyClass),
|
|
|
|
.class_init = my_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct DerivedClass {
|
|
|
|
MyClass parent_class;
|
|
|
|
|
|
|
|
MyDoSomething parent_do_something;
|
|
|
|
} DerivedClass;
|
|
|
|
|
|
|
|
static void derived_do_something(MyState *obj)
|
|
|
|
{
|
|
|
|
DerivedClass *dc = DERIVED_GET_CLASS(obj);
|
|
|
|
|
|
|
|
// do something here
|
|
|
|
dc->parent_do_something(obj);
|
|
|
|
// do something else here
|
|
|
|
}
|
|
|
|
|
|
|
|
static void derived_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
MyClass *mc = MY_CLASS(oc);
|
|
|
|
DerivedClass *dc = DERIVED_CLASS(oc);
|
|
|
|
|
|
|
|
dc->parent_do_something = mc->do_something;
|
|
|
|
mc->do_something = derived_do_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo derived_type_info = {
|
|
|
|
.name = TYPE_DERIVED,
|
|
|
|
.parent = TYPE_MY,
|
|
|
|
.class_size = sizeof(DerivedClass),
|
|
|
|
.class_init = derived_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
Alternatively, object_class_by_name() can be used to obtain the class and
|
|
|
|
its non-overridden methods for a specific type. This would correspond to
|
|
|
|
``MyClass::method(...)`` in C++.
|
|
|
|
|
2023-06-30 21:04:12 +03:00
|
|
|
One example of such methods is ``DeviceClass.reset``. More examples
|
|
|
|
can be found at :ref:`device-life-cycle`.
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
Standard type declaration and definition macros
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
A lot of the code outlined above follows a standard pattern and naming
|
|
|
|
convention. To reduce the amount of boilerplate code that needs to be
|
|
|
|
written for a new type there are two sets of macros to generate the
|
|
|
|
common parts in a standard format.
|
|
|
|
|
|
|
|
A type is declared using the OBJECT_DECLARE macro family. In types
|
|
|
|
which do not require any virtual functions in the class, the
|
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
|
|
|
|
in the header file:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Declaring a simple type
|
|
|
|
|
2022-07-25 17:05:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, MY_DEVICE)
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
This is equivalent to the following:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Expansion from declaring a simple type
|
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
typedef struct MyDevice MyDevice;
|
|
|
|
typedef struct MyDeviceClass MyDeviceClass;
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
#define MY_DEVICE_GET_CLASS(void *obj) \
|
|
|
|
OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
|
|
|
|
#define MY_DEVICE_CLASS(void *klass) \
|
|
|
|
OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
|
|
|
|
#define MY_DEVICE(void *obj)
|
|
|
|
OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
struct MyDeviceClass {
|
|
|
|
DeviceClass parent_class;
|
|
|
|
};
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
The 'struct MyDevice' needs to be declared separately.
|
|
|
|
If the type requires virtual functions to be declared in the class
|
|
|
|
struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
|
|
|
|
used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
|
|
|
|
the 'struct MyDeviceClass' definition.
|
|
|
|
|
|
|
|
To implement the type, the OBJECT_DEFINE macro family is available.
|
|
|
|
In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Defining a simple type
|
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
This is equivalent to the following:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Expansion from defining a simple type
|
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
static void my_device_finalize(Object *obj);
|
|
|
|
static void my_device_class_init(ObjectClass *oc, void *data);
|
|
|
|
static void my_device_init(Object *obj);
|
|
|
|
|
|
|
|
static const TypeInfo my_device_info = {
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.name = TYPE_MY_DEVICE,
|
|
|
|
.instance_size = sizeof(MyDevice),
|
|
|
|
.instance_init = my_device_init,
|
|
|
|
.instance_finalize = my_device_finalize,
|
|
|
|
.class_size = sizeof(MyDeviceClass),
|
|
|
|
.class_init = my_device_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_device_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&my_device_info);
|
|
|
|
}
|
|
|
|
type_init(my_device_register_types);
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
This is sufficient to get the type registered with the type
|
|
|
|
system, and the three standard methods now need to be implemented
|
|
|
|
along with any other logic required for the type.
|
|
|
|
|
|
|
|
If the type needs to implement one or more interfaces, then the
|
|
|
|
OBJECT_DEFINE_TYPE_WITH_INTERFACES() macro can be used instead.
|
|
|
|
This accepts an array of interface type names.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Defining a simple type implementing interfaces
|
|
|
|
|
2020-10-03 05:54:21 +03:00
|
|
|
OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device,
|
|
|
|
MY_DEVICE, DEVICE,
|
2020-10-03 05:54:24 +03:00
|
|
|
{ TYPE_USER_CREATABLE },
|
|
|
|
{ NULL })
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2022-07-07 19:37:15 +03:00
|
|
|
If the type is not intended to be instantiated, then the
|
|
|
|
OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead:
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
:caption: Defining a simple abstract type
|
|
|
|
|
2020-10-03 05:54:24 +03:00
|
|
|
OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device,
|
|
|
|
MY_DEVICE, DEVICE)
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2023-06-30 21:04:12 +03:00
|
|
|
.. _device-life-cycle:
|
|
|
|
|
|
|
|
Device Life-cycle
|
|
|
|
=================
|
|
|
|
|
|
|
|
As class initialisation cannot fail devices have an two additional
|
|
|
|
methods to handle the creation of dynamic devices. The ``realize``
|
|
|
|
function is called with ``Error **`` pointer which should be set if
|
|
|
|
the device cannot complete its setup. Otherwise on successful
|
|
|
|
completion of the ``realize`` method the device object is added to the
|
|
|
|
QOM tree and made visible to the guest.
|
|
|
|
|
|
|
|
The reverse function is ``unrealize`` and should be were clean-up
|
|
|
|
code lives to tidy up after the system is done with the device.
|
|
|
|
|
|
|
|
All devices can be instantiated by C code, however only some can
|
|
|
|
created dynamically via the command line or monitor.
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2023-06-30 21:04:12 +03:00
|
|
|
Likewise only some can be unplugged after creation and need an
|
|
|
|
explicit ``unrealize`` implementation. This is determined by the
|
|
|
|
``user_creatable`` variable in the root ``DeviceClass`` structure.
|
|
|
|
Devices can only be unplugged if their ``parent_bus`` has a registered
|
|
|
|
``HotplugHandler``.
|
2020-09-23 19:21:39 +03:00
|
|
|
|
|
|
|
API Reference
|
2023-06-30 21:04:12 +03:00
|
|
|
=============
|
2020-09-23 19:21:39 +03:00
|
|
|
|
2023-06-30 21:04:11 +03:00
|
|
|
See the :ref:`QOM API<qom-api>` and :ref:`QDEV API<qdev-api>`
|
|
|
|
documents for the complete API description.
|