Full analysis of QEMU source code 10 - define a QEMU module (2)

Continued from the previous article: QEMU source code full analysis 9 - define a QEMU module (1)

References for the content of this article:

"Interesting Talk about Linux Operating System " —— Liu Chao, Geek Time

" QEMU /KVM" source code analysis and application - Li Qiang, Machinery Industry Press

Thank you very much!

Last time I mentioned that defining a QEMU module will call type_init, and taking kvm as an example, I talked about the following code (in accel/kvm/kvm-all.c):

type_init(kvm_type_init);

The last time we talked about the branch of the type_init macro definition, this time let us go back to the "Shuangyang Fork" and walk through another branch - kvm_type_init is the specific implementation of the initialization function of the KVM module.

The kvm_type_init function is actually just above the line type_init(kvm_type_init); the code is as follows:

static void kvm_type_init(void)
{
    type_register_static(&kvm_accel_type);
}

type_init(kvm_type_init);

The initialization code of kvm_accel_type is just above kvm_type_init, as follows:

static const TypeInfo kvm_accel_type = {
    .name = TYPE_KVM_ACCEL,
    .parent = TYPE_ACCEL,
    .instance_init = kvm_accel_instance_init,
    .class_init = kvm_accel_class_init,
    .instance_size = sizeof(KVMState),
};

kvm_type_init will register kvm_accel_type. It can be considered that kvm_accel_type dynamically defines a class whose name is TYPE_KVM_ACCEL; its parent class is TYPE_ACCEL; the initialization of this class should call the function kvm_accel_class_init; if an object is declared with this class, the size of the object is instance_size.

The kvm_type_init function simply calls the type_register_static function, which is in qom/object.c, and the code is as follows:

TypeImpl *type_register_static(const TypeInfo *info)
{
    return type_register(info);
}

It can be seen that the type_register_static function is a simple encapsulation of the type_register function. The type_register function is just above the type_register_static function, the code is as follows:

TypeImpl *type_register(const TypeInfo *info)
{
    assert(info->parent);
    return type_register_internal(info);
}

Continue to follow up, the type_register_internal function is above the type_register function again, the code is as follows:

static TypeImpl *type_register_internal(const TypeInfo *info)
{
    TypeImpl *ti;
    ti = type_new(info);

    type_table_add(ti);
    return ti;
}

Finally, it is no longer a simple wrapper function. There are 2 functions in the type_register_internal function: type_new and type_table_add.

For the in-depth analysis of the type_register_internal function and two of them, we will put it in the next article.

Guess you like

Origin blog.csdn.net/phmatthaus/article/details/131932554