xref: /qemu/hw/core/machine-qmp-cmds.c (revision ead62c75f618c072a3a18221fd03ae99ae923cca)
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 
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     MemdevList **list = opaque;
162     Memdev *m;
163     QObject *host_nodes;
164     Visitor *v;
165 
166     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
167         m = g_malloc0(sizeof(*m));
168 
169         m->id = g_strdup(object_get_canonical_path_component(obj));
170         m->has_id = !!m->id;
171 
172         m->size = object_property_get_uint(obj, "size", &error_abort);
173         m->merge = object_property_get_bool(obj, "merge", &error_abort);
174         m->dump = object_property_get_bool(obj, "dump", &error_abort);
175         m->prealloc = object_property_get_bool(obj, "prealloc", &error_abort);
176         m->policy = object_property_get_enum(obj, "policy", "HostMemPolicy",
177                                              &error_abort);
178         host_nodes = object_property_get_qobject(obj,
179                                                  "host-nodes",
180                                                  &error_abort);
181         v = qobject_input_visitor_new(host_nodes);
182         visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort);
183         visit_free(v);
184         qobject_unref(host_nodes);
185 
186         QAPI_LIST_PREPEND(*list, m);
187     }
188 
189     return 0;
190 }
191 
192 MemdevList *qmp_query_memdev(Error **errp)
193 {
194     Object *obj = object_get_objects_root();
195     MemdevList *list = NULL;
196 
197     object_child_foreach(obj, query_memdev, &list);
198     return list;
199 }
200