master
c 229 lines 6.98 KB
Raw
1 /*
2 * QEMU monitor.c for ARM.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include "qemu/osdep.h"
24 #include "qemu/target-info.h"
25 #include "hw/core/boards.h"
26 #include "kvm_arm.h"
27 #include "qapi/error.h"
28 #include "qapi/visitor.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/qapi-commands-machine.h"
31 #include "qapi/qapi-commands-misc-arm.h"
32 #include "qobject/qdict.h"
33 #include "qom/qom-qobject.h"
34 #include "cpu.h"
35
36 static GICCapability *gic_cap_new(int version)
37 {
38 GICCapability *cap = g_new0(GICCapability, 1);
39 cap->version = version;
40 /* by default, support none */
41 cap->emulated = false;
42 cap->kernel = false;
43 return cap;
44 }
45
46 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
47 {
48 GICCapabilityList *head = NULL;
49 GICCapability *v2 = gic_cap_new(2), *v3 = gic_cap_new(3);
50
51 v2->emulated = true;
52 v3->emulated = true;
53
54 if (kvm_enabled()) {
55 arm_gic_cap_kvm_probe(v2, v3);
56 }
57
58 QAPI_LIST_PREPEND(head, v2);
59 QAPI_LIST_PREPEND(head, v3);
60
61 return head;
62 }
63
64 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
65
66 /*
67 * These are cpu model features we want to advertise. The order here
68 * matters as this is the order in which qmp_query_cpu_model_expansion
69 * will attempt to set them. If there are dependencies between features,
70 * then the order that considers those dependencies must be used.
71 */
72 static const char *cpu_model_advertised_features[] = {
73 "aarch64", "pmu", "sve",
74 "sve128", "sve256", "sve384", "sve512",
75 "sve640", "sve768", "sve896", "sve1024", "sve1152", "sve1280",
76 "sve1408", "sve1536", "sve1664", "sve1792", "sve1920", "sve2048",
77 "kvm-no-adjvtime", "kvm-steal-time",
78 "pauth", "pauth-impdef", "pauth-qarma3", "pauth-qarma5",
79 NULL
80 };
81
82 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
83 CpuModelInfo *model,
84 Error **errp)
85 {
86 CpuModelExpansionInfo *expansion_info;
87 const QDict *qdict_in;
88 QDict *qdict_out;
89 ObjectClass *oc;
90 Object *obj;
91 const char *name;
92 int i;
93
94 if (type != CPU_MODEL_EXPANSION_TYPE_FULL) {
95 error_setg(errp, "The requested expansion type is not supported");
96 return NULL;
97 }
98
99 if (!kvm_enabled() && !strcmp(model->name, "host")) {
100 error_setg(errp, "The CPU type '%s' requires KVM", model->name);
101 return NULL;
102 }
103
104 oc = cpu_class_by_name(TYPE_ARM_CPU, model->name);
105 if (!oc) {
106 error_setg(errp, "The CPU type '%s' is not a recognized ARM CPU type",
107 model->name);
108 return NULL;
109 }
110
111 if (kvm_enabled()) {
112 bool supported = false;
113
114 if (!strcmp(model->name, "host") || !strcmp(model->name, "max")) {
115 /* These are kvmarm's recommended cpu types */
116 supported = true;
117 } else if (current_machine->cpu_type) {
118 const char *cpu_type = current_machine->cpu_type;
119 int len = strlen(cpu_type) - strlen(ARM_CPU_TYPE_SUFFIX);
120
121 if (strlen(model->name) == len &&
122 !strncmp(model->name, cpu_type, len)) {
123 /* KVM is enabled and we're using this type, so it works. */
124 supported = true;
125 }
126 }
127 if (!supported) {
128 error_setg(errp, "We cannot guarantee the CPU type '%s' works "
129 "with KVM on this host", model->name);
130 return NULL;
131 }
132 }
133
134 obj = object_new(object_class_get_name(oc));
135
136 if (model->props) {
137 Visitor *visitor;
138 Error *err = NULL;
139
140 visitor = qobject_input_visitor_new(model->props);
141 if (!visit_start_struct(visitor, "model.props", NULL, 0, errp)) {
142 visit_free(visitor);
143 object_unref(obj);
144 return NULL;
145 }
146
147 qdict_in = qobject_to(QDict, model->props);
148 i = 0;
149 while ((name = cpu_model_advertised_features[i++]) != NULL) {
150 if (qdict_get(qdict_in, name)) {
151 if (!object_property_set(obj, name, visitor, &err)) {
152 break;
153 }
154 }
155 }
156
157 if (!err) {
158 visit_check_struct(visitor, &err);
159 }
160 if (!err) {
161 arm_cpu_finalize_features(ARM_CPU(obj), &err);
162 }
163 visit_end_struct(visitor, NULL);
164 visit_free(visitor);
165 if (err) {
166 object_unref(obj);
167 error_propagate(errp, err);
168 return NULL;
169 }
170 } else {
171 arm_cpu_finalize_features(ARM_CPU(obj), &error_abort);
172 }
173
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);
177
178 qdict_out = qdict_new();
179
180 i = 0;
181 while ((name = cpu_model_advertised_features[i++]) != NULL) {
182 ObjectProperty *prop = object_property_find(obj, name);
183 if (prop) {
184 QObject *value;
185
186 assert(prop->get);
187 value = object_property_get_qobject(obj, name, &error_abort);
188
189 qdict_put_obj(qdict_out, name, value);
190 }
191 }
192
193 if (!qdict_size(qdict_out)) {
194 qobject_unref(qdict_out);
195 } else {
196 expansion_info->model->props = QOBJECT(qdict_out);
197 }
198
199 object_unref(obj);
200
201 return expansion_info;
202 }
203
204 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
205 {
206 ObjectClass *oc = data;
207 CpuDefinitionInfoList **cpu_list = user_data;
208 CpuDefinitionInfo *info;
209 const char *typename;
210
211 typename = object_class_get_name(oc);
212 info = g_malloc0(sizeof(*info));
213 info->name = cpu_model_from_type(typename);
214 info->q_typename = g_strdup(typename);
215
216 QAPI_LIST_PREPEND(*cpu_list, info);
217 }
218
219 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
220 {
221 CpuDefinitionInfoList *cpu_list = NULL;
222 GSList *list;
223
224 list = object_class_get_list(target_cpu_type(), false);
225 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
226 g_slist_free(list);
227
228 return cpu_list;
229 }