2 * QEMU CPU QMP commands for RISC-V
4 * Copyright (c) 2023 Ventana Micro Systems Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-machine-target.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/visitor.h"
33 #include "qom/qom-qobject.h"
37 static void riscv_cpu_add_definition(gpointer data
, gpointer user_data
)
39 ObjectClass
*oc
= data
;
40 CpuDefinitionInfoList
**cpu_list
= user_data
;
41 CpuDefinitionInfo
*info
= g_malloc0(sizeof(*info
));
42 const char *typename
= object_class_get_name(oc
);
43 ObjectClass
*dyn_class
;
45 info
->name
= g_strndup(typename
,
46 strlen(typename
) - strlen("-" TYPE_RISCV_CPU
));
47 info
->q_typename
= g_strdup(typename
);
49 dyn_class
= object_class_dynamic_cast(oc
, TYPE_RISCV_DYNAMIC_CPU
);
50 info
->q_static
= dyn_class
== NULL
;
52 QAPI_LIST_PREPEND(*cpu_list
, info
);
55 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
57 CpuDefinitionInfoList
*cpu_list
= NULL
;
58 GSList
*list
= object_class_get_list(TYPE_RISCV_CPU
, false);
60 g_slist_foreach(list
, riscv_cpu_add_definition
, &cpu_list
);
66 static void riscv_obj_add_qdict_prop(Object
*obj
, QDict
*qdict_out
,
69 ObjectProperty
*prop
= object_property_find(obj
, name
);
75 value
= object_property_get_qobject(obj
, name
, &error_abort
);
77 qdict_put_obj(qdict_out
, name
, value
);
81 static void riscv_obj_add_multiext_props(Object
*obj
, QDict
*qdict_out
,
82 const RISCVCPUMultiExtConfig
*arr
)
84 for (int i
= 0; arr
[i
].name
!= NULL
; i
++) {
85 riscv_obj_add_qdict_prop(obj
, qdict_out
, arr
[i
].name
);
89 static void riscv_cpuobj_validate_qdict_in(Object
*obj
, QObject
*props
,
90 const QDict
*qdict_in
,
95 Error
*local_err
= NULL
;
97 visitor
= qobject_input_visitor_new(props
);
98 if (!visit_start_struct(visitor
, NULL
, NULL
, 0, &local_err
)) {
102 for (qe
= qdict_first(qdict_in
); qe
; qe
= qdict_next(qdict_in
, qe
)) {
103 object_property_find_err(obj
, qe
->key
, &local_err
);
108 object_property_set(obj
, qe
->key
, visitor
, &local_err
);
114 visit_check_struct(visitor
, &local_err
);
119 riscv_cpu_finalize_features(RISCV_CPU(obj
), &local_err
);
124 visit_end_struct(visitor
, NULL
);
127 error_propagate(errp
, local_err
);
131 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
135 CpuModelExpansionInfo
*expansion_info
;
136 const QDict
*qdict_in
= NULL
;
140 Error
*local_err
= NULL
;
142 if (type
!= CPU_MODEL_EXPANSION_TYPE_FULL
) {
143 error_setg(errp
, "The requested expansion type is not supported");
147 oc
= cpu_class_by_name(TYPE_RISCV_CPU
, model
->name
);
149 error_setg(errp
, "The CPU type '%s' is not a known RISC-V CPU type",
155 qdict_in
= qobject_to(QDict
, model
->props
);
157 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
162 obj
= object_new(object_class_get_name(oc
));
165 riscv_cpuobj_validate_qdict_in(obj
, model
->props
, qdict_in
,
168 error_propagate(errp
, local_err
);
174 expansion_info
= g_new0(CpuModelExpansionInfo
, 1);
175 expansion_info
->model
= g_malloc0(sizeof(*expansion_info
->model
));
176 expansion_info
->model
->name
= g_strdup(model
->name
);
178 qdict_out
= qdict_new();
180 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_extensions
);
181 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_experimental_exts
);
182 riscv_obj_add_multiext_props(obj
, qdict_out
, riscv_cpu_vendor_exts
);
184 /* Add our CPU boolean options too */
185 riscv_obj_add_qdict_prop(obj
, qdict_out
, "mmu");
186 riscv_obj_add_qdict_prop(obj
, qdict_out
, "pmp");
188 if (!qdict_size(qdict_out
)) {
189 qobject_unref(qdict_out
);
191 expansion_info
->model
->props
= QOBJECT(qdict_out
);
196 return expansion_info
;