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