Full analysis of QEMU source code 36 - Machine (6)

Continued from the previous article: Full Analysis of QEMU Source Code 35 —— Machine (5)

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!

The last book explained 3 functions: object_class_get_list_tramp, object_foreach_tramp and type_table_get. Go back to the select_machine function and continue down. In order to facilitate understanding, the select_machine function code is posted again, in softmmu/vl.c, as follows:

static MachineClass *select_machine(QDict *qdict, Error **errp)
{
    const char *optarg = qdict_get_try_str(qdict, "type");
    GSList *machines = object_class_get_list(TYPE_MACHINE, false);
    MachineClass *machine_class;
    Error *local_err = NULL;
 
    if (optarg) {
        machine_class = find_machine(optarg, machines);
        qdict_del(qdict, "type");
        if (!machine_class) {
            error_setg(&local_err, "unsupported machine type");
        }
    } else {
        machine_class = find_default_machine(machines);
        if (!machine_class) {
            error_setg(&local_err, "No machine specified, and there is no default");
        }
    }
 
    g_slist_free(machines);
    if (local_err) {
        error_append_hint(&local_err, "Use -machine help to list supported machines\n");
        error_propagate(errp, local_err);
    }
    return machine_class;
}

As mentioned in the previous document, in the select_machine function, there are two ways to generate a MachineClass: one way is to call the find_machine function to generate a MachineClass by parsing the QEMU command line parameters, that is, the user-specified way; the other way is to find one through the find_default_machine function The default MachineClass is the system default method. Look at them one by one:

  • Command line specification method

The find_machine function is in softmmu/vl.c, the code is as follows:

static MachineClass *find_machine(const char *name, GSList *machines)
{
    GSList *el;

    for (el = machines; el; el = el->next) {
        MachineClass *mc = el->data;

        if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
            return mc;
        }
    }

    return NULL;
}

If the name specified on the command line is the same as the name or alias of the created MachineClass, return the MachineClass object mc; otherwise return NULL, indicating that it is not found.

  • System default mode

The find_default_machine function is also in softmmu/vl.c (actually just below the find_machine function), the code is as follows:

static MachineClass *find_default_machine(GSList *machines)
{
    GSList *el;
    MachineClass *default_machineclass = NULL;

    for (el = machines; el; el = el->next) {
        MachineClass *mc = el->data;

        if (mc->is_default) {
            assert(default_machineclass == NULL && "Multiple default machines");
            default_machineclass = mc;
        }
    }

    return default_machineclass;
}

It can be seen that the meaning is similar to that of the find_machine function, which also traverses the linked list of machines. However, instead of comparing the names here, it finds the MachineClass object mc whose is_default attribute is true, and returns it after checking that it is correct.

So far, the select_machine function has completed its mission and finally returned the required MachineClass object (address).

Note: The length of this chapter is short, mainly due to the length of the previous article, which was intentionally made to avoid reading fatigue. As the saying goes, "the way of civil and military affairs, one piece and one relaxation", so that you will not be too tired when studying and ensure learning and enthusiasm for reading.

In the next article, the second step function of the qemu_create_machine function current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class))); will be explained. If you want to know what happened next, let's see the next chapter.

Guess you like

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