如何让我的设备对 QEMU 可用

How do I make my device available to QEMU

我写了一个我认为是最小的自定义设备来测试我对 QOM 和 QEMU 的总体理解。以下是省略注释的相关代码。不幸的是,当我启动来宾并将我的设备名称作为命令行参数传递时,它无法找到我的设备并退出。我正在发布:

qemu-system-x86_64 -device hello-world-device ...

我怎样才能让 QEMU 知道我的设备?我需要采取哪些步骤才能正确构建我的设备?

我可以在我放置源代码的文件夹中看到一个对象列表 (qemu/hw/misc),但我无法找到定义其他目标的位置以添加到我的新设备的目标中。代码:

#include "qom/object.h"
#include "hw/qdev-core.h"
#define TYPE_HELLO "hello-world-device"
#define HELLO(obj) OBJECT_CHECK(HelloState, (obj), TYPE_HELLO)

typedef struct{
    DeviceClass parent_obj;
    uint8_t member0, member1;
} HelloState;

static const TypeInfo hello_info = {
    .name = TYPE_HELLO,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(HelloState),
    .class_init = class_init,
};

static void class_init(ObjectClass *klass, void *data){
    DeviceClass *dc = DEVICE_CLASS(klass);
}

static void hello_register_types(void){
    type_register_static(&hello_info);
}

type_init(hello_register_types)

有关应该如何完成的文档不清楚或缺失,但我确实找到了一个临时有效的解决方案。

在存储源文件的目录中(此处为 qemu/hw/misc/),有一个名为 Makefile.objs 的文件,其中包含要创建的目标文件目标列表。添加以下行将导致主 Makefile(在 QEMU 根目录中)在定义配置标志 CONFIG_HELLO 时创建一个目标:

common-obj-$(CONFIG_HELLO) += hello-world-device.o

要定义此自定义标志,可以将一个条目添加到所选的目标体系结构中。在这里,我将它添加到 qemu/default-configs/x86_64-softmmu.mak:

中 x86_64 的配置中
CONFIG_HELLO=y

进行上述更改后,运行make 创建构建自定义设备的规则,并在构建体系结构时在适当的时间运行它。这揭示了现有代码中的包含错误,为方便起见,在此处重复更正:

#include "qemu/osdeps.h"
#include "hw/hw.h"
#define TYPE_HELLO "hello-world-device"
#define HELLO(obj) OBJECT_CHECK(HelloState, (obj), TYPE_HELLO)

typedef struct{
    DeviceClass parent_obj;
    uint8_t member0, member1;
} HelloState;

static const TypeInfo hello_info = {
    .name = TYPE_HELLO,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(HelloState),
    .class_init = class_init,
};

static void class_init(ObjectClass *klass, void *data){
    DeviceClass *dc = DEVICE_CLASS(klass);
}

static void hello_register_types(void){
    type_register_static(&hello_info);
}

type_init(hello_register_types)