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