如何在QEMU源代码中添加新设备?

21

使用QOM方法在qemu中仿真/添加新设备的步骤是什么?

与DeviceState/BusState和其他属性相关的更改应该在哪里进行?


三,是什么类型的设备? - osgx
2个回答

21

edu 是一个内置的教育PCI设备

它非常易于理解并且有很好的记录,所以我建议你去了解一下。

它暴露了一个最小的PCI设备,具有基本的IO、中断生成和DMA功能。

我写了一个最小的Linux内核模块+用户测试程序来使用它:

外置设备

我问过是否有可能在外部异步中制作out-of-tree设备:如何创建out-of-tree QEMU设备? 但看起来不太可能。


1
在这里的最小示例中,TypeInfo结构不包括.interfaces字段。为了通过父类中的断言,可以像最新示例edu.c设备的431行中所示进行修改。 - Onofog
@user786 你好,不确定你使用的是哪个版本,这个答案指向 https://github.com/cirosantilli/linux-kernel-module-cheat/tree/6788a577c394a2fc512d8f3df0806d84dc09f355 ,但在那个版本中没有configure,当前的主版本也没有:https://github.com/cirosantilli/linux-kernel-module-cheat/tree/38eb67ad2ec46b58dbc701073ce0e980fad7788b 只需使用此部分作为起点:https://github.com/cirosantilli/linux-kernel-module-cheat/tree/38eb67ad2ec46b58dbc701073ce0e980fad7788b#qemu-buildroot-setup ,你应该没问题。 - Ciro Santilli OurBigBook.com
@user786 我没看到 ./configure 命令。我给出的最后一个链接是执行以下命令:./setup && ./build --download-dependencies qemu-buildroot && ./run - Ciro Santilli OurBigBook.com
请问我需要运行什么确切的命令来克隆它?我尝试了,但是使用克隆下载的tar文件中没有包含子模块目录和许多其他文件和目录。在查看您的文档之前,我查看了克隆命令,该命令为git clone https://github.com/cirosantilli/linux-kernel-module-cheat - user786
内核模块代码在哪里可以引起“内核崩溃”,您能给我提供c文件链接吗?我想看看。这引起了我的兴趣,您能发个链接给我吗? - user786
显示剩余13条评论

5

在2014年的“QOM exegesis and apocalypse”演示中,有一些例子,演示位于http://events.linuxfoundation.org/sites/events/files/slides/kvmforum14-qom_0.pdf

Creating an object

Object *o = object_new(TYPE_RNG_BACKEND_RANDOM);
object_property_set_str(o, "filename", "/dev/random", NULL);
object_property_set_bool(o, "opened", "true", NULL);
object_property_add_child(container_get("/somewhere"), "my-rng", o, NULL);
object_unref(o);

Inside properties

static bool rng_get_opened(Object *obj, Error **errp)
{
    RngBackend *s = RNG_BACKEND(obj);
    return s->opened;
}
static void rng_set_opened(Object *obj, bool value, Error **errp)
{
    RngBackend *s = RNG_BACKEND(obj);
    RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
    ...
    if (k->opened) {
        k->opened(s, errp)
    }
}
static void rng_backend_init(Object *obj)
{
    object_property_add_bool(obj, "opened",
        rng_get_opened, rng_set_opened, NULL);
}
static const TypeInfo rng_backend_info = {
   .name = TYPE_RNG_BACKEND,
   .parent = TYPE_OBJECT,
   .instance_size = sizeof(RngBackend),
   .instance_init = rng_backend_init,
   .class_size = sizeof(RngBackendClass),
   .abstract = true,
};
这两个页面可能有用: * http://wiki.qemu.org/Features/QOM * http://wiki.qemu.org/QOMConventions 请参照实际代码:http://code.metager.de/source/xref/qemu/backends/rng.c 和 RNG_BACKEND 的一个实现 http://code.metager.de/source/xref/qemu/backends/rng-random.c
Siro Mugabi的文章“Essential QEMU PCI API”:http://nairobi-embedded.org/001_qemu_pci_device_essentials.htmlhttp://web.archive.org/web/20151116022950/http://nairobi-embedded.org/001_qemu_pci_device_essentials.html)提供了一个完整的QOM-enabled PCI driver示例。

The QEMU Object Model (QOM) provides a framework for registering user creatable Types. QOM models buses, interfaces, devices, etc as types. In QOM, information by a user Type is used to create its ObjectClass instance as well as its Object instance. This information is specified in a TypeInfo structure (include/qom/object.h). For example:

/* hw/misc/pci-testdev.c */

static const TypeInfo pci_testdev_info = {
        .name          = TYPE_PCI_TEST_DEV,
        .parent        = TYPE_PCI_DEVICE,
        .instance_size = sizeof(PCITestDevState),
        .class_init    = pci_testdev_class_init,
};

where:

  • .name a string that indicates the user Type.
  • .parent a string that specifies the Type from which this user Type derives from.
  • .instance_size size of the Type's Object instance. Its allocation will be performed internally by QOM. Objects will be discussed in more detail in Section Object Instantiation.
  • .class_init the constructor hook. This function will be responsible for initializing the Type's ObjectClass instance.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接