xref: /openbmc/qemu/hw/core/machine-qmp-cmds.c (revision fc309207)
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/boards.h"
12 #include "qapi/error.h"
13 #include "qapi/qapi-builtin-visit.h"
14 #include "qapi/qapi-commands-machine.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi/qmp/qobject.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/type-helpers.h"
19 #include "qemu/main-loop.h"
20 #include "qom/qom-qobject.h"
21 #include "sysemu/hostmem.h"
22 #include "sysemu/hw_accel.h"
23 #include "sysemu/numa.h"
24 #include "sysemu/runstate.h"
25 
26 static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
27 {
28 #ifdef TARGET_S390X
29     S390CPU *s390_cpu = S390_CPU(cpu);
30     CPUS390XState *env = &s390_cpu->env;
31 
32     info->cpu_state = env->cpu_state;
33 #else
34     abort();
35 #endif
36 }
37 
38 /*
39  * fast means: we NEVER interrupt vCPU threads to retrieve
40  * information from KVM.
41  */
42 CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
43 {
44     MachineState *ms = MACHINE(qdev_get_machine());
45     MachineClass *mc = MACHINE_GET_CLASS(ms);
46     CpuInfoFastList *head = NULL, **tail = &head;
47     SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
48                                           -1, &error_abort);
49     CPUState *cpu;
50 
51     CPU_FOREACH(cpu) {
52         CpuInfoFast *value = g_malloc0(sizeof(*value));
53 
54         value->cpu_index = cpu->cpu_index;
55         value->qom_path = object_get_canonical_path(OBJECT(cpu));
56         value->thread_id = cpu->thread_id;
57 
58         value->has_props = !!mc->cpu_index_to_instance_props;
59         if (value->has_props) {
60             CpuInstanceProperties *props;
61             props = g_malloc0(sizeof(*props));
62             *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
63             value->props = props;
64         }
65 
66         value->target = target;
67         if (target == SYS_EMU_TARGET_S390X) {
68             cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
69         }
70 
71         QAPI_LIST_APPEND(tail, value);
72     }
73 
74     return head;
75 }
76 
77 MachineInfoList *qmp_query_machines(Error **errp)
78 {
79     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
80     MachineInfoList *mach_list = NULL;
81 
82     for (el = machines; el; el = el->next) {
83         MachineClass *mc = el->data;
84         MachineInfo *info;
85 
86         info = g_malloc0(sizeof(*info));
87         if (mc->is_default) {
88             info->has_is_default = true;
89             info->is_default = true;
90         }
91 
92         if (mc->alias) {
93             info->has_alias = true;
94             info->alias = g_strdup(mc->alias);
95         }
96 
97         info->name = g_strdup(mc->name);
98         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
99         info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
100         info->numa_mem_supported = mc->numa_mem_supported;
101         info->deprecated = !!mc->deprecation_reason;
102         if (mc->default_cpu_type) {
103             info->default_cpu_type = g_strdup(mc->default_cpu_type);
104             info->has_default_cpu_type = true;
105         }
106         if (mc->default_ram_id) {
107             info->default_ram_id = g_strdup(mc->default_ram_id);
108             info->has_default_ram_id = true;
109         }
110 
111         QAPI_LIST_PREPEND(mach_list, info);
112     }
113 
114     g_slist_free(machines);
115     return mach_list;
116 }
117 
118 CurrentMachineParams *qmp_query_current_machine(Error **errp)
119 {
120     CurrentMachineParams *params = g_malloc0(sizeof(*params));
121     params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
122 
123     return params;
124 }
125 
126 TargetInfo *qmp_query_target(Error **errp)
127 {
128     TargetInfo *info = g_malloc0(sizeof(*info));
129 
130     info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
131                                  &error_abort);
132 
133     return info;
134 }
135 
136 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
137 {
138     MachineState *ms = MACHINE(qdev_get_machine());
139     MachineClass *mc = MACHINE_GET_CLASS(ms);
140 
141     if (!mc->has_hotpluggable_cpus) {
142         error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
143         return NULL;
144     }
145 
146     return machine_query_hotpluggable_cpus(ms);
147 }
148 
149 void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
150 {
151     if (phase_check(PHASE_MACHINE_INITIALIZED)) {
152         error_setg(errp, "The command is permitted only before the machine has been created");
153         return;
154     }
155 
156     set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
157 }
158 
159 static int query_memdev(Object *obj, void *opaque)
160 {
161     Error *err = NULL;
162     MemdevList **list = opaque;
163     Memdev *m;
164     QObject *host_nodes;
165     Visitor *v;
166 
167     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
168         m = g_malloc0(sizeof(*m));
169 
170         m->id = g_strdup(object_get_canonical_path_component(obj));
171         m->has_id = !!m->id;
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->has_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