xref: /openbmc/qemu/hw/core/machine-qmp-cmds.c (revision d177892d)
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 "cpu.h"
12 #include "hw/boards.h"
13 #include "qapi/error.h"
14 #include "qapi/qapi-builtin-visit.h"
15 #include "qapi/qapi-commands-machine.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi/qmp/qobject.h"
18 #include "qapi/qobject-input-visitor.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 #include "sysemu/sysemu.h"
26 
27 static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
28 {
29 #ifdef TARGET_S390X
30     S390CPU *s390_cpu = S390_CPU(cpu);
31     CPUS390XState *env = &s390_cpu->env;
32 
33     info->cpu_state = env->cpu_state;
34 #else
35     abort();
36 #endif
37 }
38 
39 /*
40  * fast means: we NEVER interrupt vCPU threads to retrieve
41  * information from KVM.
42  */
43 CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
44 {
45     MachineState *ms = MACHINE(qdev_get_machine());
46     MachineClass *mc = MACHINE_GET_CLASS(ms);
47     CpuInfoFastList *head = NULL, **tail = &head;
48     SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
49                                           -1, &error_abort);
50     CPUState *cpu;
51 
52     CPU_FOREACH(cpu) {
53         CpuInfoFast *value = g_malloc0(sizeof(*value));
54 
55         value->cpu_index = cpu->cpu_index;
56         value->qom_path = object_get_canonical_path(OBJECT(cpu));
57         value->thread_id = cpu->thread_id;
58 
59         value->has_props = !!mc->cpu_index_to_instance_props;
60         if (value->has_props) {
61             CpuInstanceProperties *props;
62             props = g_malloc0(sizeof(*props));
63             *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
64             value->props = props;
65         }
66 
67         value->target = target;
68         if (target == SYS_EMU_TARGET_S390X) {
69             cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
70         }
71 
72         QAPI_LIST_APPEND(tail, value);
73     }
74 
75     return head;
76 }
77 
78 MachineInfoList *qmp_query_machines(Error **errp)
79 {
80     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
81     MachineInfoList *mach_list = NULL;
82 
83     for (el = machines; el; el = el->next) {
84         MachineClass *mc = el->data;
85         MachineInfo *info;
86 
87         info = g_malloc0(sizeof(*info));
88         if (mc->is_default) {
89             info->has_is_default = true;
90             info->is_default = true;
91         }
92 
93         if (mc->alias) {
94             info->has_alias = true;
95             info->alias = g_strdup(mc->alias);
96         }
97 
98         info->name = g_strdup(mc->name);
99         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
100         info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
101         info->numa_mem_supported = mc->numa_mem_supported;
102         info->deprecated = !!mc->deprecation_reason;
103         if (mc->default_cpu_type) {
104             info->default_cpu_type = g_strdup(mc->default_cpu_type);
105             info->has_default_cpu_type = true;
106         }
107         if (mc->default_ram_id) {
108             info->default_ram_id = g_strdup(mc->default_ram_id);
109             info->has_default_ram_id = true;
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, QERR_FEATURE_DISABLED, "query-hotpluggable-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     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->policy = object_property_get_enum(obj, "policy", "HostMemPolicy",
178                                              &error_abort);
179         host_nodes = object_property_get_qobject(obj,
180                                                  "host-nodes",
181                                                  &error_abort);
182         v = qobject_input_visitor_new(host_nodes);
183         visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort);
184         visit_free(v);
185         qobject_unref(host_nodes);
186 
187         QAPI_LIST_PREPEND(*list, m);
188     }
189 
190     return 0;
191 }
192 
193 MemdevList *qmp_query_memdev(Error **errp)
194 {
195     Object *obj = object_get_objects_root();
196     MemdevList *list = NULL;
197 
198     object_child_foreach(obj, query_memdev, &list);
199     return list;
200 }
201