xref: /openbmc/qemu/hw/core/machine-qmp-cmds.c (revision b8116f4c)
1 /*
2  * QMP commands related to machines and CPUs
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/acpi/vmgenid.h"
12 #include "hw/boards.h"
13 #include "hw/intc/intc.h"
14 #include "hw/mem/memory-device.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-builtin-visit.h"
17 #include "qapi/qapi-commands-machine.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "qapi/type-helpers.h"
21 #include "qemu/uuid.h"
22 #include "qom/qom-qobject.h"
23 #include "sysemu/hostmem.h"
24 #include "sysemu/hw_accel.h"
25 #include "sysemu/numa.h"
26 #include "sysemu/runstate.h"
27 #include "sysemu/sysemu.h"
28 
29 /*
30  * fast means: we NEVER interrupt vCPU threads to retrieve
31  * information from KVM.
32  */
33 CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
34 {
35     MachineState *ms = MACHINE(qdev_get_machine());
36     MachineClass *mc = MACHINE_GET_CLASS(ms);
37     CpuInfoFastList *head = NULL, **tail = &head;
38     SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, target_name(),
39                                           -1, &error_abort);
40     CPUState *cpu;
41 
42     CPU_FOREACH(cpu) {
43         CpuInfoFast *value = g_malloc0(sizeof(*value));
44 
45         value->cpu_index = cpu->cpu_index;
46         value->qom_path = object_get_canonical_path(OBJECT(cpu));
47         value->thread_id = cpu->thread_id;
48 
49         if (mc->cpu_index_to_instance_props) {
50             CpuInstanceProperties *props;
51             props = g_malloc0(sizeof(*props));
52             *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
53             value->props = props;
54         }
55 
56         value->target = target;
57         if (cpu->cc->query_cpu_fast) {
58             cpu->cc->query_cpu_fast(cpu, value);
59         }
60 
61         QAPI_LIST_APPEND(tail, value);
62     }
63 
64     return head;
65 }
66 
67 MachineInfoList *qmp_query_machines(bool has_compat_props, bool compat_props,
68                                     Error **errp)
69 {
70     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
71     MachineInfoList *mach_list = NULL;
72 
73     for (el = machines; el; el = el->next) {
74         MachineClass *mc = el->data;
75         MachineInfo *info;
76 
77         info = g_malloc0(sizeof(*info));
78         if (mc->is_default) {
79             info->has_is_default = true;
80             info->is_default = true;
81         }
82 
83         if (mc->alias) {
84             info->alias = g_strdup(mc->alias);
85         }
86 
87         info->name = g_strdup(mc->name);
88         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
89         info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
90         info->numa_mem_supported = mc->numa_mem_supported;
91         info->deprecated = !!mc->deprecation_reason;
92         info->acpi = !!object_class_property_find(OBJECT_CLASS(mc), "acpi");
93         if (mc->default_cpu_type) {
94             info->default_cpu_type = g_strdup(mc->default_cpu_type);
95         }
96         if (mc->default_ram_id) {
97             info->default_ram_id = g_strdup(mc->default_ram_id);
98         }
99 
100         if (compat_props && mc->compat_props) {
101             int i;
102             info->compat_props = NULL;
103             CompatPropertyList **tail = &(info->compat_props);
104             info->has_compat_props = true;
105 
106             for (i = 0; i < mc->compat_props->len; i++) {
107                 GlobalProperty *mt_prop = g_ptr_array_index(mc->compat_props,
108                                                             i);
109                 CompatProperty *prop;
110 
111                 prop = g_malloc0(sizeof(*prop));
112                 prop->qom_type = g_strdup(mt_prop->driver);
113                 prop->property = g_strdup(mt_prop->property);
114                 prop->value = g_strdup(mt_prop->value);
115 
116                 QAPI_LIST_APPEND(tail, prop);
117             }
118         }
119 
120         QAPI_LIST_PREPEND(mach_list, info);
121     }
122 
123     g_slist_free(machines);
124     return mach_list;
125 }
126 
127 CurrentMachineParams *qmp_query_current_machine(Error **errp)
128 {
129     CurrentMachineParams *params = g_malloc0(sizeof(*params));
130     params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
131 
132     return params;
133 }
134 
135 TargetInfo *qmp_query_target(Error **errp)
136 {
137     TargetInfo *info = g_malloc0(sizeof(*info));
138 
139     info->arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
140                                  &error_abort);
141 
142     return info;
143 }
144 
145 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
146 {
147     MachineState *ms = MACHINE(qdev_get_machine());
148     MachineClass *mc = MACHINE_GET_CLASS(ms);
149 
150     if (!mc->has_hotpluggable_cpus) {
151         error_setg(errp, "machine does not support hot-plugging CPUs");
152         return NULL;
153     }
154 
155     return machine_query_hotpluggable_cpus(ms);
156 }
157 
158 void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
159 {
160     if (phase_check(PHASE_MACHINE_INITIALIZED)) {
161         error_setg(errp, "The command is permitted only before the machine has been created");
162         return;
163     }
164 
165     set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
166 }
167 
168 static int query_memdev(Object *obj, void *opaque)
169 {
170     Error *err = NULL;
171     MemdevList **list = opaque;
172     Memdev *m;
173     QObject *host_nodes;
174     Visitor *v;
175 
176     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
177         m = g_malloc0(sizeof(*m));
178 
179         m->id = g_strdup(object_get_canonical_path_component(obj));
180 
181         m->size = object_property_get_uint(obj, "size", &error_abort);
182         m->merge = object_property_get_bool(obj, "merge", &error_abort);
183         m->dump = object_property_get_bool(obj, "dump", &error_abort);
184         m->prealloc = object_property_get_bool(obj, "prealloc", &error_abort);
185         m->share = object_property_get_bool(obj, "share", &error_abort);
186         m->reserve = object_property_get_bool(obj, "reserve", &err);
187         if (err) {
188             error_free_or_abort(&err);
189         } else {
190             m->has_reserve = true;
191         }
192         m->policy = object_property_get_enum(obj, "policy", "HostMemPolicy",
193                                              &error_abort);
194         host_nodes = object_property_get_qobject(obj,
195                                                  "host-nodes",
196                                                  &error_abort);
197         v = qobject_input_visitor_new(host_nodes);
198         visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort);
199         visit_free(v);
200         qobject_unref(host_nodes);
201 
202         QAPI_LIST_PREPEND(*list, m);
203     }
204 
205     return 0;
206 }
207 
208 MemdevList *qmp_query_memdev(Error **errp)
209 {
210     Object *obj = object_get_objects_root();
211     MemdevList *list = NULL;
212 
213     object_child_foreach(obj, query_memdev, &list);
214     return list;
215 }
216 
217 HumanReadableText *qmp_x_query_numa(Error **errp)
218 {
219     g_autoptr(GString) buf = g_string_new("");
220     int i, nb_numa_nodes;
221     NumaNodeMem *node_mem;
222     CpuInfoFastList *cpu_list, *cpu;
223     MachineState *ms = MACHINE(qdev_get_machine());
224 
225     nb_numa_nodes = ms->numa_state ? ms->numa_state->num_nodes : 0;
226     g_string_append_printf(buf, "%d nodes\n", nb_numa_nodes);
227     if (!nb_numa_nodes) {
228         goto done;
229     }
230 
231     cpu_list = qmp_query_cpus_fast(&error_abort);
232     node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
233 
234     query_numa_node_mem(node_mem, ms);
235     for (i = 0; i < nb_numa_nodes; i++) {
236         g_string_append_printf(buf, "node %d cpus:", i);
237         for (cpu = cpu_list; cpu; cpu = cpu->next) {
238             if (cpu->value->props && cpu->value->props->has_node_id &&
239                 cpu->value->props->node_id == i) {
240                 g_string_append_printf(buf, " %" PRIi64, cpu->value->cpu_index);
241             }
242         }
243         g_string_append_printf(buf, "\n");
244         g_string_append_printf(buf, "node %d size: %" PRId64 " MB\n", i,
245                                node_mem[i].node_mem >> 20);
246         g_string_append_printf(buf, "node %d plugged: %" PRId64 " MB\n", i,
247                                node_mem[i].node_plugged_mem >> 20);
248     }
249     qapi_free_CpuInfoFastList(cpu_list);
250     g_free(node_mem);
251 
252  done:
253     return human_readable_text_from_str(buf);
254 }
255 
256 KvmInfo *qmp_query_kvm(Error **errp)
257 {
258     KvmInfo *info = g_malloc0(sizeof(*info));
259 
260     info->enabled = kvm_enabled();
261     info->present = accel_find("kvm");
262 
263     return info;
264 }
265 
266 UuidInfo *qmp_query_uuid(Error **errp)
267 {
268     UuidInfo *info = g_malloc0(sizeof(*info));
269 
270     info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
271     return info;
272 }
273 
274 void qmp_system_reset(Error **errp)
275 {
276     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
277 }
278 
279 void qmp_system_powerdown(Error **errp)
280 {
281     qemu_system_powerdown_request();
282 }
283 
284 void qmp_system_wakeup(Error **errp)
285 {
286     if (!qemu_wakeup_suspend_enabled()) {
287         error_setg(errp,
288                    "wake-up from suspend is not supported by this guest");
289         return;
290     }
291 
292     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
293 }
294 
295 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
296 {
297     return qmp_memory_device_list();
298 }
299 
300 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
301 {
302     MemoryInfo *mem_info = g_new0(MemoryInfo, 1);
303     MachineState *ms = MACHINE(qdev_get_machine());
304 
305     mem_info->base_memory = ms->ram_size;
306 
307     mem_info->plugged_memory = get_plugged_memory_size();
308     mem_info->has_plugged_memory =
309         mem_info->plugged_memory != (uint64_t)-1;
310 
311     return mem_info;
312 }
313 
314 HumanReadableText *qmp_x_query_ramblock(Error **errp)
315 {
316     g_autoptr(GString) buf = ram_block_format();
317 
318     return human_readable_text_from_str(buf);
319 }
320 
321 static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
322 {
323     InterruptStatsProvider *intc;
324     InterruptStatsProviderClass *k;
325     GString *buf = opaque;
326 
327     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
328         intc = INTERRUPT_STATS_PROVIDER(obj);
329         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
330         uint64_t *irq_counts;
331         unsigned int nb_irqs, i;
332         if (k->get_statistics &&
333             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
334             if (nb_irqs > 0) {
335                 g_string_append_printf(buf, "IRQ statistics for %s:\n",
336                                        object_get_typename(obj));
337                 for (i = 0; i < nb_irqs; i++) {
338                     if (irq_counts[i] > 0) {
339                         g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
340                                                irq_counts[i]);
341                     }
342                 }
343             }
344         } else {
345             g_string_append_printf(buf,
346                                    "IRQ statistics not available for %s.\n",
347                                    object_get_typename(obj));
348         }
349     }
350 
351     return 0;
352 }
353 
354 HumanReadableText *qmp_x_query_irq(Error **errp)
355 {
356     g_autoptr(GString) buf = g_string_new("");
357 
358     object_child_foreach_recursive(object_get_root(),
359                                    qmp_x_query_irq_foreach, buf);
360 
361     return human_readable_text_from_str(buf);
362 }
363 
364 GuidInfo *qmp_query_vm_generation_id(Error **errp)
365 {
366     GuidInfo *info;
367     VmGenIdState *vms;
368     Object *obj = find_vmgenid_dev();
369 
370     if (!obj) {
371         error_setg(errp, "VM Generation ID device not found");
372         return NULL;
373     }
374     vms = VMGENID(obj);
375 
376     info = g_malloc0(sizeof(*info));
377     info->guid = qemu_uuid_unparse_strdup(&vms->guid);
378     return info;
379 }
380