1 /* 2 * QEMU CPU QMP commands for RISC-V 3 * 4 * Copyright (c) 2023 Ventana Micro Systems Inc. 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 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" 34 #include "sysemu/kvm.h" 35 #include "sysemu/tcg.h" 36 #include "cpu-qom.h" 37 #include "cpu.h" 38 39 static void riscv_cpu_add_definition(gpointer data, gpointer user_data) 40 { 41 ObjectClass *oc = data; 42 CpuDefinitionInfoList **cpu_list = user_data; 43 CpuDefinitionInfo *info = g_malloc0(sizeof(*info)); 44 const char *typename = object_class_get_name(oc); 45 ObjectClass *dyn_class; 46 47 info->name = g_strndup(typename, 48 strlen(typename) - strlen("-" TYPE_RISCV_CPU)); 49 info->q_typename = g_strdup(typename); 50 51 dyn_class = object_class_dynamic_cast(oc, TYPE_RISCV_DYNAMIC_CPU); 52 info->q_static = dyn_class == NULL; 53 54 QAPI_LIST_PREPEND(*cpu_list, info); 55 } 56 57 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) 58 { 59 CpuDefinitionInfoList *cpu_list = NULL; 60 GSList *list = object_class_get_list(TYPE_RISCV_CPU, false); 61 62 g_slist_foreach(list, riscv_cpu_add_definition, &cpu_list); 63 g_slist_free(list); 64 65 return cpu_list; 66 } 67 68 static void riscv_check_if_cpu_available(RISCVCPU *cpu, Error **errp) 69 { 70 if (!riscv_cpu_accelerator_compatible(cpu)) { 71 g_autofree char *name = riscv_cpu_get_name(cpu); 72 const char *accel = kvm_enabled() ? "kvm" : "tcg"; 73 74 error_setg(errp, "'%s' CPU not available with %s", name, accel); 75 return; 76 } 77 } 78 79 static void riscv_obj_add_qdict_prop(Object *obj, QDict *qdict_out, 80 const char *name) 81 { 82 ObjectProperty *prop = object_property_find(obj, name); 83 84 if (prop) { 85 QObject *value; 86 87 assert(prop->get); 88 value = object_property_get_qobject(obj, name, &error_abort); 89 90 qdict_put_obj(qdict_out, name, value); 91 } 92 } 93 94 static void riscv_obj_add_multiext_props(Object *obj, QDict *qdict_out, 95 const RISCVCPUMultiExtConfig *arr) 96 { 97 for (int i = 0; arr[i].name != NULL; i++) { 98 riscv_obj_add_qdict_prop(obj, qdict_out, arr[i].name); 99 } 100 } 101 102 static void riscv_cpuobj_validate_qdict_in(Object *obj, QObject *props, 103 const QDict *qdict_in, 104 Error **errp) 105 { 106 const QDictEntry *qe; 107 Visitor *visitor; 108 Error *local_err = NULL; 109 110 visitor = qobject_input_visitor_new(props); 111 if (!visit_start_struct(visitor, NULL, NULL, 0, &local_err)) { 112 goto err; 113 } 114 115 for (qe = qdict_first(qdict_in); qe; qe = qdict_next(qdict_in, qe)) { 116 object_property_find_err(obj, qe->key, &local_err); 117 if (local_err) { 118 goto err; 119 } 120 121 object_property_set(obj, qe->key, visitor, &local_err); 122 if (local_err) { 123 goto err; 124 } 125 } 126 127 visit_check_struct(visitor, &local_err); 128 if (local_err) { 129 goto err; 130 } 131 132 riscv_cpu_finalize_features(RISCV_CPU(obj), &local_err); 133 if (local_err) { 134 goto err; 135 } 136 137 visit_end_struct(visitor, NULL); 138 139 err: 140 error_propagate(errp, local_err); 141 visit_free(visitor); 142 } 143 144 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type, 145 CpuModelInfo *model, 146 Error **errp) 147 { 148 CpuModelExpansionInfo *expansion_info; 149 const QDict *qdict_in = NULL; 150 QDict *qdict_out; 151 ObjectClass *oc; 152 Object *obj; 153 Error *local_err = NULL; 154 155 if (type != CPU_MODEL_EXPANSION_TYPE_FULL) { 156 error_setg(errp, "The requested expansion type is not supported"); 157 return NULL; 158 } 159 160 oc = cpu_class_by_name(TYPE_RISCV_CPU, model->name); 161 if (!oc) { 162 error_setg(errp, "The CPU type '%s' is not a known RISC-V CPU type", 163 model->name); 164 return NULL; 165 } 166 167 if (model->props) { 168 qdict_in = qobject_to(QDict, model->props); 169 if (!qdict_in) { 170 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict"); 171 return NULL; 172 } 173 } 174 175 obj = object_new(object_class_get_name(oc)); 176 177 riscv_check_if_cpu_available(RISCV_CPU(obj), &local_err); 178 if (local_err != NULL) { 179 error_propagate(errp, local_err); 180 object_unref(obj); 181 return NULL; 182 } 183 184 if (qdict_in) { 185 riscv_cpuobj_validate_qdict_in(obj, model->props, qdict_in, 186 &local_err); 187 if (local_err) { 188 error_propagate(errp, local_err); 189 object_unref(obj); 190 return NULL; 191 } 192 } 193 194 expansion_info = g_new0(CpuModelExpansionInfo, 1); 195 expansion_info->model = g_malloc0(sizeof(*expansion_info->model)); 196 expansion_info->model->name = g_strdup(model->name); 197 198 qdict_out = qdict_new(); 199 200 riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_extensions); 201 riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_experimental_exts); 202 riscv_obj_add_multiext_props(obj, qdict_out, riscv_cpu_vendor_exts); 203 204 /* Add our CPU boolean options too */ 205 riscv_obj_add_qdict_prop(obj, qdict_out, "mmu"); 206 riscv_obj_add_qdict_prop(obj, qdict_out, "pmp"); 207 208 if (!qdict_size(qdict_out)) { 209 qobject_unref(qdict_out); 210 } else { 211 expansion_info->model->props = QOBJECT(qdict_out); 212 } 213 214 object_unref(obj); 215 216 return expansion_info; 217 } 218