# 设备模型

qemu作为一个虚拟机的软件，其重要功能之一就是模拟设备。说实话，这个设备模拟的模型还挺复杂的。

看过好几次都没有记清楚，这次重新梳理一遍，并记录在此。

## 设备类型注册

qemu中模拟的每一种设备都在代码中对应了一个类型，这个类型在使用之前需要注册到系统中。这样的好处是后续增添设备的流程变得简单化了。

这一节就来看看设备类型注册的流程。

[设备类型注册](/understanding_qemu/00-devices/01-type_register.md)

## 设备类型初始化

设备类型注册后，在需要使用之前得初始化该类型，并生成对应得ObjectClass对象。

[设备类型初始化](/understanding_qemu/00-devices/02-register_objectclass.md)

## 设备实例化

接着就是实例化设备类型，也就是真的生成一个设备给虚拟机使用。

[设备实例化](/understanding_qemu/00-devices/03-objectclass_instance.md)

## DeviceClass实例化细节

对于qemu中一个"device"设备，除了实例化中instance\_init函数之外，还隐藏了很多实现的细节。

[DeviceClass实例化细节](/understanding_qemu/00-devices/04-deviceclass_instance.md)

## 面向对象的设备模型

在整理了一遍设备类型和实例的初始化过程后，发现qemu的整个设备模型是完整的面向对象模型。

小生斗胆在这里总结一下整个面向对象的模型架构

[面向对象的设备模型](/understanding_qemu/00-devices/05-device_oo_model.md)

## 接口

随着系统的复杂，设备模型中又提出了接口的概念。没怎么用过java，也不知道概念是不是类似。

原本不想看这个部分，谁想到代码中使用到接口的地方还挺多。所以只好硬着头皮看了一遍。

[接口](/understanding_qemu/00-devices/06-interface.md)

## 类型、对象和接口之间的转换

在设备模型中我们涉及了三个概念：

* 类型
* 对象
* 接口

三者之间相互独立又互有关联，在代码中我们也通常会在这几个成员之间转换。

我们单独列出一节总结一下他们之间转换的故事。

[类型、对象和接口之间的转换](/understanding_qemu/00-devices/07-class_obj_interface.md)

## PCDIMM设备

最后我们以PCDIMM设备为例，详细剖析一下该设备初始化并加入系统的过程。

[PCDIMM](/understanding_qemu/00-devices/00-an_example.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://richardweiyang-2.gitbook.io/understanding_qemu/00-devices.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
