aboutsummaryrefslogtreecommitdiff
path: root/target/riscv/riscv-qmp-cmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'target/riscv/riscv-qmp-cmds.c')
-rw-r--r--target/riscv/riscv-qmp-cmds.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/target/riscv/riscv-qmp-cmds.c b/target/riscv/riscv-qmp-cmds.c
index 5ecff1afb3..2f2dbae7c8 100644
--- a/target/riscv/riscv-qmp-cmds.c
+++ b/target/riscv/riscv-qmp-cmds.c
@@ -24,8 +24,17 @@
#include "qemu/osdep.h"
+#include "qapi/error.h"
#include "qapi/qapi-commands-machine-target.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qerror.h"
+#include "qapi/qobject-input-visitor.h"
+#include "qapi/visitor.h"
+#include "qom/qom-qobject.h"
+#include "sysemu/kvm.h"
+#include "sysemu/tcg.h"
#include "cpu-qom.h"
+#include "cpu.h"
static void riscv_cpu_add_definition(gpointer data, gpointer user_data)
{
@@ -55,3 +64,154 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
return cpu_list;
}
+
+static void riscv_check_if_cpu_available(RISCVCPU *cpu, Error **errp)
+{
+ if (!riscv_cpu_accelerator_compatible(cpu)) {
+ g_autofree char *name = riscv_cpu_get_name(cpu);
+ const char *accel = kvm_enabled() ? "kvm" : "tcg";
+
+ error_setg(errp, "'%s' CPU not available with %s", name, accel);
+ return;
+ }
+}
+
+static void riscv_obj_add_qdict_prop(Object *obj, QDict *qdict_out,
+ const char *name)
+{
+ ObjectProperty *prop = object_property_find(obj, name);
+
+ if (prop) {
+ QObject *value;
+
+ assert(prop->get);
+ value = object_property_get_qobject(obj, name, &error_abort);
+
+ qdict_put_obj(qdict_out, name, value);
+ }
+}
+
+static void riscv_obj_add_multiext_props(Object *obj, QDict *qdict_out,
+ const RISCVCPUMultiExtConfig *arr)
+{
+ for (int i = 0; arr[i].name != NULL; i++) {
+ riscv_obj_add_qdict_prop(obj, qdict_out, arr[i].name);
+ }
+}
+
+static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props,
+ const QDict *qdict_in,
+ Error **errp)
+{
+ const QDictEntry *qe;
+ Visitor *visitor;
+ Error *local_err = NULL;
+
+ visitor = qobject_input_visitor_new(props);
+ if (!visit_start_struct(visitor, NULL, NULL, 0, &local_err)) {
+ goto err;
+ }
+
+ for (qe = qdict_first(qdict_in); qe; qe = qdict_next(qdict_in, qe)) {
+ object_property_find_err(obj, qe->key, &local_err);
+ if (local_err) {
+ goto err;
+ }
+
+ object_property_set(obj, qe->key, visitor, &local_err);
+ if (local_err) {
+ goto err;
+ }
+ }
+
+ visit_check_struct(visitor, &local_err);
+ if (local_err) {
+ goto err;
+ }
+
+ riscv_cpu_finalize_features(RISCV_CPU(obj), &local_err);
+ if (local_err) {
+ goto err;
+ }
+
+ visit_end_struct(visitor, NULL);
+
+err:
+ error_propagate(errp, local_err);
+ visit_free(visitor);
+}
+
+CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
+ CpuModelInfo *model,
+ Error **errp)
+{
+ CpuModelExpansionInfo *expansion_info;
+ const QDict *qdict_in = NULL;
+ QDict *qdict_out;
+ ObjectClass *oc;
+ Object *obj;
+ Error *local_err = NULL;
+
+ if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
+ error_setg(errp, "The requested expansion type is not supported");
+ return NULL;
+ }
+
+ oc = cpu_class_by_name(TYPE_RISCV_CPU, model->name);
+ if (!oc) {
+ error_setg(errp, "The CPU type '%s' is not a known RISC-V CPU type",
+ model->name);
+ return NULL;
+ }
+
+ if (model->props) {
+ qdict_in = qobject_to(QDict, model->props);
+ if (!qdict_in) {
+ error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
+ return NULL;
+ }
+ }
+
+ obj = object_new(object_class_get_name(oc));
+
+ riscv_check_if_cpu_available(RISCV_CPU(obj), &local_err);
+ if (local_err != NULL) {
+ error_propagate(errp, local_err);
+ object_unref(obj);
+ return NULL;
+ }
+
+ if (qdict_in) {
+ riscv_cpuobj_validate_qdict_in(obj, model->props, qdict_in,
+ &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ object_unref(obj);
+ return NULL;
+ }
+ }
+
+ expansion_info = g_new0(CpuModelExpansionInfo, 1);
+ expansion_info->model = g_malloc0(sizeof(*expansion_info->model));
+ expansion_info->model->name = g_strdup(model->name);
+
+ qdict_out = qdict_new();
+
+ riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_extensions);
+ riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_experimental_exts);
+ riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_vendor_exts);
+
+ /* Add our CPU boolean options too */
+ riscv_obj_add_qdict_prop(obj, qdict_out, "mmu");
+ riscv_obj_add_qdict_prop(obj, qdict_out, "pmp");
+
+ if (!qdict_size(qdict_out)) {
+ qobject_unref(qdict_out);
+ } else {
+ expansion_info->model->props = QOBJECT(qdict_out);
+ }
+
+ object_unref(obj);
+
+ return expansion_info;
+}